Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not throw exception on systems not supported by async-profiler #4061

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,27 @@ public final class AsyncProfiler {
public static final String TYPE = "async";

private static final class Singleton {
private static final AsyncProfiler INSTANCE;
private static final AsyncProfiler INSTANCE = newInstance();
}

static {
try {
INSTANCE = new AsyncProfiler();
} catch (Throwable t) {
throw new RuntimeException(t);
}
static AsyncProfiler newInstance() {
AsyncProfiler instance = null;
try {
instance = new AsyncProfiler();
} catch (Throwable t) {
instance = new AsyncProfiler((Void) null);
}
return instance;
}

static AsyncProfiler newInstance(ConfigProvider configProvider) {
AsyncProfiler instance = null;
try {
instance = new AsyncProfiler(configProvider);
} catch (Throwable t) {
instance = new AsyncProfiler((Void) null);
}
return instance;
}

private final long memleakIntervalDefault;
Expand All @@ -51,7 +63,13 @@ private AsyncProfiler() throws UnsupportedEnvironmentException {
this(ConfigProvider.getInstance());
}

AsyncProfiler(ConfigProvider configProvider) throws UnsupportedEnvironmentException {
private AsyncProfiler(Void dummy) {
this.configProvider = null;
this.asyncProfiler = null;
this.memleakIntervalDefault = 0L;
}

private AsyncProfiler(ConfigProvider configProvider) throws UnsupportedEnvironmentException {
this.configProvider = configProvider;
String libDir = configProvider.getString(ProfilingConfig.PROFILING_ASYNC_LIBPATH);
if (libDir != null && Files.exists(Paths.get(libDir))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ class AsyncProfilerRecordingTest {

@BeforeEach
void setup() throws Exception {
profiler = new AsyncProfiler(ConfigProvider.getInstance());
profiler = AsyncProfiler.newInstance(ConfigProvider.getInstance());
log.info(
"Async Profiler: available={}, active={}", profiler.isAvailable(), profiler.isActive());
Assume.assumeTrue(profiler.isAvailable());
Assume.assumeFalse(profiler.isActive());
if (profiler.isAvailable()) {
Assume.assumeFalse(profiler.isActive());

recording = (AsyncProfilerRecording) profiler.start();
Assume.assumeTrue(recording != null);
recording = (AsyncProfilerRecording) profiler.start();
Assume.assumeTrue(recording != null);
} else {
// async profiler not available
}
}

@AfterEach
Expand All @@ -43,20 +46,32 @@ void shutdown() throws Exception {

@Test
void testClose() throws Exception {
if (!profiler.isAvailable()) {
log.warn("Async Profiler not available. Skipping test.");
return;
}
assertTrue(Files.exists(recording.getRecordingFile()));
recording.close();
assertFalse(Files.exists(recording.getRecordingFile()));
}

@Test
void testStop() throws Exception {
if (!profiler.isAvailable()) {
log.warn("Async Profiler not available. Skipping test.");
return;
}
RecordingData data = recording.stop();
assertNotNull(data);
assertTrue(Files.exists(recording.getRecordingFile()));
}

@Test
void testSnapshot() throws Exception {
if (!profiler.isAvailable()) {
log.warn("Async Profiler not available. Skipping test.");
return;
}
RecordingData data = recording.snapshot(Instant.now());
assertNotNull(data);
assertTrue(Files.exists(recording.getRecordingFile()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AsyncProfilerTest {

@Test
void test() throws Exception {
AsyncProfiler profiler = new AsyncProfiler(ConfigProvider.getInstance());
AsyncProfiler profiler = AsyncProfiler.newInstance(ConfigProvider.getInstance());
if (!profiler.isAvailable()) {
log.warn("Async Profiler not available. Skipping test.");
return;
Expand Down Expand Up @@ -69,7 +69,12 @@ void testStartCmd(boolean cpu, boolean wall, boolean alloc, boolean memleak) thr
props.put(ProfilingConfig.PROFILING_ASYNC_ALLOC_ENABLED, Boolean.toString(alloc));
props.put(ProfilingConfig.PROFILING_ASYNC_MEMLEAK_ENABLED, Boolean.toString(memleak));

AsyncProfiler profiler = new AsyncProfiler(ConfigProvider.withPropertiesOverride(props));
AsyncProfiler profiler =
AsyncProfiler.newInstance(ConfigProvider.withPropertiesOverride(props));
if (!profiler.isAvailable()) {
log.warn("Async Profiler not available. Skipping test.");
return;
}

Path targetFile = Paths.get("/tmp/target.jfr");
String cmd = profiler.cmdStartProfiling(targetFile);
Expand Down