forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an experimental flag to capture a CPU profile in JFR format.
Support for additional profile types or output formats may be added in the future. These are just the minimal changes to introduce the dependency on async-profiler and verify that it works. Closes bazelbuild#18029. PiperOrigin-RevId: 524798925 Change-Id: I46c384bee6240d36c732f10e50f0544b9132f321
- Loading branch information
1 parent
0a47a1f
commit 5cbb153
Showing
9 changed files
with
300 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/com/google/devtools/build/lib/profiler/CommandProfilerModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2023 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.profiler; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.flogger.GoogleLogger; | ||
import com.google.devtools.build.lib.events.Event; | ||
import com.google.devtools.build.lib.events.Reporter; | ||
import com.google.devtools.build.lib.runtime.BlazeModule; | ||
import com.google.devtools.build.lib.runtime.Command; | ||
import com.google.devtools.build.lib.runtime.CommandEnvironment; | ||
import com.google.devtools.build.lib.vfs.Path; | ||
import com.google.devtools.common.options.Option; | ||
import com.google.devtools.common.options.OptionDocumentationCategory; | ||
import com.google.devtools.common.options.OptionEffectTag; | ||
import com.google.devtools.common.options.OptionsBase; | ||
import java.time.Duration; | ||
import javax.annotation.Nullable; | ||
import one.profiler.AsyncProfiler; | ||
|
||
/** Bazel module to record pprof-compatible profiles for single invocations. */ | ||
public class CommandProfilerModule extends BlazeModule { | ||
|
||
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); | ||
|
||
private static final Duration PROFILING_INTERVAL = Duration.ofMillis(10); | ||
|
||
@Nullable private static final AsyncProfiler profiler; | ||
|
||
static { | ||
AsyncProfiler profilerInstance = null; | ||
try { | ||
profilerInstance = AsyncProfiler.getInstance(); | ||
} catch (Throwable t) { | ||
// Loading the JNI must be allowed to fail, as we might be running on an unsupported platform. | ||
logger.atWarning().withCause(t).log("Failed to load async_profiler JNI"); | ||
} | ||
profiler = profilerInstance; | ||
} | ||
|
||
/** CommandProfilerModule options. */ | ||
public static final class Options extends OptionsBase { | ||
@Option( | ||
name = "experimental_command_profile", | ||
defaultValue = "false", | ||
documentationCategory = OptionDocumentationCategory.LOGGING, | ||
effectTags = {OptionEffectTag.UNKNOWN}, | ||
help = | ||
"Records a Java Flight Recorder CPU profile into a profile.jfr file in the output base" | ||
+ " directory. The syntax and semantics of this flag might change in the future to" | ||
+ " support different profile types or output formats; use at your own risk.") | ||
public boolean captureCommandProfile; | ||
} | ||
|
||
@Override | ||
public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command) { | ||
return ImmutableList.of(Options.class); | ||
} | ||
|
||
private boolean captureCommandProfile; | ||
@Nullable private Reporter reporter; | ||
@Nullable private Path outputBase; | ||
@Nullable private Path outputPath; | ||
|
||
@Override | ||
public void beforeCommand(CommandEnvironment env) { | ||
Options options = env.getOptions().getOptions(Options.class); | ||
captureCommandProfile = options.captureCommandProfile; | ||
outputBase = env.getBlazeWorkspace().getOutputBase(); | ||
reporter = env.getReporter(); | ||
|
||
if (profiler == null || !captureCommandProfile) { | ||
return; | ||
} | ||
|
||
outputPath = outputBase.getRelative("profile.jfr"); | ||
|
||
try { | ||
profiler.execute(getProfilerCommand(outputPath)); | ||
} catch (Exception e) { | ||
// This may occur if the user has insufficient privileges to capture performance events. | ||
reporter.handle(Event.error("Starting JFR CPU profile failed: " + e)); | ||
captureCommandProfile = false; | ||
} | ||
|
||
if (captureCommandProfile) { | ||
reporter.handle(Event.info("Writing JFR CPU profile to " + outputPath)); | ||
} | ||
} | ||
|
||
@Override | ||
public void afterCommand() { | ||
if (profiler == null || !captureCommandProfile) { | ||
return; | ||
} | ||
|
||
profiler.stop(); | ||
|
||
captureCommandProfile = false; | ||
outputBase = null; | ||
reporter = null; | ||
outputPath = null; | ||
} | ||
|
||
private static String getProfilerCommand(Path outputPath) { | ||
// See https://github.com/async-profiler/async-profiler/blob/master/src/arguments.cpp. | ||
return String.format( | ||
"start,event=cpu,interval=%s,file=%s,jfr", PROFILING_INTERVAL.toNanos(), outputPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/com/google/devtools/build/lib/profiler/CommandProfilerModuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.devtools.build.lib.profiler; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assume.assumeTrue; | ||
|
||
import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase; | ||
import com.google.devtools.build.lib.runtime.BlazeRuntime; | ||
import com.google.devtools.build.lib.util.OS; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for {@link CommandProfilerModule}. */ | ||
@RunWith(JUnit4.class) | ||
public final class CommandProfilerModuleTest extends BuildIntegrationTestCase { | ||
|
||
@Override | ||
protected BlazeRuntime.Builder getRuntimeBuilder() throws Exception { | ||
return super.getRuntimeBuilder().addBlazeModule(new CommandProfilerModule()); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
write("BUILD", ""); | ||
} | ||
|
||
@Test | ||
public void testProfilingDisabled() throws Exception { | ||
buildTarget("//:BUILD"); | ||
assertThat(outputBase.getChild("profile.jfr").exists()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testProfilingEnabled() throws Exception { | ||
addOptions("--experimental_command_profile"); | ||
|
||
try { | ||
buildTarget("//:BUILD"); | ||
} catch (Exception e) { | ||
// Linux perf events are not supported on CI. | ||
// See https://github.com/async-profiler/async-profiler/#troubleshooting. | ||
if (e.getMessage().contains("No access to perf events") | ||
|| e.getMessage().contains("Perf events unavailable")) { | ||
return; | ||
} | ||
} | ||
|
||
assumeTrue(OS.getCurrent() == OS.LINUX || OS.getCurrent() == OS.DARWIN); | ||
|
||
assertThat(outputBase.getChild("profile.jfr").exists()).isTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters