2018-12-13 08:01:49 +08: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-07-14 07:01:18 +08:00
|
|
|
"time"
|
2018-12-13 08:01:49 +08:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2019-11-27 08:16:57 +08:00
|
|
|
|
|
|
|
soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
|
2018-12-13 08:01:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-07 20:48:26 +08:00
|
|
|
PrimaryNinja = "ninja"
|
|
|
|
RunKati = "kati"
|
|
|
|
RunSetupTool = "setup"
|
|
|
|
RunShutdownTool = "shutdown"
|
|
|
|
RunSoong = "soong"
|
|
|
|
TestRun = "test"
|
|
|
|
Total = "total"
|
2018-12-13 08:01:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Metrics struct {
|
2019-06-15 06:27:46 +08:00
|
|
|
metrics soong_metrics_proto.MetricsBase
|
2018-12-13 08:01:49 +08:00
|
|
|
TimeTracer TimeTracer
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() (metrics *Metrics) {
|
|
|
|
m := &Metrics{
|
2019-06-15 06:27:46 +08:00
|
|
|
metrics: soong_metrics_proto.MetricsBase{},
|
2018-12-13 08:01:49 +08:00
|
|
|
TimeTracer: &timeTracerImpl{},
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-06-15 06:27:46 +08:00
|
|
|
func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
|
2018-12-13 08:01:49 +08: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 07:25:50 +08:00
|
|
|
case Total:
|
|
|
|
m.metrics.Total = &perf
|
2018-12-13 08:01:49 +08:00
|
|
|
default:
|
|
|
|
// ignored
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 06:27:46 +08:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
|
2018-12-13 08:01:49 +08:00
|
|
|
case "userdebug":
|
2019-06-15 06:27:46 +08:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
|
2018-12-13 08:01:49 +08:00
|
|
|
case "eng":
|
2019-06-15 06:27:46 +08:00
|
|
|
m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
|
2018-12-13 08:01:49 +08: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":
|
|
|
|
m.metrics.HostOs = proto.String(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 06:27:46 +08:00
|
|
|
func (m *Metrics) getArch(arch string) *soong_metrics_proto.MetricsBase_Arch {
|
2018-12-13 08:01:49 +08:00
|
|
|
switch arch {
|
|
|
|
case "arm":
|
2019-06-15 06:27:46 +08:00
|
|
|
return soong_metrics_proto.MetricsBase_ARM.Enum()
|
2018-12-13 08:01:49 +08:00
|
|
|
case "arm64":
|
2019-06-15 06:27:46 +08:00
|
|
|
return soong_metrics_proto.MetricsBase_ARM64.Enum()
|
2018-12-13 08:01:49 +08:00
|
|
|
case "x86":
|
2019-06-15 06:27:46 +08:00
|
|
|
return soong_metrics_proto.MetricsBase_X86.Enum()
|
2018-12-13 08:01:49 +08:00
|
|
|
case "x86_64":
|
2019-06-15 06:27:46 +08:00
|
|
|
return soong_metrics_proto.MetricsBase_X86_64.Enum()
|
2018-12-13 08:01:49 +08:00
|
|
|
default:
|
2019-06-15 06:27:46 +08:00
|
|
|
return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
|
2018-12-13 08:01:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:01:18 +08:00
|
|
|
func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
|
|
|
|
m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
|
2018-12-13 08:01:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// exports the output to the file at outputPath
|
|
|
|
func (m *Metrics) Dump(outputPath string) (err error) {
|
2019-11-27 08:16:57 +08:00
|
|
|
return writeMessageToFile(&m.metrics, outputPath)
|
|
|
|
}
|
|
|
|
|
2020-02-11 03:23:49 +08:00
|
|
|
func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
|
|
|
|
m.metrics.SoongBuildMetrics = metrics
|
|
|
|
}
|
|
|
|
|
2019-11-27 08:16:57 +08: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 08:01:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tempPath := outputPath + ".tmp"
|
2019-06-15 06:27:46 +08:00
|
|
|
err = ioutil.WriteFile(tempPath, []byte(data), 0644)
|
2018-12-13 08:01:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Rename(tempPath, outputPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|