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 15// Package metrics represents the metrics system for Android Platform Build Systems. 16package metrics 17 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 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. 33 34import ( 35 "os" 36 "runtime" 37 "strings" 38 "time" 39 40 "android/soong/shared" 41 42 "google.golang.org/protobuf/proto" 43 44 soong_metrics_proto "android/soong/ui/metrics/metrics_proto" 45 mk_metrics_proto "android/soong/ui/metrics/mk_metrics_proto" 46) 47 48const ( 49 // 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. 53 RunSetupTool = "setup" 54 RunShutdownTool = "shutdown" 55 TestRun = "test" 56 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" 65) 66 67// 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. 70type Metrics struct { 71 // 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. 78 metrics soong_metrics_proto.MetricsBase 79 80 // Protobuf containing metrics pertaining to number of makefiles in a build. 81 mkMetrics mk_metrics_proto.MkMetrics 82 83 // A list of pending build events. 84 EventTracer *EventTracer 85} 86 87// New returns a pointer of Metrics to store a set of metrics. 88func New() (metrics *Metrics) { 89 m := &Metrics{ 90 metrics: soong_metrics_proto.MetricsBase{}, 91 mkMetrics: mk_metrics_proto.MkMetrics{}, 92 EventTracer: &EventTracer{}, 93 } 94 return m 95} 96 97func (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 109// SetTimeMetrics stores performance information from an executed block of 110// code. 111func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) { 112 switch perf.GetName() { 113 case RunKati: 114 m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf) 115 case RunSoong: 116 m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf) 117 case RunBazel: 118 m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf) 119 case PrimaryNinja: 120 m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf) 121 case RunSetupTool: 122 m.metrics.SetupTools = append(m.metrics.SetupTools, &perf) 123 case Total: 124 m.metrics.Total = &perf 125 } 126} 127 128// BuildConfig stores information about the build configuration. 129func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) { 130 m.metrics.BuildConfig = b 131} 132 133// SystemResourceInfo stores information related to the host system such 134// as total CPU and memory. 135func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) { 136 m.metrics.SystemResourceInfo = b 137} 138 139// ExpConfigFetcher stores information about the expconfigfetcher. 140func (m *Metrics) ExpConfigFetcher(b *soong_metrics_proto.ExpConfigFetcher) { 141 m.metrics.ExpConfigFetcher = b 142} 143 144// SetMetadataMetrics sets information about the build such as the target 145// product, host architecture and out directory. 146func (m *Metrics) SetMetadataMetrics(metadata map[string]string) { 147 for k, v := range metadata { 148 switch k { 149 case "BUILD_ID": 150 m.metrics.BuildId = proto.String(v) 151 case "PLATFORM_VERSION_CODENAME": 152 m.metrics.PlatformVersionCodename = proto.String(v) 153 case "TARGET_PRODUCT": 154 m.metrics.TargetProduct = proto.String(v) 155 case "TARGET_BUILD_VARIANT": 156 switch v { 157 case "user": 158 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum() 159 case "userdebug": 160 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum() 161 case "eng": 162 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum() 163 } 164 case "TARGET_ARCH": 165 m.metrics.TargetArch = arch(v) 166 case "TARGET_ARCH_VARIANT": 167 m.metrics.TargetArchVariant = proto.String(v) 168 case "TARGET_CPU_VARIANT": 169 m.metrics.TargetCpuVariant = proto.String(v) 170 case "HOST_ARCH": 171 m.metrics.HostArch = arch(v) 172 case "HOST_2ND_ARCH": 173 m.metrics.Host_2NdArch = arch(v) 174 case "HOST_OS_EXTRA": 175 m.metrics.HostOsExtra = proto.String(v) 176 case "HOST_CROSS_OS": 177 m.metrics.HostCrossOs = proto.String(v) 178 case "HOST_CROSS_ARCH": 179 m.metrics.HostCrossArch = proto.String(v) 180 case "HOST_CROSS_2ND_ARCH": 181 m.metrics.HostCross_2NdArch = proto.String(v) 182 case "OUT_DIR": 183 m.metrics.OutDir = proto.String(v) 184 } 185 } 186} 187 188// arch returns the corresponding MetricsBase_Arch based on the string 189// parameter. 190func arch(a string) *soong_metrics_proto.MetricsBase_Arch { 191 switch a { 192 case "arm": 193 return soong_metrics_proto.MetricsBase_ARM.Enum() 194 case "arm64": 195 return soong_metrics_proto.MetricsBase_ARM64.Enum() 196 case "x86": 197 return soong_metrics_proto.MetricsBase_X86.Enum() 198 case "x86_64": 199 return soong_metrics_proto.MetricsBase_X86_64.Enum() 200 default: 201 return soong_metrics_proto.MetricsBase_UNKNOWN.Enum() 202 } 203} 204 205// SetBuildDateTime sets the build date and time. The value written 206// to the protobuf file is in seconds. 207func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) { 208 m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second)) 209} 210 211// SetBuildCommand adds the build command specified by the user to the 212// list of collected metrics. 213func (m *Metrics) SetBuildCommand(cmd []string) { 214 m.metrics.BuildCommand = proto.String(strings.Join(cmd, " ")) 215} 216 217// Dump exports the collected metrics from the executed build to the file at 218// out path. 219func (m *Metrics) Dump(out string) error { 220 // ignore the error if the hostname could not be retrieved as it 221 // is not a critical metric to extract. 222 if hostname, err := os.Hostname(); err == nil { 223 m.metrics.Hostname = proto.String(hostname) 224 } 225 m.metrics.HostOs = proto.String(runtime.GOOS) 226 227 return shared.Save(&m.metrics, out) 228} 229 230// SetSoongBuildMetrics sets the metrics collected from the soong_build 231// execution. 232func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) { 233 m.metrics.SoongBuildMetrics = metrics 234} 235 236// A CriticalUserJourneysMetrics is a struct that contains critical user journey 237// metrics. These critical user journeys are defined under cuj/cuj.go file. 238type CriticalUserJourneysMetrics struct { 239 // A list of collected CUJ metrics. 240 cujs soong_metrics_proto.CriticalUserJourneysMetrics 241} 242 243// NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics 244// to capture CUJs metrics. 245func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics { 246 return &CriticalUserJourneysMetrics{} 247} 248 249// Add adds a set of collected metrics from an executed critical user journey. 250func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) { 251 c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{ 252 Name: proto.String(name), 253 Metrics: &metrics.metrics, 254 }) 255} 256 257// Dump saves the collected CUJs metrics to the raw protobuf file. 258func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) { 259 return shared.Save(&c.cujs, filename) 260} 261