blob: 2a0946111e55a45b31a38d36ca86819ed59ae897 [file] [log] [blame]
Nan Zhang17f27672018-12-12 16:01:49 -08001// 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
15package metrics
16
17import (
18 "io/ioutil"
19 "os"
Patrice Arrudaadd7ea92020-08-07 17:55:23 +000020 "runtime"
Patrice Arrudae92c30d2020-10-29 11:01:32 -070021 "strings"
Patrice Arruda73c790f2020-07-13 23:01:18 +000022 "time"
Nan Zhang17f27672018-12-12 16:01:49 -080023
Nan Zhang17f27672018-12-12 16:01:49 -080024 "github.com/golang/protobuf/proto"
Colin Crossd0be2102019-11-26 16:16:57 -080025
26 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Nan Zhang17f27672018-12-12 16:01:49 -080027)
28
29const (
Patrice Arruda62f1bf22020-07-07 12:48:26 +000030 PrimaryNinja = "ninja"
31 RunKati = "kati"
32 RunSetupTool = "setup"
33 RunShutdownTool = "shutdown"
34 RunSoong = "soong"
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -080035 RunBazel = "bazel"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000036 TestRun = "test"
37 Total = "total"
Nan Zhang17f27672018-12-12 16:01:49 -080038)
39
40type Metrics struct {
Patrice Arruda457c5d32020-10-19 11:20:21 -070041 metrics soong_metrics_proto.MetricsBase
Patrice Arruda8a44a372020-12-14 20:55:23 +000042 EventTracer *EventTracer
Nan Zhang17f27672018-12-12 16:01:49 -080043}
44
45func New() (metrics *Metrics) {
46 m := &Metrics{
Patrice Arruda457c5d32020-10-19 11:20:21 -070047 metrics: soong_metrics_proto.MetricsBase{},
Patrice Arruda8a44a372020-12-14 20:55:23 +000048 EventTracer: &EventTracer{},
Nan Zhang17f27672018-12-12 16:01:49 -080049 }
50 return m
51}
52
Patrice Arruda0cc5b212019-06-14 15:27:46 -070053func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
Nan Zhang17f27672018-12-12 16:01:49 -080054 switch perf.GetName() {
55 case RunKati:
56 m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -080057 case RunSoong:
58 m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -080059 case RunBazel:
60 m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
Nan Zhang17f27672018-12-12 16:01:49 -080061 case PrimaryNinja:
62 m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
Colin Cross74cda722020-01-16 15:25:50 -080063 case Total:
64 m.metrics.Total = &perf
Nan Zhang17f27672018-12-12 16:01:49 -080065 }
66}
67
Patrice Arruda96850362020-08-11 20:41:11 +000068func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
69 m.metrics.BuildConfig = b
70}
71
Patrice Arruda3edfd482020-10-13 23:58:41 +000072func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
73 m.metrics.SystemResourceInfo = b
74}
75
Nan Zhang17f27672018-12-12 16:01:49 -080076func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
77 for k, v := range metadata {
78 switch k {
79 case "BUILD_ID":
80 m.metrics.BuildId = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -080081 case "PLATFORM_VERSION_CODENAME":
82 m.metrics.PlatformVersionCodename = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -080083 case "TARGET_PRODUCT":
84 m.metrics.TargetProduct = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -080085 case "TARGET_BUILD_VARIANT":
86 switch v {
87 case "user":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070088 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080089 case "userdebug":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070090 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080091 case "eng":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070092 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080093 }
94 case "TARGET_ARCH":
95 m.metrics.TargetArch = m.getArch(v)
96 case "TARGET_ARCH_VARIANT":
97 m.metrics.TargetArchVariant = proto.String(v)
98 case "TARGET_CPU_VARIANT":
99 m.metrics.TargetCpuVariant = proto.String(v)
100 case "HOST_ARCH":
101 m.metrics.HostArch = m.getArch(v)
102 case "HOST_2ND_ARCH":
103 m.metrics.Host_2NdArch = m.getArch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800104 case "HOST_OS_EXTRA":
105 m.metrics.HostOsExtra = proto.String(v)
106 case "HOST_CROSS_OS":
107 m.metrics.HostCrossOs = proto.String(v)
108 case "HOST_CROSS_ARCH":
109 m.metrics.HostCrossArch = proto.String(v)
110 case "HOST_CROSS_2ND_ARCH":
111 m.metrics.HostCross_2NdArch = proto.String(v)
112 case "OUT_DIR":
113 m.metrics.OutDir = proto.String(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800114 }
115 }
116}
117
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700118func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch {
Nan Zhang17f27672018-12-12 16:01:49 -0800119 switch arch {
120 case "arm":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700121 return soong_metrics_proto.MetricsBase_ARM.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800122 case "arm64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700123 return soong_metrics_proto.MetricsBase_ARM64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800124 case "x86":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700125 return soong_metrics_proto.MetricsBase_X86.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800126 case "x86_64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700127 return soong_metrics_proto.MetricsBase_X86_64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800128 default:
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700129 return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800130 }
131}
132
Patrice Arruda73c790f2020-07-13 23:01:18 +0000133func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
134 m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
Nan Zhang17f27672018-12-12 16:01:49 -0800135}
136
Patrice Arrudae92c30d2020-10-29 11:01:32 -0700137func (m *Metrics) SetBuildCommand(cmd []string) {
138 m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
139}
140
Nan Zhang17f27672018-12-12 16:01:49 -0800141// exports the output to the file at outputPath
Patrice Arruda4fb8adc2020-10-12 22:38:06 +0000142func (m *Metrics) Dump(outputPath string) error {
143 // ignore the error if the hostname could not be retrieved as it
144 // is not a critical metric to extract.
145 if hostname, err := os.Hostname(); err == nil {
146 m.metrics.Hostname = proto.String(hostname)
147 }
Patrice Arrudaadd7ea92020-08-07 17:55:23 +0000148 m.metrics.HostOs = proto.String(runtime.GOOS)
Colin Crossd0be2102019-11-26 16:16:57 -0800149 return writeMessageToFile(&m.metrics, outputPath)
150}
151
Colin Crossb72c9092020-02-10 11:23:49 -0800152func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
153 m.metrics.SoongBuildMetrics = metrics
154}
155
Colin Crossd0be2102019-11-26 16:16:57 -0800156type CriticalUserJourneysMetrics struct {
157 cujs soong_metrics_proto.CriticalUserJourneysMetrics
158}
159
160func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
161 return &CriticalUserJourneysMetrics{}
162}
163
164func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
165 c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
166 Name: proto.String(name),
167 Metrics: &metrics.metrics,
168 })
169}
170
171func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) {
172 return writeMessageToFile(&c.cujs, outputPath)
173}
174
175func writeMessageToFile(pb proto.Message, outputPath string) (err error) {
176 data, err := proto.Marshal(pb)
Nan Zhang17f27672018-12-12 16:01:49 -0800177 if err != nil {
178 return err
179 }
180 tempPath := outputPath + ".tmp"
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700181 err = ioutil.WriteFile(tempPath, []byte(data), 0644)
Nan Zhang17f27672018-12-12 16:01:49 -0800182 if err != nil {
183 return err
184 }
185 err = os.Rename(tempPath, outputPath)
186 if err != nil {
187 return err
188 }
189
190 return nil
191}