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