2018-12-13 01:01:49 +01:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// Package metrics represents the metrics system for Android Platform Build Systems.
|
2018-12-13 01:01:49 +01:00
|
|
|
package metrics
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// This is the main heart of the metrics system for Android Platform Build Systems.
|
|
|
|
// The starting of the soong_ui (cmd/soong_ui/main.go), the metrics system is
|
|
|
|
// initialized by the invocation of New and is then stored in the context
|
|
|
|
// (ui/build/context.go) to be used throughout the system. During the build
|
|
|
|
// initialization phase, several functions in this file are invoked to store
|
|
|
|
// information such as the environment, build configuration and build metadata.
|
|
|
|
// There are several scoped code that has Begin() and defer End() functions
|
|
|
|
// that captures the metrics and is them added as a perfInfo into the set
|
|
|
|
// of the collected metrics. Finally, when soong_ui has finished the build,
|
|
|
|
// the defer Dump function is invoked to store the collected metrics to the
|
2021-07-27 23:29:06 +02:00
|
|
|
// raw protobuf file in the $OUT directory and this raw protobuf file will be
|
|
|
|
// uploaded to the destination. See ui/build/upload.go for more details. The
|
|
|
|
// filename of the raw protobuf file and the list of files to be uploaded is
|
|
|
|
// defined in cmd/soong_ui/main.go. See ui/metrics/event.go for the explanation
|
|
|
|
// of what an event is and how the metrics system is a stack based system.
|
2020-12-16 00:14:55 +01:00
|
|
|
|
2018-12-13 01:01:49 +01:00
|
|
|
import (
|
2022-12-07 22:57:38 +01:00
|
|
|
"fmt"
|
2018-12-13 01:01:49 +01:00
|
|
|
"os"
|
2020-08-07 19:55:23 +02:00
|
|
|
"runtime"
|
2020-10-29 19:01:32 +01:00
|
|
|
"strings"
|
2020-07-14 01:01:18 +02:00
|
|
|
"time"
|
2018-12-13 01:01:49 +01:00
|
|
|
|
2021-11-10 15:55:20 +01:00
|
|
|
"android/soong/shared"
|
2023-03-07 07:19:44 +01:00
|
|
|
|
2021-05-24 23:24:12 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2019-11-27 01:16:57 +01:00
|
|
|
|
2021-05-24 23:24:12 +02:00
|
|
|
soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
|
2022-03-03 18:01:40 +01:00
|
|
|
mk_metrics_proto "android/soong/ui/metrics/mk_metrics_proto"
|
2018-12-13 01:01:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-12-16 00:14:55 +01:00
|
|
|
// Below is a list of names passed in to the Begin tracing functions. These
|
|
|
|
// names are used to group a set of metrics.
|
|
|
|
|
|
|
|
// Setup and tear down of the build systems.
|
2020-07-07 14:48:26 +02:00
|
|
|
RunSetupTool = "setup"
|
|
|
|
RunShutdownTool = "shutdown"
|
|
|
|
TestRun = "test"
|
2020-12-16 00:14:55 +01:00
|
|
|
|
|
|
|
// List of build system tools.
|
|
|
|
RunSoong = "soong"
|
|
|
|
PrimaryNinja = "ninja"
|
|
|
|
RunKati = "kati"
|
|
|
|
RunBazel = "bazel"
|
|
|
|
|
|
|
|
// Overall build from building the graph to building the target.
|
|
|
|
Total = "total"
|
2018-12-13 01:01:49 +01:00
|
|
|
)
|
|
|
|
|
2022-03-03 18:01:40 +01:00
|
|
|
// Metrics is a struct that stores collected metrics during the course of a
|
|
|
|
// build. It is later dumped to protobuf files. See underlying metrics protos
|
|
|
|
// for further details on what information is collected.
|
2018-12-13 01:01:49 +01:00
|
|
|
type Metrics struct {
|
2022-03-03 18:01:40 +01:00
|
|
|
// Protobuf containing various top-level build metrics. These include:
|
|
|
|
// 1. Build identifiers (ex: branch ID, requested product, hostname,
|
|
|
|
// originating command)
|
|
|
|
// 2. Per-subprocess top-level metrics (ex: ninja process IO and runtime).
|
|
|
|
// Note that, since these metrics are reported by soong_ui, there is little
|
|
|
|
// insight that can be provided into performance breakdowns of individual
|
|
|
|
// subprocesses.
|
2020-12-16 00:14:55 +01:00
|
|
|
metrics soong_metrics_proto.MetricsBase
|
|
|
|
|
2022-03-03 18:01:40 +01:00
|
|
|
// Protobuf containing metrics pertaining to number of makefiles in a build.
|
|
|
|
mkMetrics mk_metrics_proto.MkMetrics
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// A list of pending build events.
|
2020-12-14 21:55:23 +01:00
|
|
|
EventTracer *EventTracer
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// New returns a pointer of Metrics to store a set of metrics.
|
2018-12-13 01:01:49 +01:00
|
|
|
func New() (metrics *Metrics) {
|
|
|
|
m := &Metrics{
|
2020-10-19 20:20:21 +02:00
|
|
|
metrics: soong_metrics_proto.MetricsBase{},
|
2022-03-03 18:01:40 +01:00
|
|
|
mkMetrics: mk_metrics_proto.MkMetrics{},
|
2020-12-14 21:55:23 +01:00
|
|
|
EventTracer: &EventTracer{},
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-03-03 18:01:40 +01:00
|
|
|
func (m *Metrics) SetTotalMakefiles(total int) {
|
|
|
|
m.mkMetrics.TotalMakefiles = uint32(total)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) SetToplevelMakefiles(total int) {
|
|
|
|
m.mkMetrics.ToplevelMakefiles = uint32(total)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) DumpMkMetrics(outPath string) {
|
|
|
|
shared.Save(&m.mkMetrics, outPath)
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// SetTimeMetrics stores performance information from an executed block of
|
|
|
|
// code.
|
2019-06-15 00:27:46 +02:00
|
|
|
func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
|
2018-12-13 01:01:49 +01:00
|
|
|
switch perf.GetName() {
|
|
|
|
case RunKati:
|
|
|
|
m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
|
|
|
|
case RunSoong:
|
|
|
|
m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
|
2020-11-13 22:04:17 +01:00
|
|
|
case RunBazel:
|
|
|
|
m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
|
2018-12-13 01:01:49 +01:00
|
|
|
case PrimaryNinja:
|
|
|
|
m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
|
2020-12-15 20:34:20 +01:00
|
|
|
case RunSetupTool:
|
|
|
|
m.metrics.SetupTools = append(m.metrics.SetupTools, &perf)
|
2020-01-17 00:25:50 +01:00
|
|
|
case Total:
|
|
|
|
m.metrics.Total = &perf
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 07:19:44 +01:00
|
|
|
func (m *Metrics) SetCriticalPathInfo(criticalPathInfo soong_metrics_proto.CriticalPathInfo) {
|
|
|
|
m.metrics.CriticalPathInfo = &criticalPathInfo
|
|
|
|
}
|
|
|
|
|
2022-10-21 16:42:35 +02:00
|
|
|
// SetFatalOrPanicMessage stores a non-zero exit and the relevant message in the latest event if
|
|
|
|
// available or the metrics base.
|
|
|
|
func (m *Metrics) SetFatalOrPanicMessage(errMsg string) {
|
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if event := m.EventTracer.peek(); event != nil {
|
|
|
|
event.nonZeroExitCode = true
|
|
|
|
event.errorMsg = &errMsg
|
|
|
|
} else {
|
|
|
|
m.metrics.ErrorMessage = proto.String(errMsg)
|
|
|
|
}
|
|
|
|
m.metrics.NonZeroExit = proto.Bool(true)
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// BuildConfig stores information about the build configuration.
|
2020-08-11 22:41:11 +02:00
|
|
|
func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
|
|
|
|
m.metrics.BuildConfig = b
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// SystemResourceInfo stores information related to the host system such
|
|
|
|
// as total CPU and memory.
|
2020-10-14 01:58:41 +02:00
|
|
|
func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
|
|
|
|
m.metrics.SystemResourceInfo = b
|
|
|
|
}
|
2022-04-08 15:42:04 +02:00
|
|
|
|
|
|
|
// ExpConfigFetcher stores information about the expconfigfetcher.
|
|
|
|
func (m *Metrics) ExpConfigFetcher(b *soong_metrics_proto.ExpConfigFetcher) {
|
|
|
|
m.metrics.ExpConfigFetcher = b
|
|
|
|
}
|
2020-10-14 01:58:41 +02:00
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// SetMetadataMetrics sets information about the build such as the target
|
|
|
|
// product, host architecture and out directory.
|
2018-12-13 01:01:49 +01:00
|
|
|
func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
|
|
|
|
for k, v := range metadata {
|
|
|
|
switch k {
|
|
|
|
case "BUILD_ID":
|
|
|
|
m.metrics.BuildId = proto.String(v)
|
|
|
|
case "PLATFORM_VERSION_CODENAME":
|
|
|
|
m.metrics.PlatformVersionCodename = proto.String(v)
|
|
|
|
case "TARGET_PRODUCT":
|
|
|
|
m.metrics.TargetProduct = proto.String(v)
|
|
|
|
case "TARGET_BUILD_VARIANT":
|
|
|
|
switch v {
|
|
|
|
case "user":
|
2019-06-15 00:27:46 +02:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "userdebug":
|
2019-06-15 00:27:46 +02:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "eng":
|
2019-06-15 00:27:46 +02:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
case "TARGET_ARCH":
|
2020-12-16 00:14:55 +01:00
|
|
|
m.metrics.TargetArch = arch(v)
|
2018-12-13 01:01:49 +01:00
|
|
|
case "TARGET_ARCH_VARIANT":
|
|
|
|
m.metrics.TargetArchVariant = proto.String(v)
|
|
|
|
case "TARGET_CPU_VARIANT":
|
|
|
|
m.metrics.TargetCpuVariant = proto.String(v)
|
|
|
|
case "HOST_ARCH":
|
2020-12-16 00:14:55 +01:00
|
|
|
m.metrics.HostArch = arch(v)
|
2018-12-13 01:01:49 +01:00
|
|
|
case "HOST_2ND_ARCH":
|
2020-12-16 00:14:55 +01:00
|
|
|
m.metrics.Host_2NdArch = arch(v)
|
2018-12-13 01:01:49 +01:00
|
|
|
case "HOST_OS_EXTRA":
|
|
|
|
m.metrics.HostOsExtra = proto.String(v)
|
|
|
|
case "HOST_CROSS_OS":
|
|
|
|
m.metrics.HostCrossOs = proto.String(v)
|
|
|
|
case "HOST_CROSS_ARCH":
|
|
|
|
m.metrics.HostCrossArch = proto.String(v)
|
|
|
|
case "HOST_CROSS_2ND_ARCH":
|
|
|
|
m.metrics.HostCross_2NdArch = proto.String(v)
|
|
|
|
case "OUT_DIR":
|
|
|
|
m.metrics.OutDir = proto.String(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// arch returns the corresponding MetricsBase_Arch based on the string
|
|
|
|
// parameter.
|
|
|
|
func arch(a string) *soong_metrics_proto.MetricsBase_Arch {
|
|
|
|
switch a {
|
2018-12-13 01:01:49 +01:00
|
|
|
case "arm":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_ARM.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "arm64":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_ARM64.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "x86":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_X86.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "x86_64":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_X86_64.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
default:
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// SetBuildDateTime sets the build date and time. The value written
|
|
|
|
// to the protobuf file is in seconds.
|
2020-07-14 01:01:18 +02:00
|
|
|
func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
|
|
|
|
m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
|
2023-05-04 22:40:04 +02:00
|
|
|
func (m *Metrics) UpdateTotalRealTimeAndNonZeroExit(data []byte, bazelExitCode int32) error {
|
2022-12-07 22:57:38 +01:00
|
|
|
if err := proto.Unmarshal(data, &m.metrics); err != nil {
|
2022-04-21 21:50:51 +02:00
|
|
|
return fmt.Errorf("Failed to unmarshal proto: %w", err)
|
2022-12-07 22:57:38 +01:00
|
|
|
}
|
|
|
|
startTime := *m.metrics.Total.StartTime
|
|
|
|
endTime := uint64(time.Now().UnixNano())
|
|
|
|
|
|
|
|
*m.metrics.Total.RealTime = *proto.Uint64(endTime - startTime)
|
2023-05-04 22:40:04 +02:00
|
|
|
|
|
|
|
bazelError := bazelExitCode != 0
|
|
|
|
m.metrics.NonZeroExit = proto.Bool(bazelError)
|
2022-12-07 22:57:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// SetBuildCommand adds the build command specified by the user to the
|
|
|
|
// list of collected metrics.
|
2020-10-29 19:01:32 +01:00
|
|
|
func (m *Metrics) SetBuildCommand(cmd []string) {
|
|
|
|
m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
|
|
|
|
}
|
|
|
|
|
2023-05-31 01:45:36 +02:00
|
|
|
// AddChangedEnvironmentVariable adds the changed environment variable to
|
|
|
|
// ChangedEnvironmentVariable field.
|
|
|
|
func (m *Metrics) AddChangedEnvironmentVariable(ChangedEnvironmentVariable string) {
|
|
|
|
m.metrics.ChangedEnvironmentVariable = append(m.metrics.ChangedEnvironmentVariable,
|
|
|
|
ChangedEnvironmentVariable)
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// Dump exports the collected metrics from the executed build to the file at
|
|
|
|
// out path.
|
|
|
|
func (m *Metrics) Dump(out string) error {
|
2020-10-13 00:38:06 +02:00
|
|
|
// ignore the error if the hostname could not be retrieved as it
|
|
|
|
// is not a critical metric to extract.
|
|
|
|
if hostname, err := os.Hostname(); err == nil {
|
|
|
|
m.metrics.Hostname = proto.String(hostname)
|
|
|
|
}
|
2020-08-07 19:55:23 +02:00
|
|
|
m.metrics.HostOs = proto.String(runtime.GOOS)
|
2020-12-16 00:14:55 +01:00
|
|
|
|
2021-11-10 15:55:20 +01:00
|
|
|
return shared.Save(&m.metrics, out)
|
2019-11-27 01:16:57 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// SetSoongBuildMetrics sets the metrics collected from the soong_build
|
|
|
|
// execution.
|
2020-02-10 20:23:49 +01:00
|
|
|
func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
|
|
|
|
m.metrics.SoongBuildMetrics = metrics
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// A CriticalUserJourneysMetrics is a struct that contains critical user journey
|
|
|
|
// metrics. These critical user journeys are defined under cuj/cuj.go file.
|
2019-11-27 01:16:57 +01:00
|
|
|
type CriticalUserJourneysMetrics struct {
|
2020-12-16 00:14:55 +01:00
|
|
|
// A list of collected CUJ metrics.
|
2019-11-27 01:16:57 +01:00
|
|
|
cujs soong_metrics_proto.CriticalUserJourneysMetrics
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics
|
|
|
|
// to capture CUJs metrics.
|
2019-11-27 01:16:57 +01:00
|
|
|
func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
|
|
|
|
return &CriticalUserJourneysMetrics{}
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// Add adds a set of collected metrics from an executed critical user journey.
|
2019-11-27 01:16:57 +01:00
|
|
|
func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
|
|
|
|
c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
|
|
|
|
Name: proto.String(name),
|
|
|
|
Metrics: &metrics.metrics,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-16 00:14:55 +01:00
|
|
|
// Dump saves the collected CUJs metrics to the raw protobuf file.
|
|
|
|
func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
|
2021-11-10 15:55:20 +01:00
|
|
|
return shared.Save(&c.cujs, filename)
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|