blob: 5a62847a550912c86530b031eed90c448da71e08 [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
Patrice Arruda457c5d32020-10-19 11:20:21 -070028type event struct {
Nan Zhang17f27672018-12-12 16:01:49 -080029 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
Patrice Arruda457c5d32020-10-19 11:20:21 -070036type EventTracer interface {
Nan Zhang17f27672018-12-12 16:01:49 -080037 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
Patrice Arruda457c5d32020-10-19 11:20:21 -070041type eventTracerImpl struct {
42 activeEvents []event
Nan Zhang17f27672018-12-12 16:01:49 -080043}
44
Patrice Arruda457c5d32020-10-19 11:20:21 -070045var _ EventTracer = &eventTracerImpl{}
Nan Zhang17f27672018-12-12 16:01:49 -080046
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 Arruda457c5d32020-10-19 11:20:21 -070051func (t *eventTracerImpl) Begin(name, desc string, _ tracer.Thread) {
52 t.activeEvents = append(t.activeEvents, event{name: name, desc: desc, start: _now()})
Nan Zhang17f27672018-12-12 16:01:49 -080053}
54
Patrice Arruda457c5d32020-10-19 11:20:21 -070055func (t *eventTracerImpl) 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}