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 |
| 28 | // raw protobuf file in the $OUT directory. |
| 29 | // |
| 30 | // There is one additional step that occurs after the raw protobuf file is written. |
| 31 | // If the configuration environment variable ANDROID_ENABLE_METRICS_UPLOAD is |
| 32 | // set with the path, the raw protobuf file is uploaded to the destination. See |
| 33 | // ui/build/upload.go for more details. The filename of the raw protobuf file |
| 34 | // and the list of files to be uploaded is defined in cmd/soong_ui/main.go. |
| 35 | // |
| 36 | // See ui/metrics/event.go for the explanation of what an event is and how |
| 37 | // the metrics system is a stack based system. |
| 38 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 39 | import ( |
| 40 | "io/ioutil" |
| 41 | "os" |
Patrice Arruda | add7ea9 | 2020-08-07 17:55:23 +0000 | [diff] [blame] | 42 | "runtime" |
Patrice Arruda | e92c30d | 2020-10-29 11:01:32 -0700 | [diff] [blame] | 43 | "strings" |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 44 | "time" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 45 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 46 | "github.com/golang/protobuf/proto" |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 47 | |
Patrice Arruda | 5c58b64 | 2020-12-15 19:41:40 +0000 | [diff] [blame] | 48 | "android/soong/ui/metrics/metrics_proto" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 49 | ) |
| 50 | |
| 51 | const ( |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 52 | // Below is a list of names passed in to the Begin tracing functions. These |
| 53 | // names are used to group a set of metrics. |
| 54 | |
| 55 | // Setup and tear down of the build systems. |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 56 | RunSetupTool = "setup" |
| 57 | RunShutdownTool = "shutdown" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 58 | TestRun = "test" |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 59 | |
| 60 | // List of build system tools. |
| 61 | RunSoong = "soong" |
| 62 | PrimaryNinja = "ninja" |
| 63 | RunKati = "kati" |
| 64 | RunBazel = "bazel" |
| 65 | |
| 66 | // Overall build from building the graph to building the target. |
| 67 | Total = "total" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 68 | ) |
| 69 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 70 | // Metrics is a struct that stores collected metrics during the course |
| 71 | // of a build which later is dumped to a MetricsBase protobuf file. |
| 72 | // See ui/metrics/metrics_proto/metrics.proto for further details |
| 73 | // on what information is collected. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 74 | type Metrics struct { |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 75 | // The protobuf message that is later written to the file. |
| 76 | metrics soong_metrics_proto.MetricsBase |
| 77 | |
| 78 | // A list of pending build events. |
Patrice Arruda | 8a44a37 | 2020-12-14 20:55:23 +0000 | [diff] [blame] | 79 | EventTracer *EventTracer |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 82 | // New returns a pointer of Metrics to store a set of metrics. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 83 | func New() (metrics *Metrics) { |
| 84 | m := &Metrics{ |
Patrice Arruda | 457c5d3 | 2020-10-19 11:20:21 -0700 | [diff] [blame] | 85 | metrics: soong_metrics_proto.MetricsBase{}, |
Patrice Arruda | 8a44a37 | 2020-12-14 20:55:23 +0000 | [diff] [blame] | 86 | EventTracer: &EventTracer{}, |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 87 | } |
| 88 | return m |
| 89 | } |
| 90 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 91 | // SetTimeMetrics stores performance information from an executed block of |
| 92 | // code. |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 93 | func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 94 | switch perf.GetName() { |
| 95 | case RunKati: |
| 96 | m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 97 | case RunSoong: |
| 98 | m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf) |
Patrice Arruda | b7cf9ba | 2020-11-13 13:04:17 -0800 | [diff] [blame] | 99 | case RunBazel: |
| 100 | m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 101 | case PrimaryNinja: |
| 102 | m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf) |
Patrice Arruda | 48d55ad | 2020-12-15 19:34:20 +0000 | [diff] [blame] | 103 | case RunSetupTool: |
| 104 | m.metrics.SetupTools = append(m.metrics.SetupTools, &perf) |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 105 | case Total: |
| 106 | m.metrics.Total = &perf |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 110 | // BuildConfig stores information about the build configuration. |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 111 | func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) { |
| 112 | m.metrics.BuildConfig = b |
| 113 | } |
| 114 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 115 | // SystemResourceInfo stores information related to the host system such |
| 116 | // as total CPU and memory. |
Patrice Arruda | 3edfd48 | 2020-10-13 23:58:41 +0000 | [diff] [blame] | 117 | func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) { |
| 118 | m.metrics.SystemResourceInfo = b |
| 119 | } |
| 120 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 121 | // SetMetadataMetrics sets information about the build such as the target |
| 122 | // product, host architecture and out directory. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 123 | func (m *Metrics) SetMetadataMetrics(metadata map[string]string) { |
| 124 | for k, v := range metadata { |
| 125 | switch k { |
| 126 | case "BUILD_ID": |
| 127 | m.metrics.BuildId = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 128 | case "PLATFORM_VERSION_CODENAME": |
| 129 | m.metrics.PlatformVersionCodename = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 130 | case "TARGET_PRODUCT": |
| 131 | m.metrics.TargetProduct = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 132 | case "TARGET_BUILD_VARIANT": |
| 133 | switch v { |
| 134 | case "user": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 135 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 136 | case "userdebug": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 137 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 138 | case "eng": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 139 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 140 | } |
| 141 | case "TARGET_ARCH": |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 142 | m.metrics.TargetArch = arch(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 143 | case "TARGET_ARCH_VARIANT": |
| 144 | m.metrics.TargetArchVariant = proto.String(v) |
| 145 | case "TARGET_CPU_VARIANT": |
| 146 | m.metrics.TargetCpuVariant = proto.String(v) |
| 147 | case "HOST_ARCH": |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 148 | m.metrics.HostArch = arch(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 149 | case "HOST_2ND_ARCH": |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 150 | m.metrics.Host_2NdArch = arch(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 151 | case "HOST_OS_EXTRA": |
| 152 | m.metrics.HostOsExtra = proto.String(v) |
| 153 | case "HOST_CROSS_OS": |
| 154 | m.metrics.HostCrossOs = proto.String(v) |
| 155 | case "HOST_CROSS_ARCH": |
| 156 | m.metrics.HostCrossArch = proto.String(v) |
| 157 | case "HOST_CROSS_2ND_ARCH": |
| 158 | m.metrics.HostCross_2NdArch = proto.String(v) |
| 159 | case "OUT_DIR": |
| 160 | m.metrics.OutDir = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 165 | // arch returns the corresponding MetricsBase_Arch based on the string |
| 166 | // parameter. |
| 167 | func arch(a string) *soong_metrics_proto.MetricsBase_Arch { |
| 168 | switch a { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 169 | case "arm": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 170 | return soong_metrics_proto.MetricsBase_ARM.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 171 | case "arm64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 172 | return soong_metrics_proto.MetricsBase_ARM64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 173 | case "x86": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 174 | return soong_metrics_proto.MetricsBase_X86.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 175 | case "x86_64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 176 | return soong_metrics_proto.MetricsBase_X86_64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 177 | default: |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 178 | return soong_metrics_proto.MetricsBase_UNKNOWN.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 182 | // SetBuildDateTime sets the build date and time. The value written |
| 183 | // to the protobuf file is in seconds. |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 184 | func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) { |
| 185 | m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second)) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 188 | // SetBuildCommand adds the build command specified by the user to the |
| 189 | // list of collected metrics. |
Patrice Arruda | e92c30d | 2020-10-29 11:01:32 -0700 | [diff] [blame] | 190 | func (m *Metrics) SetBuildCommand(cmd []string) { |
| 191 | m.metrics.BuildCommand = proto.String(strings.Join(cmd, " ")) |
| 192 | } |
| 193 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 194 | // Dump exports the collected metrics from the executed build to the file at |
| 195 | // out path. |
| 196 | func (m *Metrics) Dump(out string) error { |
Patrice Arruda | 4fb8adc | 2020-10-12 22:38:06 +0000 | [diff] [blame] | 197 | // ignore the error if the hostname could not be retrieved as it |
| 198 | // is not a critical metric to extract. |
| 199 | if hostname, err := os.Hostname(); err == nil { |
| 200 | m.metrics.Hostname = proto.String(hostname) |
| 201 | } |
Patrice Arruda | add7ea9 | 2020-08-07 17:55:23 +0000 | [diff] [blame] | 202 | m.metrics.HostOs = proto.String(runtime.GOOS) |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 203 | |
| 204 | return save(&m.metrics, out) |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 207 | // SetSoongBuildMetrics sets the metrics collected from the soong_build |
| 208 | // execution. |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 209 | func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 210 | m.metrics.SoongBuildMetrics = metrics |
| 211 | } |
| 212 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 213 | // A CriticalUserJourneysMetrics is a struct that contains critical user journey |
| 214 | // metrics. These critical user journeys are defined under cuj/cuj.go file. |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 215 | type CriticalUserJourneysMetrics struct { |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 216 | // A list of collected CUJ metrics. |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 217 | cujs soong_metrics_proto.CriticalUserJourneysMetrics |
| 218 | } |
| 219 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 220 | // NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics |
| 221 | // to capture CUJs metrics. |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 222 | func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics { |
| 223 | return &CriticalUserJourneysMetrics{} |
| 224 | } |
| 225 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 226 | // 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] | 227 | func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) { |
| 228 | c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{ |
| 229 | Name: proto.String(name), |
| 230 | Metrics: &metrics.metrics, |
| 231 | }) |
| 232 | } |
| 233 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 234 | // Dump saves the collected CUJs metrics to the raw protobuf file. |
| 235 | func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) { |
| 236 | return save(&c.cujs, filename) |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 239 | // save takes a protobuf message, marshals to an array of bytes |
| 240 | // and is then saved to a file. |
| 241 | func save(pb proto.Message, filename string) (err error) { |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 242 | data, err := proto.Marshal(pb) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 243 | if err != nil { |
| 244 | return err |
| 245 | } |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 246 | |
| 247 | tempFilename := filename + ".tmp" |
| 248 | if err := ioutil.WriteFile(tempFilename, []byte(data), 0644 /* rw-r--r-- */); err != nil { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 249 | return err |
| 250 | } |
Patrice Arruda | 589826b | 2020-12-15 23:14:55 +0000 | [diff] [blame^] | 251 | |
| 252 | if err := os.Rename(tempFilename, filename); err != nil { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 253 | return err |
| 254 | } |
| 255 | |
| 256 | return nil |
| 257 | } |