Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 1 | // 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 Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 15 | // Package metrics represents the metrics system for Android Platform Build Systems. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 16 | package metrics |
| 17 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 18 | // 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 Liu | 6e13b40 | 2021-07-27 14:29:06 -0700 | [diff] [blame] | 28 | // 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 Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 33 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 34 | import ( |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 35 | "os" |
Patrice Arruda | add7ea9 | 2020-08-07 17:55:23 +0000 | [diff] [blame] | 36 | "runtime" |
Patrice Arruda | e92c30d | 2020-10-29 11:01:32 -0700 | [diff] [blame] | 37 | "strings" |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 38 | "time" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 39 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 40 | "android/soong/shared" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 41 | "google.golang.org/protobuf/proto" |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 42 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 43 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame^] | 44 | mk_metrics_proto "android/soong/ui/metrics/mk_metrics_proto" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 45 | ) |
| 46 | |
| 47 | const ( |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 48 | // Below is a list of names passed in to the Begin tracing functions. These |
| 49 | // names are used to group a set of metrics. |
| 50 | |
| 51 | // Setup and tear down of the build systems. |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 52 | RunSetupTool = "setup" |
| 53 | RunShutdownTool = "shutdown" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 54 | TestRun = "test" |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 55 | |
| 56 | // List of build system tools. |
| 57 | RunSoong = "soong" |
| 58 | PrimaryNinja = "ninja" |
| 59 | RunKati = "kati" |
| 60 | RunBazel = "bazel" |
| 61 | |
| 62 | // Overall build from building the graph to building the target. |
| 63 | Total = "total" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 64 | ) |
| 65 | |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame^] | 66 | // Metrics is a struct that stores collected metrics during the course of a |
| 67 | // build. It is later dumped to protobuf files. See underlying metrics protos |
| 68 | // for further details on what information is collected. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 69 | type Metrics struct { |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame^] | 70 | // Protobuf containing various top-level build metrics. These include: |
| 71 | // 1. Build identifiers (ex: branch ID, requested product, hostname, |
| 72 | // originating command) |
| 73 | // 2. Per-subprocess top-level metrics (ex: ninja process IO and runtime). |
| 74 | // Note that, since these metrics are reported by soong_ui, there is little |
| 75 | // insight that can be provided into performance breakdowns of individual |
| 76 | // subprocesses. |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 77 | metrics soong_metrics_proto.MetricsBase |
| 78 | |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame^] | 79 | // Protobuf containing metrics pertaining to number of makefiles in a build. |
| 80 | mkMetrics mk_metrics_proto.MkMetrics |
| 81 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 82 | // A list of pending build events. |
Patrice Arruda | 8a44a37 | 2020-12-14 20:55:23 +0000 | [diff] [blame] | 83 | EventTracer *EventTracer |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 86 | // New returns a pointer of Metrics to store a set of metrics. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 87 | func New() (metrics *Metrics) { |
| 88 | m := &Metrics{ |
Patrice Arruda | 457c5d3 | 2020-10-19 11:20:21 -0700 | [diff] [blame] | 89 | metrics: soong_metrics_proto.MetricsBase{}, |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame^] | 90 | mkMetrics: mk_metrics_proto.MkMetrics{}, |
Patrice Arruda | 8a44a37 | 2020-12-14 20:55:23 +0000 | [diff] [blame] | 91 | EventTracer: &EventTracer{}, |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 92 | } |
| 93 | return m |
| 94 | } |
| 95 | |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame^] | 96 | func (m *Metrics) SetTotalMakefiles(total int) { |
| 97 | m.mkMetrics.TotalMakefiles = uint32(total) |
| 98 | } |
| 99 | |
| 100 | func (m *Metrics) SetToplevelMakefiles(total int) { |
| 101 | m.mkMetrics.ToplevelMakefiles = uint32(total) |
| 102 | } |
| 103 | |
| 104 | func (m *Metrics) DumpMkMetrics(outPath string) { |
| 105 | shared.Save(&m.mkMetrics, outPath) |
| 106 | } |
| 107 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 108 | // SetTimeMetrics stores performance information from an executed block of |
| 109 | // code. |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 110 | func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 111 | switch perf.GetName() { |
| 112 | case RunKati: |
| 113 | m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 114 | case RunSoong: |
| 115 | m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf) |
Patrice Arruda | b7cf9ba | 2020-11-13 13:04:17 -0800 | [diff] [blame] | 116 | case RunBazel: |
| 117 | m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 118 | case PrimaryNinja: |
| 119 | m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf) |
Patrice Arruda | 48d55ad | 2020-12-15 19:34:20 +0000 | [diff] [blame] | 120 | case RunSetupTool: |
| 121 | m.metrics.SetupTools = append(m.metrics.SetupTools, &perf) |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 122 | case Total: |
| 123 | m.metrics.Total = &perf |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 127 | // BuildConfig stores information about the build configuration. |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 128 | func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) { |
| 129 | m.metrics.BuildConfig = b |
| 130 | } |
| 131 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 132 | // SystemResourceInfo stores information related to the host system such |
| 133 | // as total CPU and memory. |
Patrice Arruda | 3edfd48 | 2020-10-13 23:58:41 +0000 | [diff] [blame] | 134 | func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) { |
| 135 | m.metrics.SystemResourceInfo = b |
| 136 | } |
| 137 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 138 | // SetMetadataMetrics sets information about the build such as the target |
| 139 | // product, host architecture and out directory. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 140 | func (m *Metrics) SetMetadataMetrics(metadata map[string]string) { |
| 141 | for k, v := range metadata { |
| 142 | switch k { |
| 143 | case "BUILD_ID": |
| 144 | m.metrics.BuildId = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 145 | case "PLATFORM_VERSION_CODENAME": |
| 146 | m.metrics.PlatformVersionCodename = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 147 | case "TARGET_PRODUCT": |
| 148 | m.metrics.TargetProduct = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 149 | case "TARGET_BUILD_VARIANT": |
| 150 | switch v { |
| 151 | case "user": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 152 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 153 | case "userdebug": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 154 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 155 | case "eng": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 156 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 157 | } |
| 158 | case "TARGET_ARCH": |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 159 | m.metrics.TargetArch = arch(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 160 | case "TARGET_ARCH_VARIANT": |
| 161 | m.metrics.TargetArchVariant = proto.String(v) |
| 162 | case "TARGET_CPU_VARIANT": |
| 163 | m.metrics.TargetCpuVariant = proto.String(v) |
| 164 | case "HOST_ARCH": |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 165 | m.metrics.HostArch = arch(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 166 | case "HOST_2ND_ARCH": |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 167 | m.metrics.Host_2NdArch = arch(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 168 | case "HOST_OS_EXTRA": |
| 169 | m.metrics.HostOsExtra = proto.String(v) |
| 170 | case "HOST_CROSS_OS": |
| 171 | m.metrics.HostCrossOs = proto.String(v) |
| 172 | case "HOST_CROSS_ARCH": |
| 173 | m.metrics.HostCrossArch = proto.String(v) |
| 174 | case "HOST_CROSS_2ND_ARCH": |
| 175 | m.metrics.HostCross_2NdArch = proto.String(v) |
| 176 | case "OUT_DIR": |
| 177 | m.metrics.OutDir = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 182 | // arch returns the corresponding MetricsBase_Arch based on the string |
| 183 | // parameter. |
| 184 | func arch(a string) *soong_metrics_proto.MetricsBase_Arch { |
| 185 | switch a { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 186 | case "arm": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 187 | return soong_metrics_proto.MetricsBase_ARM.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 188 | case "arm64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 189 | return soong_metrics_proto.MetricsBase_ARM64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 190 | case "x86": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 191 | return soong_metrics_proto.MetricsBase_X86.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 192 | case "x86_64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 193 | return soong_metrics_proto.MetricsBase_X86_64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 194 | default: |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 195 | return soong_metrics_proto.MetricsBase_UNKNOWN.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 199 | // SetBuildDateTime sets the build date and time. The value written |
| 200 | // to the protobuf file is in seconds. |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 201 | func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) { |
| 202 | m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second)) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 205 | // SetBuildCommand adds the build command specified by the user to the |
| 206 | // list of collected metrics. |
Patrice Arruda | e92c30d | 2020-10-29 11:01:32 -0700 | [diff] [blame] | 207 | func (m *Metrics) SetBuildCommand(cmd []string) { |
| 208 | m.metrics.BuildCommand = proto.String(strings.Join(cmd, " ")) |
| 209 | } |
| 210 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 211 | // Dump exports the collected metrics from the executed build to the file at |
| 212 | // out path. |
| 213 | func (m *Metrics) Dump(out string) error { |
Patrice Arruda | 4fb8adc | 2020-10-12 22:38:06 +0000 | [diff] [blame] | 214 | // ignore the error if the hostname could not be retrieved as it |
| 215 | // is not a critical metric to extract. |
| 216 | if hostname, err := os.Hostname(); err == nil { |
| 217 | m.metrics.Hostname = proto.String(hostname) |
| 218 | } |
Patrice Arruda | add7ea9 | 2020-08-07 17:55:23 +0000 | [diff] [blame] | 219 | m.metrics.HostOs = proto.String(runtime.GOOS) |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 220 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 221 | return shared.Save(&m.metrics, out) |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 224 | // SetSoongBuildMetrics sets the metrics collected from the soong_build |
| 225 | // execution. |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 226 | func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 227 | m.metrics.SoongBuildMetrics = metrics |
| 228 | } |
| 229 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 230 | // A CriticalUserJourneysMetrics is a struct that contains critical user journey |
| 231 | // metrics. These critical user journeys are defined under cuj/cuj.go file. |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 232 | type CriticalUserJourneysMetrics struct { |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 233 | // A list of collected CUJ metrics. |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 234 | cujs soong_metrics_proto.CriticalUserJourneysMetrics |
| 235 | } |
| 236 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 237 | // NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics |
| 238 | // to capture CUJs metrics. |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 239 | func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics { |
| 240 | return &CriticalUserJourneysMetrics{} |
| 241 | } |
| 242 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 243 | // Add adds a set of collected metrics from an executed critical user journey. |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 244 | func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) { |
| 245 | c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{ |
| 246 | Name: proto.String(name), |
| 247 | Metrics: &metrics.metrics, |
| 248 | }) |
| 249 | } |
| 250 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame] | 251 | // Dump saves the collected CUJs metrics to the raw protobuf file. |
| 252 | func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 253 | return shared.Save(&c.cujs, filename) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 254 | } |