Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | package com.google.devtools.build.lib.runtime; |
| 15 | |
| 16 | import com.google.common.base.Joiner; |
| 17 | import com.google.common.eventbus.EventBus; |
| 18 | import com.google.common.eventbus.Subscribe; |
| 19 | import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent; |
| 20 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent; |
| 21 | import com.google.devtools.build.lib.events.Event; |
| 22 | import com.google.devtools.build.lib.events.Reporter; |
| 23 | import com.google.devtools.build.lib.profiler.Profiler; |
| 24 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
| 25 | import com.google.devtools.build.lib.util.BlazeClock; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | import java.util.logging.Logger; |
| 31 | |
| 32 | /** |
| 33 | * Blaze module for the build summary message that reports various stats to the user. |
| 34 | */ |
| 35 | public class BuildSummaryStatsModule extends BlazeModule { |
| 36 | |
| 37 | private static final Logger LOG = Logger.getLogger(BuildSummaryStatsModule.class.getName()); |
| 38 | |
| 39 | private SimpleCriticalPathComputer criticalPathComputer; |
| 40 | private EventBus eventBus; |
| 41 | private Reporter reporter; |
| 42 | |
| 43 | @Override |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 44 | public void beforeCommand(Command command, CommandEnvironment env) { |
| 45 | this.reporter = env.getReporter(); |
| 46 | this.eventBus = env.getEventBus(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | eventBus.register(this); |
| 48 | } |
| 49 | |
Ulf Adams | 10efbf2 | 2016-04-18 11:35:25 +0000 | [diff] [blame] | 50 | @Override |
| 51 | public void afterCommand() { |
| 52 | this.criticalPathComputer = null; |
| 53 | this.eventBus = null; |
| 54 | this.reporter = null; |
| 55 | } |
| 56 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | @Subscribe |
| 58 | public void executionPhaseStarting(ExecutionStartingEvent event) { |
| 59 | criticalPathComputer = new SimpleCriticalPathComputer(BlazeClock.instance()); |
| 60 | eventBus.register(criticalPathComputer); |
| 61 | } |
| 62 | |
| 63 | @Subscribe |
| 64 | public void buildComplete(BuildCompleteEvent event) { |
| 65 | try { |
| 66 | // We might want to make this conditional on a flag; it can sometimes be a bit of a nuisance. |
| 67 | List<String> items = new ArrayList<>(); |
| 68 | items.add(String.format("Elapsed time: %.3fs", event.getResult().getElapsedSeconds())); |
| 69 | |
| 70 | if (criticalPathComputer != null) { |
| 71 | Profiler.instance().startTask(ProfilerTask.CRITICAL_PATH, "Critical path"); |
| 72 | AggregatedCriticalPath<SimpleCriticalPathComponent> criticalPath = |
| 73 | criticalPathComputer.aggregate(); |
| 74 | items.add(criticalPath.toStringSummary()); |
| 75 | LOG.info(criticalPath.toString()); |
| 76 | LOG.info("Slowest actions:\n " + Joiner.on("\n ") |
| 77 | .join(criticalPathComputer.getSlowestComponents())); |
| 78 | // We reverse the critical path because the profiler expect events ordered by the time |
| 79 | // when the actions were executed while critical path computation is stored in the reverse |
| 80 | // way. |
| 81 | for (SimpleCriticalPathComponent stat : criticalPath.components().reverse()) { |
| 82 | Profiler.instance().logSimpleTaskDuration( |
Miguel Alcon Pinto | c5102fc | 2015-02-25 00:10:14 +0000 | [diff] [blame] | 83 | TimeUnit.MILLISECONDS.toNanos(stat.getStartWallTimeMillis(BlazeClock.instance())), |
| 84 | stat.getElapsedTimeNanos(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | ProfilerTask.CRITICAL_PATH_COMPONENT, stat.getAction()); |
| 86 | } |
| 87 | Profiler.instance().completeTask(ProfilerTask.CRITICAL_PATH); |
| 88 | } |
| 89 | |
| 90 | reporter.handle(Event.info(Joiner.on(", ").join(items))); |
| 91 | } finally { |
| 92 | criticalPathComputer = null; |
| 93 | } |
| 94 | } |
| 95 | } |