Skip to content

Commit

Permalink
Add command profile to the BEP under a fixed name
Browse files Browse the repository at this point in the history
If users specify a custom profile location with `--profile`, the file is added to the BES under its basename. This makes it harder for BES consumers to pick out the profile. Instead, always announce it under its default name `command.profile.gz` or `command.profile` if uncompressed. This additionally allows consumers to detect compression based on just the file name.

Closes #22345.

PiperOrigin-RevId: 633485833
Change-Id: I07fff9032d7dda34654e7a1acf9586f58c06a35b
  • Loading branch information
fmeum authored and Copybara-Service committed May 14, 2024
1 parent a448f1c commit bc16c57
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploader.UploadContext;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.profiler.Profiler;
import com.google.devtools.build.lib.vfs.Path;
import javax.annotation.Nullable;

Expand All @@ -24,11 +25,14 @@
public class ProfilerStartedEvent implements ExtendedEventHandler.Postable {
@Nullable private final Path profilePath;
@Nullable private final UploadContext streamingContext;
private final Profiler.Format format;
@Nullable private final String name;

public ProfilerStartedEvent(String name, Path profilePath, UploadContext streamingContext) {
public ProfilerStartedEvent(
String name, Path profilePath, Profiler.Format format, UploadContext streamingContext) {
this.profilePath = profilePath;
this.streamingContext = streamingContext;
this.format = format;
this.name = name;
}

Expand All @@ -40,6 +44,10 @@ public UploadContext getStreamingContext() {
return streamingContext;
}

public Profiler.Format getFormat() {
return format;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ ProfilerStartedEvent initProfiler(
} catch (IOException e) {
eventHandler.handle(Event.error("Error while creating profile file: " + e.getMessage()));
}
return new ProfilerStartedEvent(profileName, profilePath, streamingContext);
return new ProfilerStartedEvent(profileName, profilePath, format, streamingContext);
}

public FileSystem getFileSystem() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class BuildSummaryStatsModule extends BlazeModule {
private long executionEndMillis;
private SpawnStats spawnStats;
private Path profilePath;
private Profiler.Format profileFormat;
private AtomicBoolean executionStarted;

@Override
Expand Down Expand Up @@ -123,6 +124,7 @@ private void markExecutionPhaseStarted() {
@Subscribe
public void profileStarting(ProfilerStartedEvent event) {
this.profilePath = event.getProfilePath();
this.profileFormat = event.getFormat();
}

@Subscribe
Expand Down Expand Up @@ -189,7 +191,12 @@ public void buildComplete(BuildCompleteEvent event) {
event
.getResult()
.getBuildToolLogCollection()
.addLocalFile(profilePath.getBaseName(), profilePath);
.addLocalFile(
switch (profileFormat) {
case JSON_TRACE_FILE_FORMAT -> "command.profile.json";
case JSON_TRACE_FILE_COMPRESSED_FORMAT -> "command.profile.gz";
},
profilePath);
} catch (IOException e) {
reporter.handle(Event.error("Error while writing profile file: " + e.getMessage()));
}
Expand Down
24 changes: 22 additions & 2 deletions src/test/shell/bazel/remote/remote_build_event_uploader_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ EOF
--build_event_json_file=$BEP_JSON \
//a:foo >& $TEST_log || fail "Failed to build"

expect_bes_file_uploaded "mycommand.profile.gz"
expect_bes_file_uploaded "command.profile.gz"
}

function test_upload_all_upload_profile() {
Expand All @@ -560,7 +560,27 @@ EOF
--build_event_json_file=$BEP_JSON \
//a:foo >& $TEST_log || fail "Failed to build"

expect_bes_file_uploaded "mycommand.profile.gz"
expect_bes_file_uploaded "command.profile.gz"
}

function test_upload_upload_uncompressed_profile() {
mkdir -p a
cat > a/BUILD <<EOF
genrule(
name = 'foo',
outs = ["foo.txt"],
cmd = "echo \"foo bar\" > \$@",
)
EOF

bazel build \
--remote_executor=grpc://localhost:${worker_port} \
--remote_build_event_upload=all \
--profile=mycommand.profile \
--build_event_json_file=$BEP_JSON \
//a:foo >& $TEST_log || fail "Failed to build"

expect_bes_file_uploaded "command.profile.json"
}

run_suite "Remote build event uploader tests"

0 comments on commit bc16c57

Please sign in to comment.