blob: f1bb862bd723e3a05099f5488bc1514d35b157a1 [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
Patrice Arruda589826b2020-12-15 23:14:55 +000015// Package metrics represents the metrics system for Android Platform Build Systems.
Nan Zhang17f27672018-12-12 16:01:49 -080016package metrics
17
Patrice Arruda589826b2020-12-15 23:14:55 +000018// This is the main heart of the metrics system for Android Platform Build Systems.
19// The starting of the soong_ui (cmd/soong_ui/main.go), the metrics system is
20// initialized by the invocation of New and is then stored in the context
21// (ui/build/context.go) to be used throughout the system. During the build
22// initialization phase, several functions in this file are invoked to store
23// information such as the environment, build configuration and build metadata.
24// There are several scoped code that has Begin() and defer End() functions
25// that captures the metrics and is them added as a perfInfo into the set
26// of the collected metrics. Finally, when soong_ui has finished the build,
27// the defer Dump function is invoked to store the collected metrics to the
Yu Liu6e13b402021-07-27 14:29:06 -070028// raw protobuf file in the $OUT directory and this raw protobuf file will be
29// uploaded to the destination. See ui/build/upload.go for more details. The
30// filename of the raw protobuf file and the list of files to be uploaded is
31// defined in cmd/soong_ui/main.go. See ui/metrics/event.go for the explanation
32// of what an event is and how the metrics system is a stack based system.
Patrice Arruda589826b2020-12-15 23:14:55 +000033
Nan Zhang17f27672018-12-12 16:01:49 -080034import (
35 "io/ioutil"
36 "os"
Patrice Arrudaadd7ea92020-08-07 17:55:23 +000037 "runtime"
Patrice Arrudae92c30d2020-10-29 11:01:32 -070038 "strings"
Patrice Arruda73c790f2020-07-13 23:01:18 +000039 "time"
Nan Zhang17f27672018-12-12 16:01:49 -080040
Dan Willemsen4591b642021-05-24 14:24:12 -070041 "google.golang.org/protobuf/proto"
Colin Crossd0be2102019-11-26 16:16:57 -080042
Dan Willemsen4591b642021-05-24 14:24:12 -070043 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Nan Zhang17f27672018-12-12 16:01:49 -080044)
45
46const (
Patrice Arruda589826b2020-12-15 23:14:55 +000047 // Below is a list of names passed in to the Begin tracing functions. These
48 // names are used to group a set of metrics.
49
50 // Setup and tear down of the build systems.
Patrice Arruda62f1bf22020-07-07 12:48:26 +000051 RunSetupTool = "setup"
52 RunShutdownTool = "shutdown"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000053 TestRun = "test"
Patrice Arruda589826b2020-12-15 23:14:55 +000054
55 // List of build system tools.
56 RunSoong = "soong"
57 PrimaryNinja = "ninja"
58 RunKati = "kati"
59 RunBazel = "bazel"
60
61 // Overall build from building the graph to building the target.
62 Total = "total"
Nan Zhang17f27672018-12-12 16:01:49 -080063)
64
Patrice Arruda589826b2020-12-15 23:14:55 +000065// Metrics is a struct that stores collected metrics during the course
66// of a build which later is dumped to a MetricsBase protobuf file.
67// See ui/metrics/metrics_proto/metrics.proto for further details
68// on what information is collected.
Nan Zhang17f27672018-12-12 16:01:49 -080069type Metrics struct {
Patrice Arruda589826b2020-12-15 23:14:55 +000070 // The protobuf message that is later written to the file.
71 metrics soong_metrics_proto.MetricsBase
72
73 // A list of pending build events.
Patrice Arruda8a44a372020-12-14 20:55:23 +000074 EventTracer *EventTracer
Nan Zhang17f27672018-12-12 16:01:49 -080075}
76
Patrice Arruda589826b2020-12-15 23:14:55 +000077// New returns a pointer of Metrics to store a set of metrics.
Nan Zhang17f27672018-12-12 16:01:49 -080078func New() (metrics *Metrics) {
79 m := &Metrics{
Patrice Arruda457c5d32020-10-19 11:20:21 -070080 metrics: soong_metrics_proto.MetricsBase{},
Patrice Arruda8a44a372020-12-14 20:55:23 +000081 EventTracer: &EventTracer{},
Nan Zhang17f27672018-12-12 16:01:49 -080082 }
83 return m
84}
85
Patrice Arruda589826b2020-12-15 23:14:55 +000086// SetTimeMetrics stores performance information from an executed block of
87// code.
Patrice Arruda0cc5b212019-06-14 15:27:46 -070088func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
Nan Zhang17f27672018-12-12 16:01:49 -080089 switch perf.GetName() {
90 case RunKati:
91 m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -080092 case RunSoong:
93 m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -080094 case RunBazel:
95 m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -080096 case PrimaryNinja:
97 m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
Patrice Arruda48d55ad2020-12-15 19:34:20 +000098 case RunSetupTool:
99 m.metrics.SetupTools = append(m.metrics.SetupTools, &perf)
Colin Cross74cda722020-01-16 15:25:50 -0800100 case Total:
101 m.metrics.Total = &perf
Nan Zhang17f27672018-12-12 16:01:49 -0800102 }
103}
104
Patrice Arruda589826b2020-12-15 23:14:55 +0000105// BuildConfig stores information about the build configuration.
Patrice Arruda96850362020-08-11 20:41:11 +0000106func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
107 m.metrics.BuildConfig = b
108}
109
Patrice Arruda589826b2020-12-15 23:14:55 +0000110// SystemResourceInfo stores information related to the host system such
111// as total CPU and memory.
Patrice Arruda3edfd482020-10-13 23:58:41 +0000112func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
113 m.metrics.SystemResourceInfo = b
114}
115
Patrice Arruda589826b2020-12-15 23:14:55 +0000116// SetMetadataMetrics sets information about the build such as the target
117// product, host architecture and out directory.
Nan Zhang17f27672018-12-12 16:01:49 -0800118func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
119 for k, v := range metadata {
120 switch k {
121 case "BUILD_ID":
122 m.metrics.BuildId = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800123 case "PLATFORM_VERSION_CODENAME":
124 m.metrics.PlatformVersionCodename = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800125 case "TARGET_PRODUCT":
126 m.metrics.TargetProduct = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800127 case "TARGET_BUILD_VARIANT":
128 switch v {
129 case "user":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700130 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800131 case "userdebug":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700132 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800133 case "eng":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700134 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800135 }
136 case "TARGET_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000137 m.metrics.TargetArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800138 case "TARGET_ARCH_VARIANT":
139 m.metrics.TargetArchVariant = proto.String(v)
140 case "TARGET_CPU_VARIANT":
141 m.metrics.TargetCpuVariant = proto.String(v)
142 case "HOST_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000143 m.metrics.HostArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800144 case "HOST_2ND_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000145 m.metrics.Host_2NdArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800146 case "HOST_OS_EXTRA":
147 m.metrics.HostOsExtra = proto.String(v)
148 case "HOST_CROSS_OS":
149 m.metrics.HostCrossOs = proto.String(v)
150 case "HOST_CROSS_ARCH":
151 m.metrics.HostCrossArch = proto.String(v)
152 case "HOST_CROSS_2ND_ARCH":
153 m.metrics.HostCross_2NdArch = proto.String(v)
154 case "OUT_DIR":
155 m.metrics.OutDir = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800156 }
157 }
158}
159
Patrice Arruda589826b2020-12-15 23:14:55 +0000160// arch returns the corresponding MetricsBase_Arch based on the string
161// parameter.
162func arch(a string) *soong_metrics_proto.MetricsBase_Arch {
163 switch a {
Nan Zhang17f27672018-12-12 16:01:49 -0800164 case "arm":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700165 return soong_metrics_proto.MetricsBase_ARM.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800166 case "arm64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700167 return soong_metrics_proto.MetricsBase_ARM64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800168 case "x86":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700169 return soong_metrics_proto.MetricsBase_X86.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800170 case "x86_64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700171 return soong_metrics_proto.MetricsBase_X86_64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800172 default:
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700173 return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800174 }
175}
176
Patrice Arruda589826b2020-12-15 23:14:55 +0000177// SetBuildDateTime sets the build date and time. The value written
178// to the protobuf file is in seconds.
Patrice Arruda73c790f2020-07-13 23:01:18 +0000179func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
180 m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
Nan Zhang17f27672018-12-12 16:01:49 -0800181}
182
Patrice Arruda589826b2020-12-15 23:14:55 +0000183// SetBuildCommand adds the build command specified by the user to the
184// list of collected metrics.
Patrice Arrudae92c30d2020-10-29 11:01:32 -0700185func (m *Metrics) SetBuildCommand(cmd []string) {
186 m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
187}
188
Patrice Arruda589826b2020-12-15 23:14:55 +0000189// Dump exports the collected metrics from the executed build to the file at
190// out path.
191func (m *Metrics) Dump(out string) error {
Patrice Arruda4fb8adc2020-10-12 22:38:06 +0000192 // ignore the error if the hostname could not be retrieved as it
193 // is not a critical metric to extract.
194 if hostname, err := os.Hostname(); err == nil {
195 m.metrics.Hostname = proto.String(hostname)
196 }
Patrice Arrudaadd7ea92020-08-07 17:55:23 +0000197 m.metrics.HostOs = proto.String(runtime.GOOS)
Patrice Arruda589826b2020-12-15 23:14:55 +0000198
199 return save(&m.metrics, out)
Colin Crossd0be2102019-11-26 16:16:57 -0800200}
201
Patrice Arruda589826b2020-12-15 23:14:55 +0000202// SetSoongBuildMetrics sets the metrics collected from the soong_build
203// execution.
Colin Crossb72c9092020-02-10 11:23:49 -0800204func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
205 m.metrics.SoongBuildMetrics = metrics
206}
207
Patrice Arruda589826b2020-12-15 23:14:55 +0000208// A CriticalUserJourneysMetrics is a struct that contains critical user journey
209// metrics. These critical user journeys are defined under cuj/cuj.go file.
Colin Crossd0be2102019-11-26 16:16:57 -0800210type CriticalUserJourneysMetrics struct {
Patrice Arruda589826b2020-12-15 23:14:55 +0000211 // A list of collected CUJ metrics.
Colin Crossd0be2102019-11-26 16:16:57 -0800212 cujs soong_metrics_proto.CriticalUserJourneysMetrics
213}
214
Patrice Arruda589826b2020-12-15 23:14:55 +0000215// NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics
216// to capture CUJs metrics.
Colin Crossd0be2102019-11-26 16:16:57 -0800217func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
218 return &CriticalUserJourneysMetrics{}
219}
220
Patrice Arruda589826b2020-12-15 23:14:55 +0000221// Add adds a set of collected metrics from an executed critical user journey.
Colin Crossd0be2102019-11-26 16:16:57 -0800222func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
223 c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
224 Name: proto.String(name),
225 Metrics: &metrics.metrics,
226 })
227}
228
Patrice Arruda589826b2020-12-15 23:14:55 +0000229// Dump saves the collected CUJs metrics to the raw protobuf file.
230func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
231 return save(&c.cujs, filename)
Colin Crossd0be2102019-11-26 16:16:57 -0800232}
233
Patrice Arruda589826b2020-12-15 23:14:55 +0000234// save takes a protobuf message, marshals to an array of bytes
235// and is then saved to a file.
236func save(pb proto.Message, filename string) (err error) {
Colin Crossd0be2102019-11-26 16:16:57 -0800237 data, err := proto.Marshal(pb)
Nan Zhang17f27672018-12-12 16:01:49 -0800238 if err != nil {
239 return err
240 }
Patrice Arruda589826b2020-12-15 23:14:55 +0000241
242 tempFilename := filename + ".tmp"
243 if err := ioutil.WriteFile(tempFilename, []byte(data), 0644 /* rw-r--r-- */); err != nil {
Nan Zhang17f27672018-12-12 16:01:49 -0800244 return err
245 }
Patrice Arruda589826b2020-12-15 23:14:55 +0000246
247 if err := os.Rename(tempFilename, filename); err != nil {
Nan Zhang17f27672018-12-12 16:01:49 -0800248 return err
249 }
250
251 return nil
252}