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; |
ulfjack | df00416 | 2018-11-13 07:13:42 -0800 | [diff] [blame] | 17 | import com.google.common.eventbus.AllowConcurrentEvents; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.eventbus.EventBus; |
| 19 | import com.google.common.eventbus.Subscribe; |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 20 | import com.google.common.flogger.GoogleLogger; |
Tobias Werth | c018051 | 2020-09-08 08:03:03 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionCompletionEvent; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ActionResultReceivedEvent; |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.buildtool.BuildRequest; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent; |
| 26 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent; |
Tobias Werth | 522099d | 2019-08-13 07:29:04 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.buildtool.buildevent.ProfilerStartedEvent; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 28 | import com.google.devtools.build.lib.clock.BlazeClock; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | import com.google.devtools.build.lib.events.Event; |
| 30 | import com.google.devtools.build.lib.events.Reporter; |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
| 32 | import com.google.devtools.build.lib.exec.ExecutorBuilder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import com.google.devtools.build.lib.profiler.Profiler; |
| 34 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
ulfjack | 4cf2ebd | 2018-06-11 06:00:36 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
janakr | ac58ca8 | 2019-02-14 09:34:20 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.skyframe.ExecutionFinishedEvent; |
Tobias Werth | 522099d | 2019-08-13 07:29:04 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.Path; |
| 38 | import java.io.IOException; |
ulfjack | 54c8fcb | 2018-11-19 05:36:37 -0800 | [diff] [blame] | 39 | import java.nio.charset.StandardCharsets; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Blaze module for the build summary message that reports various stats to the user. |
| 45 | */ |
| 46 | public class BuildSummaryStatsModule extends BlazeModule { |
| 47 | |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 48 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 50 | private ActionKeyContext actionKeyContext; |
ulfjack | 3b0d72a | 2018-09-20 06:17:16 -0700 | [diff] [blame] | 51 | private CriticalPathComputer criticalPathComputer; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | private EventBus eventBus; |
| 53 | private Reporter reporter; |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 54 | private boolean enabled; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 56 | private boolean statsSummary; |
| 57 | private long commandStartMillis; |
| 58 | private long executionStartMillis; |
| 59 | private long executionEndMillis; |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 60 | private SpawnStats spawnStats; |
Tobias Werth | 522099d | 2019-08-13 07:29:04 -0700 | [diff] [blame] | 61 | private Path profilePath; |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 62 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | @Override |
ulfjack | a6a9910 | 2017-06-13 17:15:45 +0200 | [diff] [blame] | 64 | public void beforeCommand(CommandEnvironment env) { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 65 | this.reporter = env.getReporter(); |
| 66 | this.eventBus = env.getEventBus(); |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 67 | this.actionKeyContext = env.getSkyframeExecutor().getActionKeyContext(); |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 68 | commandStartMillis = env.getCommandStartTime(); |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 69 | this.spawnStats = new SpawnStats(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | eventBus.register(this); |
| 71 | } |
| 72 | |
Ulf Adams | 10efbf2 | 2016-04-18 11:35:25 +0000 | [diff] [blame] | 73 | @Override |
| 74 | public void afterCommand() { |
| 75 | this.criticalPathComputer = null; |
| 76 | this.eventBus = null; |
| 77 | this.reporter = null; |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 78 | this.spawnStats = null; |
Ulf Adams | 10efbf2 | 2016-04-18 11:35:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 81 | @Override |
| 82 | public void executorInit(CommandEnvironment env, BuildRequest request, ExecutorBuilder builder) { |
| 83 | enabled = env.getOptions().getOptions(ExecutionOptions.class).enableCriticalPathProfiling; |
Tobias Werth | b398119 | 2020-06-05 06:13:30 -0700 | [diff] [blame] | 84 | statsSummary = env.getOptions().getOptions(ExecutionOptions.class).statsSummary; |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 87 | @Subscribe |
| 88 | public void executionPhaseStarting(ExecutionStartingEvent event) { |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 89 | // TODO(ulfjack): Make sure to use the same clock as for commandStartMillis. |
| 90 | executionStartMillis = BlazeClock.instance().currentTimeMillis(); |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 91 | if (enabled) { |
janakr | 16274e4 | 2018-11-13 13:04:14 -0800 | [diff] [blame] | 92 | criticalPathComputer = new CriticalPathComputer(actionKeyContext, BlazeClock.instance()); |
janakr | 1fb90fd | 2017-03-30 21:07:04 +0000 | [diff] [blame] | 93 | eventBus.register(criticalPathComputer); |
| 94 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Subscribe |
Tobias Werth | 522099d | 2019-08-13 07:29:04 -0700 | [diff] [blame] | 98 | public void profileStarting(ProfilerStartedEvent event) { |
| 99 | this.profilePath = event.getProfilePath(); |
| 100 | } |
| 101 | |
| 102 | @Subscribe |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 103 | public void executionPhaseFinish(@SuppressWarnings("unused") ExecutionFinishedEvent event) { |
| 104 | executionEndMillis = BlazeClock.instance().currentTimeMillis(); |
| 105 | } |
| 106 | |
| 107 | @Subscribe |
ulfjack | df00416 | 2018-11-13 07:13:42 -0800 | [diff] [blame] | 108 | @AllowConcurrentEvents |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 109 | public void actionResultReceived(ActionResultReceivedEvent event) { |
| 110 | spawnStats.countActionResult(event.getActionResult()); |
| 111 | } |
| 112 | |
| 113 | @Subscribe |
Tobias Werth | c018051 | 2020-09-08 08:03:03 -0700 | [diff] [blame] | 114 | @AllowConcurrentEvents |
| 115 | public void actionCompletion(ActionCompletionEvent event) { |
| 116 | spawnStats.incrementActionCount(); |
| 117 | } |
| 118 | |
| 119 | @Subscribe |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | public void buildComplete(BuildCompleteEvent event) { |
| 121 | try { |
| 122 | // We might want to make this conditional on a flag; it can sometimes be a bit of a nuisance. |
| 123 | List<String> items = new ArrayList<>(); |
| 124 | items.add(String.format("Elapsed time: %.3fs", event.getResult().getElapsedSeconds())); |
ulfjack | 54c8fcb | 2018-11-19 05:36:37 -0800 | [diff] [blame] | 125 | event.getResult().getBuildToolLogCollection() |
| 126 | .addDirectValue( |
| 127 | "elapsed time", |
| 128 | String.format( |
| 129 | "%f", event.getResult().getElapsedSeconds()).getBytes(StandardCharsets.UTF_8)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 130 | |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 131 | AggregatedCriticalPath criticalPath = AggregatedCriticalPath.EMPTY; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 132 | if (criticalPathComputer != null) { |
ulfjack | 4cf2ebd | 2018-06-11 06:00:36 -0700 | [diff] [blame] | 133 | try (SilentCloseable c = |
| 134 | Profiler.instance().profile(ProfilerTask.CRITICAL_PATH, "Critical path")) { |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 135 | criticalPath = criticalPathComputer.aggregate(); |
Googler | bed7aba | 2018-10-18 14:19:11 -0700 | [diff] [blame] | 136 | items.add(criticalPath.toStringSummaryNoRemote()); |
ulfjack | 54c8fcb | 2018-11-19 05:36:37 -0800 | [diff] [blame] | 137 | event.getResult().getBuildToolLogCollection() |
| 138 | .addDirectValue( |
| 139 | "critical path", criticalPath.toString().getBytes(StandardCharsets.UTF_8)); |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 140 | logger.atInfo().log(criticalPath.toString()); |
| 141 | logger.atInfo().log( |
| 142 | "Slowest actions:\n %s", |
| 143 | Joiner.on("\n ").join(criticalPathComputer.getSlowestComponents())); |
ulfjack | 4cf2ebd | 2018-06-11 06:00:36 -0700 | [diff] [blame] | 144 | // We reverse the critical path because the profiler expect events ordered by the time |
| 145 | // when the actions were executed while critical path computation is stored in the reverse |
| 146 | // way. |
ulfjack | 8b7aad4 | 2018-09-19 04:05:59 -0700 | [diff] [blame] | 147 | for (CriticalPathComponent stat : criticalPath.components().reverse()) { |
ulfjack | 4cf2ebd | 2018-06-11 06:00:36 -0700 | [diff] [blame] | 148 | Profiler.instance() |
| 149 | .logSimpleTaskDuration( |
ulfjack | 1af71e5 | 2018-07-24 00:31:37 -0700 | [diff] [blame] | 150 | stat.getStartTimeNanos(), |
| 151 | stat.getElapsedTime(), |
ulfjack | 4cf2ebd | 2018-06-11 06:00:36 -0700 | [diff] [blame] | 152 | ProfilerTask.CRITICAL_PATH_COMPONENT, |
| 153 | stat.prettyPrintAction()); |
| 154 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | } |
Tobias Werth | 522099d | 2019-08-13 07:29:04 -0700 | [diff] [blame] | 157 | if (profilePath != null) { |
| 158 | // This leads to missing the afterCommand profiles of the other modules in the profile. |
| 159 | // Since the BEP currently shuts down at the BuildCompleteEvent, we cannot just move posting |
| 160 | // the BuildToolLogs to afterCommand of this module. |
| 161 | try { |
| 162 | Profiler.instance().stop(); |
| 163 | event |
| 164 | .getResult() |
| 165 | .getBuildToolLogCollection() |
| 166 | .addLocalFile(profilePath.getBaseName(), profilePath); |
| 167 | } catch (IOException e) { |
| 168 | reporter.handle(Event.error("Error while writing profile file: " + e.getMessage())); |
| 169 | } |
| 170 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 171 | |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 172 | String spawnSummary = spawnStats.getSummary(); |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 173 | if (statsSummary) { |
Googler | 8bbc058 | 2018-11-23 06:37:41 -0800 | [diff] [blame] | 174 | reporter.handle(Event.info(spawnSummary)); |
ulfjack | 97a3ee4 | 2018-11-20 05:40:35 -0800 | [diff] [blame] | 175 | reporter.handle( |
| 176 | Event.info( |
| 177 | String.format( |
| 178 | "Total action wall time %.2fs", spawnStats.getTotalWallTimeMillis() / 1000.0))); |
| 179 | if (criticalPath != AggregatedCriticalPath.EMPTY) { |
| 180 | reporter.handle(Event.info(criticalPath.getNewStringSummary())); |
| 181 | } |
| 182 | long now = event.getResult().getStopTime(); |
| 183 | long executionTime = executionEndMillis - executionStartMillis; |
| 184 | long overheadTime = now - commandStartMillis - executionTime; |
| 185 | reporter.handle( |
| 186 | Event.info( |
| 187 | String.format( |
| 188 | "Elapsed time %.2fs (preparation %.2fs, execution %.2fs)", |
| 189 | (now - commandStartMillis) / 1000.0, |
| 190 | overheadTime / 1000.0, |
| 191 | executionTime / 1000.0))); |
| 192 | } else { |
| 193 | reporter.handle(Event.info(Joiner.on(", ").join(items))); |
| 194 | reporter.handle(Event.info(spawnSummary)); |
| 195 | } |
Googler | a8023b7 | 2018-04-03 08:08:18 -0700 | [diff] [blame] | 196 | |
ulfjack | 54c8fcb | 2018-11-19 05:36:37 -0800 | [diff] [blame] | 197 | event.getResult().getBuildToolLogCollection() |
| 198 | .addDirectValue("process stats", spawnSummary.getBytes(StandardCharsets.UTF_8)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | } finally { |
Tobias Werth | 9db9872 | 2019-08-13 08:55:13 -0700 | [diff] [blame] | 200 | if (criticalPathComputer != null) { |
| 201 | eventBus.unregister(criticalPathComputer); |
| 202 | criticalPathComputer = null; |
| 203 | } |
Tobias Werth | 522099d | 2019-08-13 07:29:04 -0700 | [diff] [blame] | 204 | profilePath = null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |