blob: 7a3726bc2fa3d35e4c2fc80168a00900116dd30f [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 Arruda457c5d32020-10-19 11:20:21 -070039 metrics soong_metrics_proto.MetricsBase
40 EventTracer EventTracer
Nan Zhang17f27672018-12-12 16:01:49 -080041}
42
43func New() (metrics *Metrics) {
44 m := &Metrics{
Patrice Arruda457c5d32020-10-19 11:20:21 -070045 metrics: soong_metrics_proto.MetricsBase{},
46 EventTracer: &eventTracerImpl{},
Nan Zhang17f27672018-12-12 16:01:49 -080047 }
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
Patrice Arruda3edfd482020-10-13 23:58:41 +000073func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
74 m.metrics.SystemResourceInfo = b
75}
76
Nan Zhang17f27672018-12-12 16:01:49 -080077func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
78 for k, v := range metadata {
79 switch k {
80 case "BUILD_ID":
81 m.metrics.BuildId = proto.String(v)
82 break
83 case "PLATFORM_VERSION_CODENAME":
84 m.metrics.PlatformVersionCodename = proto.String(v)
85 break
86 case "TARGET_PRODUCT":
87 m.metrics.TargetProduct = proto.String(v)
88 break
89 case "TARGET_BUILD_VARIANT":
90 switch v {
91 case "user":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070092 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080093 case "userdebug":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070094 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080095 case "eng":
Patrice Arruda0cc5b212019-06-14 15:27:46 -070096 m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -080097 default:
98 // ignored
99 }
100 case "TARGET_ARCH":
101 m.metrics.TargetArch = m.getArch(v)
102 case "TARGET_ARCH_VARIANT":
103 m.metrics.TargetArchVariant = proto.String(v)
104 case "TARGET_CPU_VARIANT":
105 m.metrics.TargetCpuVariant = proto.String(v)
106 case "HOST_ARCH":
107 m.metrics.HostArch = m.getArch(v)
108 case "HOST_2ND_ARCH":
109 m.metrics.Host_2NdArch = m.getArch(v)
Nan Zhang17f27672018-12-12 16:01:49 -0800110 case "HOST_OS_EXTRA":
111 m.metrics.HostOsExtra = proto.String(v)
112 case "HOST_CROSS_OS":
113 m.metrics.HostCrossOs = proto.String(v)
114 case "HOST_CROSS_ARCH":
115 m.metrics.HostCrossArch = proto.String(v)
116 case "HOST_CROSS_2ND_ARCH":
117 m.metrics.HostCross_2NdArch = proto.String(v)
118 case "OUT_DIR":
119 m.metrics.OutDir = proto.String(v)
120 default:
121 // ignored
122 }
123 }
124}
125
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700126func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch {
Nan Zhang17f27672018-12-12 16:01:49 -0800127 switch arch {
128 case "arm":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700129 return soong_metrics_proto.MetricsBase_ARM.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800130 case "arm64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700131 return soong_metrics_proto.MetricsBase_ARM64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800132 case "x86":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700133 return soong_metrics_proto.MetricsBase_X86.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800134 case "x86_64":
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700135 return soong_metrics_proto.MetricsBase_X86_64.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800136 default:
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700137 return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
Nan Zhang17f27672018-12-12 16:01:49 -0800138 }
139}
140
Patrice Arruda73c790f2020-07-13 23:01:18 +0000141func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
142 m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
Nan Zhang17f27672018-12-12 16:01:49 -0800143}
144
Nan Zhang17f27672018-12-12 16:01:49 -0800145// exports the output to the file at outputPath
Patrice Arruda4fb8adc2020-10-12 22:38:06 +0000146func (m *Metrics) Dump(outputPath string) error {
147 // ignore the error if the hostname could not be retrieved as it
148 // is not a critical metric to extract.
149 if hostname, err := os.Hostname(); err == nil {
150 m.metrics.Hostname = proto.String(hostname)
151 }
Patrice Arrudaadd7ea92020-08-07 17:55:23 +0000152 m.metrics.HostOs = proto.String(runtime.GOOS)
Colin Crossd0be2102019-11-26 16:16:57 -0800153 return writeMessageToFile(&m.metrics, outputPath)
154}
155
Colin Crossb72c9092020-02-10 11:23:49 -0800156func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
157 m.metrics.SoongBuildMetrics = metrics
158}
159
Colin Crossd0be2102019-11-26 16:16:57 -0800160type CriticalUserJourneysMetrics struct {
161 cujs soong_metrics_proto.CriticalUserJourneysMetrics
162}
163
164func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
165 return &CriticalUserJourneysMetrics{}
166}
167
168func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
169 c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
170 Name: proto.String(name),
171 Metrics: &metrics.metrics,
172 })
173}
174
175func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) {
176 return writeMessageToFile(&c.cujs, outputPath)
177}
178
179func writeMessageToFile(pb proto.Message, outputPath string) (err error) {
180 data, err := proto.Marshal(pb)
Nan Zhang17f27672018-12-12 16:01:49 -0800181 if err != nil {
182 return err
183 }
184 tempPath := outputPath + ".tmp"
Patrice Arruda0cc5b212019-06-14 15:27:46 -0700185 err = ioutil.WriteFile(tempPath, []byte(data), 0644)
Nan Zhang17f27672018-12-12 16:01:49 -0800186 if err != nil {
187 return err
188 }
189 err = os.Rename(tempPath, outputPath)
190 if err != nil {
191 return err
192 }
193
194 return nil
195}