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 ( |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 18 | "bytes" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 19 | "io/ioutil" |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 20 | "os" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 21 | "runtime" |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 22 | "strconv" |
| 23 | "time" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 24 | |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 25 | "github.com/google/blueprint/metrics" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 26 | "google.golang.org/protobuf/proto" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 27 | |
| 28 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
| 29 | ) |
| 30 | |
| 31 | var soongMetricsOnceKey = NewOnceKey("soong metrics") |
| 32 | |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 33 | type soongMetrics struct { |
| 34 | modules int |
| 35 | variants int |
| 36 | perfCollector perfCollector |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 39 | type perfCollector struct { |
| 40 | events []*soong_metrics_proto.PerfCounters |
| 41 | stop chan<- bool |
| 42 | } |
| 43 | |
| 44 | func getSoongMetrics(config Config) *soongMetrics { |
| 45 | return config.Once(soongMetricsOnceKey, func() interface{} { |
| 46 | return &soongMetrics{} |
| 47 | }).(*soongMetrics) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func init() { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 51 | RegisterParallelSingletonType("soong_metrics", soongMetricsSingletonFactory) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | func soongMetricsSingletonFactory() Singleton { return soongMetricsSingleton{} } |
| 55 | |
| 56 | type soongMetricsSingleton struct{} |
| 57 | |
| 58 | func (soongMetricsSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 59 | metrics := getSoongMetrics(ctx.Config()) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 60 | ctx.VisitAllModules(func(m Module) { |
| 61 | if ctx.PrimaryModule(m) == m { |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 62 | metrics.modules++ |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 63 | } |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 64 | metrics.variants++ |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 65 | }) |
| 66 | } |
| 67 | |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 68 | func collectMetrics(config Config, eventHandler *metrics.EventHandler) *soong_metrics_proto.SoongBuildMetrics { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 69 | metrics := &soong_metrics_proto.SoongBuildMetrics{} |
| 70 | |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 71 | soongMetrics := getSoongMetrics(config) |
| 72 | if soongMetrics.modules > 0 { |
| 73 | metrics.Modules = proto.Uint32(uint32(soongMetrics.modules)) |
| 74 | metrics.Variants = proto.Uint32(uint32(soongMetrics.variants)) |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 75 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 76 | |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 77 | soongMetrics.perfCollector.stop <- true |
| 78 | metrics.PerfCounters = soongMetrics.perfCollector.events |
| 79 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 80 | memStats := runtime.MemStats{} |
| 81 | runtime.ReadMemStats(&memStats) |
| 82 | metrics.MaxHeapSize = proto.Uint64(memStats.HeapSys) |
| 83 | metrics.TotalAllocCount = proto.Uint64(memStats.Mallocs) |
| 84 | metrics.TotalAllocSize = proto.Uint64(memStats.TotalAlloc) |
| 85 | |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 86 | for _, event := range eventHandler.CompletedEvents() { |
| 87 | perfInfo := soong_metrics_proto.PerfInfo{ |
| 88 | Description: proto.String(event.Id), |
| 89 | Name: proto.String("soong_build"), |
| 90 | StartTime: proto.Uint64(uint64(event.Start.UnixNano())), |
| 91 | RealTime: proto.Uint64(event.RuntimeNanoseconds()), |
| 92 | } |
| 93 | metrics.Events = append(metrics.Events, &perfInfo) |
| 94 | } |
| 95 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 96 | return metrics |
| 97 | } |
| 98 | |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 99 | func StartBackgroundMetrics(config Config) { |
| 100 | perfCollector := &getSoongMetrics(config).perfCollector |
| 101 | stop := make(chan bool) |
| 102 | perfCollector.stop = stop |
| 103 | |
| 104 | previousTime := time.Now() |
| 105 | previousCpuTime := readCpuTime() |
| 106 | |
| 107 | ticker := time.NewTicker(time.Second) |
| 108 | |
| 109 | go func() { |
| 110 | for { |
| 111 | select { |
| 112 | case <-stop: |
| 113 | ticker.Stop() |
| 114 | return |
| 115 | case <-ticker.C: |
| 116 | // carry on |
| 117 | } |
| 118 | |
| 119 | currentTime := time.Now() |
| 120 | |
| 121 | var memStats runtime.MemStats |
| 122 | runtime.ReadMemStats(&memStats) |
| 123 | |
| 124 | currentCpuTime := readCpuTime() |
| 125 | |
| 126 | interval := currentTime.Sub(previousTime) |
| 127 | intervalCpuTime := currentCpuTime - previousCpuTime |
| 128 | intervalCpuPercent := intervalCpuTime * 100 / interval |
| 129 | |
| 130 | // heapAlloc is the memory that has been allocated on the heap but not yet GC'd. It may be referenced, |
| 131 | // or unrefenced but not yet GC'd. |
| 132 | heapAlloc := memStats.HeapAlloc |
| 133 | // heapUnused is the memory that was previously used by the heap, but is currently not used. It does not |
| 134 | // count memory that was used and then returned to the OS. |
| 135 | heapUnused := memStats.HeapIdle - memStats.HeapReleased |
| 136 | // heapOverhead is the memory used by the allocator and GC |
| 137 | heapOverhead := memStats.MSpanSys + memStats.MCacheSys + memStats.GCSys |
| 138 | // otherMem is the memory used outside of the heap. |
| 139 | otherMem := memStats.Sys - memStats.HeapSys - heapOverhead |
| 140 | |
| 141 | perfCollector.events = append(perfCollector.events, &soong_metrics_proto.PerfCounters{ |
| 142 | Time: proto.Uint64(uint64(currentTime.UnixNano())), |
| 143 | Groups: []*soong_metrics_proto.PerfCounterGroup{ |
| 144 | { |
| 145 | Name: proto.String("cpu"), |
| 146 | Counters: []*soong_metrics_proto.PerfCounter{ |
| 147 | {Name: proto.String("cpu_percent"), Value: proto.Int64(int64(intervalCpuPercent))}, |
| 148 | }, |
| 149 | }, { |
| 150 | Name: proto.String("memory"), |
| 151 | Counters: []*soong_metrics_proto.PerfCounter{ |
| 152 | {Name: proto.String("heap_alloc"), Value: proto.Int64(int64(heapAlloc))}, |
| 153 | {Name: proto.String("heap_unused"), Value: proto.Int64(int64(heapUnused))}, |
| 154 | {Name: proto.String("heap_overhead"), Value: proto.Int64(int64(heapOverhead))}, |
| 155 | {Name: proto.String("other"), Value: proto.Int64(int64(otherMem))}, |
| 156 | }, |
| 157 | }, |
| 158 | }, |
| 159 | }) |
| 160 | |
| 161 | previousTime = currentTime |
| 162 | previousCpuTime = currentCpuTime |
| 163 | } |
| 164 | }() |
| 165 | } |
| 166 | |
| 167 | func readCpuTime() time.Duration { |
| 168 | if runtime.GOOS != "linux" { |
| 169 | return 0 |
| 170 | } |
| 171 | |
| 172 | stat, err := os.ReadFile("/proc/self/stat") |
| 173 | if err != nil { |
| 174 | return 0 |
| 175 | } |
| 176 | |
| 177 | endOfComm := bytes.LastIndexByte(stat, ')') |
| 178 | if endOfComm < 0 || endOfComm > len(stat)-2 { |
| 179 | return 0 |
| 180 | } |
| 181 | |
| 182 | stat = stat[endOfComm+2:] |
| 183 | |
| 184 | statFields := bytes.Split(stat, []byte{' '}) |
| 185 | // This should come from sysconf(_SC_CLK_TCK), but there's no way to call that from Go. Assume it's 100, |
| 186 | // which is the value for all platforms we support. |
| 187 | const HZ = 100 |
| 188 | const MS_PER_HZ = 1e3 / HZ * time.Millisecond |
| 189 | |
| 190 | const STAT_UTIME_FIELD = 14 - 2 |
| 191 | const STAT_STIME_FIELD = 15 - 2 |
| 192 | if len(statFields) < STAT_STIME_FIELD { |
| 193 | return 0 |
| 194 | } |
| 195 | userCpuTicks, err := strconv.ParseUint(string(statFields[STAT_UTIME_FIELD]), 10, 64) |
| 196 | if err != nil { |
| 197 | return 0 |
| 198 | } |
| 199 | kernelCpuTicks, _ := strconv.ParseUint(string(statFields[STAT_STIME_FIELD]), 10, 64) |
| 200 | if err != nil { |
| 201 | return 0 |
| 202 | } |
| 203 | return time.Duration(userCpuTicks+kernelCpuTicks) * MS_PER_HZ |
| 204 | } |
| 205 | |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 206 | func WriteMetrics(config Config, eventHandler *metrics.EventHandler, metricsFile string) error { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 207 | metrics := collectMetrics(config, eventHandler) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 208 | |
| 209 | buf, err := proto.Marshal(metrics) |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | err = ioutil.WriteFile(absolutePath(metricsFile), buf, 0666) |
| 214 | if err != nil { |
| 215 | return err |
| 216 | } |
| 217 | |
| 218 | return nil |
| 219 | } |