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	soong_metrics_proto "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	CriticalPath *status.CriticalPath
45}
46
47// BeginTrace starts a new Duration Event.
48func (c ContextImpl) BeginTrace(name, desc string) {
49	if c.Tracer != nil {
50		c.Tracer.Begin(desc, c.Thread)
51	}
52	if c.Metrics != nil {
53		c.Metrics.EventTracer.Begin(name, desc)
54	}
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	}
62	if c.Metrics != nil {
63		c.Metrics.SetTimeMetrics(c.Metrics.EventTracer.End())
64	}
65}
66
67// CompleteTrace writes a trace with a beginning and end times.
68func (c ContextImpl) CompleteTrace(name, desc string, begin, end uint64) {
69	if c.Tracer != nil {
70		c.Tracer.Complete(desc, c.Thread, begin, end)
71	}
72	if c.Metrics != nil {
73		realTime := end - begin
74		c.Metrics.SetTimeMetrics(
75			soong_metrics_proto.PerfInfo{
76				Description: &desc,
77				Name:        &name,
78				StartTime:   &begin,
79				RealTime:    &realTime})
80	}
81}
82