Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "runtime" |
MarkDacek | ff851b8 | 2022-04-21 18:33:17 +0000 | [diff] [blame] | 20 | "sort" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 21 | |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 22 | "github.com/google/blueprint/metrics" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 23 | "google.golang.org/protobuf/proto" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 24 | |
| 25 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
| 26 | ) |
| 27 | |
| 28 | var soongMetricsOnceKey = NewOnceKey("soong metrics") |
| 29 | |
| 30 | type SoongMetrics struct { |
| 31 | Modules int |
| 32 | Variants int |
| 33 | } |
| 34 | |
| 35 | func ReadSoongMetrics(config Config) SoongMetrics { |
| 36 | return config.Get(soongMetricsOnceKey).(SoongMetrics) |
| 37 | } |
| 38 | |
| 39 | func init() { |
| 40 | RegisterSingletonType("soong_metrics", soongMetricsSingletonFactory) |
| 41 | } |
| 42 | |
| 43 | func soongMetricsSingletonFactory() Singleton { return soongMetricsSingleton{} } |
| 44 | |
| 45 | type soongMetricsSingleton struct{} |
| 46 | |
| 47 | func (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 Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 60 | func collectMetrics(config Config, eventHandler metrics.EventHandler) *soong_metrics_proto.SoongBuildMetrics { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 61 | 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 Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 73 | 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 | } |
MarkDacek | ff851b8 | 2022-04-21 18:33:17 +0000 | [diff] [blame] | 82 | 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 Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 99 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 100 | return metrics |
| 101 | } |
| 102 | |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 103 | func WriteMetrics(config Config, eventHandler metrics.EventHandler, metricsFile string) error { |
| 104 | metrics := collectMetrics(config, eventHandler) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 105 | |
| 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 | } |