From 522099d4907dd4c1baefbfaba656219b57f41892 Mon Sep 17 00:00:00 2001 From: Tobias Werth Date: Tue, 13 Aug 2019 07:29:04 -0700 Subject: [PATCH] Reference the JSON profile as build tool log in the BEP. RELNOTES: None Closes #9159. PiperOrigin-RevId: 263132427 --- .../lib/runtime/BuildSummaryStatsModule.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java index f90621524a2a1e..f2c55b7aa5b119 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java @@ -22,6 +22,7 @@ import com.google.devtools.build.lib.buildtool.BuildRequest; import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent; import com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent; +import com.google.devtools.build.lib.buildtool.buildevent.ProfilerStartedEvent; import com.google.devtools.build.lib.clock.BlazeClock; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.Reporter; @@ -31,6 +32,8 @@ import com.google.devtools.build.lib.profiler.ProfilerTask; import com.google.devtools.build.lib.profiler.SilentCloseable; import com.google.devtools.build.lib.skyframe.ExecutionFinishedEvent; +import com.google.devtools.build.lib.vfs.Path; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -54,6 +57,7 @@ public class BuildSummaryStatsModule extends BlazeModule { private long executionStartMillis; private long executionEndMillis; private SpawnStats spawnStats; + private Path profilePath; @Override public void beforeCommand(CommandEnvironment env) { @@ -88,6 +92,11 @@ public void executionPhaseStarting(ExecutionStartingEvent event) { } } + @Subscribe + public void profileStarting(ProfilerStartedEvent event) { + this.profilePath = event.getProfilePath(); + } + @Subscribe public void executionPhaseFinish(@SuppressWarnings("unused") ExecutionFinishedEvent event) { executionEndMillis = BlazeClock.instance().currentTimeMillis(); @@ -137,6 +146,20 @@ public void buildComplete(BuildCompleteEvent event) { } } } + if (profilePath != null) { + // This leads to missing the afterCommand profiles of the other modules in the profile. + // Since the BEP currently shuts down at the BuildCompleteEvent, we cannot just move posting + // the BuildToolLogs to afterCommand of this module. + try { + Profiler.instance().stop(); + event + .getResult() + .getBuildToolLogCollection() + .addLocalFile(profilePath.getBaseName(), profilePath); + } catch (IOException e) { + reporter.handle(Event.error("Error while writing profile file: " + e.getMessage())); + } + } String spawnSummary = spawnStats.getSummary(); if (statsSummary) { @@ -167,6 +190,7 @@ public void buildComplete(BuildCompleteEvent event) { .addDirectValue("process stats", spawnSummary.getBytes(StandardCharsets.UTF_8)); } finally { criticalPathComputer = null; + profilePath = null; } } }