Skip to content

Commit

Permalink
Add configuration has to trace profile
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroPatti committed Aug 7, 2024
1 parent 2c28477 commit 9e94a62
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 22 deletions.
31 changes: 20 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 @@ -30,6 +30,7 @@
import com.google.devtools.build.lib.profiler.StatRecorder.VfsHeuristics;
import com.google.gson.stream.JsonWriter;
import com.sun.management.OperatingSystemMXBean;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
Expand Down Expand Up @@ -196,7 +197,7 @@ 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 @@ -205,6 +206,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 @@ -233,6 +237,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 @@ -242,11 +247,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 @@ -333,6 +340,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 +452,7 @@ public synchronized void start(
boolean slimProfile,
boolean includePrimaryOutput,
boolean includeTargetLabel,
boolean includeConfiguration,
boolean collectTaskHistograms,
LocalResourceCollector localResourceCollector)
throws IOException {
Expand All @@ -462,6 +471,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 @@ -792,7 +802,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 @@ -806,7 +817,8 @@ public SilentCloseable profileAction(
description,
mnemonic,
includePrimaryOutput ? primaryOutput : null,
includeTargetLabel ? targetLabel : null);
includeTargetLabel ? targetLabel : null,
includeConfiguration ? configuration : null);
} finally {
releaseLane(lane);
}
Expand All @@ -816,11 +828,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 @@ -855,7 +862,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 @@ -870,7 +878,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 @@ -388,6 +388,7 @@ ProfilerStartedEvent initProfiler(
options.slimProfile,
options.includePrimaryOutput,
options.profileIncludeTargetLabel,
options.profileIncludeTargetConfiguration,
options.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 9e94a62

Please sign in to comment.