blob: 6becfd19f0e9d0f64249b43b55b197d53bd23c24 [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 (
Patrice Arrudaca221f32020-10-19 11:38:54 -070018 "os"
19 "syscall"
Nan Zhang17f27672018-12-12 16:01:49 -080020 "time"
21
22 "android/soong/ui/metrics/metrics_proto"
23 "android/soong/ui/tracer"
Patrice Arruda958b89c2020-07-13 18:21:14 +000024 "github.com/golang/protobuf/proto"
Nan Zhang17f27672018-12-12 16:01:49 -080025)
26
Patrice Arruda958b89c2020-07-13 18:21:14 +000027// for testing purpose only
28var _now = now
29
Patrice Arruda457c5d32020-10-19 11:20:21 -070030type event struct {
Nan Zhang17f27672018-12-12 16:01:49 -080031 desc string
32 name string
33
Patrice Arruda958b89c2020-07-13 18:21:14 +000034 // the time that the event started to occur.
35 start time.Time
Patrice Arrudaca221f32020-10-19 11:38:54 -070036
37 // The list of process resource information that was executed
38 procResInfo []*soong_metrics_proto.ProcessResourceInfo
Nan Zhang17f27672018-12-12 16:01:49 -080039}
40
Patrice Arruda457c5d32020-10-19 11:20:21 -070041type EventTracer interface {
Nan Zhang17f27672018-12-12 16:01:49 -080042 Begin(name, desc string, thread tracer.Thread)
Patrice Arruda0cc5b212019-06-14 15:27:46 -070043 End(thread tracer.Thread) soong_metrics_proto.PerfInfo
Patrice Arrudaca221f32020-10-19 11:38:54 -070044 AddProcResInfo(string, *os.ProcessState)
Nan Zhang17f27672018-12-12 16:01:49 -080045}
46
Patrice Arruda457c5d32020-10-19 11:20:21 -070047type eventTracerImpl struct {
48 activeEvents []event
Nan Zhang17f27672018-12-12 16:01:49 -080049}
50
Patrice Arruda457c5d32020-10-19 11:20:21 -070051var _ EventTracer = &eventTracerImpl{}
Nan Zhang17f27672018-12-12 16:01:49 -080052
Patrice Arruda958b89c2020-07-13 18:21:14 +000053func now() time.Time {
54 return time.Now()
Nan Zhang17f27672018-12-12 16:01:49 -080055}
56
Patrice Arrudaca221f32020-10-19 11:38:54 -070057// AddProcResInfo adds information on an executed process such as max resident set memory
58// and the number of voluntary context switches.
59func (t *eventTracerImpl) AddProcResInfo(name string, state *os.ProcessState) {
60 if len(t.activeEvents) < 1 {
61 return
62 }
63
64 rusage := state.SysUsage().(*syscall.Rusage)
65 // The implementation of the metrics system is a stacked based system. The steps of the
66 // build system in the UI layer is sequential so the Begin function is invoked when a
67 // function (or scoped code) is invoked. That is translated to a new event which is added
68 // at the end of the activeEvents array. When the invoking function is completed, End is
69 // invoked which is a pop operation from activeEvents.
70 curEvent := &t.activeEvents[len(t.activeEvents)-1]
71 curEvent.procResInfo = append(curEvent.procResInfo, &soong_metrics_proto.ProcessResourceInfo{
72 Name: proto.String(name),
73 UserTimeMicros: proto.Uint64(uint64(rusage.Utime.Usec)),
74 SystemTimeMicros: proto.Uint64(uint64(rusage.Stime.Usec)),
75 MinorPageFaults: proto.Uint64(uint64(rusage.Minflt)),
76 MajorPageFaults: proto.Uint64(uint64(rusage.Majflt)),
77 // ru_inblock and ru_oublock are measured in blocks of 512 bytes.
78 IoInputKb: proto.Uint64(uint64(rusage.Inblock / 2)),
79 IoOutputKb: proto.Uint64(uint64(rusage.Oublock / 2)),
80 VoluntaryContextSwitches: proto.Uint64(uint64(rusage.Nvcsw)),
81 InvoluntaryContextSwitches: proto.Uint64(uint64(rusage.Nivcsw)),
82 })
83}
84
Patrice Arruda457c5d32020-10-19 11:20:21 -070085func (t *eventTracerImpl) Begin(name, desc string, _ tracer.Thread) {
86 t.activeEvents = append(t.activeEvents, event{name: name, desc: desc, start: _now()})
Nan Zhang17f27672018-12-12 16:01:49 -080087}
88
Patrice Arruda457c5d32020-10-19 11:20:21 -070089func (t *eventTracerImpl) End(tracer.Thread) soong_metrics_proto.PerfInfo {
Nan Zhang17f27672018-12-12 16:01:49 -080090 if len(t.activeEvents) < 1 {
91 panic("Internal error: No pending events for endAt to end!")
92 }
93 lastEvent := t.activeEvents[len(t.activeEvents)-1]
94 t.activeEvents = t.activeEvents[:len(t.activeEvents)-1]
Patrice Arruda958b89c2020-07-13 18:21:14 +000095 realTime := uint64(_now().Sub(lastEvent.start).Nanoseconds())
Nan Zhang17f27672018-12-12 16:01:49 -080096
Patrice Arruda0cc5b212019-06-14 15:27:46 -070097 return soong_metrics_proto.PerfInfo{
Patrice Arrudaca221f32020-10-19 11:38:54 -070098 Desc: proto.String(lastEvent.desc),
99 Name: proto.String(lastEvent.name),
100 StartTime: proto.Uint64(uint64(lastEvent.start.UnixNano())),
101 RealTime: proto.Uint64(realTime),
102 ProcessesResourceInfo: lastEvent.procResInfo,
Patrice Arruda958b89c2020-07-13 18:21:14 +0000103 }
Nan Zhang17f27672018-12-12 16:01:49 -0800104}