metrics.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright 2018 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 metrics represents the metrics system for Android Platform Build Systems.
  15. package metrics
  16. // This is the main heart of the metrics system for Android Platform Build Systems.
  17. // The starting of the soong_ui (cmd/soong_ui/main.go), the metrics system is
  18. // initialized by the invocation of New and is then stored in the context
  19. // (ui/build/context.go) to be used throughout the system. During the build
  20. // initialization phase, several functions in this file are invoked to store
  21. // information such as the environment, build configuration and build metadata.
  22. // There are several scoped code that has Begin() and defer End() functions
  23. // that captures the metrics and is them added as a perfInfo into the set
  24. // of the collected metrics. Finally, when soong_ui has finished the build,
  25. // the defer Dump function is invoked to store the collected metrics to the
  26. // raw protobuf file in the $OUT directory and this raw protobuf file will be
  27. // uploaded to the destination. See ui/build/upload.go for more details. The
  28. // filename of the raw protobuf file and the list of files to be uploaded is
  29. // defined in cmd/soong_ui/main.go. See ui/metrics/event.go for the explanation
  30. // of what an event is and how the metrics system is a stack based system.
  31. import (
  32. "fmt"
  33. "os"
  34. "runtime"
  35. "strings"
  36. "time"
  37. "android/soong/shared"
  38. "google.golang.org/protobuf/proto"
  39. soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
  40. mk_metrics_proto "android/soong/ui/metrics/mk_metrics_proto"
  41. )
  42. const (
  43. // Below is a list of names passed in to the Begin tracing functions. These
  44. // names are used to group a set of metrics.
  45. // Setup and tear down of the build systems.
  46. RunSetupTool = "setup"
  47. RunShutdownTool = "shutdown"
  48. TestRun = "test"
  49. // List of build system tools.
  50. RunSoong = "soong"
  51. PrimaryNinja = "ninja"
  52. RunKati = "kati"
  53. RunBazel = "bazel"
  54. // Overall build from building the graph to building the target.
  55. Total = "total"
  56. )
  57. // Metrics is a struct that stores collected metrics during the course of a
  58. // build. It is later dumped to protobuf files. See underlying metrics protos
  59. // for further details on what information is collected.
  60. type Metrics struct {
  61. // Protobuf containing various top-level build metrics. These include:
  62. // 1. Build identifiers (ex: branch ID, requested product, hostname,
  63. // originating command)
  64. // 2. Per-subprocess top-level metrics (ex: ninja process IO and runtime).
  65. // Note that, since these metrics are reported by soong_ui, there is little
  66. // insight that can be provided into performance breakdowns of individual
  67. // subprocesses.
  68. metrics soong_metrics_proto.MetricsBase
  69. // Protobuf containing metrics pertaining to number of makefiles in a build.
  70. mkMetrics mk_metrics_proto.MkMetrics
  71. // A list of pending build events.
  72. EventTracer *EventTracer
  73. }
  74. // New returns a pointer of Metrics to store a set of metrics.
  75. func New() (metrics *Metrics) {
  76. m := &Metrics{
  77. metrics: soong_metrics_proto.MetricsBase{},
  78. mkMetrics: mk_metrics_proto.MkMetrics{},
  79. EventTracer: &EventTracer{},
  80. }
  81. return m
  82. }
  83. func (m *Metrics) SetTotalMakefiles(total int) {
  84. m.mkMetrics.TotalMakefiles = uint32(total)
  85. }
  86. func (m *Metrics) SetToplevelMakefiles(total int) {
  87. m.mkMetrics.ToplevelMakefiles = uint32(total)
  88. }
  89. func (m *Metrics) DumpMkMetrics(outPath string) {
  90. shared.Save(&m.mkMetrics, outPath)
  91. }
  92. // SetTimeMetrics stores performance information from an executed block of
  93. // code.
  94. func (m *Metrics) SetTimeMetrics(perf soong_metrics_proto.PerfInfo) {
  95. switch perf.GetName() {
  96. case RunKati:
  97. m.metrics.KatiRuns = append(m.metrics.KatiRuns, &perf)
  98. case RunSoong:
  99. m.metrics.SoongRuns = append(m.metrics.SoongRuns, &perf)
  100. case RunBazel:
  101. m.metrics.BazelRuns = append(m.metrics.BazelRuns, &perf)
  102. case PrimaryNinja:
  103. m.metrics.NinjaRuns = append(m.metrics.NinjaRuns, &perf)
  104. case RunSetupTool:
  105. m.metrics.SetupTools = append(m.metrics.SetupTools, &perf)
  106. case Total:
  107. m.metrics.Total = &perf
  108. }
  109. }
  110. func (m *Metrics) SetCriticalPathInfo(criticalPathInfo soong_metrics_proto.CriticalPathInfo) {
  111. m.metrics.CriticalPathInfo = &criticalPathInfo
  112. }
  113. // SetFatalOrPanicMessage stores a non-zero exit and the relevant message in the latest event if
  114. // available or the metrics base.
  115. func (m *Metrics) SetFatalOrPanicMessage(errMsg string) {
  116. if m == nil {
  117. return
  118. }
  119. if event := m.EventTracer.peek(); event != nil {
  120. event.nonZeroExitCode = true
  121. event.errorMsg = &errMsg
  122. } else {
  123. m.metrics.ErrorMessage = proto.String(errMsg)
  124. }
  125. m.metrics.NonZeroExit = proto.Bool(true)
  126. }
  127. // BuildConfig stores information about the build configuration.
  128. func (m *Metrics) BuildConfig(b *soong_metrics_proto.BuildConfig) {
  129. m.metrics.BuildConfig = b
  130. }
  131. // SystemResourceInfo stores information related to the host system such
  132. // as total CPU and memory.
  133. func (m *Metrics) SystemResourceInfo(b *soong_metrics_proto.SystemResourceInfo) {
  134. m.metrics.SystemResourceInfo = b
  135. }
  136. // ExpConfigFetcher stores information about the expconfigfetcher.
  137. func (m *Metrics) ExpConfigFetcher(b *soong_metrics_proto.ExpConfigFetcher) {
  138. m.metrics.ExpConfigFetcher = b
  139. }
  140. // SetMetadataMetrics sets information about the build such as the target
  141. // product, host architecture and out directory.
  142. func (m *Metrics) SetMetadataMetrics(metadata map[string]string) {
  143. for k, v := range metadata {
  144. switch k {
  145. case "BUILD_ID":
  146. m.metrics.BuildId = proto.String(v)
  147. case "PLATFORM_VERSION_CODENAME":
  148. m.metrics.PlatformVersionCodename = proto.String(v)
  149. case "TARGET_PRODUCT":
  150. m.metrics.TargetProduct = proto.String(v)
  151. case "TARGET_BUILD_VARIANT":
  152. switch v {
  153. case "user":
  154. m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USER.Enum()
  155. case "userdebug":
  156. m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_USERDEBUG.Enum()
  157. case "eng":
  158. m.metrics.TargetBuildVariant = soong_metrics_proto.MetricsBase_ENG.Enum()
  159. }
  160. case "TARGET_ARCH":
  161. m.metrics.TargetArch = arch(v)
  162. case "TARGET_ARCH_VARIANT":
  163. m.metrics.TargetArchVariant = proto.String(v)
  164. case "TARGET_CPU_VARIANT":
  165. m.metrics.TargetCpuVariant = proto.String(v)
  166. case "HOST_ARCH":
  167. m.metrics.HostArch = arch(v)
  168. case "HOST_2ND_ARCH":
  169. m.metrics.Host_2NdArch = arch(v)
  170. case "HOST_OS_EXTRA":
  171. m.metrics.HostOsExtra = proto.String(v)
  172. case "HOST_CROSS_OS":
  173. m.metrics.HostCrossOs = proto.String(v)
  174. case "HOST_CROSS_ARCH":
  175. m.metrics.HostCrossArch = proto.String(v)
  176. case "HOST_CROSS_2ND_ARCH":
  177. m.metrics.HostCross_2NdArch = proto.String(v)
  178. case "OUT_DIR":
  179. m.metrics.OutDir = proto.String(v)
  180. }
  181. }
  182. }
  183. // arch returns the corresponding MetricsBase_Arch based on the string
  184. // parameter.
  185. func arch(a string) *soong_metrics_proto.MetricsBase_Arch {
  186. switch a {
  187. case "arm":
  188. return soong_metrics_proto.MetricsBase_ARM.Enum()
  189. case "arm64":
  190. return soong_metrics_proto.MetricsBase_ARM64.Enum()
  191. case "x86":
  192. return soong_metrics_proto.MetricsBase_X86.Enum()
  193. case "x86_64":
  194. return soong_metrics_proto.MetricsBase_X86_64.Enum()
  195. default:
  196. return soong_metrics_proto.MetricsBase_UNKNOWN.Enum()
  197. }
  198. }
  199. // SetBuildDateTime sets the build date and time. The value written
  200. // to the protobuf file is in seconds.
  201. func (m *Metrics) SetBuildDateTime(buildTimestamp time.Time) {
  202. m.metrics.BuildDateTimestamp = proto.Int64(buildTimestamp.UnixNano() / int64(time.Second))
  203. }
  204. func (m *Metrics) UpdateTotalRealTimeAndNonZeroExit(data []byte, bazelExitCode int32) error {
  205. if err := proto.Unmarshal(data, &m.metrics); err != nil {
  206. return fmt.Errorf("Failed to unmarshal proto: %w", err)
  207. }
  208. startTime := *m.metrics.Total.StartTime
  209. endTime := uint64(time.Now().UnixNano())
  210. *m.metrics.Total.RealTime = *proto.Uint64(endTime - startTime)
  211. bazelError := bazelExitCode != 0
  212. m.metrics.NonZeroExit = proto.Bool(bazelError)
  213. return nil
  214. }
  215. // SetBuildCommand adds the build command specified by the user to the
  216. // list of collected metrics.
  217. func (m *Metrics) SetBuildCommand(cmd []string) {
  218. m.metrics.BuildCommand = proto.String(strings.Join(cmd, " "))
  219. }
  220. // AddChangedEnvironmentVariable adds the changed environment variable to
  221. // ChangedEnvironmentVariable field.
  222. func (m *Metrics) AddChangedEnvironmentVariable(ChangedEnvironmentVariable string) {
  223. m.metrics.ChangedEnvironmentVariable = append(m.metrics.ChangedEnvironmentVariable,
  224. ChangedEnvironmentVariable)
  225. }
  226. // Dump exports the collected metrics from the executed build to the file at
  227. // out path.
  228. func (m *Metrics) Dump(out string) error {
  229. // ignore the error if the hostname could not be retrieved as it
  230. // is not a critical metric to extract.
  231. if hostname, err := os.Hostname(); err == nil {
  232. m.metrics.Hostname = proto.String(hostname)
  233. }
  234. m.metrics.HostOs = proto.String(runtime.GOOS)
  235. return shared.Save(&m.metrics, out)
  236. }
  237. // SetSoongBuildMetrics sets the metrics collected from the soong_build
  238. // execution.
  239. func (m *Metrics) SetSoongBuildMetrics(metrics *soong_metrics_proto.SoongBuildMetrics) {
  240. m.metrics.SoongBuildMetrics = metrics
  241. }
  242. // A CriticalUserJourneysMetrics is a struct that contains critical user journey
  243. // metrics. These critical user journeys are defined under cuj/cuj.go file.
  244. type CriticalUserJourneysMetrics struct {
  245. // A list of collected CUJ metrics.
  246. cujs soong_metrics_proto.CriticalUserJourneysMetrics
  247. }
  248. // NewCriticalUserJourneyMetrics returns a pointer of CriticalUserJourneyMetrics
  249. // to capture CUJs metrics.
  250. func NewCriticalUserJourneysMetrics() *CriticalUserJourneysMetrics {
  251. return &CriticalUserJourneysMetrics{}
  252. }
  253. // Add adds a set of collected metrics from an executed critical user journey.
  254. func (c *CriticalUserJourneysMetrics) Add(name string, metrics *Metrics) {
  255. c.cujs.Cujs = append(c.cujs.Cujs, &soong_metrics_proto.CriticalUserJourneyMetrics{
  256. Name: proto.String(name),
  257. Metrics: &metrics.metrics,
  258. })
  259. }
  260. // Dump saves the collected CUJs metrics to the raw protobuf file.
  261. func (c *CriticalUserJourneysMetrics) Dump(filename string) (err error) {
  262. return shared.Save(&c.cujs, filename)
  263. }