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