blob: 4a275a88f65ef9b6d44c939bef055b6f6e64d670 [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 (
Nan Zhang17f27672018-12-12 16:01:49 -080035 "os"
Patrice Arrudaadd7ea92020-08-07 17:55:23 +000036 "runtime"
Patrice Arrudae92c30d2020-10-29 11:01:32 -070037 "strings"
Patrice Arruda73c790f2020-07-13 23:01:18 +000038 "time"
Nan Zhang17f27672018-12-12 16:01:49 -080039
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050040 "android/soong/shared"
Jeongik Cha28c1fe52023-03-07 15:19:44 +090041
Dan Willemsen4591b642021-05-24 14:24:12 -070042 "google.golang.org/protobuf/proto"
Colin Crossd0be2102019-11-26 16:16:57 -080043
Dan Willemsen4591b642021-05-24 14:24:12 -070044 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Chris Parsons53f68ae2022-03-03 12:01:40 -050045 mk_metrics_proto "android/soong/ui/metrics/mk_metrics_proto"
Nan Zhang17f27672018-12-12 16:01:49 -080046)
47
48const (
Patrice Arruda589826b2020-12-15 23:14:55 +000049 // Below is a list of names passed in to the Begin tracing functions. These
50 // names are used to group a set of metrics.
51
52 // Setup and tear down of the build systems.
Patrice Arruda62f1bf22020-07-07 12:48:26 +000053 RunSetupTool = "setup"
54 RunShutdownTool = "shutdown"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000055 TestRun = "test"
Patrice Arruda589826b2020-12-15 23:14:55 +000056
57 // List of build system tools.
58 RunSoong = "soong"
59 PrimaryNinja = "ninja"
60 RunKati = "kati"
61 RunBazel = "bazel"
62
63 // Overall build from building the graph to building the target.
64 Total = "total"
Nan Zhang17f27672018-12-12 16:01:49 -080065)
66
Chris Parsons53f68ae2022-03-03 12:01:40 -050067// Metrics is a struct that stores collected metrics during the course of a
68// build. It is later dumped to protobuf files. See underlying metrics protos
69// for further details on what information is collected.
Nan Zhang17f27672018-12-12 16:01:49 -080070type Metrics struct {
Chris Parsons53f68ae2022-03-03 12:01:40 -050071 // Protobuf containing various top-level build metrics. These include:
72 // 1. Build identifiers (ex: branch ID, requested product, hostname,
73 // originating command)
74 // 2. Per-subprocess top-level metrics (ex: ninja process IO and runtime).
75 // Note that, since these metrics are reported by soong_ui, there is little
76 // insight that can be provided into performance breakdowns of individual
77 // subprocesses.
Patrice Arruda589826b2020-12-15 23:14:55 +000078 metrics soong_metrics_proto.MetricsBase
79
Chris Parsons53f68ae2022-03-03 12:01:40 -050080 // Protobuf containing metrics pertaining to number of makefiles in a build.
81 mkMetrics mk_metrics_proto.MkMetrics
82
Patrice Arruda589826b2020-12-15 23:14:55 +000083 // A list of pending build events.
Patrice Arruda8a44a372020-12-14 20:55:23 +000084 EventTracer *EventTracer
Nan Zhang17f27672018-12-12 16:01:49 -080085}
86
Patrice Arruda589826b2020-12-15 23:14:55 +000087// New returns a pointer of Metrics to store a set of metrics.
Nan Zhang17f27672018-12-12 16:01:49 -080088func New() (metrics *Metrics) {
89 m := &Metrics{
Patrice Arruda457c5d32020-10-19 11:20:21 -070090 metrics: soong_metrics_proto.MetricsBase{},
Chris Parsons53f68ae2022-03-03 12:01:40 -050091 mkMetrics: mk_metrics_proto.MkMetrics{},
Patrice Arruda8a44a372020-12-14 20:55:23 +000092 EventTracer: &EventTracer{},
Nan Zhang17f27672018-12-12 16:01:49 -080093 }
94 return m
95}
96
Chris Parsons53f68ae2022-03-03 12:01:40 -050097func (m *Metrics) SetTotalMakefiles(total int) {
98 m.mkMetrics.TotalMakefiles = uint32(total)
99}
100
101func (m *Metrics) SetToplevelMakefiles(total int) {
102 m.mkMetrics.ToplevelMakefiles = uint32(total)
103}
104
105func (m *Metrics) DumpMkMetrics(outPath string) {
106 shared.Save(&m.mkMetrics, outPath)
107}
108
Patrice Arruda589826b2020-12-15 23:14:55 +0000109// SetTimeMetrics stores performance information from an executed block of
110// code.
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700111func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
Nan Zhang17f27672018-12-12 16:01:49 -0800112 switch perf.GetName() {
113 case RunKati:
114 m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -0800115 case RunSoong:
116 m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -0800117 case RunBazel:
118 m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -0800119 case PrimaryNinja:
120 m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
Patrice Arruda48d55ad2020-12-15 19:34:20 +0000121 case RunSetupTool:
122 m.metrics.SetupTools = append(m.metrics.SetupTools, &perf)
Colin Cross74cda722020-01-16 15:25:50 -0800123 case Total:
124 m.metrics.Total = &perf
Nan Zhang17f27672018-12-12 16:01:49 -0800125 }
126}
127
Jeongik Cha28c1fe52023-03-07 15:19:44 +0900128func (m *Metrics) SetCriticalPathInfo(criticalPathInfo soong_metrics_proto.CriticalPathInfo) {
129 m.metrics.CriticalPathInfo = &criticalPathInfo
130}
131
Liz Kammerf2a80c62022-10-21 10:42:35 -0400132// SetFatalOrPanicMessage stores a non-zero exit and the relevant message in the latest event if
133// available or the metrics base.
134func (m *Metrics) SetFatalOrPanicMessage(errMsg string) {
135 if m == nil {
136 return
137 }
138 if event := m.EventTracer.peek(); event != nil {
139 event.nonZeroExitCode = true
140 event.errorMsg = &errMsg
141 } else {
142 m.metrics.ErrorMessage = proto.String(errMsg)
143 }
144 m.metrics.NonZeroExit = proto.Bool(true)
145}
146
Patrice Arruda589826b2020-12-15 23:14:55 +0000147// BuildConfig stores information about the build configuration.
Patrice Arruda96850362020-08-11 20:41:11 +0000148func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
149 m.metrics.BuildConfig = b
150}
151
Patrice Arruda589826b2020-12-15 23:14:55 +0000152// SystemResourceInfo stores information related to the host system such
153// as total CPU and memory.
Patrice Arruda3edfd482020-10-13 23:58:41 +0000154func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
155 m.metrics.SystemResourceInfo = b
156}
157
David Goldsmith62243a32022-04-08 13:42:04 +0000158// ExpConfigFetcher stores information about the expconfigfetcher.
159func (m *Metrics) ExpConfigFetcher(b *soong_metrics_proto.ExpConfigFetcher) {
160 m.metrics.ExpConfigFetcher = b
161}
162
Patrice Arruda589826b2020-12-15 23:14:55 +0000163// SetMetadataMetrics sets information about the build such as the target
164// product, host architecture and out directory.
Nan Zhang17f27672018-12-12 16:01:49 -0800165func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
166 for k, v := range metadata {
167 switch k {
168 case "BUILD_ID":
169 m.metrics.BuildId = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800170 case "PLATFORM_VERSION_CODENAME":
171 m.metrics.PlatformVersionCodename = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800172 case "TARGET_PRODUCT":
173 m.metrics.TargetProduct = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800174 case "TARGET_BUILD_VARIANT":
175 switch v {
176 case "user":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700177 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800178 case "userdebug":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700179 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800180 case "eng":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700181 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800182 }
183 case "TARGET_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000184 m.metrics.TargetArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800185 case "TARGET_ARCH_VARIANT":
186 m.metrics.TargetArchVariant = proto.String(v)
187 case "TARGET_CPU_VARIANT":
188 m.metrics.TargetCpuVariant = proto.String(v)
189 case "HOST_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000190 m.metrics.HostArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800191 case "HOST_2ND_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000192 m.metrics.Host_2NdArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800193 case "HOST_OS_EXTRA":
194 m.metrics.HostOsExtra = proto.String(v)
195 case "HOST_CROSS_OS":
196 m.metrics.HostCrossOs = proto.String(v)
197 case "HOST_CROSS_ARCH":
198 m.metrics.HostCrossArch = proto.String(v)
199 case "HOST_CROSS_2ND_ARCH":
200 m.metrics.HostCross_2NdArch = proto.String(v)
201 case "OUT_DIR":
202 m.metrics.OutDir = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800203 }
204 }
205}
206
Patrice Arruda589826b2020-12-15 23:14:55 +0000207// arch returns the corresponding MetricsBase_Arch based on the string
208// parameter.
209func arch(a string) *soong_metrics_proto.MetricsBase_Arch {
210 switch a {
Nan Zhang17f27672018-12-12 16:01:49 -0800211 case "arm":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700212 return soong_metrics_proto.MetricsBase_ARM.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800213 case "arm64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700214 return soong_metrics_proto.MetricsBase_ARM64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800215 case "x86":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700216 return soong_metrics_proto.MetricsBase_X86.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800217 case "x86_64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700218 return soong_metrics_proto.MetricsBase_X86_64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800219 default:
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700220 return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800221 }
222}
223
Patrice Arruda589826b2020-12-15 23:14:55 +0000224// SetBuildDateTime sets the build date and time. The value written
225// to the protobuf file is in seconds.
Patrice Arruda73c790f2020-07-13 23:01:18 +0000226func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
227 m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
Nan Zhang17f27672018-12-12 16:01:49 -0800228}
229
Patrice Arruda589826b2020-12-15 23:14:55 +0000230// SetBuildCommand adds the build command specified by the user to the
231// list of collected metrics.
Patrice Arrudae92c30d2020-10-29 11:01:32 -0700232func (m *Metrics) SetBuildCommand(cmd []string) {
233 m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
234}
235
Jason Wu2520f5e2023-05-30 19:45:36 -0400236// AddChangedEnvironmentVariable adds the changed environment variable to
237// ChangedEnvironmentVariable field.
238func (m *Metrics) AddChangedEnvironmentVariable(ChangedEnvironmentVariable string) {
239 m.metrics.ChangedEnvironmentVariable = append(m.metrics.ChangedEnvironmentVariable,
240 ChangedEnvironmentVariable)
241}
242
Patrice Arruda589826b2020-12-15 23:14:55 +0000243// Dump exports the collected metrics from the executed build to the file at
244// out path.
245func (m *Metrics) Dump(out string) error {
Patrice Arruda4fb8adc2020-10-12 22:38:06 +0000246 // ignore the error if the hostname could not be retrieved as it
247 // is not a critical metric to extract.
248 if hostname, err := os.Hostname(); err == nil {
249 m.metrics.Hostname = proto.String(hostname)
250 }
Patrice Arrudaadd7ea92020-08-07 17:55:23 +0000251 m.metrics.HostOs = proto.String(runtime.GOOS)
Patrice Arruda589826b2020-12-15 23:14:55 +0000252
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -0500253 return shared.Save(&m.metrics, out)
Colin Crossd0be2102019-11-26 16:16:57 -0800254}
255
Patrice Arruda589826b2020-12-15 23:14:55 +0000256// SetSoongBuildMetrics sets the metrics collected from the soong_build
257// execution.
Colin Crossb72c9092020-02-10 11:23:49 -0800258func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
259 m.metrics.SoongBuildMetrics = metrics
260}
261
Patrice Arruda589826b2020-12-15 23:14:55 +0000262// A CriticalUserJourneysMetrics is a struct that contains critical user journey
263// metrics. These critical user journeys are defined under cuj/cuj.go file.
Colin Crossd0be2102019-11-26 16:16:57 -0800264type CriticalUserJourneysMetrics struct {
Patrice Arruda589826b2020-12-15 23:14:55 +0000265 // A list of collected CUJ metrics.
Colin Crossd0be2102019-11-26 16:16:57 -0800266 cujs soong_metrics_proto.CriticalUserJourneysMetrics
267}
268
Patrice Arruda589826b2020-12-15 23:14:55 +0000269// NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics
270// to capture CUJs metrics.
Colin Crossd0be2102019-11-26 16:16:57 -0800271func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
272 return &CriticalUserJourneysMetrics{}
273}
274
Patrice Arruda589826b2020-12-15 23:14:55 +0000275// Add adds a set of collected metrics from an executed critical user journey.
Colin Crossd0be2102019-11-26 16:16:57 -0800276func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
277 c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
278 Name: proto.String(name),
279 Metrics: &metrics.metrics,
280 })
281}
282
Patrice Arruda589826b2020-12-15 23:14:55 +0000283// Dump saves the collected CUJs metrics to the raw protobuf file.
284func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -0500285 return shared.Save(&c.cujs, filename)
Nan Zhang17f27672018-12-12 16:01:49 -0800286}