Skip to content

Commit d9c5260

Browse files
committed
Fix AsyncProfiler.isSupported, fixes #5
1 parent 35c327c commit d9c5260

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Loader for AsyncProfiler
44
[![Maven Central](https://img.shields.io/maven-central/v/me.bechberger/ap-loader-all)](https://search.maven.org/search?q=ap-loader) [![GitHub](https://img.shields.io/github/license/jvm-profiling-tools/ap-loader)](https://github.com/jvm-profiling-tools/ap-loader/blob/main/LICENSE)
55

66
Packages [async-profiler](https://github.com/jvm-profiling-tools/async-profiler) releases in a JAR
7-
with an `AsyncProfilerLoader` (version 2+) that loads the suitable native library for the current platform.
7+
with an `AsyncProfilerLoader` (version 2.*) that loads the suitable native library for the current platform.
88

99
This is usable as a java agent (same arguments as the async-profiler agent) and as the basis for other libraries.
1010
The real rationale behind this library is that the async-profiler is a nice tool, but it cannot be easily integrated
@@ -42,7 +42,7 @@ Or you can depend on the artifacts from maven central, they should be slightly m
4242
<dependency>
4343
<groupId>me.bechberger</groupId>
4444
<artifactId>ap-loader-all</artifactId>
45-
<version>2.9-3</version>
45+
<version>2.9-4</version>
4646
</dependency>
4747
```
4848

@@ -195,7 +195,7 @@ The latest `all` version can be added via:
195195
<dependency>
196196
<groupId>me.bechberger</groupId>
197197
<artifactId>ap-loader-all</artifactId>
198-
<version>2.9-3</version>
198+
<version>2.9-4</version>
199199
</dependency>
200200
```
201201
@@ -215,7 +215,7 @@ For example for the `all` variant of version 2.9:
215215
<dependency>
216216
<groupId>me.bechberger</groupId>
217217
<artifactId>ap-loader-all</artifactId>
218-
<version>2.9-3-SNAPSHOT</version>
218+
<version>2.9-4-SNAPSHOT</version>
219219
</dependency>
220220
```
221221
@@ -252,11 +252,11 @@ python3 ./bin/releaser.py download 2.9
252252
# build the JAR for the release
253253
# maven might throw warnings, related to the project version setting,
254254
# but the alternative solutions don't work, so we ignore the warning for now
255-
mvn -Dproject.vversion=2.9 -Dproject.subrelease=3 -Dproject.platform=macos package assembly:single
255+
mvn -Dproject.vversion=2.9 -Dproject.subrelease=4 -Dproject.platform=macos package assembly:single
256256
# use it
257-
java -jar target/ap-loader-macos-2.9-3-full.jar ...
257+
java -jar target/ap-loader-macos-2.9-4-full.jar ...
258258
# build the all JAR
259-
mvn -Dproject.vversion=2.9 -Dproject.subrelease=3 -Dproject.platform=all package assembly:single
259+
mvn -Dproject.vversion=2.9 -Dproject.subrelease=4 -Dproject.platform=all package assembly:single
260260
```
261261
262262
Development
@@ -301,6 +301,10 @@ And the following for a new async-profiler release:
301301
Changelog
302302
---------
303303
304+
### v4
305+
306+
- `AsyncProfiler.isSupported()` now returns `false` if the OS is not supported by any async-profiler binary, fixes #5
307+
304308
### v3
305309
306310
- Create specific artifacts for each platform fixing previous issues with maven version updates (issue #4, thanks @ginkel for reporting it)

bin/releaser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from typing import Any, Dict, List, Union, Tuple, Optional
1818
from urllib import request
1919

20-
SUB_VERSION = 3
21-
RELEASE_NOTES = """- Create specific artifacts for each platform fixing previous issues with maven version updates (issue #4, thanks @ginkel for reporting it)"""
20+
SUB_VERSION = 4
21+
RELEASE_NOTES = """- `AsyncProfiler.isSupported()` now returns `false` if the OS is not supported by any async-profiler binary, fixes #5"""
2222

2323
HELP = """
2424
Usage:

src/main/java/one/profiler/AsyncProfilerLoader.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
*/
4545
public final class AsyncProfilerLoader {
4646

47+
private static class OSNotSupportedException extends Exception {
48+
OSNotSupportedException(String message) {
49+
super(message);
50+
}
51+
}
52+
4753
private static final String EXTRACTION_PROPERTY_NAME = "ap_loader_extraction_dir";
4854
private static String librarySuffix;
4955
private static Path extractedAsyncProfiler;
@@ -130,7 +136,7 @@ public static Path getExtractionDirectory() throws IOException {
130136
/**
131137
* @throws IllegalStateException if OS or Arch not supported
132138
*/
133-
private static String getLibrarySuffix() {
139+
private static String getLibrarySuffix() throws OSNotSupportedException {
134140
if (librarySuffix == null) {
135141
String version = getVersion();
136142
String os = System.getProperty("os.name").toLowerCase();
@@ -145,12 +151,12 @@ private static String getLibrarySuffix() {
145151
librarySuffix = version + "-linux-x64.so";
146152
}
147153
} else {
148-
throw new IllegalStateException("Async-profiler does not work on Linux " + arch);
154+
throw new OSNotSupportedException("Async-profiler does not work on Linux " + arch);
149155
}
150156
} else if (os.startsWith("macosx") || os.startsWith("mac os x")) {
151157
librarySuffix = version + "-macos.so";
152158
} else {
153-
throw new IllegalStateException("Async-profiler does not work on " + os);
159+
throw new OSNotSupportedException("Async-profiler does not work on " + os);
154160
}
155161
}
156162
return librarySuffix;
@@ -210,11 +216,11 @@ private static boolean isOnMusl() {
210216
}
211217
}
212218

213-
private static String getAsyncProfilerFileName() {
219+
private static String getAsyncProfilerFileName() throws OSNotSupportedException {
214220
return "libasyncProfiler-" + getLibrarySuffix();
215221
}
216222

217-
private static String getJattachFileName() {
223+
private static String getJattachFileName() throws OSNotSupportedException {
218224
return "jattach-" + getLibrarySuffix().replace(".so", "");
219225
}
220226

@@ -253,7 +259,11 @@ private static URL getUrl(String fileName) {
253259
* @return true if a library is available, false otherwise
254260
*/
255261
public static boolean isSupported() {
256-
return hasFileInResources(getAsyncProfilerFileName());
262+
try {
263+
return hasFileInResources(getAsyncProfilerFileName());
264+
} catch (OSNotSupportedException e) {
265+
return false;
266+
}
257267
}
258268

259269
/** Copy from resources if needed */
@@ -287,8 +297,12 @@ private static Path copyFromResources(String fileName, Path destination) throws
287297
*/
288298
public static Path getJattachPath() throws IOException {
289299
if (extractedJattach == null) {
290-
Path path =
291-
copyFromResources(getJattachFileName(), getExtractionDirectory().resolve("jattach"));
300+
Path path = null;
301+
try {
302+
path = copyFromResources(getJattachFileName(), getExtractionDirectory().resolve("jattach"));
303+
} catch (OSNotSupportedException e) {
304+
throw new IllegalStateException(e.getMessage());
305+
}
292306
if (!path.toFile().setExecutable(true)) {
293307
throw new IOException("Could not make jattach (" + path + ") executable");
294308
}
@@ -325,9 +339,13 @@ private static Path getProfilerPath() throws IOException {
325339
*/
326340
public static Path getAsyncProfilerPath() throws IOException {
327341
if (extractedAsyncProfiler == null) {
328-
extractedAsyncProfiler =
329-
copyFromResources(
330-
getAsyncProfilerFileName(), getExtractionDirectory().resolve("libasyncProfiler.so"));
342+
try {
343+
extractedAsyncProfiler =
344+
copyFromResources(
345+
getAsyncProfilerFileName(), getExtractionDirectory().resolve("libasyncProfiler.so"));
346+
} catch (OSNotSupportedException e) {
347+
throw new IllegalStateException(e.getMessage());
348+
}
331349
}
332350
return extractedAsyncProfiler;
333351
}

0 commit comments

Comments
 (0)