Skip to content

Commit 29f7c86

Browse files
AlessandroPatticopybara-github
authored andcommitted
Add configuration hash to trace profile
Similar to the target label and mnemonic, allow storing the configuration hash in the action trace data Closes bazelbuild#23236. PiperOrigin-RevId: 671909501 Change-Id: I04cb9d13592592155a751c62182196b0e243cdc5
1 parent 95a9145 commit 29f7c86

File tree

7 files changed

+156
-22
lines changed

7 files changed

+156
-22
lines changed

src/main/java/com/google/devtools/build/lib/profiler/Profiler.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ public void writeTraceData(JsonWriter jsonWriter, long profileStartTimeNanos)
197197
// Primary outputs are non-mergeable, thus incompatible with slim profiles.
198198
jsonWriter.name("out").value(actionTaskData.primaryOutputPath);
199199
}
200-
if (actionTaskData.targetLabel != null || actionTaskData.mnemonic != null) {
200+
if (actionTaskData.targetLabel != null
201+
|| actionTaskData.mnemonic != null
202+
|| actionTaskData.configuration != null) {
201203
jsonWriter.name("args");
202204
jsonWriter.beginObject();
203205
if (actionTaskData.targetLabel != null) {
@@ -206,6 +208,9 @@ public void writeTraceData(JsonWriter jsonWriter, long profileStartTimeNanos)
206208
if (actionTaskData.mnemonic != null) {
207209
jsonWriter.name("mnemonic").value(actionTaskData.mnemonic);
208210
}
211+
if (actionTaskData.configuration != null) {
212+
jsonWriter.name("configuration").value(actionTaskData.configuration);
213+
}
209214
jsonWriter.endObject();
210215
}
211216
}
@@ -234,6 +239,7 @@ static final class ActionTaskData extends TaskData {
234239
@Nullable final String primaryOutputPath;
235240
@Nullable final String targetLabel;
236241
@Nullable final String mnemonic;
242+
@Nullable final String configuration;
237243

238244
ActionTaskData(
239245
long threadId,
@@ -243,11 +249,13 @@ static final class ActionTaskData extends TaskData {
243249
@Nullable String mnemonic,
244250
String description,
245251
@Nullable String primaryOutputPath,
246-
@Nullable String targetLabel) {
252+
@Nullable String targetLabel,
253+
@Nullable String configuration) {
247254
super(threadId, startTimeNanos, durationNanos, eventType, description);
248255
this.primaryOutputPath = primaryOutputPath;
249256
this.targetLabel = targetLabel;
250257
this.mnemonic = mnemonic;
258+
this.configuration = configuration;
251259
}
252260
}
253261

@@ -337,6 +345,7 @@ ImmutableList<SlowTask> getSlowestTasks() {
337345
private boolean collectTaskHistograms;
338346
private boolean includePrimaryOutput;
339347
private boolean includeTargetLabel;
348+
private boolean includeConfiguration;
340349

341350
private Profiler() {
342351
actionCountTimeSeriesRef = new AtomicReference<>();
@@ -444,6 +453,7 @@ public synchronized void start(
444453
boolean slimProfile,
445454
boolean includePrimaryOutput,
446455
boolean includeTargetLabel,
456+
boolean includeConfiguration,
447457
boolean collectTaskHistograms,
448458
LocalResourceCollector localResourceCollector)
449459
throws IOException {
@@ -463,6 +473,7 @@ public synchronized void start(
463473
this.collectTaskHistograms = collectTaskHistograms;
464474
this.includePrimaryOutput = includePrimaryOutput;
465475
this.includeTargetLabel = includeTargetLabel;
476+
this.includeConfiguration = includeConfiguration;
466477
this.recordAllDurations = recordAllDurations;
467478

468479
JsonTraceFileWriter writer = null;
@@ -811,7 +822,8 @@ public SilentCloseable profileAction(
811822
String mnemonic,
812823
String description,
813824
String primaryOutput,
814-
String targetLabel) {
825+
String targetLabel,
826+
String configuration) {
815827
checkNotNull(description);
816828
if (isActive() && isProfiling(type)) {
817829
final long startTimeNanos = clock.nanoTime();
@@ -825,7 +837,8 @@ public SilentCloseable profileAction(
825837
description,
826838
mnemonic,
827839
includePrimaryOutput ? primaryOutput : null,
828-
includeTargetLabel ? targetLabel : null);
840+
includeTargetLabel ? targetLabel : null,
841+
includeConfiguration ? configuration : null);
829842
} finally {
830843
releaseLane(lane);
831844
}
@@ -835,11 +848,6 @@ public SilentCloseable profileAction(
835848
}
836849
}
837850

838-
public SilentCloseable profileAction(
839-
ProfilerTask type, String description, String primaryOutput, String targetLabel) {
840-
return profileAction(type, /* mnemonic= */ null, description, primaryOutput, targetLabel);
841-
}
842-
843851
private static final SilentCloseable NOP = () -> {};
844852

845853
private boolean countAction(ProfilerTask type) {
@@ -874,7 +882,8 @@ private void completeAction(
874882
String description,
875883
String mnemonic,
876884
@Nullable String primaryOutput,
877-
@Nullable String targetLabel) {
885+
@Nullable String targetLabel,
886+
@Nullable String configuration) {
878887
if (isActive()) {
879888
long endTimeNanos = clock.nanoTime();
880889
long duration = endTimeNanos - startTimeNanos;
@@ -889,7 +898,8 @@ private void completeAction(
889898
mnemonic,
890899
description,
891900
primaryOutput,
892-
targetLabel));
901+
targetLabel,
902+
configuration));
893903
}
894904
}
895905
}

src/main/java/com/google/devtools/build/lib/profiler/TraceEvent.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public static TraceEvent create(
4444
@Nullable ImmutableMap<String, Object> args,
4545
@Nullable String primaryOutputPath,
4646
@Nullable String targetLabel,
47-
@Nullable String mnemonic) {
47+
@Nullable String mnemonic,
48+
@Nullable String configuration) {
4849
return new AutoValue_TraceEvent(
4950
category,
5051
name,
@@ -56,7 +57,8 @@ public static TraceEvent create(
5657
args,
5758
primaryOutputPath,
5859
targetLabel,
59-
mnemonic);
60+
mnemonic,
61+
configuration);
6062
}
6163

6264
@Nullable
@@ -90,6 +92,9 @@ public static TraceEvent create(
9092
@Nullable
9193
public abstract String mnemonic();
9294

95+
@Nullable
96+
public abstract String configuration();
97+
9398
private static TraceEvent createFromJsonReader(JsonReader reader) throws IOException {
9499
String category = null;
95100
String name = null;
@@ -101,6 +106,7 @@ private static TraceEvent createFromJsonReader(JsonReader reader) throws IOExcep
101106
String targetLabel = null;
102107
String mnemonic = null;
103108
String type = null;
109+
String configuration = null;
104110
ImmutableMap<String, Object> args = null;
105111

106112
reader.beginObject();
@@ -122,6 +128,8 @@ private static TraceEvent createFromJsonReader(JsonReader reader) throws IOExcep
122128
targetLabel = target instanceof String ? (String) target : null;
123129
Object mnemonicValue = args.get("mnemonic");
124130
mnemonic = mnemonicValue instanceof String ? (String) mnemonicValue : null;
131+
Object configurationValue = args.get("configuration");
132+
configuration = configurationValue instanceof String ? (String) configurationValue : null;
125133
}
126134
default -> reader.skipValue();
127135
}
@@ -138,7 +146,8 @@ private static TraceEvent createFromJsonReader(JsonReader reader) throws IOExcep
138146
args,
139147
primaryOutputPath,
140148
targetLabel,
141-
mnemonic);
149+
mnemonic,
150+
configuration);
142151
}
143152

144153
private static ImmutableMap<String, Object> parseMap(JsonReader reader) throws IOException {

src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ ProfilerStartedEvent initProfiler(
419419
commandOptions.slimProfile,
420420
commandOptions.includePrimaryOutput,
421421
commandOptions.profileIncludeTargetLabel,
422+
commandOptions.profileIncludeTargetConfiguration,
422423
commandOptions.alwaysProfileSlowOperations,
423424
new CollectLocalResourceUsage(
424425
bugReporter,

src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ public String getTypeDescription() {
257257
help = "Includes target label in action events' JSON profile data.")
258258
public boolean profileIncludeTargetLabel;
259259

260+
@Option(
261+
name = "experimental_profile_include_target_configuration",
262+
defaultValue = "false",
263+
documentationCategory = OptionDocumentationCategory.LOGGING,
264+
effectTags = {OptionEffectTag.BAZEL_MONITORING},
265+
help = "Includes target configuration hash in action events' JSON profile data.")
266+
public boolean profileIncludeTargetConfiguration;
267+
260268
@Option(
261269
name = "profile",
262270
defaultValue = "null",

src/main/java/com/google/devtools/build/lib/skyframe/SkyframeActionExecutor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,8 @@ public ActionStepOrResult run(Environment env)
10201020
action.getMnemonic(),
10211021
action.describe(),
10221022
action.getPrimaryOutput().getExecPathString(),
1023-
getOwnerLabelAsString(action))) {
1023+
getOwnerLabelAsString(action),
1024+
getOwnerConfigurationAsString(action))) {
10241025
String message = action.getProgressMessage();
10251026
if (message != null) {
10261027
reporter.startTask(null, prependExecPhaseStats(message));
@@ -1089,6 +1090,14 @@ private String getOwnerLabelAsString(Action action) {
10891090
return ownerLabel.getCanonicalForm();
10901091
}
10911092

1093+
private String getOwnerConfigurationAsString(Action action) {
1094+
ActionOwner owner = action.getOwner();
1095+
if (owner == null) {
1096+
return "";
1097+
}
1098+
return owner.getConfigurationChecksum();
1099+
}
1100+
10921101
private void notifyActionCompletion(
10931102
ExtendedEventHandler eventHandler, boolean postActionCompletionEvent) {
10941103
if (statusReporter != null) {

src/test/java/com/google/devtools/build/lib/buildtool/util/BlazeRuntimeWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ private void beforeCommand() throws Exception {
413413
/* slimProfile= */ false,
414414
/* includePrimaryOutput= */ false,
415415
/* includeTargetLabel= */ false,
416+
/* includeConfiguration= */ false,
416417
/* collectTaskHistograms= */ true,
417418
new CollectLocalResourceUsage(
418419
runtime.getBugReporter(),

0 commit comments

Comments
 (0)