blob: a13a106fa01b76c2d95bdc83ea9f42caa98e27f4 [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 Arruda73c790f2020-07-13 23:01:18 +000021 "time"
Nan Zhang17f27672018-12-12 16:01:49 -080022
Nan Zhang17f27672018-12-12 16:01:49 -080023 "github.com/golang/protobuf/proto"
Colin Crossd0be2102019-11-26 16:16:57 -080024
25 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Nan Zhang17f27672018-12-12 16:01:49 -080026)
27
28const (
Patrice Arruda62f1bf22020-07-07 12:48:26 +000029 PrimaryNinja = "ninja"
30 RunKati = "kati"
31 RunSetupTool = "setup"
32 RunShutdownTool = "shutdown"
33 RunSoong = "soong"
34 TestRun = "test"
35 Total = "total"
Nan Zhang17f27672018-12-12 16:01:49 -080036)
37
38type Metrics struct {
Patrice Arruda0cc5b212019-06-14 15:27:46 -070039 metrics soong_metrics_proto.MetricsBase
Nan Zhang17f27672018-12-12 16:01:49 -080040 TimeTracer TimeTracer
41}
42
43func New() (metrics *Metrics) {
44 m := &Metrics{
Patrice Arruda0cc5b212019-06-14 15:27:46 -070045 metrics: soong_metrics_proto.MetricsBase{},
Nan Zhang17f27672018-12-12 16:01:49 -080046 TimeTracer: &timeTracerImpl{},
47 }
48 return m
49}
50
Patrice Arruda0cc5b212019-06-14 15:27:46 -070051func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
Nan Zhang17f27672018-12-12 16:01:49 -080052 switch perf.GetName() {
53 case RunKati:
54 m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
55 break
56 case RunSoong:
57 m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
58 break
59 case PrimaryNinja:
60 m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
61 break
Colin Cross74cda722020-01-16 15:25:50 -080062 case Total:
63 m.metrics.Total = &perf
Nan Zhang17f27672018-12-12 16:01:49 -080064 default:
65 // ignored
66 }
67}
68
Patrice Arruda96850362020-08-11 20:41:11 +000069func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
70 m.metrics.BuildConfig = b
71}
72
Nan Zhang17f27672018-12-12 16:01:49 -080073func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
74 for k, v := range metadata {
75 switch k {
76 case "BUILD_ID":
77 m.metrics.BuildId = proto.String(v)
78 break
79 case "PLATFORM_VERSION_CODENAME":
80 m.metrics.PlatformVersionCodename = proto.String(v)
81 break
82 case "TARGET_PRODUCT":
83 m.metrics.TargetProduct = proto.String(v)
84 break
85 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 default:
94 // ignored
95 }
96 case "TARGET_ARCH":
97 m.metrics.TargetArch = m.getArch(v)
98 case "TARGET_ARCH_VARIANT":
99 m.metrics.TargetArchVariant = proto.String(v)
100 case "TARGET_CPU_VARIANT":
101 m.metrics.TargetCpuVariant = proto.String(v)
102 case "HOST_ARCH":
103 m.metrics.HostArch = m.getArch(v)
104 case "HOST_2ND_ARCH":
105 m.metrics.Host_2NdArch = m.getArch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800106 case "HOST_OS_EXTRA":
107 m.metrics.HostOsExtra = proto.String(v)
108 case "HOST_CROSS_OS":
109 m.metrics.HostCrossOs = proto.String(v)
110 case "HOST_CROSS_ARCH":
111 m.metrics.HostCrossArch = proto.String(v)
112 case "HOST_CROSS_2ND_ARCH":
113 m.metrics.HostCross_2NdArch = proto.String(v)
114 case "OUT_DIR":
115 m.metrics.OutDir = proto.String(v)
116 default:
117 // ignored
118 }
119 }
120}
121
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700122func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch {
Nan Zhang17f27672018-12-12 16:01:49 -0800123 switch arch {
124 case "arm":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700125 return soong_metrics_proto.MetricsBase_ARM.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800126 case "arm64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700127 return soong_metrics_proto.MetricsBase_ARM64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800128 case "x86":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700129 return soong_metrics_proto.MetricsBase_X86.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800130 case "x86_64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700131 return soong_metrics_proto.MetricsBase_X86_64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800132 default:
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700133 return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800134 }
135}
136
Patrice Arruda73c790f2020-07-13 23:01:18 +0000137func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
138 m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
Nan Zhang17f27672018-12-12 16:01:49 -0800139}
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}