metrics.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. package android
  15. import (
  16. "io/ioutil"
  17. "runtime"
  18. "sort"
  19. "github.com/google/blueprint/metrics"
  20. "google.golang.org/protobuf/proto"
  21. soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
  22. )
  23. var soongMetricsOnceKey = NewOnceKey("soong metrics")
  24. type SoongMetrics struct {
  25. Modules int
  26. Variants int
  27. }
  28. func readSoongMetrics(config Config) (SoongMetrics, bool) {
  29. soongMetrics, ok := config.Peek(soongMetricsOnceKey)
  30. if ok {
  31. return soongMetrics.(SoongMetrics), true
  32. } else {
  33. return SoongMetrics{}, false
  34. }
  35. }
  36. func init() {
  37. RegisterParallelSingletonType("soong_metrics", soongMetricsSingletonFactory)
  38. }
  39. func soongMetricsSingletonFactory() Singleton { return soongMetricsSingleton{} }
  40. type soongMetricsSingleton struct{}
  41. func (soongMetricsSingleton) GenerateBuildActions(ctx SingletonContext) {
  42. metrics := SoongMetrics{}
  43. ctx.VisitAllModules(func(m Module) {
  44. if ctx.PrimaryModule(m) == m {
  45. metrics.Modules++
  46. }
  47. metrics.Variants++
  48. })
  49. ctx.Config().Once(soongMetricsOnceKey, func() interface{} {
  50. return metrics
  51. })
  52. }
  53. func collectMetrics(config Config, eventHandler *metrics.EventHandler) *soong_metrics_proto.SoongBuildMetrics {
  54. metrics := &soong_metrics_proto.SoongBuildMetrics{}
  55. soongMetrics, ok := readSoongMetrics(config)
  56. if ok {
  57. metrics.Modules = proto.Uint32(uint32(soongMetrics.Modules))
  58. metrics.Variants = proto.Uint32(uint32(soongMetrics.Variants))
  59. }
  60. memStats := runtime.MemStats{}
  61. runtime.ReadMemStats(&memStats)
  62. metrics.MaxHeapSize = proto.Uint64(memStats.HeapSys)
  63. metrics.TotalAllocCount = proto.Uint64(memStats.Mallocs)
  64. metrics.TotalAllocSize = proto.Uint64(memStats.TotalAlloc)
  65. for _, event := range eventHandler.CompletedEvents() {
  66. perfInfo := soong_metrics_proto.PerfInfo{
  67. Description: proto.String(event.Id),
  68. Name: proto.String("soong_build"),
  69. StartTime: proto.Uint64(uint64(event.Start.UnixNano())),
  70. RealTime: proto.Uint64(event.RuntimeNanoseconds()),
  71. }
  72. metrics.Events = append(metrics.Events, &perfInfo)
  73. }
  74. mixedBuildsInfo := soong_metrics_proto.MixedBuildsInfo{}
  75. mixedBuildEnabledModules := make([]string, 0, len(config.mixedBuildEnabledModules))
  76. for module, _ := range config.mixedBuildEnabledModules {
  77. mixedBuildEnabledModules = append(mixedBuildEnabledModules, module)
  78. }
  79. mixedBuildDisabledModules := make([]string, 0, len(config.mixedBuildDisabledModules))
  80. for module, _ := range config.mixedBuildDisabledModules {
  81. mixedBuildDisabledModules = append(mixedBuildDisabledModules, module)
  82. }
  83. // Sorted for deterministic output.
  84. sort.Strings(mixedBuildEnabledModules)
  85. sort.Strings(mixedBuildDisabledModules)
  86. mixedBuildsInfo.MixedBuildEnabledModules = mixedBuildEnabledModules
  87. mixedBuildsInfo.MixedBuildDisabledModules = mixedBuildDisabledModules
  88. metrics.MixedBuildsInfo = &mixedBuildsInfo
  89. return metrics
  90. }
  91. func WriteMetrics(config Config, eventHandler *metrics.EventHandler, metricsFile string) error {
  92. metrics := collectMetrics(config, eventHandler)
  93. buf, err := proto.Marshal(metrics)
  94. if err != nil {
  95. return err
  96. }
  97. err = ioutil.WriteFile(absolutePath(metricsFile), buf, 0666)
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. }