Skip to content

Commit

Permalink
Ignore --starlark_cpu_profile on Windows.
Browse files Browse the repository at this point in the history
Closes #14196.

PiperOrigin-RevId: 407843471
  • Loading branch information
andrewkatson authored and copybara-github committed Nov 5, 2021
1 parent a5c6b65 commit 2255ce4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ private BlazeCommandResult execExclusively(
storedEventHandler.post(profilerStartedEvent);

// Enable Starlark CPU profiling (--starlark_cpu_profile=/tmp/foo.pprof.gz)
boolean success = false;
if (!commonOptions.starlarkCpuProfile.isEmpty()) {
FileOutputStream out;
try {
Expand All @@ -362,7 +363,7 @@ private BlazeCommandResult execExclusively(
message, FailureDetails.Command.Code.STARLARK_CPU_PROFILE_FILE_INITIALIZATION_FAILURE);
}
try {
Starlark.startCpuProfile(out, Duration.ofMillis(10));
success = Starlark.startCpuProfile(out, Duration.ofMillis(10));
} catch (IllegalStateException ex) { // e.g. SIGPROF in use
String message = Strings.nullToEmpty(ex.getMessage());
outErr.printErrLn(message);
Expand Down Expand Up @@ -591,7 +592,7 @@ private BlazeCommandResult execExclusively(
}

// Finalize the Starlark CPU profile.
if (!commonOptions.starlarkCpuProfile.isEmpty()) {
if (!commonOptions.starlarkCpuProfile.isEmpty() && success) {
try {
Starlark.stopCpuProfile();
} catch (IOException ex) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/starlark/java/eval/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ java_library(
"//src/main/java/net/starlark/java/spelling",
"//src/main/java/net/starlark/java/syntax",
"//third_party:error_prone_annotations",
"//third_party:flogger",
"//third_party:guava",
"//third_party:jsr305",
],
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/net/starlark/java/eval/CpuProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.collect.ImmutableList;
import com.google.common.flogger.GoogleLogger;
import java.io.ByteArrayOutputStream;
import java.io.FileDescriptor;
import java.io.FileInputStream;
Expand Down Expand Up @@ -93,6 +94,8 @@ final class CpuProfiler {
JNI.load();
}

private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();

private final PprofWriter pprof;

private CpuProfiler(OutputStream out, Duration period) {
Expand Down Expand Up @@ -126,9 +129,10 @@ static StarlarkThread setStarlarkThread(StarlarkThread thread) {
}

/** Start the profiler. */
static void start(OutputStream out, Duration period) {
static boolean start(OutputStream out, Duration period) {
if (!supported()) {
throw new UnsupportedOperationException("this platform does not support Starlark profiling");
logger.atWarning().log("--starlark_cpu_profile is unsupported on this platform");
return false;
}
if (instance != null) {
throw new IllegalStateException("profiler started twice without intervening stop");
Expand All @@ -140,6 +144,7 @@ static void start(OutputStream out, Duration period) {
}

instance = new CpuProfiler(out, period);
return true;
}

/** Stop the profiler and wait for the log to be written. */
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/starlark/java/eval/Starlark.java
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,8 @@ private static StarlarkFunction newExprFunction(
* @throws IllegalStateException exception if the Starlark profiler is already running or if the
* operating system's profiling resources for this process are already in use.
*/
public static void startCpuProfile(OutputStream out, Duration period) {
CpuProfiler.start(out, period);
public static boolean startCpuProfile(OutputStream out, Duration period) {
return CpuProfiler.start(out, period);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/net/starlark/java/eval/CpuProfilerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public static void main(String[] args) throws Exception {
// Start writing profile to temporary file.
File profile = java.io.File.createTempFile("pprof", ".gz", null);
OutputStream prof = new FileOutputStream(profile);
Starlark.startCpuProfile(prof, Duration.ofMillis(10));
boolean success = Starlark.startCpuProfile(prof, Duration.ofMillis(10));

if (!success) {
System.err.println("Failed to start cpu profiler");
System.exit(1);
}

// This program consumes about 3s of CPU.
ParserInput input =
Expand Down

0 comments on commit 2255ce4

Please sign in to comment.