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 | add7ea9 | 2020-08-07 17:55:23 +0000 | [diff] [blame] | 20 | "runtime" |
Patrice Arruda | e92c30d | 2020-10-29 11:01:32 -0700 | [diff] [blame] | 21 | "strings" |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 22 | "time" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 23 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 24 | "github.com/golang/protobuf/proto" |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 25 | |
| 26 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | const ( |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 30 | PrimaryNinja = "ninja" |
| 31 | RunKati = "kati" |
| 32 | RunSetupTool = "setup" |
| 33 | RunShutdownTool = "shutdown" |
| 34 | RunSoong = "soong" |
Patrice Arruda | b7cf9ba | 2020-11-13 13:04:17 -0800 | [diff] [blame] | 35 | RunBazel = "bazel" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 36 | TestRun = "test" |
| 37 | Total = "total" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 38 | ) |
| 39 | |
| 40 | type Metrics struct { |
Patrice Arruda | 457c5d3 | 2020-10-19 11:20:21 -0700 | [diff] [blame] | 41 | metrics soong_metrics_proto.MetricsBase |
Patrice Arruda | 8a44a37 | 2020-12-14 20:55:23 +0000 | [diff] [blame^] | 42 | EventTracer *EventTracer |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func New() (metrics *Metrics) { |
| 46 | m := &Metrics{ |
Patrice Arruda | 457c5d3 | 2020-10-19 11:20:21 -0700 | [diff] [blame] | 47 | metrics: soong_metrics_proto.MetricsBase{}, |
Patrice Arruda | 8a44a37 | 2020-12-14 20:55:23 +0000 | [diff] [blame^] | 48 | EventTracer: &EventTracer{}, |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 49 | } |
| 50 | return m |
| 51 | } |
| 52 | |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 53 | func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 54 | switch perf.GetName() { |
| 55 | case RunKati: |
| 56 | m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 57 | case RunSoong: |
| 58 | m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf) |
Patrice Arruda | b7cf9ba | 2020-11-13 13:04:17 -0800 | [diff] [blame] | 59 | case RunBazel: |
| 60 | m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 61 | case PrimaryNinja: |
| 62 | m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf) |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 63 | case Total: |
| 64 | m.metrics.Total = &perf |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 68 | func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) { |
| 69 | m.metrics.BuildConfig = b |
| 70 | } |
| 71 | |
Patrice Arruda | 3edfd48 | 2020-10-13 23:58:41 +0000 | [diff] [blame] | 72 | func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) { |
| 73 | m.metrics.SystemResourceInfo = b |
| 74 | } |
| 75 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 76 | func (m *Metrics) SetMetadataMetrics(metadata map[string]string) { |
| 77 | for k, v := range metadata { |
| 78 | switch k { |
| 79 | case "BUILD_ID": |
| 80 | m.metrics.BuildId = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 81 | case "PLATFORM_VERSION_CODENAME": |
| 82 | m.metrics.PlatformVersionCodename = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 83 | case "TARGET_PRODUCT": |
| 84 | m.metrics.TargetProduct = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 85 | case "TARGET_BUILD_VARIANT": |
| 86 | switch v { |
| 87 | case "user": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 88 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 89 | case "userdebug": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 90 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 91 | case "eng": |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 92 | m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum() |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 93 | } |
| 94 | case "TARGET_ARCH": |
| 95 | m.metrics.TargetArch = m.getArch(v) |
| 96 | case "TARGET_ARCH_VARIANT": |
| 97 | m.metrics.TargetArchVariant = proto.String(v) |
| 98 | case "TARGET_CPU_VARIANT": |
| 99 | m.metrics.TargetCpuVariant = proto.String(v) |
| 100 | case "HOST_ARCH": |
| 101 | m.metrics.HostArch = m.getArch(v) |
| 102 | case "HOST_2ND_ARCH": |
| 103 | m.metrics.Host_2NdArch = m.getArch(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 104 | case "HOST_OS_EXTRA": |
| 105 | m.metrics.HostOsExtra = proto.String(v) |
| 106 | case "HOST_CROSS_OS": |
| 107 | m.metrics.HostCrossOs = proto.String(v) |
| 108 | case "HOST_CROSS_ARCH": |
| 109 | m.metrics.HostCrossArch = proto.String(v) |
| 110 | case "HOST_CROSS_2ND_ARCH": |
| 111 | m.metrics.HostCross_2NdArch = proto.String(v) |
| 112 | case "OUT_DIR": |
| 113 | m.metrics.OutDir = proto.String(v) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 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 | |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 133 | func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) { |
| 134 | m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second)) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Patrice Arruda | e92c30d | 2020-10-29 11:01:32 -0700 | [diff] [blame] | 137 | func (m *Metrics) SetBuildCommand(cmd []string) { |
| 138 | m.metrics.BuildCommand = proto.String(strings.Join(cmd, " ")) |
| 139 | } |
| 140 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 141 | // exports the output to the file at outputPath |
Patrice Arruda | 4fb8adc | 2020-10-12 22:38:06 +0000 | [diff] [blame] | 142 | func (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 Arruda | add7ea9 | 2020-08-07 17:55:23 +0000 | [diff] [blame] | 148 | m.metrics.HostOs = proto.String(runtime.GOOS) |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 149 | return writeMessageToFile(&m.metrics, outputPath) |
| 150 | } |
| 151 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 152 | func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 153 | m.metrics.SoongBuildMetrics = metrics |
| 154 | } |
| 155 | |
Colin Cross | d0be210 | 2019-11-26 16:16:57 -0800 | [diff] [blame] | 156 | type CriticalUserJourneysMetrics struct { |
| 157 | cujs soong_metrics_proto.CriticalUserJourneysMetrics |
| 158 | } |
| 159 | |
| 160 | func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics { |
| 161 | return &CriticalUserJourneysMetrics{} |
| 162 | } |
| 163 | |
| 164 | func (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 | |
| 171 | func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) { |
| 172 | return writeMessageToFile(&c.cujs, outputPath) |
| 173 | } |
| 174 | |
| 175 | func writeMessageToFile(pb proto.Message, outputPath string) (err error) { |
| 176 | data, err := proto.Marshal(pb) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 177 | if err != nil { |
| 178 | return err |
| 179 | } |
| 180 | tempPath := outputPath + ".tmp" |
Patrice Arruda | 0cc5b21 | 2019-06-14 15:27:46 -0700 | [diff] [blame] | 181 | err = ioutil.WriteFile(tempPath, []byte(data), 0644) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 182 | 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 | } |