Skip to content

Commit

Permalink
Add configuration hash to trace profile
Browse files Browse the repository at this point in the history
Similar to the target label and mnemonic, allow storing the configuration hash in the action trace data

Closes #23236.

PiperOrigin-RevId: 671909501
Change-Id: I04cb9d13592592155a751c62182196b0e243cdc5
  • Loading branch information
AlessandroPatti authored and copybara-github committed Sep 6, 2024
1 parent 95a9145 commit 29f7c86
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 22 deletions.
32 changes: 21 additions & 11 deletions src/main/java/com/google/devtools/build/lib/profiler/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ public void writeTraceData(JsonWriter jsonWriter, long profileStartTimeNanos)
// Primary outputs are non-mergeable, thus incompatible with slim profiles.
jsonWriter.name("out").value(actionTaskData.primaryOutputPath);
}
if (actionTaskData.targetLabel != null || actionTaskData.mnemonic != null) {
if (actionTaskData.targetLabel != null
|| actionTaskData.mnemonic != null
|| actionTaskData.configuration != null) {
jsonWriter.name("args");
jsonWriter.beginObject();
if (actionTaskData.targetLabel != null) {
Expand All @@ -206,6 +208,9 @@ public void writeTraceData(JsonWriter jsonWriter, long profileStartTimeNanos)
if (actionTaskData.mnemonic != null) {
jsonWriter.name("mnemonic").value(actionTaskData.mnemonic);
}
if (actionTaskData.configuration != null) {
jsonWriter.name("configuration").value(actionTaskData.configuration);
}
jsonWriter.endObject();
}
}
Expand Down Expand Up @@ -234,6 +239,7 @@ static final class ActionTaskData extends TaskData {
@Nullable final String primaryOutputPath;
@Nullable final String targetLabel;
@Nullable final String mnemonic;
@Nullable final String configuration;

ActionTaskData(
long threadId,
Expand All @@ -243,11 +249,13 @@ static final class ActionTaskData extends TaskData {
@Nullable String mnemonic,
String description,
@Nullable String primaryOutputPath,
@Nullable String targetLabel) {
@Nullable String targetLabel,
@Nullable String configuration) {
super(threadId, startTimeNanos, durationNanos, eventType, description);
this.primaryOutputPath = primaryOutputPath;
this.targetLabel = targetLabel;
this.mnemonic = mnemonic;
this.configuration = configuration;
}
}

Expand Down Expand Up @@ -337,6 +345,7 @@ ImmutableList<SlowTask> getSlowestTasks() {
private boolean collectTaskHistograms;
private boolean includePrimaryOutput;
private boolean includeTargetLabel;
private boolean includeConfiguration;

private Profiler() {
actionCountTimeSeriesRef = new AtomicReference<>();
Expand Down Expand Up @@ -444,6 +453,7 @@ public synchronized void start(
boolean slimProfile,
boolean includePrimaryOutput,
boolean includeTargetLabel,
boolean includeConfiguration,
boolean collectTaskHistograms,
LocalResourceCollector localResourceCollector)
throws IOException {
Expand All @@ -463,6 +473,7 @@ public synchronized void start(
this.collectTaskHistograms = collectTaskHistograms;
this.includePrimaryOutput = includePrimaryOutput;
this.includeTargetLabel = includeTargetLabel;
this.includeConfiguration = includeConfiguration;
this.recordAllDurations = recordAllDurations;

JsonTraceFileWriter writer = null;
Expand Down Expand Up @@ -811,7 +822,8 @@ public SilentCloseable profileAction(
String mnemonic,
String description,
String primaryOutput,
String targetLabel) {
String targetLabel,
String configuration) {
checkNotNull(description);
if (isActive() && isProfiling(type)) {
final long startTimeNanos = clock.nanoTime();
Expand All @@ -825,7 +837,8 @@ public SilentCloseable profileAction(
description,
mnemonic,
includePrimaryOutput ? primaryOutput : null,
includeTargetLabel ? targetLabel : null);
includeTargetLabel ? targetLabel : null,
includeConfiguration ? configuration : null);
} finally {
releaseLane(lane);
}
Expand All @@ -835,11 +848,6 @@ public SilentCloseable profileAction(
}
}

public SilentCloseable profileAction(
ProfilerTask type, String description, String primaryOutput, String targetLabel) {
return profileAction(type, /* mnemonic= */ null, description, primaryOutput, targetLabel);
}

private static final SilentCloseable NOP = () -> {};

private boolean countAction(ProfilerTask type) {
Expand Down Expand Up @@ -874,7 +882,8 @@ private void completeAction(
String description,
String mnemonic,
@Nullable String primaryOutput,
@Nullable String targetLabel) {
@Nullable String targetLabel,
@Nullable String configuration) {
if (isActive()) {
long endTimeNanos = clock.nanoTime();
long duration = endTimeNanos - startTimeNanos;
Expand All @@ -889,7 +898,8 @@ private void completeAction(
mnemonic,
description,
primaryOutput,
targetLabel));
targetLabel,
configuration));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static TraceEvent create(
@Nullable ImmutableMap<String, Object> args,
@Nullable String primaryOutputPath,
@Nullable String targetLabel,
@Nullable String mnemonic) {
@Nullable String mnemonic,
@Nullable String configuration) {
return new AutoValue_TraceEvent(
category,
name,
Expand All @@ -56,7 +57,8 @@ public static TraceEvent create(
args,
primaryOutputPath,
targetLabel,
mnemonic);
mnemonic,
configuration);
}

@Nullable
Expand Down Expand Up @@ -90,6 +92,9 @@ public static TraceEvent create(
@Nullable
public abstract String mnemonic();

@Nullable
public abstract String configuration();

private static TraceEvent createFromJsonReader(JsonReader reader) throws IOException {
String category = null;
String name = null;
Expand All @@ -101,6 +106,7 @@ private static TraceEvent createFromJsonReader(JsonReader reader) throws IOExcep
String targetLabel = null;
String mnemonic = null;
String type = null;
String configuration = null;
ImmutableMap<String, Object> args = null;

reader.beginObject();
Expand All @@ -122,6 +128,8 @@ private static TraceEvent createFromJsonReader(JsonReader reader) throws IOExcep
targetLabel = target instanceof String ? (String) target : null;
Object mnemonicValue = args.get("mnemonic");
mnemonic = mnemonicValue instanceof String ? (String) mnemonicValue : null;
Object configurationValue = args.get("configuration");
configuration = configurationValue instanceof String ? (String) configurationValue : null;
}
default -> reader.skipValue();
}
Expand All @@ -138,7 +146,8 @@ private static TraceEvent createFromJsonReader(JsonReader reader) throws IOExcep
args,
primaryOutputPath,
targetLabel,
mnemonic);
mnemonic,
configuration);
}

private static ImmutableMap<String, Object> parseMap(JsonReader reader) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ ProfilerStartedEvent initProfiler(
commandOptions.slimProfile,
commandOptions.includePrimaryOutput,
commandOptions.profileIncludeTargetLabel,
commandOptions.profileIncludeTargetConfiguration,
commandOptions.alwaysProfileSlowOperations,
new CollectLocalResourceUsage(
bugReporter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ public String getTypeDescription() {
help = "Includes target label in action events' JSON profile data.")
public boolean profileIncludeTargetLabel;

@Option(
name = "experimental_profile_include_target_configuration",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.LOGGING,
effectTags = {OptionEffectTag.BAZEL_MONITORING},
help = "Includes target configuration hash in action events' JSON profile data.")
public boolean profileIncludeTargetConfiguration;

@Option(
name = "profile",
defaultValue = "null",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,8 @@ public ActionStepOrResult run(Environment env)
action.getMnemonic(),
action.describe(),
action.getPrimaryOutput().getExecPathString(),
getOwnerLabelAsString(action))) {
getOwnerLabelAsString(action),
getOwnerConfigurationAsString(action))) {
String message = action.getProgressMessage();
if (message != null) {
reporter.startTask(null, prependExecPhaseStats(message));
Expand Down Expand Up @@ -1089,6 +1090,14 @@ private String getOwnerLabelAsString(Action action) {
return ownerLabel.getCanonicalForm();
}

private String getOwnerConfigurationAsString(Action action) {
ActionOwner owner = action.getOwner();
if (owner == null) {
return "";
}
return owner.getConfigurationChecksum();
}

private void notifyActionCompletion(
ExtendedEventHandler eventHandler, boolean postActionCompletionEvent) {
if (statusReporter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ private void beforeCommand() throws Exception {
/* slimProfile= */ false,
/* includePrimaryOutput= */ false,
/* includeTargetLabel= */ false,
/* includeConfiguration= */ false,
/* collectTaskHistograms= */ true,
new CollectLocalResourceUsage(
runtime.getBugReporter(),
Expand Down
Loading

0 comments on commit 29f7c86

Please sign in to comment.