critical_path_logger.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 status
  15. import (
  16. "time"
  17. "android/soong/ui/logger"
  18. )
  19. // Create a new CriticalPathLogger. if criticalPath is nil, it creates a new criticalPath,
  20. // if not, it uses that.(its purpose is using a critical path outside logger)
  21. func NewCriticalPathLogger(log logger.Logger, criticalPath *CriticalPath) StatusOutput {
  22. if criticalPath == nil {
  23. criticalPath = NewCriticalPath()
  24. }
  25. return &criticalPathLogger{
  26. log: log,
  27. criticalPath: criticalPath,
  28. }
  29. }
  30. type criticalPathLogger struct {
  31. log logger.Logger
  32. criticalPath *CriticalPath
  33. }
  34. func (cp *criticalPathLogger) StartAction(action *Action, counts Counts) {
  35. cp.criticalPath.StartAction(action)
  36. }
  37. func (cp *criticalPathLogger) FinishAction(result ActionResult, counts Counts) {
  38. cp.criticalPath.FinishAction(result.Action)
  39. }
  40. func (cp *criticalPathLogger) Flush() {
  41. criticalPath, elapsedTime, criticalTime := cp.criticalPath.criticalPath()
  42. if len(criticalPath) > 0 {
  43. cp.log.Verbosef("critical path took %s", criticalTime.String())
  44. if !cp.criticalPath.start.IsZero() {
  45. cp.log.Verbosef("elapsed time %s", elapsedTime.String())
  46. if elapsedTime > 0 {
  47. cp.log.Verbosef("perfect parallelism ratio %d%%",
  48. int(float64(criticalTime)/float64(elapsedTime)*100))
  49. }
  50. }
  51. cp.log.Verbose("critical path:")
  52. for i := len(criticalPath) - 1; i >= 0; i-- {
  53. duration := criticalPath[i].duration
  54. duration = duration.Round(time.Second)
  55. seconds := int(duration.Seconds())
  56. cp.log.Verbosef(" %2d:%02d %s",
  57. seconds/60, seconds%60, criticalPath[i].action.Description)
  58. }
  59. }
  60. }
  61. func (cp *criticalPathLogger) Message(level MsgLevel, msg string) {}
  62. func (cp *criticalPathLogger) Write(p []byte) (n int, err error) { return len(p), nil }