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" |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame^] | 20 | "time" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 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 ( |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 28 | PrimaryNinja = "ninja" |
| 29 | RunKati = "kati" |
| 30 | RunSetupTool = "setup" |
| 31 | RunShutdownTool = "shutdown" |
| 32 | RunSoong = "soong" |
| 33 | TestRun = "test" |
| 34 | Total = "total" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | type Metrics struct { |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 38 | metrics soong_metrics_proto.MetricsBase |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 39 | TimeTracer TimeTracer |
| 40 | } |
| 41 | |
| 42 | func New() (metrics *Metrics) { |
| 43 | m := &Metrics{ |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 44 | metrics: soong_metrics_proto.MetricsBase{}, |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 45 | TimeTracer: &timeTracerImpl{}, |
| 46 | } |
| 47 | return m |
| 48 | } |
| 49 | |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 50 | func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 51 | switch perf.GetName() { |
| 52 | case RunKati: |
| 53 | m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf) |
| 54 | break |
| 55 | case RunSoong: |
| 56 | m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf) |
| 57 | break |
| 58 | case PrimaryNinja: |
| 59 | m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf) |
| 60 | break |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 61 | case Total: |
| 62 | m.metrics.Total = &perf |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 63 | default: |
| 64 | // ignored |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func (m *Metrics) SetMetadataMetrics(metadata map[string]string) { |
| 69 | for k, v := range metadata { |
| 70 | switch k { |
| 71 | case "BUILD_ID": |
| 72 | m.metrics.BuildId = proto.String(v) |
| 73 | break |
| 74 | case "PLATFORM_VERSION_CODENAME": |
| 75 | m.metrics.PlatformVersionCodename = proto.String(v) |
| 76 | break |
| 77 | case "TARGET_PRODUCT": |
| 78 | m.metrics.TargetProduct = proto.String(v) |
| 79 | break |
| 80 | case "TARGET_BUILD_VARIANT": |
| 81 | switch v { |
| 82 | case "user": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 83 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 84 | case "userdebug": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 85 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 86 | case "eng": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 87 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 88 | default: |
| 89 | // ignored |
| 90 | } |
| 91 | case "TARGET_ARCH": |
| 92 | m.metrics.TargetArch = m.getArch(v) |
| 93 | case "TARGET_ARCH_VARIANT": |
| 94 | m.metrics.TargetArchVariant = proto.String(v) |
| 95 | case "TARGET_CPU_VARIANT": |
| 96 | m.metrics.TargetCpuVariant = proto.String(v) |
| 97 | case "HOST_ARCH": |
| 98 | m.metrics.HostArch = m.getArch(v) |
| 99 | case "HOST_2ND_ARCH": |
| 100 | m.metrics.Host_2NdArch = m.getArch(v) |
| 101 | case "HOST_OS": |
| 102 | m.metrics.HostOs = proto.String(v) |
| 103 | case "HOST_OS_EXTRA": |
| 104 | m.metrics.HostOsExtra = proto.String(v) |
| 105 | case "HOST_CROSS_OS": |
| 106 | m.metrics.HostCrossOs = proto.String(v) |
| 107 | case "HOST_CROSS_ARCH": |
| 108 | m.metrics.HostCrossArch = proto.String(v) |
| 109 | case "HOST_CROSS_2ND_ARCH": |
| 110 | m.metrics.HostCross_2NdArch = proto.String(v) |
| 111 | case "OUT_DIR": |
| 112 | m.metrics.OutDir = proto.String(v) |
| 113 | default: |
| 114 | // ignored |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 119 | func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 120 | switch arch { |
| 121 | case "arm": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 122 | return soong_metrics_proto.MetricsBase_ARM.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 123 | case "arm64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 124 | return soong_metrics_proto.MetricsBase_ARM64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 125 | case "x86": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 126 | return soong_metrics_proto.MetricsBase_X86.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 127 | case "x86_64": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 128 | return soong_metrics_proto.MetricsBase_X86_64.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 129 | default: |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 130 | return soong_metrics_proto.MetricsBase_UNKNOWN.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame^] | 134 | func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) { |
| 135 | m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second)) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 138 | // exports the output to the file at outputPath |
| 139 | func (m *Metrics) Dump(outputPath string) (err error) { |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 140 | return writeMessageToFile(&m.metrics, outputPath) |
| 141 | } |
| 142 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 143 | func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 144 | m.metrics.SoongBuildMetrics = metrics |
| 145 | } |
| 146 | |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 147 | type CriticalUserJourneysMetrics struct { |
| 148 | cujs soong_metrics_proto.CriticalUserJourneysMetrics |
| 149 | } |
| 150 | |
| 151 | func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics { |
| 152 | return &CriticalUserJourneysMetrics{} |
| 153 | } |
| 154 | |
| 155 | func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) { |
| 156 | c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{ |
| 157 | Name: proto.String(name), |
| 158 | Metrics: &metrics.metrics, |
| 159 | }) |
| 160 | } |
| 161 | |
| 162 | func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) { |
| 163 | return writeMessageToFile(&c.cujs, outputPath) |
| 164 | } |
| 165 | |
| 166 | func writeMessageToFile(pb proto.Message, outputPath string) (err error) { |
| 167 | data, err := proto.Marshal(pb) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | tempPath := outputPath + ".tmp" |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 172 | err = ioutil.WriteFile(tempPath, []byte(data), 0644) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | err = os.Rename(tempPath, outputPath) |
| 177 | if err != nil { |
| 178 | return err |
| 179 | } |
| 180 | |
| 181 | return nil |
| 182 | } |