blob: 69e5f96a98366d24ac2d138cbd5cf1a7a06f6104 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// 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
15package build
16
17import (
18 "context"
Colin Cross097ed2a2019-06-08 21:48:58 -070019 "io"
Dan Willemsen1e704462016-08-21 15:17:17 -070020
LaMont Jones8490ffb2024-11-14 16:42:54 -080021 "android/soong/ui/execution_metrics"
Dan Willemsen1e704462016-08-21 15:17:17 -070022 "android/soong/ui/logger"
Nan Zhang17f27672018-12-12 16:01:49 -080023 "android/soong/ui/metrics"
Dan Willemsen4591b642021-05-24 14:24:12 -070024 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Dan Willemsenb82471a2018-05-17 16:37:09 -070025 "android/soong/ui/status"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070026 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070027)
28
Dan Willemsenb82471a2018-05-17 16:37:09 -070029// Context combines a context.Context, logger.Logger, and terminal.Writer.
Dan Willemsen1e704462016-08-21 15:17:17 -070030// These all are agnostic of the current build, and may be used for multiple
31// builds, while the Config objects contain per-build information.
Dan Willemsend9f6fa22016-08-21 15:17:17 -070032type Context struct{ *ContextImpl }
Dan Willemsen1e704462016-08-21 15:17:17 -070033type ContextImpl struct {
34 context.Context
35 logger.Logger
36
LaMont Jones8490ffb2024-11-14 16:42:54 -080037 Metrics *metrics.Metrics
38 ExecutionMetrics *execution_metrics.ExecutionMetrics
Nan Zhang17f27672018-12-12 16:01:49 -080039
Colin Cross097ed2a2019-06-08 21:48:58 -070040 Writer io.Writer
Dan Willemsenb82471a2018-05-17 16:37:09 -070041 Status *status.Status
Dan Willemsend9f6fa22016-08-21 15:17:17 -070042
43 Thread tracer.Thread
44 Tracer tracer.Tracer
Jeongik Cha28c1fe52023-03-07 15:19:44 +090045
46 CriticalPath *status.CriticalPath
Dan Willemsend9f6fa22016-08-21 15:17:17 -070047}
48
49// BeginTrace starts a new Duration Event.
Nan Zhang17f27672018-12-12 16:01:49 -080050func (c ContextImpl) BeginTrace(name, desc string) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070051 if c.Tracer != nil {
Nan Zhang17f27672018-12-12 16:01:49 -080052 c.Tracer.Begin(desc, c.Thread)
53 }
54 if c.Metrics != nil {
Liz Kammerf2a80c62022-10-21 10:42:35 -040055 c.Metrics.EventTracer.Begin(name, desc)
Dan Willemsend9f6fa22016-08-21 15:17:17 -070056 }
57}
58
59// EndTrace finishes the last Duration Event.
60func (c ContextImpl) EndTrace() {
61 if c.Tracer != nil {
62 c.Tracer.End(c.Thread)
63 }
Nan Zhang17f27672018-12-12 16:01:49 -080064 if c.Metrics != nil {
Liz Kammerf2a80c62022-10-21 10:42:35 -040065 c.Metrics.SetTimeMetrics(c.Metrics.EventTracer.End())
Nan Zhang17f27672018-12-12 16:01:49 -080066 }
Dan Willemsend9f6fa22016-08-21 15:17:17 -070067}
68
69// CompleteTrace writes a trace with a beginning and end times.
Nan Zhang17f27672018-12-12 16:01:49 -080070func (c ContextImpl) CompleteTrace(name, desc string, begin, end uint64) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070071 if c.Tracer != nil {
Nan Zhang17f27672018-12-12 16:01:49 -080072 c.Tracer.Complete(desc, c.Thread, begin, end)
73 }
74 if c.Metrics != nil {
75 realTime := end - begin
76 c.Metrics.SetTimeMetrics(
Patrice Arruda0cc5b212019-06-14 15:27:46 -070077 soong_metrics_proto.PerfInfo{
Yu Liu37c3dd32021-09-30 14:46:18 -070078 Description: &desc,
79 Name: &name,
80 StartTime: &begin,
81 RealTime: &realTime})
Dan Willemsend9f6fa22016-08-21 15:17:17 -070082 }
83}