Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package metrics |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "strconv" |
| 21 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 23 | |
| 24 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | RunSetupTool = "setup" |
| 29 | RunKati = "kati" |
| 30 | RunSoong = "soong" |
| 31 | PrimaryNinja = "ninja" |
| 32 | TestRun = "test" |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 33 | Total = "total" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | type Metrics struct { |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 37 | metrics soong_metrics_proto.MetricsBase |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 38 | TimeTracer TimeTracer |
| 39 | } |
| 40 | |
| 41 | func New() (metrics *Metrics) { |
| 42 | m := &Metrics{ |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 43 | metrics: soong_metrics_proto.MetricsBase{}, |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 44 | TimeTracer: &timeTracerImpl{}, |
| 45 | } |
| 46 | return m |
| 47 | } |
| 48 | |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 49 | func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 50 | 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 Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 60 | case Total: |
| 61 | m.metrics.Total = &perf |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 62 | default: |
| 63 | // ignored |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (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 Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 82 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 83 | case "userdebug": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 84 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 85 | case "eng": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 86 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 87 | 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 Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 118 | func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 119 | switch arch { |
| 120 | case "arm": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 121 | return soong_metrics_proto.MetricsBase_ARM.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 122 | case "arm64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 123 | return soong_metrics_proto.MetricsBase_ARM64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 124 | case "x86": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 125 | return soong_metrics_proto.MetricsBase_X86.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 126 | case "x86_64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 127 | return soong_metrics_proto.MetricsBase_X86_64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 128 | default: |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 129 | return soong_metrics_proto.MetricsBase_UNKNOWN.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | func (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 Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 143 | // exports the output to the file at outputPath |
| 144 | func (m *Metrics) Dump(outputPath string) (err error) { |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 145 | return writeMessageToFile(&m.metrics, outputPath) |
| 146 | } |
| 147 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame^] | 148 | func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 149 | m.metrics.SoongBuildMetrics = metrics |
| 150 | } |
| 151 | |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 152 | type CriticalUserJourneysMetrics struct { |
| 153 | cujs soong_metrics_proto.CriticalUserJourneysMetrics |
| 154 | } |
| 155 | |
| 156 | func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics { |
| 157 | return &CriticalUserJourneysMetrics{} |
| 158 | } |
| 159 | |
| 160 | func (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 | |
| 167 | func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) { |
| 168 | return writeMessageToFile(&c.cujs, outputPath) |
| 169 | } |
| 170 | |
| 171 | func writeMessageToFile(pb proto.Message, outputPath string) (err error) { |
| 172 | data, err := proto.Marshal(pb) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | tempPath := outputPath + ".tmp" |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 177 | err = ioutil.WriteFile(tempPath, []byte(data), 0644) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 178 | 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 | } |