context.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2017 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. import (
  16. "context"
  17. "io"
  18. "android/soong/ui/logger"
  19. "android/soong/ui/metrics"
  20. soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
  21. "android/soong/ui/status"
  22. "android/soong/ui/tracer"
  23. )
  24. // Context combines a context.Context, logger.Logger, and terminal.Writer.
  25. // These all are agnostic of the current build, and may be used for multiple
  26. // builds, while the Config objects contain per-build information.
  27. type Context struct{ *ContextImpl }
  28. type ContextImpl struct {
  29. context.Context
  30. logger.Logger
  31. Metrics *metrics.Metrics
  32. Writer io.Writer
  33. Status *status.Status
  34. Thread tracer.Thread
  35. Tracer tracer.Tracer
  36. CriticalPath *status.CriticalPath
  37. }
  38. // BeginTrace starts a new Duration Event.
  39. func (c ContextImpl) BeginTrace(name, desc string) {
  40. if c.Tracer != nil {
  41. c.Tracer.Begin(desc, c.Thread)
  42. }
  43. if c.Metrics != nil {
  44. c.Metrics.EventTracer.Begin(name, desc)
  45. }
  46. }
  47. // EndTrace finishes the last Duration Event.
  48. func (c ContextImpl) EndTrace() {
  49. if c.Tracer != nil {
  50. c.Tracer.End(c.Thread)
  51. }
  52. if c.Metrics != nil {
  53. c.Metrics.SetTimeMetrics(c.Metrics.EventTracer.End())
  54. }
  55. }
  56. // CompleteTrace writes a trace with a beginning and end times.
  57. func (c ContextImpl) CompleteTrace(name, desc string, begin, end uint64) {
  58. if c.Tracer != nil {
  59. c.Tracer.Complete(desc, c.Thread, begin, end)
  60. }
  61. if c.Metrics != nil {
  62. realTime := end - begin
  63. c.Metrics.SetTimeMetrics(
  64. soong_metrics_proto.PerfInfo{
  65. Description: &desc,
  66. Name: &name,
  67. StartTime: &begin,
  68. RealTime: &realTime})
  69. }
  70. }