Skip to content

Commit 2255ce4

Browse files
andrewkatsoncopybara-github
authored andcommitted
Ignore --starlark_cpu_profile on Windows.
Closes #14196. PiperOrigin-RevId: 407843471
1 parent a5c6b65 commit 2255ce4

5 files changed

Lines changed: 19 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ private BlazeCommandResult execExclusively(
351351
storedEventHandler.post(profilerStartedEvent);
352352

353353
// Enable Starlark CPU profiling (--starlark_cpu_profile=/tmp/foo.pprof.gz)
354+
boolean success = false;
354355
if (!commonOptions.starlarkCpuProfile.isEmpty()) {
355356
FileOutputStream out;
356357
try {
@@ -362,7 +363,7 @@ private BlazeCommandResult execExclusively(
362363
message, FailureDetails.Command.Code.STARLARK_CPU_PROFILE_FILE_INITIALIZATION_FAILURE);
363364
}
364365
try {
365-
Starlark.startCpuProfile(out, Duration.ofMillis(10));
366+
success = Starlark.startCpuProfile(out, Duration.ofMillis(10));
366367
} catch (IllegalStateException ex) { // e.g. SIGPROF in use
367368
String message = Strings.nullToEmpty(ex.getMessage());
368369
outErr.printErrLn(message);
@@ -591,7 +592,7 @@ private BlazeCommandResult execExclusively(
591592
}
592593

593594
// Finalize the Starlark CPU profile.
594-
if (!commonOptions.starlarkCpuProfile.isEmpty()) {
595+
if (!commonOptions.starlarkCpuProfile.isEmpty() && success) {
595596
try {
596597
Starlark.stopCpuProfile();
597598
} catch (IOException ex) {

src/main/java/net/starlark/java/eval/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ java_library(
5757
"//src/main/java/net/starlark/java/spelling",
5858
"//src/main/java/net/starlark/java/syntax",
5959
"//third_party:error_prone_annotations",
60+
"//third_party:flogger",
6061
"//third_party:guava",
6162
"//third_party:jsr305",
6263
],

src/main/java/net/starlark/java/eval/CpuProfiler.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static java.nio.charset.StandardCharsets.UTF_8;
1818

1919
import com.google.common.collect.ImmutableList;
20+
import com.google.common.flogger.GoogleLogger;
2021
import java.io.ByteArrayOutputStream;
2122
import java.io.FileDescriptor;
2223
import java.io.FileInputStream;
@@ -93,6 +94,8 @@ final class CpuProfiler {
9394
JNI.load();
9495
}
9596

97+
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
98+
9699
private final PprofWriter pprof;
97100

98101
private CpuProfiler(OutputStream out, Duration period) {
@@ -126,9 +129,10 @@ static StarlarkThread setStarlarkThread(StarlarkThread thread) {
126129
}
127130

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

142146
instance = new CpuProfiler(out, period);
147+
return true;
143148
}
144149

145150
/** Stop the profiler and wait for the log to be written. */

src/main/java/net/starlark/java/eval/Starlark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,8 @@ private static StarlarkFunction newExprFunction(
959959
* @throws IllegalStateException exception if the Starlark profiler is already running or if the
960960
* operating system's profiling resources for this process are already in use.
961961
*/
962-
public static void startCpuProfile(OutputStream out, Duration period) {
963-
CpuProfiler.start(out, period);
962+
public static boolean startCpuProfile(OutputStream out, Duration period) {
963+
return CpuProfiler.start(out, period);
964964
}
965965

966966
/**

src/test/java/net/starlark/java/eval/CpuProfilerTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ public static void main(String[] args) throws Exception {
4545
// Start writing profile to temporary file.
4646
File profile = java.io.File.createTempFile("pprof", ".gz", null);
4747
OutputStream prof = new FileOutputStream(profile);
48-
Starlark.startCpuProfile(prof, Duration.ofMillis(10));
48+
boolean success = Starlark.startCpuProfile(prof, Duration.ofMillis(10));
49+
50+
if (!success) {
51+
System.err.println("Failed to start cpu profiler");
52+
System.exit(1);
53+
}
4954

5055
// This program consumes about 3s of CPU.
5156
ParserInput input =

0 commit comments

Comments
 (0)