blob: a282e2030b53e2156f9fffa674ba22c520d2d443 [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 (
MarkDacek6614d9c2022-12-07 21:57:38 +000035 "fmt"
Nan Zhang17f27672018-12-12 16:01:49 -080036 "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
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050041 "android/soong/shared"
Jeongik Cha28c1fe52023-03-07 15:19:44 +090042
Dan Willemsen4591b642021-05-24 14:24:12 -070043 "google.golang.org/protobuf/proto"
Colin Crossd0be2102019-11-26 16:16:57 -080044
Dan Willemsen4591b642021-05-24 14:24:12 -070045 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Chris Parsons53f68ae2022-03-03 12:01:40 -050046 mk_metrics_proto "android/soong/ui/metrics/mk_metrics_proto"
Nan Zhang17f27672018-12-12 16:01:49 -080047)
48
49const (
Patrice Arruda589826b2020-12-15 23:14:55 +000050 // Below is a list of names passed in to the Begin tracing functions. These
51 // names are used to group a set of metrics.
52
53 // Setup and tear down of the build systems.
Patrice Arruda62f1bf22020-07-07 12:48:26 +000054 RunSetupTool = "setup"
55 RunShutdownTool = "shutdown"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000056 TestRun = "test"
Patrice Arruda589826b2020-12-15 23:14:55 +000057
58 // List of build system tools.
59 RunSoong = "soong"
60 PrimaryNinja = "ninja"
61 RunKati = "kati"
62 RunBazel = "bazel"
63
64 // Overall build from building the graph to building the target.
65 Total = "total"
Nan Zhang17f27672018-12-12 16:01:49 -080066)
67
Chris Parsons53f68ae2022-03-03 12:01:40 -050068// Metrics is a struct that stores collected metrics during the course of a
69// build. It is later dumped to protobuf files. See underlying metrics protos
70// for further details on what information is collected.
Nan Zhang17f27672018-12-12 16:01:49 -080071type Metrics struct {
Chris Parsons53f68ae2022-03-03 12:01:40 -050072 // Protobuf containing various top-level build metrics. These include:
73 // 1. Build identifiers (ex: branch ID, requested product, hostname,
74 // originating command)
75 // 2. Per-subprocess top-level metrics (ex: ninja process IO and runtime).
76 // Note that, since these metrics are reported by soong_ui, there is little
77 // insight that can be provided into performance breakdowns of individual
78 // subprocesses.
Patrice Arruda589826b2020-12-15 23:14:55 +000079 metrics soong_metrics_proto.MetricsBase
80
Chris Parsons53f68ae2022-03-03 12:01:40 -050081 // Protobuf containing metrics pertaining to number of makefiles in a build.
82 mkMetrics mk_metrics_proto.MkMetrics
83
Patrice Arruda589826b2020-12-15 23:14:55 +000084 // A list of pending build events.
Patrice Arruda8a44a372020-12-14 20:55:23 +000085 EventTracer *EventTracer
Nan Zhang17f27672018-12-12 16:01:49 -080086}
87
Patrice Arruda589826b2020-12-15 23:14:55 +000088// New returns a pointer of Metrics to store a set of metrics.
Nan Zhang17f27672018-12-12 16:01:49 -080089func New() (metrics *Metrics) {
90 m := &Metrics{
Patrice Arruda457c5d32020-10-19 11:20:21 -070091 metrics: soong_metrics_proto.MetricsBase{},
Chris Parsons53f68ae2022-03-03 12:01:40 -050092 mkMetrics: mk_metrics_proto.MkMetrics{},
Patrice Arruda8a44a372020-12-14 20:55:23 +000093 EventTracer: &EventTracer{},
Nan Zhang17f27672018-12-12 16:01:49 -080094 }
95 return m
96}
97
Chris Parsons53f68ae2022-03-03 12:01:40 -050098func (m *Metrics) SetTotalMakefiles(total int) {
99 m.mkMetrics.TotalMakefiles = uint32(total)
100}
101
102func (m *Metrics) SetToplevelMakefiles(total int) {
103 m.mkMetrics.ToplevelMakefiles = uint32(total)
104}
105
106func (m *Metrics) DumpMkMetrics(outPath string) {
107 shared.Save(&m.mkMetrics, outPath)
108}
109
Patrice Arruda589826b2020-12-15 23:14:55 +0000110// SetTimeMetrics stores performance information from an executed block of
111// code.
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700112func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
Nan Zhang17f27672018-12-12 16:01:49 -0800113 switch perf.GetName() {
114 case RunKati:
115 m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -0800116 case RunSoong:
117 m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -0800118 case RunBazel:
119 m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -0800120 case PrimaryNinja:
121 m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
Patrice Arruda48d55ad2020-12-15 19:34:20 +0000122 case RunSetupTool:
123 m.metrics.SetupTools = append(m.metrics.SetupTools, &perf)
Colin Cross74cda722020-01-16 15:25:50 -0800124 case Total:
125 m.metrics.Total = &perf
Nan Zhang17f27672018-12-12 16:01:49 -0800126 }
127}
128
Jeongik Cha28c1fe52023-03-07 15:19:44 +0900129func (m *Metrics) SetCriticalPathInfo(criticalPathInfo soong_metrics_proto.CriticalPathInfo) {
130 m.metrics.CriticalPathInfo = &criticalPathInfo
131}
132
Liz Kammerf2a80c62022-10-21 10:42:35 -0400133// SetFatalOrPanicMessage stores a non-zero exit and the relevant message in the latest event if
134// available or the metrics base.
135func (m *Metrics) SetFatalOrPanicMessage(errMsg string) {
136 if m == nil {
137 return
138 }
139 if event := m.EventTracer.peek(); event != nil {
140 event.nonZeroExitCode = true
141 event.errorMsg = &errMsg
142 } else {
143 m.metrics.ErrorMessage = proto.String(errMsg)
144 }
145 m.metrics.NonZeroExit = proto.Bool(true)
146}
147
Patrice Arruda589826b2020-12-15 23:14:55 +0000148// BuildConfig stores information about the build configuration.
Patrice Arruda96850362020-08-11 20:41:11 +0000149func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
150 m.metrics.BuildConfig = b
151}
152
Patrice Arruda589826b2020-12-15 23:14:55 +0000153// SystemResourceInfo stores information related to the host system such
154// as total CPU and memory.
Patrice Arruda3edfd482020-10-13 23:58:41 +0000155func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
156 m.metrics.SystemResourceInfo = b
157}
158
David Goldsmith62243a32022-04-08 13:42:04 +0000159// ExpConfigFetcher stores information about the expconfigfetcher.
160func (m *Metrics) ExpConfigFetcher(b *soong_metrics_proto.ExpConfigFetcher) {
161 m.metrics.ExpConfigFetcher = b
162}
163
Patrice Arruda589826b2020-12-15 23:14:55 +0000164// SetMetadataMetrics sets information about the build such as the target
165// product, host architecture and out directory.
Nan Zhang17f27672018-12-12 16:01:49 -0800166func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
167 for k, v := range metadata {
168 switch k {
169 case "BUILD_ID":
170 m.metrics.BuildId = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800171 case "PLATFORM_VERSION_CODENAME":
172 m.metrics.PlatformVersionCodename = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800173 case "TARGET_PRODUCT":
174 m.metrics.TargetProduct = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800175 case "TARGET_BUILD_VARIANT":
176 switch v {
177 case "user":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700178 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800179 case "userdebug":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700180 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800181 case "eng":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700182 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800183 }
184 case "TARGET_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000185 m.metrics.TargetArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800186 case "TARGET_ARCH_VARIANT":
187 m.metrics.TargetArchVariant = proto.String(v)
188 case "TARGET_CPU_VARIANT":
189 m.metrics.TargetCpuVariant = proto.String(v)
190 case "HOST_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000191 m.metrics.HostArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800192 case "HOST_2ND_ARCH":
Patrice Arruda589826b2020-12-15 23:14:55 +0000193 m.metrics.Host_2NdArch = arch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800194 case "HOST_OS_EXTRA":
195 m.metrics.HostOsExtra = proto.String(v)
196 case "HOST_CROSS_OS":
197 m.metrics.HostCrossOs = proto.String(v)
198 case "HOST_CROSS_ARCH":
199 m.metrics.HostCrossArch = proto.String(v)
200 case "HOST_CROSS_2ND_ARCH":
201 m.metrics.HostCross_2NdArch = proto.String(v)
202 case "OUT_DIR":
203 m.metrics.OutDir = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800204 }
205 }
206}
207
Patrice Arruda589826b2020-12-15 23:14:55 +0000208// arch returns the corresponding MetricsBase_Arch based on the string
209// parameter.
210func arch(a string) *soong_metrics_proto.MetricsBase_Arch {
211 switch a {
Nan Zhang17f27672018-12-12 16:01:49 -0800212 case "arm":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700213 return soong_metrics_proto.MetricsBase_ARM.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800214 case "arm64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700215 return soong_metrics_proto.MetricsBase_ARM64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800216 case "x86":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700217 return soong_metrics_proto.MetricsBase_X86.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800218 case "x86_64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700219 return soong_metrics_proto.MetricsBase_X86_64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800220 default:
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700221 return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800222 }
223}
224
Patrice Arruda589826b2020-12-15 23:14:55 +0000225// SetBuildDateTime sets the build date and time. The value written
226// to the protobuf file is in seconds.
Patrice Arruda73c790f2020-07-13 23:01:18 +0000227func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
228 m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
Nan Zhang17f27672018-12-12 16:01:49 -0800229}
230
MarkDacekd33c2fd2023-05-04 20:40:04 +0000231func (m *Metrics) UpdateTotalRealTimeAndNonZeroExit(data []byte, bazelExitCode int32) error {
MarkDacek6614d9c2022-12-07 21:57:38 +0000232 if err := proto.Unmarshal(data, &m.metrics); err != nil {
233 return fmt.Errorf("Failed to unmarshal proto", err)
234 }
235 startTime := *m.metrics.Total.StartTime
236 endTime := uint64(time.Now().UnixNano())
237
238 *m.metrics.Total.RealTime = *proto.Uint64(endTime - startTime)
MarkDacekd33c2fd2023-05-04 20:40:04 +0000239
240 bazelError := bazelExitCode != 0
241 m.metrics.NonZeroExit = proto.Bool(bazelError)
MarkDacek6614d9c2022-12-07 21:57:38 +0000242 return nil
243}
244
Patrice Arruda589826b2020-12-15 23:14:55 +0000245// SetBuildCommand adds the build command specified by the user to the
246// list of collected metrics.
Patrice Arrudae92c30d2020-10-29 11:01:32 -0700247func (m *Metrics) SetBuildCommand(cmd []string) {
248 m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
249}
250
Patrice Arruda589826b2020-12-15 23:14:55 +0000251// Dump exports the collected metrics from the executed build to the file at
252// out path.
253func (m *Metrics) Dump(out string) error {
Patrice Arruda4fb8adc2020-10-12 22:38:06 +0000254 // ignore the error if the hostname could not be retrieved as it
255 // is not a critical metric to extract.
256 if hostname, err := os.Hostname(); err == nil {
257 m.metrics.Hostname = proto.String(hostname)
258 }
Patrice Arrudaadd7ea92020-08-07 17:55:23 +0000259 m.metrics.HostOs = proto.String(runtime.GOOS)
Patrice Arruda589826b2020-12-15 23:14:55 +0000260
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -0500261 return shared.Save(&m.metrics, out)
Colin Crossd0be2102019-11-26 16:16:57 -0800262}
263
Patrice Arruda589826b2020-12-15 23:14:55 +0000264// SetSoongBuildMetrics sets the metrics collected from the soong_build
265// execution.
Colin Crossb72c9092020-02-10 11:23:49 -0800266func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
267 m.metrics.SoongBuildMetrics = metrics
268}
269
Patrice Arruda589826b2020-12-15 23:14:55 +0000270// A CriticalUserJourneysMetrics is a struct that contains critical user journey
271// metrics. These critical user journeys are defined under cuj/cuj.go file.
Colin Crossd0be2102019-11-26 16:16:57 -0800272type CriticalUserJourneysMetrics struct {
Patrice Arruda589826b2020-12-15 23:14:55 +0000273 // A list of collected CUJ metrics.
Colin Crossd0be2102019-11-26 16:16:57 -0800274 cujs soong_metrics_proto.CriticalUserJourneysMetrics
275}
276
Patrice Arruda589826b2020-12-15 23:14:55 +0000277// NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics
278// to capture CUJs metrics.
Colin Crossd0be2102019-11-26 16:16:57 -0800279func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
280 return &CriticalUserJourneysMetrics{}
281}
282
Patrice Arruda589826b2020-12-15 23:14:55 +0000283// Add adds a set of collected metrics from an executed critical user journey.
Colin Crossd0be2102019-11-26 16:16:57 -0800284func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
285 c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
286 Name: proto.String(name),
287 Metrics: &metrics.metrics,
288 })
289}
290
Patrice Arruda589826b2020-12-15 23:14:55 +0000291// Dump saves the collected CUJs metrics to the raw protobuf file.
292func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -0500293 return shared.Save(&c.cujs, filename)
Nan Zhang17f27672018-12-12 16:01:49 -0800294}