blob: 3e76d37719f9e3803ccf8817f9edb02d67f12f1a [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"
20 "strconv"
21
Nan Zhang17f27672018-12-12 16:01:49 -080022 "github.com/golang/protobuf/proto"
Colin Crossd0be2102019-11-26 16:16:57 -080023
24 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Nan Zhang17f27672018-12-12 16:01:49 -080025)
26
27const (
28 RunSetupTool = "setup"
29 RunKati = "kati"
30 RunSoong = "soong"
31 PrimaryNinja = "ninja"
32 TestRun = "test"
Colin Cross74cda722020-01-16 15:25:50 -080033 Total = "total"
Nan Zhang17f27672018-12-12 16:01:49 -080034)
35
36type Metrics struct {
Patrice Arruda0cc5b212019-06-14 15:27:46 -070037 metrics soong_metrics_proto.MetricsBase
Nan Zhang17f27672018-12-12 16:01:49 -080038 TimeTracer TimeTracer
39}
40
41func New() (metrics *Metrics) {
42 m := &Metrics{
Patrice Arruda0cc5b212019-06-14 15:27:46 -070043 metrics: soong_metrics_proto.MetricsBase{},
Nan Zhang17f27672018-12-12 16:01:49 -080044 TimeTracer: &timeTracerImpl{},
45 }
46 return m
47}
48
Patrice Arruda0cc5b212019-06-14 15:27:46 -070049func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
Nan Zhang17f27672018-12-12 16:01:49 -080050 switch perf.GetName() {
51 case RunKati:
52 m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
53 break
54 case RunSoong:
55 m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
56 break
57 case PrimaryNinja:
58 m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
59 break
Colin Cross74cda722020-01-16 15:25:50 -080060 case Total:
61 m.metrics.Total = &perf
Nan Zhang17f27672018-12-12 16:01:49 -080062 default:
63 // ignored
64 }
65}
66
67func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
68 for k, v := range metadata {
69 switch k {
70 case "BUILD_ID":
71 m.metrics.BuildId = proto.String(v)
72 break
73 case "PLATFORM_VERSION_CODENAME":
74 m.metrics.PlatformVersionCodename = proto.String(v)
75 break
76 case "TARGET_PRODUCT":
77 m.metrics.TargetProduct = proto.String(v)
78 break
79 case "TARGET_BUILD_VARIANT":
80 switch v {
81 case "user":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070082 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080083 case "userdebug":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070084 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080085 case "eng":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070086 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080087 default:
88 // ignored
89 }
90 case "TARGET_ARCH":
91 m.metrics.TargetArch = m.getArch(v)
92 case "TARGET_ARCH_VARIANT":
93 m.metrics.TargetArchVariant = proto.String(v)
94 case "TARGET_CPU_VARIANT":
95 m.metrics.TargetCpuVariant = proto.String(v)
96 case "HOST_ARCH":
97 m.metrics.HostArch = m.getArch(v)
98 case "HOST_2ND_ARCH":
99 m.metrics.Host_2NdArch = m.getArch(v)
100 case "HOST_OS":
101 m.metrics.HostOs = proto.String(v)
102 case "HOST_OS_EXTRA":
103 m.metrics.HostOsExtra = proto.String(v)
104 case "HOST_CROSS_OS":
105 m.metrics.HostCrossOs = proto.String(v)
106 case "HOST_CROSS_ARCH":
107 m.metrics.HostCrossArch = proto.String(v)
108 case "HOST_CROSS_2ND_ARCH":
109 m.metrics.HostCross_2NdArch = proto.String(v)
110 case "OUT_DIR":
111 m.metrics.OutDir = proto.String(v)
112 default:
113 // ignored
114 }
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
133func (m *Metrics) SetBuildDateTime(date_time string) {
134 if date_time != "" {
135 date_time_timestamp, err := strconv.ParseInt(date_time, 10, 64)
136 if err != nil {
137 panic(err)
138 }
139 m.metrics.BuildDateTimestamp = &date_time_timestamp
140 }
141}
142
Nan Zhang17f27672018-12-12 16:01:49 -0800143// exports the output to the file at outputPath
144func (m *Metrics) Dump(outputPath string) (err error) {
Colin Crossd0be2102019-11-26 16:16:57 -0800145 return writeMessageToFile(&m.metrics, outputPath)
146}
147
Colin Crossb72c9092020-02-10 11:23:49 -0800148func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
149 m.metrics.SoongBuildMetrics = metrics
150}
151
Colin Crossd0be2102019-11-26 16:16:57 -0800152type CriticalUserJourneysMetrics struct {
153 cujs soong_metrics_proto.CriticalUserJourneysMetrics
154}
155
156func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
157 return &CriticalUserJourneysMetrics{}
158}
159
160func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
161 c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
162 Name: proto.String(name),
163 Metrics: &metrics.metrics,
164 })
165}
166
167func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) {
168 return writeMessageToFile(&c.cujs, outputPath)
169}
170
171func writeMessageToFile(pb proto.Message, outputPath string) (err error) {
172 data, err := proto.Marshal(pb)
Nan Zhang17f27672018-12-12 16:01:49 -0800173 if err != nil {
174 return err
175 }
176 tempPath := outputPath + ".tmp"
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700177 err = ioutil.WriteFile(tempPath, []byte(data), 0644)
Nan Zhang17f27672018-12-12 16:01:49 -0800178 if err != nil {
179 return err
180 }
181 err = os.Rename(tempPath, outputPath)
182 if err != nil {
183 return err
184 }
185
186 return nil
187}