blob: 7ff98ef78615459926335df9496a9af321ddadfa [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"
23 "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
43}
44
45// BeginTrace starts a new Duration Event.
Nan Zhang17f27672018-12-12 16:01:49 -080046func (c ContextImpl) BeginTrace(name, desc string) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070047 if c.Tracer != nil {
Nan Zhang17f27672018-12-12 16:01:49 -080048 c.Tracer.Begin(desc, c.Thread)
49 }
50 if c.Metrics != nil {
51 c.Metrics.TimeTracer.Begin(name, desc, c.Thread)
Dan Willemsend9f6fa22016-08-21 15:17:17 -070052 }
53}
54
55// EndTrace finishes the last Duration Event.
56func (c ContextImpl) EndTrace() {
57 if c.Tracer != nil {
58 c.Tracer.End(c.Thread)
59 }
Nan Zhang17f27672018-12-12 16:01:49 -080060 if c.Metrics != nil {
61 c.Metrics.SetTimeMetrics(c.Metrics.TimeTracer.End(c.Thread))
62 }
Dan Willemsend9f6fa22016-08-21 15:17:17 -070063}
64
65// CompleteTrace writes a trace with a beginning and end times.
Nan Zhang17f27672018-12-12 16:01:49 -080066func (c ContextImpl) CompleteTrace(name, desc string, begin, end uint64) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070067 if c.Tracer != nil {
Nan Zhang17f27672018-12-12 16:01:49 -080068 c.Tracer.Complete(desc, c.Thread, begin, end)
69 }
70 if c.Metrics != nil {
71 realTime := end - begin
72 c.Metrics.SetTimeMetrics(
73 metrics_proto.PerfInfo{
74 Desc: &desc,
75 Name: &name,
76 StartTime: &begin,
77 RealTime: &realTime})
Dan Willemsend9f6fa22016-08-21 15:17:17 -070078 }
79}