blob: 1580f82b17f399b18a217d39f856b59e210de393 [file] [log] [blame]
Colin Crossb72c9092020-02-10 11:23:49 -08001// Copyright 2020 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
15package android
16
17import (
18 "io/ioutil"
19 "runtime"
MarkDacekff851b82022-04-21 18:33:17 +000020 "sort"
Colin Crossb72c9092020-02-10 11:23:49 -080021
Chris Parsons715b08f2022-03-22 19:23:40 -040022 "github.com/google/blueprint/metrics"
Dan Willemsen4591b642021-05-24 14:24:12 -070023 "google.golang.org/protobuf/proto"
Colin Crossb72c9092020-02-10 11:23:49 -080024
25 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
26)
27
28var soongMetricsOnceKey = NewOnceKey("soong metrics")
29
30type SoongMetrics struct {
31 Modules int
32 Variants int
33}
34
35func ReadSoongMetrics(config Config) SoongMetrics {
36 return config.Get(soongMetricsOnceKey).(SoongMetrics)
37}
38
39func init() {
40 RegisterSingletonType("soong_metrics", soongMetricsSingletonFactory)
41}
42
43func soongMetricsSingletonFactory() Singleton { return soongMetricsSingleton{} }
44
45type soongMetricsSingleton struct{}
46
47func (soongMetricsSingleton) GenerateBuildActions(ctx SingletonContext) {
48 metrics := SoongMetrics{}
49 ctx.VisitAllModules(func(m Module) {
50 if ctx.PrimaryModule(m) == m {
51 metrics.Modules++
52 }
53 metrics.Variants++
54 })
55 ctx.Config().Once(soongMetricsOnceKey, func() interface{} {
56 return metrics
57 })
58}
59
Chris Parsons715b08f2022-03-22 19:23:40 -040060func collectMetrics(config Config, eventHandler metrics.EventHandler) *soong_metrics_proto.SoongBuildMetrics {
Colin Crossb72c9092020-02-10 11:23:49 -080061 metrics := &soong_metrics_proto.SoongBuildMetrics{}
62
63 soongMetrics := ReadSoongMetrics(config)
64 metrics.Modules = proto.Uint32(uint32(soongMetrics.Modules))
65 metrics.Variants = proto.Uint32(uint32(soongMetrics.Variants))
66
67 memStats := runtime.MemStats{}
68 runtime.ReadMemStats(&memStats)
69 metrics.MaxHeapSize = proto.Uint64(memStats.HeapSys)
70 metrics.TotalAllocCount = proto.Uint64(memStats.Mallocs)
71 metrics.TotalAllocSize = proto.Uint64(memStats.TotalAlloc)
72
Chris Parsons715b08f2022-03-22 19:23:40 -040073 for _, event := range eventHandler.CompletedEvents() {
74 perfInfo := soong_metrics_proto.PerfInfo{
75 Description: proto.String(event.Id),
76 Name: proto.String("soong_build"),
77 StartTime: proto.Uint64(uint64(event.Start.UnixNano())),
78 RealTime: proto.Uint64(event.RuntimeNanoseconds()),
79 }
80 metrics.Events = append(metrics.Events, &perfInfo)
81 }
MarkDacekff851b82022-04-21 18:33:17 +000082 mixedBuildsInfo := soong_metrics_proto.MixedBuildsInfo{}
83 mixedBuildEnabledModules := make([]string, 0, len(config.mixedBuildEnabledModules))
84 for module, _ := range config.mixedBuildEnabledModules {
85 mixedBuildEnabledModules = append(mixedBuildEnabledModules, module)
86 }
87
88 mixedBuildDisabledModules := make([]string, 0, len(config.mixedBuildDisabledModules))
89 for module, _ := range config.mixedBuildDisabledModules {
90 mixedBuildDisabledModules = append(mixedBuildDisabledModules, module)
91 }
92 // Sorted for deterministic output.
93 sort.Strings(mixedBuildEnabledModules)
94 sort.Strings(mixedBuildDisabledModules)
95
96 mixedBuildsInfo.MixedBuildEnabledModules = mixedBuildEnabledModules
97 mixedBuildsInfo.MixedBuildDisabledModules = mixedBuildDisabledModules
98 metrics.MixedBuildsInfo = &mixedBuildsInfo
Chris Parsons715b08f2022-03-22 19:23:40 -040099
Colin Crossb72c9092020-02-10 11:23:49 -0800100 return metrics
101}
102
Chris Parsons715b08f2022-03-22 19:23:40 -0400103func WriteMetrics(config Config, eventHandler metrics.EventHandler, metricsFile string) error {
104 metrics := collectMetrics(config, eventHandler)
Colin Crossb72c9092020-02-10 11:23:49 -0800105
106 buf, err := proto.Marshal(metrics)
107 if err != nil {
108 return err
109 }
110 err = ioutil.WriteFile(absolutePath(metricsFile), buf, 0666)
111 if err != nil {
112 return err
113 }
114
115 return nil
116}