1// Copyright 2023 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 status
16
17import (
18	"time"
19
20	"android/soong/ui/logger"
21)
22
23// Create a new CriticalPathLogger. if criticalPath is nil, it creates a new criticalPath,
24// if not, it uses that.(its purpose is using a critical path outside logger)
25func NewCriticalPathLogger(log logger.Logger, criticalPath *CriticalPath) StatusOutput {
26	if criticalPath == nil {
27		criticalPath = NewCriticalPath()
28	}
29	return &criticalPathLogger{
30		log:          log,
31		criticalPath: criticalPath,
32	}
33}
34
35type criticalPathLogger struct {
36	log          logger.Logger
37	criticalPath *CriticalPath
38}
39
40func (cp *criticalPathLogger) StartAction(action *Action, counts Counts) {
41	cp.criticalPath.StartAction(action)
42}
43
44func (cp *criticalPathLogger) FinishAction(result ActionResult, counts Counts) {
45	cp.criticalPath.FinishAction(result.Action)
46}
47
48func (cp *criticalPathLogger) Flush() {
49	criticalPath, elapsedTime, criticalTime := cp.criticalPath.criticalPath()
50
51	if len(criticalPath) > 0 {
52		cp.log.Verbosef("critical path took %s", criticalTime.String())
53		if !cp.criticalPath.start.IsZero() {
54			cp.log.Verbosef("elapsed time %s", elapsedTime.String())
55			if elapsedTime > 0 {
56				cp.log.Verbosef("perfect parallelism ratio %d%%",
57					int(float64(criticalTime)/float64(elapsedTime)*100))
58			}
59		}
60		cp.log.Verbose("critical path:")
61		for i := len(criticalPath) - 1; i >= 0; i-- {
62			duration := criticalPath[i].duration
63			duration = duration.Round(time.Second)
64			seconds := int(duration.Seconds())
65			cp.log.Verbosef("   %2d:%02d %s",
66				seconds/60, seconds%60, criticalPath[i].action.Description)
67		}
68	}
69}
70
71func (cp *criticalPathLogger) Message(level MsgLevel, msg string) {}
72
73func (cp *criticalPathLogger) Write(p []byte) (n int, err error) { return len(p), nil }
74