blob: fd20e265cde1a819b41571c6ef39663680f0ffc0 [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
21 "android/soong/ui/logger"
Nan Zhang17f27672018-12-12 16:01:49 -080022 "android/soong/ui/metrics"
Dan Willemsen4591b642021-05-24 14:24:12 -070023 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Dan Willemsenb82471a2018-05-17 16:37:09 -070024 "android/soong/ui/status"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070025 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070026)
27
Dan Willemsenb82471a2018-05-17 16:37:09 -070028// Context combines a context.Context, logger.Logger, and terminal.Writer.
Dan Willemsen1e704462016-08-21 15:17:17 -070029// These all are agnostic of the current build, and may be used for multiple
30// builds, while the Config objects contain per-build information.
Dan Willemsend9f6fa22016-08-21 15:17:17 -070031type Context struct{ *ContextImpl }
Dan Willemsen1e704462016-08-21 15:17:17 -070032type ContextImpl struct {
33 context.Context
34 logger.Logger
35
Nan Zhang17f27672018-12-12 16:01:49 -080036 Metrics *metrics.Metrics
37
Colin Cross097ed2a2019-06-08 21:48:58 -070038 Writer io.Writer
Dan Willemsenb82471a2018-05-17 16:37:09 -070039 Status *status.Status
Dan Willemsend9f6fa22016-08-21 15:17:17 -070040
41 Thread tracer.Thread
42 Tracer tracer.Tracer
Jeongik Cha28c1fe52023-03-07 15:19:44 +090043
44 CriticalPath *status.CriticalPath
Dan Willemsend9f6fa22016-08-21 15:17:17 -070045}
46
47// BeginTrace starts a new Duration Event.
Nan Zhang17f27672018-12-12 16:01:49 -080048func (c ContextImpl) BeginTrace(name, desc string) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070049 if c.Tracer != nil {
Nan Zhang17f27672018-12-12 16:01:49 -080050 c.Tracer.Begin(desc, c.Thread)
51 }
52 if c.Metrics != nil {
Liz Kammerf2a80c62022-10-21 10:42:35 -040053 c.Metrics.EventTracer.Begin(name, desc)
Dan Willemsend9f6fa22016-08-21 15:17:17 -070054 }
55}
56
57// EndTrace finishes the last Duration Event.
58func (c ContextImpl) EndTrace() {
59 if c.Tracer != nil {
60 c.Tracer.End(c.Thread)
61 }
Nan Zhang17f27672018-12-12 16:01:49 -080062 if c.Metrics != nil {
Liz Kammerf2a80c62022-10-21 10:42:35 -040063 c.Metrics.SetTimeMetrics(c.Metrics.EventTracer.End())
Nan Zhang17f27672018-12-12 16:01:49 -080064 }
Dan Willemsend9f6fa22016-08-21 15:17:17 -070065}
66
67// CompleteTrace writes a trace with a beginning and end times.
Nan Zhang17f27672018-12-12 16:01:49 -080068func (c ContextImpl) CompleteTrace(name, desc string, begin, end uint64) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070069 if c.Tracer != nil {
Nan Zhang17f27672018-12-12 16:01:49 -080070 c.Tracer.Complete(desc, c.Thread, begin, end)
71 }
72 if c.Metrics != nil {
73 realTime := end - begin
74 c.Metrics.SetTimeMetrics(
Patrice Arruda0cc5b212019-06-14 15:27:46 -070075 soong_metrics_proto.PerfInfo{
Yu Liu37c3dd32021-09-30 14:46:18 -070076 Description: &desc,
77 Name: &name,
78 StartTime: &begin,
79 RealTime: &realTime})
Dan Willemsend9f6fa22016-08-21 15:17:17 -070080 }
81}