bazel_metrics.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2023 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 build
  15. // This file contains functionality to parse bazel profile data into
  16. // a bazel_metrics proto, defined in build/soong/ui/metrics/bazel_metrics_proto
  17. // These metrics are later uploaded in upload.go
  18. import (
  19. "bufio"
  20. "os"
  21. "strconv"
  22. "strings"
  23. "android/soong/shared"
  24. "google.golang.org/protobuf/proto"
  25. bazel_metrics_proto "android/soong/ui/metrics/bazel_metrics_proto"
  26. )
  27. func parseTimingToNanos(str string) int64 {
  28. millisString := removeDecimalPoint(str)
  29. timingMillis, _ := strconv.ParseInt(millisString, 10, 64)
  30. return timingMillis * 1000000
  31. }
  32. func parsePercentageToTenThousandths(str string) int32 {
  33. percentageString := removeDecimalPoint(str)
  34. //remove the % at the end of the string
  35. percentage := strings.ReplaceAll(percentageString, "%", "")
  36. percentagePortion, _ := strconv.ParseInt(percentage, 10, 32)
  37. return int32(percentagePortion)
  38. }
  39. func removeDecimalPoint(numString string) string {
  40. // The format is always 0.425 or 10.425
  41. return strings.ReplaceAll(numString, ".", "")
  42. }
  43. func parseTotal(line string) int64 {
  44. words := strings.Fields(line)
  45. timing := words[3]
  46. return parseTimingToNanos(timing)
  47. }
  48. func parsePhaseTiming(line string) bazel_metrics_proto.PhaseTiming {
  49. words := strings.Fields(line)
  50. getPhaseNameAndTimingAndPercentage := func([]string) (string, int64, int32) {
  51. // Sample lines include:
  52. // Total launch phase time 0.011 s 2.59%
  53. // Total target pattern evaluation phase time 0.011 s 2.59%
  54. var beginning int
  55. var end int
  56. for ind, word := range words {
  57. if word == "Total" {
  58. beginning = ind + 1
  59. } else if beginning > 0 && word == "phase" {
  60. end = ind
  61. break
  62. }
  63. }
  64. phaseName := strings.Join(words[beginning:end], " ")
  65. // end is now "phase" - advance by 2 for timing and 4 for percentage
  66. percentageString := words[end+4]
  67. timingString := words[end+2]
  68. timing := parseTimingToNanos(timingString)
  69. percentagePortion := parsePercentageToTenThousandths(percentageString)
  70. return phaseName, timing, percentagePortion
  71. }
  72. phaseName, timing, portion := getPhaseNameAndTimingAndPercentage(words)
  73. phaseTiming := bazel_metrics_proto.PhaseTiming{}
  74. phaseTiming.DurationNanos = &timing
  75. phaseTiming.PortionOfBuildTime = &portion
  76. phaseTiming.PhaseName = &phaseName
  77. return phaseTiming
  78. }
  79. // This method takes a file created by bazel's --analyze-profile mode and
  80. // writes bazel metrics data to the provided filepath.
  81. func ProcessBazelMetrics(bazelProfileFile string, bazelMetricsFile string, ctx Context, config Config) {
  82. if bazelProfileFile == "" {
  83. return
  84. }
  85. readBazelProto := func(filepath string) bazel_metrics_proto.BazelMetrics {
  86. //serialize the proto, write it
  87. bazelMetrics := bazel_metrics_proto.BazelMetrics{}
  88. file, err := os.ReadFile(filepath)
  89. if err != nil {
  90. ctx.Fatalln("Error reading metrics file\n", err)
  91. }
  92. scanner := bufio.NewScanner(strings.NewReader(string(file)))
  93. scanner.Split(bufio.ScanLines)
  94. var phaseTimings []*bazel_metrics_proto.PhaseTiming
  95. for scanner.Scan() {
  96. line := scanner.Text()
  97. if strings.HasPrefix(line, "Total run time") {
  98. total := parseTotal(line)
  99. bazelMetrics.Total = &total
  100. } else if strings.HasPrefix(line, "Total") {
  101. phaseTiming := parsePhaseTiming(line)
  102. phaseTimings = append(phaseTimings, &phaseTiming)
  103. }
  104. }
  105. bazelMetrics.PhaseTimings = phaseTimings
  106. bazelMetrics.BesId = proto.String(config.besId)
  107. return bazelMetrics
  108. }
  109. if _, err := os.Stat(bazelProfileFile); err != nil {
  110. // We can assume bazel didn't run if the profile doesn't exist
  111. return
  112. }
  113. bazelProto := readBazelProto(bazelProfileFile)
  114. bazelProto.ExitCode = proto.Int32(config.bazelExitCode)
  115. shared.Save(&bazelProto, bazelMetricsFile)
  116. }