Skip to content

Commit

Permalink
Create less garbage for starlark profiles.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 608967932
Change-Id: Ieddf6a986ca068dfa3b685986a2f7e6629df1889
  • Loading branch information
meisterT authored and copybara-github committed Feb 21, 2024
1 parent 00c99ab commit d9b0cfd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,19 +563,20 @@ public void end(Object span) {
StarlarkThread.setCallProfiler(
new StarlarkThread.CallProfiler() {
@Override
public Object start(StarlarkCallable fn) {
return Profiler.instance()
.profile(
public long start() {
return Profiler.nanoTimeMaybe();
}

@Override
public void end(long startTimeNanos, StarlarkCallable fn) {
Profiler.instance()
.completeTask(
startTimeNanos,
fn instanceof StarlarkFunction
? ProfilerTask.STARLARK_USER_FN
: ProfilerTask.STARLARK_BUILTIN_FN,
fn.getName());
}

@Override
public void end(Object span) {
((SilentCloseable) span).close();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* @see ProfilerTask enum for recognized task types.
*/
@ThreadSafe
@SuppressWarnings("GoodTime") // This code is very performance sensitive.
public final class Profiler {
/** The profiler (a static singleton instance). Inactive by default. */
private static final Profiler instance = new Profiler();
Expand Down Expand Up @@ -802,14 +803,17 @@ private boolean countAction(ProfilerTask type, TaskData taskData) {
|| (type == ProfilerTask.INFO && "discoverInputs".equals(taskData.description));
}

public void completeTask(long startTimeNanos, ProfilerTask type, String description) {
completeTask(Thread.currentThread().getId(), startTimeNanos, type, description);
}

/** Records the end of the task. */
private void completeTask(
long laneId, long startTimeNanos, ProfilerTask type, String description) {
if (isActive()) {
long endTimeNanos = clock.nanoTime();
long duration = endTimeNanos - startTimeNanos;
boolean shouldRecordTask = wasTaskSlowEnoughToRecord(type, duration);
if (shouldRecordTask) {
if (wasTaskSlowEnoughToRecord(type, duration)) {
recordTask(new TaskData(laneId, startTimeNanos, duration, type, description));
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/net/starlark/java/eval/StarlarkThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static final class Frame implements Debug.Frame {
// values, or wrapped in StarlarkFunction.Cells if shared with a nested function.
@Nullable Object[] locals;

@Nullable private Object profileSpan; // current span of walltime call profiler
private long profileStartTimeNanos; // start time nanos of walltime call profiler

private Frame(StarlarkThread thread, StarlarkCallable fn) {
this.thread = thread;
Expand Down Expand Up @@ -237,7 +237,7 @@ void push(StarlarkCallable fn) {
// Start wall-time call profile span.
CallProfiler callProfiler = StarlarkThread.callProfiler;
if (callProfiler != null) {
fr.profileSpan = callProfiler.start(fn);
fr.profileStartTimeNanos = callProfiler.start();
}

// Poll for newly installed CPU profiler.
Expand Down Expand Up @@ -277,8 +277,8 @@ void pop() {

// End wall-time profile span.
CallProfiler callProfiler = StarlarkThread.callProfiler;
if (callProfiler != null && fr.profileSpan != null) {
callProfiler.end(fr.profileSpan);
if (callProfiler != null && fr.profileStartTimeNanos >= 0) {
callProfiler.end(fr.profileStartTimeNanos, fr.fn);
}

// Notify debug tools of the thread's last pop.
Expand Down Expand Up @@ -570,9 +570,10 @@ public String toString() {

/** CallProfiler records the start and end wall times of function calls. */
public interface CallProfiler {
Object start(StarlarkCallable fn);
long start();

void end(Object span);
@SuppressWarnings("GoodTime") // This code is very performance sensitive.
void end(long startTimeNanos, StarlarkCallable fn);
}

/** Installs a global hook that will be notified of function calls. */
Expand Down

0 comments on commit d9b0cfd

Please sign in to comment.