From 2255ce4165f936f695111020fa664b259a875c4a Mon Sep 17 00:00:00 2001 From: Andrew Katson Date: Fri, 5 Nov 2021 09:45:00 -0700 Subject: [PATCH] Ignore --starlark_cpu_profile on Windows. Closes #14196. PiperOrigin-RevId: 407843471 --- .../build/lib/runtime/BlazeCommandDispatcher.java | 5 +++-- src/main/java/net/starlark/java/eval/BUILD | 1 + src/main/java/net/starlark/java/eval/CpuProfiler.java | 9 +++++++-- src/main/java/net/starlark/java/eval/Starlark.java | 4 ++-- .../java/net/starlark/java/eval/CpuProfilerTest.java | 7 ++++++- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java index 529bc8fc8dee75..e7a0ce81a1fb4d 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java @@ -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 { @@ -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); @@ -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) { diff --git a/src/main/java/net/starlark/java/eval/BUILD b/src/main/java/net/starlark/java/eval/BUILD index 3ee51dfa23a20d..c3b5897ae4b813 100644 --- a/src/main/java/net/starlark/java/eval/BUILD +++ b/src/main/java/net/starlark/java/eval/BUILD @@ -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", ], diff --git a/src/main/java/net/starlark/java/eval/CpuProfiler.java b/src/main/java/net/starlark/java/eval/CpuProfiler.java index 85f0b24b29e60c..fd2e8bbd1df51e 100644 --- a/src/main/java/net/starlark/java/eval/CpuProfiler.java +++ b/src/main/java/net/starlark/java/eval/CpuProfiler.java @@ -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; @@ -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) { @@ -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"); @@ -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. */ diff --git a/src/main/java/net/starlark/java/eval/Starlark.java b/src/main/java/net/starlark/java/eval/Starlark.java index d84e17c0a9ee3f..c014a11ece578c 100644 --- a/src/main/java/net/starlark/java/eval/Starlark.java +++ b/src/main/java/net/starlark/java/eval/Starlark.java @@ -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); } /** diff --git a/src/test/java/net/starlark/java/eval/CpuProfilerTest.java b/src/test/java/net/starlark/java/eval/CpuProfilerTest.java index 5ce9d9adbcf4b9..d36e537bba4cf0 100644 --- a/src/test/java/net/starlark/java/eval/CpuProfilerTest.java +++ b/src/test/java/net/starlark/java/eval/CpuProfilerTest.java @@ -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 =