Skip to content

Commit

Permalink
Improve handling of counter data in the profile.
Browse files Browse the repository at this point in the history
This will not only bypass the normal taskstack and stats creation but is also a
necessary step while preparing the code to allow for stacked charts.

PiperOrigin-RevId: 490904637
Change-Id: I7910457082de12cec24364bea75ef3ea29a5ee59
  • Loading branch information
meisterT authored and copybara-github committed Nov 25, 2022
1 parent 4bec12b commit 1af61b2
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/main/java/com/google/devtools/build/lib/profiler/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ Iterable<SlowTask> getSlowestTasks() {
}
}

// TODO(twerth): Make use of counterValue directly in a follow-up change.
private static final class CounterData extends TaskData {
public CounterData(long timeNanos, ProfilerTask type, double counterValue) {
super(/* id= */ -1, timeNanos, type, String.valueOf(counterValue));
}
}

private Clock clock;
private ImmutableSet<ProfilerTask> profiledTasks;
private volatile long profileStartTime;
Expand All @@ -278,7 +285,7 @@ Iterable<SlowTask> getSlowestTasks() {
private CollectLocalResourceUsage resourceUsageThread;

private TimeSeries actionCountTimeSeries;
private long actionCountStartTime;
private Duration actionCountStartTime;
private boolean collectTaskHistograms;

private Profiler() {
Expand Down Expand Up @@ -401,9 +408,8 @@ public synchronized void start(

this.profiledTasks = profiledTasks;
this.clock = clock;
this.actionCountStartTime = clock.nanoTime();
this.actionCountTimeSeries =
new TimeSeries(Duration.ofNanos(actionCountStartTime), ACTION_COUNT_BUCKET_DURATION);
this.actionCountStartTime = Duration.ofNanos(clock.nanoTime());
this.actionCountTimeSeries = new TimeSeries(actionCountStartTime, ACTION_COUNT_BUCKET_DURATION);
this.collectTaskHistograms = collectTaskHistograms;

// Check for current limitation on the number of supported types due to using enum.ordinal() to
Expand Down Expand Up @@ -479,19 +485,15 @@ public synchronized Iterable<SlowTask> getSlowestTasks() {

private void collectActionCounts() {
if (actionCountTimeSeries != null) {
long endTimeMillis = Duration.ofNanos(clock.nanoTime()).toMillis();
long profileStartMillis = Duration.ofNanos(actionCountStartTime).toMillis();
int len =
(int) ((endTimeMillis - profileStartMillis) / ACTION_COUNT_BUCKET_DURATION.toMillis())
+ 1;
Duration endTime = Duration.ofNanos(clock.nanoTime());
Duration profileStart = actionCountStartTime;
int len = (int) endTime.minus(profileStart).dividedBy(ACTION_COUNT_BUCKET_DURATION) + 1;
double[] actionCountValues = actionCountTimeSeries.toDoubleArray(len);
Profiler profiler = Profiler.instance();
for (int i = 0; i < len; i++) {
long timeMillis = profileStartMillis + i * ACTION_COUNT_BUCKET_DURATION.toMillis();
long timeNanos = TimeUnit.MILLISECONDS.toNanos(timeMillis);
profiler.logEventAtTime(
timeNanos, ProfilerTask.ACTION_COUNTS, String.valueOf(actionCountValues[i]));
}
instance.logCounters(
ProfilerTask.ACTION_COUNTS,
actionCountValues,
profileStart,
ACTION_COUNT_BUCKET_DURATION);
actionCountTimeSeries = null;
}
}
Expand Down Expand Up @@ -553,6 +555,19 @@ private boolean wasTaskSlowEnoughToRecord(ProfilerTask type, long duration) {
return (recordAllDurations || duration >= type.minDuration);
}

/** Adds a whole action count series to the writer bypassing histogram and subtask creation. */
public void logCounters(
ProfilerTask type, double[] counterValues, Duration profileStart, Duration bucketDuration) {
FileWriter currentWriter = writerRef.get();
if (isActive() && isProfiling(type) && currentWriter != null) {
for (int i = 0; i < counterValues.length; i++) {
long timeNanos = profileStart.plus(bucketDuration.multipliedBy(i)).toNanos();
TaskData data = new CounterData(timeNanos, type, counterValues[i]);
currentWriter.enqueue(data);
}
}
}

/**
* Adds task directly to the main queue bypassing task stack. Used for simple tasks that are known
* to not have any subtasks.
Expand Down

0 comments on commit 1af61b2

Please sign in to comment.