blob: 4016563378ee1a2ebbff0a6ccf8a6c829ad20cf3 [file] [log] [blame]
Nan Zhang17f27672018-12-12 16:01:49 -08001// Copyright 2018 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package metrics
16
17import (
18 "time"
19
20 "android/soong/ui/metrics/metrics_proto"
21 "android/soong/ui/tracer"
Patrice Arruda958b89c2020-07-13 18:21:14 +000022 "github.com/golang/protobuf/proto"
Nan Zhang17f27672018-12-12 16:01:49 -080023)
24
Patrice Arruda958b89c2020-07-13 18:21:14 +000025// for testing purpose only
26var _now = now
27
Nan Zhang17f27672018-12-12 16:01:49 -080028type timeEvent struct {
29 desc string
30 name string
31
Patrice Arruda958b89c2020-07-13 18:21:14 +000032 // the time that the event started to occur.
33 start time.Time
Nan Zhang17f27672018-12-12 16:01:49 -080034}
35
36type TimeTracer interface {
37 Begin(name, desc string, thread tracer.Thread)
Patrice Arruda0cc5b212019-06-14 15:27:46 -070038 End(thread tracer.Thread) soong_metrics_proto.PerfInfo
Nan Zhang17f27672018-12-12 16:01:49 -080039}
40
41type timeTracerImpl struct {
42 activeEvents []timeEvent
43}
44
45var _ TimeTracer = &timeTracerImpl{}
46
Patrice Arruda958b89c2020-07-13 18:21:14 +000047func now() time.Time {
48 return time.Now()
Nan Zhang17f27672018-12-12 16:01:49 -080049}
50
Patrice Arruda958b89c2020-07-13 18:21:14 +000051func (t *timeTracerImpl) Begin(name, desc string, _ tracer.Thread) {
52 t.activeEvents = append(t.activeEvents, timeEvent{name: name, desc: desc, start: _now()})
Nan Zhang17f27672018-12-12 16:01:49 -080053}
54
Patrice Arruda958b89c2020-07-13 18:21:14 +000055func (t *timeTracerImpl) End(tracer.Thread) soong_metrics_proto.PerfInfo {
Nan Zhang17f27672018-12-12 16:01:49 -080056 if len(t.activeEvents) < 1 {
57 panic("Internal error: No pending events for endAt to end!")
58 }
59 lastEvent := t.activeEvents[len(t.activeEvents)-1]
60 t.activeEvents = t.activeEvents[:len(t.activeEvents)-1]
Patrice Arruda958b89c2020-07-13 18:21:14 +000061 realTime := uint64(_now().Sub(lastEvent.start).Nanoseconds())
Nan Zhang17f27672018-12-12 16:01:49 -080062
Patrice Arruda0cc5b212019-06-14 15:27:46 -070063 return soong_metrics_proto.PerfInfo{
Patrice Arruda958b89c2020-07-13 18:21:14 +000064 Desc: proto.String(lastEvent.desc),
65 Name: proto.String(lastEvent.name),
66 StartTime: proto.Uint64(uint64(lastEvent.start.UnixNano())),
67 RealTime: proto.Uint64(realTime),
68 }
Nan Zhang17f27672018-12-12 16:01:49 -080069}