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; |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.buildtool.BuildRequest; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent; |
| 21 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent; |
| 22 | import com.google.devtools.build.lib.events.Event; |
| 23 | import com.google.devtools.build.lib.events.Reporter; |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
| 25 | import com.google.devtools.build.lib.exec.ExecutorBuilder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | import com.google.devtools.build.lib.profiler.Profiler; |
| 27 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
| 28 | import com.google.devtools.build.lib.util.BlazeClock; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import java.util.logging.Logger; |
| 32 | |
| 33 | /** |
| 34 | * Blaze module for the build summary message that reports various stats to the user. |
| 35 | */ |
| 36 | public class BuildSummaryStatsModule extends BlazeModule { |
| 37 | |
| 38 | private static final Logger LOG = Logger.getLogger(BuildSummaryStatsModule.class.getName()); |
| 39 | |
| 40 | private SimpleCriticalPathComputer criticalPathComputer; |
| 41 | private EventBus eventBus; |
| 42 | private Reporter reporter; |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 43 | private boolean enabled; |
janakr | 95d4280 | 2017-04-06 00:33:39 +0000 | [diff] [blame] | 44 | private boolean discardActions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | |
| 46 | @Override |
ulfjack | a6a9910 | 2017-06-13 17:15:45 +0200 | [diff] [blame] | 47 | public void beforeCommand(CommandEnvironment env) { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 48 | this.reporter = env.getReporter(); |
| 49 | this.eventBus = env.getEventBus(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | eventBus.register(this); |
| 51 | } |
| 52 | |
Ulf Adams | 10efbf2 | 2016-04-18 11:35:25 +0000 | [diff] [blame] | 53 | @Override |
| 54 | public void afterCommand() { |
| 55 | this.criticalPathComputer = null; |
| 56 | this.eventBus = null; |
| 57 | this.reporter = null; |
| 58 | } |
| 59 | |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 60 | @Override |
| 61 | public void executorInit(CommandEnvironment env, BuildRequest request, ExecutorBuilder builder) { |
| 62 | enabled = env.getOptions().getOptions(ExecutionOptions.class).enableCriticalPathProfiling; |
janakr | 95d4280 | 2017-04-06 00:33:39 +0000 | [diff] [blame] | 63 | discardActions = !env.getSkyframeExecutor().hasIncrementalState(); |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | @Subscribe |
| 67 | public void executionPhaseStarting(ExecutionStartingEvent event) { |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 68 | if (enabled) { |
janakr | 95d4280 | 2017-04-06 00:33:39 +0000 | [diff] [blame] | 69 | criticalPathComputer = new SimpleCriticalPathComputer(BlazeClock.instance(), discardActions); |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 70 | eventBus.register(criticalPathComputer); |
| 71 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Subscribe |
| 75 | public void buildComplete(BuildCompleteEvent event) { |
| 76 | try { |
| 77 | // We might want to make this conditional on a flag; it can sometimes be a bit of a nuisance. |
| 78 | List<String> items = new ArrayList<>(); |
| 79 | items.add(String.format("Elapsed time: %.3fs", event.getResult().getElapsedSeconds())); |
| 80 | |
| 81 | if (criticalPathComputer != null) { |
| 82 | Profiler.instance().startTask(ProfilerTask.CRITICAL_PATH, "Critical path"); |
| 83 | AggregatedCriticalPath<SimpleCriticalPathComponent> criticalPath = |
| 84 | criticalPathComputer.aggregate(); |
| 85 | items.add(criticalPath.toStringSummary()); |
| 86 | LOG.info(criticalPath.toString()); |
| 87 | LOG.info("Slowest actions:\n " + Joiner.on("\n ") |
| 88 | .join(criticalPathComputer.getSlowestComponents())); |
| 89 | // We reverse the critical path because the profiler expect events ordered by the time |
| 90 | // when the actions were executed while critical path computation is stored in the reverse |
| 91 | // way. |
| 92 | for (SimpleCriticalPathComponent stat : criticalPath.components().reverse()) { |
janakr | 95d4280 | 2017-04-06 00:33:39 +0000 | [diff] [blame] | 93 | Profiler.instance() |
| 94 | .logSimpleTaskDuration( |
| 95 | stat.getStartNanos(), |
| 96 | stat.getElapsedTimeNanos(), |
| 97 | ProfilerTask.CRITICAL_PATH_COMPONENT, |
| 98 | stat.prettyPrintAction()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | } |
| 100 | Profiler.instance().completeTask(ProfilerTask.CRITICAL_PATH); |
| 101 | } |
| 102 | |
| 103 | reporter.handle(Event.info(Joiner.on(", ").join(items))); |
| 104 | } finally { |
| 105 | criticalPathComputer = null; |
| 106 | } |
| 107 | } |
| 108 | } |