Java API for in-process profiling. Serves as a wrapper around async-profiler native library. This class is a singleton. The first call to #getInstance() initiates loading of libasyncProfiler.so.
| 17 | * libasyncProfiler.so. |
| 18 | */ |
| 19 | public class AsyncProfiler implements AsyncProfilerMXBean { |
| 20 | private static AsyncProfiler instance; |
| 21 | |
| 22 | private AsyncProfiler() { |
| 23 | } |
| 24 | |
| 25 | public static AsyncProfiler getInstance() { |
| 26 | return getInstance(null); |
| 27 | } |
| 28 | |
| 29 | public static synchronized AsyncProfiler getInstance(String libPath) { |
| 30 | if (instance != null) { |
| 31 | return instance; |
| 32 | } |
| 33 | |
| 34 | AsyncProfiler profiler = new AsyncProfiler(); |
| 35 | if (libPath != null) { |
| 36 | System.load(libPath); |
| 37 | } else { |
| 38 | try { |
| 39 | // No need to load library, if it has been preloaded with -agentpath |
| 40 | profiler.getVersion(); |
| 41 | } catch (UnsatisfiedLinkError e) { |
| 42 | String libraryPath = System.getProperty("one.profiler.libraryPath"); |
| 43 | if (libraryPath != null && !libraryPath.isEmpty()) { |
| 44 | System.load(new File(libraryPath).getAbsolutePath()); |
| 45 | } else { |
| 46 | File file = extractEmbeddedLib(); |
| 47 | if (file != null) { |
| 48 | try { |
| 49 | System.load(file.getAbsolutePath()); |
| 50 | } finally { |
| 51 | file.delete(); |
| 52 | } |
| 53 | } else { |
| 54 | System.loadLibrary("asyncProfiler"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | instance = profiler; |
| 62 | return profiler; |
| 63 | } |
| 64 | |
| 65 | private static File extractEmbeddedLib() { |
| 66 | String resourceName = "/" + getPlatformTag() + "/libasyncProfiler.so"; |
| 67 | InputStream in = AsyncProfiler.class.getResourceAsStream(resourceName); |
| 68 | if (in == null) { |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | String extractPath = System.getProperty("one.profiler.extractPath"); |
| 74 | File file = File.createTempFile("libasyncProfiler-", ".so", |
| 75 | extractPath == null || extractPath.isEmpty() ? null : new File(extractPath)); |
| 76 | try (FileOutputStream out = new FileOutputStream(file)) { |
nothing calls this directly
no outgoing calls
no test coverage detected