aboutsummaryrefslogtreecommitdiff
path: root/ui/metrics/metrics.go
blob: 717530c4778d6f1fbb8ae33ccfe60a4613fd6f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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.

// Package metrics represents the metrics system for Android Platform Build Systems.
package metrics

// 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
// 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.

import (
	"fmt"
	"os"
	"runtime"
	"strings"
	"time"

	"android/soong/shared"
	"google.golang.org/protobuf/proto"

	soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
	mk_metrics_proto "android/soong/ui/metrics/mk_metrics_proto"
)

const (
	// 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.
	RunSetupTool    = "setup"
	RunShutdownTool = "shutdown"
	TestRun         = "test"

	// 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"
)

// 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.
type Metrics struct {
	// 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.
	metrics soong_metrics_proto.MetricsBase

	// Protobuf containing metrics pertaining to number of makefiles in a build.
	mkMetrics mk_metrics_proto.MkMetrics

	// A list of pending build events.
	EventTracer *EventTracer
}

// New returns a pointer of Metrics to store a set of metrics.
func New() (metrics *Metrics) {
	m := &Metrics{
		metrics:     soong_metrics_proto.MetricsBase{},
		mkMetrics:   mk_metrics_proto.MkMetrics{},
		EventTracer: &EventTracer{},
	}
	return m
}

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)
}

// SetTimeMetrics stores performance information from an executed block of
// code.
func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
	switch perf.GetName() {
	case RunKati:
		m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
	case RunSoong:
		m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
	case RunBazel:
		m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
	case PrimaryNinja:
		m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
	case RunSetupTool:
		m.metrics.SetupTools = append(m.metrics.SetupTools, &perf)
	case Total:
		m.metrics.Total = &perf
	}
}

// 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)
}

// BuildConfig stores information about the build configuration.
func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
	m.metrics.BuildConfig = b
}

// SystemResourceInfo stores information related to the host system such
// as total CPU and memory.
func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
	m.metrics.SystemResourceInfo = b
}

// ExpConfigFetcher stores information about the expconfigfetcher.
func (m *Metrics) ExpConfigFetcher(b *soong_metrics_proto.ExpConfigFetcher) {
	m.metrics.ExpConfigFetcher = b
}

// SetMetadataMetrics sets information about the build such as the target
// product, host architecture and out directory.
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":
				m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
			case "userdebug":
				m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
			case "eng":
				m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
			}
		case "TARGET_ARCH":
			m.metrics.TargetArch = arch(v)
		case "TARGET_ARCH_VARIANT":
			m.metrics.TargetArchVariant = proto.String(v)
		case "TARGET_CPU_VARIANT":
			m.metrics.TargetCpuVariant = proto.String(v)
		case "HOST_ARCH":
			m.metrics.HostArch = arch(v)
		case "HOST_2ND_ARCH":
			m.metrics.Host_2NdArch = arch(v)
		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)
		}
	}
}

// arch returns the corresponding MetricsBase_Arch based on the string
// parameter.
func arch(a string) *soong_metrics_proto.MetricsBase_Arch {
	switch a {
	case "arm":
		return soong_metrics_proto.MetricsBase_ARM.Enum()
	case "arm64":
		return soong_metrics_proto.MetricsBase_ARM64.Enum()
	case "x86":
		return soong_metrics_proto.MetricsBase_X86.Enum()
	case "x86_64":
		return soong_metrics_proto.MetricsBase_X86_64.Enum()
	default:
		return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
	}
}

// SetBuildDateTime sets the build date and time. The value written
// to the protobuf file is in seconds.
func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
	m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
}

func (m *Metrics) UpdateTotalRealTime(data []byte) error {
	if err := proto.Unmarshal(data, &m.metrics); err != nil {
		return fmt.Errorf("Failed to unmarshal proto", err)
	}
	startTime := *m.metrics.Total.StartTime
	endTime := uint64(time.Now().UnixNano())

	*m.metrics.Total.RealTime = *proto.Uint64(endTime - startTime)
	return nil
}

// SetBuildCommand adds the build command specified by the user to the
// list of collected metrics.
func (m *Metrics) SetBuildCommand(cmd []string) {
	m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
}

// Dump exports the collected metrics from the executed build to the file at
// out path.
func (m *Metrics) Dump(out string) error {
	// 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)
	}
	m.metrics.HostOs = proto.String(runtime.GOOS)

	return shared.Save(&m.metrics, out)
}

// SetSoongBuildMetrics sets the metrics collected from the soong_build
// execution.
func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
	m.metrics.SoongBuildMetrics = metrics
}

// A CriticalUserJourneysMetrics is a struct that contains critical user journey
// metrics. These critical user journeys are defined under cuj/cuj.go file.
type CriticalUserJourneysMetrics struct {
	// A list of collected CUJ metrics.
	cujs soong_metrics_proto.CriticalUserJourneysMetrics
}

// NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics
// to capture CUJs metrics.
func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
	return &CriticalUserJourneysMetrics{}
}

// Add adds a set of collected metrics from an executed critical user journey.
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,
	})
}

// Dump saves the collected CUJs metrics to the raw protobuf file.
func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
	return shared.Save(&c.cujs, filename)
}