Skip to content

Commit

Permalink
Do not throw exception on systems not supported by async-profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
jbachorik committed Oct 20, 2022
1 parent 81c992a commit 980d281
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
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

0 comments on commit 980d281

Please sign in to comment.