2018-12-13 01:01:49 +01:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-08-07 19:55:23 +02:00
|
|
|
"runtime"
|
2020-07-14 01:01:18 +02:00
|
|
|
"time"
|
2018-12-13 01:01:49 +01:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2019-11-27 01:16:57 +01:00
|
|
|
|
|
|
|
soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
|
2018-12-13 01:01:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-07 14:48:26 +02:00
|
|
|
PrimaryNinja = "ninja"
|
|
|
|
RunKati = "kati"
|
|
|
|
RunSetupTool = "setup"
|
|
|
|
RunShutdownTool = "shutdown"
|
|
|
|
RunSoong = "soong"
|
|
|
|
TestRun = "test"
|
|
|
|
Total = "total"
|
2018-12-13 01:01:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Metrics struct {
|
2020-10-19 20:20:21 +02:00
|
|
|
metrics soong_metrics_proto.MetricsBase
|
|
|
|
EventTracer EventTracer
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() (metrics *Metrics) {
|
|
|
|
m := &Metrics{
|
2020-10-19 20:20:21 +02:00
|
|
|
metrics: soong_metrics_proto.MetricsBase{},
|
|
|
|
EventTracer: &eventTracerImpl{},
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:27:46 +02:00
|
|
|
func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
|
2018-12-13 01:01:49 +01:00
|
|
|
switch perf.GetName() {
|
|
|
|
case RunKati:
|
|
|
|
m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
|
|
|
|
break
|
|
|
|
case RunSoong:
|
|
|
|
m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
|
|
|
|
break
|
|
|
|
case PrimaryNinja:
|
|
|
|
m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
|
|
|
|
break
|
2020-01-17 00:25:50 +01:00
|
|
|
case Total:
|
|
|
|
m.metrics.Total = &perf
|
2018-12-13 01:01:49 +01:00
|
|
|
default:
|
|
|
|
// ignored
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:41:11 +02:00
|
|
|
func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
|
|
|
|
m.metrics.BuildConfig = b
|
|
|
|
}
|
|
|
|
|
2020-10-14 01:58:41 +02:00
|
|
|
func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
|
|
|
|
m.metrics.SystemResourceInfo = b
|
|
|
|
}
|
|
|
|
|
2018-12-13 01:01:49 +01:00
|
|
|
func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
|
|
|
|
for k, v := range metadata {
|
|
|
|
switch k {
|
|
|
|
case "BUILD_ID":
|
|
|
|
m.metrics.BuildId = proto.String(v)
|
|
|
|
break
|
|
|
|
case "PLATFORM_VERSION_CODENAME":
|
|
|
|
m.metrics.PlatformVersionCodename = proto.String(v)
|
|
|
|
break
|
|
|
|
case "TARGET_PRODUCT":
|
|
|
|
m.metrics.TargetProduct = proto.String(v)
|
|
|
|
break
|
|
|
|
case "TARGET_BUILD_VARIANT":
|
|
|
|
switch v {
|
|
|
|
case "user":
|
2019-06-15 00:27:46 +02:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "userdebug":
|
2019-06-15 00:27:46 +02:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "eng":
|
2019-06-15 00:27:46 +02:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
default:
|
|
|
|
// ignored
|
|
|
|
}
|
|
|
|
case "TARGET_ARCH":
|
|
|
|
m.metrics.TargetArch = m.getArch(v)
|
|
|
|
case "TARGET_ARCH_VARIANT":
|
|
|
|
m.metrics.TargetArchVariant = proto.String(v)
|
|
|
|
case "TARGET_CPU_VARIANT":
|
|
|
|
m.metrics.TargetCpuVariant = proto.String(v)
|
|
|
|
case "HOST_ARCH":
|
|
|
|
m.metrics.HostArch = m.getArch(v)
|
|
|
|
case "HOST_2ND_ARCH":
|
|
|
|
m.metrics.Host_2NdArch = m.getArch(v)
|
|
|
|
case "HOST_OS_EXTRA":
|
|
|
|
m.metrics.HostOsExtra = proto.String(v)
|
|
|
|
case "HOST_CROSS_OS":
|
|
|
|
m.metrics.HostCrossOs = proto.String(v)
|
|
|
|
case "HOST_CROSS_ARCH":
|
|
|
|
m.metrics.HostCrossArch = proto.String(v)
|
|
|
|
case "HOST_CROSS_2ND_ARCH":
|
|
|
|
m.metrics.HostCross_2NdArch = proto.String(v)
|
|
|
|
case "OUT_DIR":
|
|
|
|
m.metrics.OutDir = proto.String(v)
|
|
|
|
default:
|
|
|
|
// ignored
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:27:46 +02:00
|
|
|
func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch {
|
2018-12-13 01:01:49 +01:00
|
|
|
switch arch {
|
|
|
|
case "arm":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_ARM.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "arm64":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_ARM64.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "x86":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_X86.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
case "x86_64":
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_X86_64.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
default:
|
2019-06-15 00:27:46 +02:00
|
|
|
return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 01:01:18 +02:00
|
|
|
func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
|
|
|
|
m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
|
2018-12-13 01:01:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// exports the output to the file at outputPath
|
2020-10-13 00:38:06 +02:00
|
|
|
func (m *Metrics) Dump(outputPath string) error {
|
|
|
|
// ignore the error if the hostname could not be retrieved as it
|
|
|
|
// is not a critical metric to extract.
|
|
|
|
if hostname, err := os.Hostname(); err == nil {
|
|
|
|
m.metrics.Hostname = proto.String(hostname)
|
|
|
|
}
|
2020-08-07 19:55:23 +02:00
|
|
|
m.metrics.HostOs = proto.String(runtime.GOOS)
|
2019-11-27 01:16:57 +01:00
|
|
|
return writeMessageToFile(&m.metrics, outputPath)
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:23:49 +01:00
|
|
|
func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
|
|
|
|
m.metrics.SoongBuildMetrics = metrics
|
|
|
|
}
|
|
|
|
|
2019-11-27 01:16:57 +01:00
|
|
|
type CriticalUserJourneysMetrics struct {
|
|
|
|
cujs soong_metrics_proto.CriticalUserJourneysMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
|
|
|
|
return &CriticalUserJourneysMetrics{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
|
|
|
|
c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
|
|
|
|
Name: proto.String(name),
|
|
|
|
Metrics: &metrics.metrics,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CriticalUserJourneysMetrics) Dump(outputPath string) (err error) {
|
|
|
|
return writeMessageToFile(&c.cujs, outputPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeMessageToFile(pb proto.Message, outputPath string) (err error) {
|
|
|
|
data, err := proto.Marshal(pb)
|
2018-12-13 01:01:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tempPath := outputPath + ".tmp"
|
2019-06-15 00:27:46 +02:00
|
|
|
err = ioutil.WriteFile(tempPath, []byte(data), 0644)
|
2018-12-13 01:01:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Rename(tempPath, outputPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|