1010#include < limits.h>
1111#include " asprof.h"
1212#include < pthread.h>
13-
14- // detect arch for java 8
15- #if defined(__x86_64__) || defined(_M_X64)
16- # define JAVA8_ARCH_PATH " amd64/"
17- #elif defined(__i386) || defined(_M_IX86)
18- # define JAVA8_ARCH_PATH " i386/"
19- #elif defined(__aarch64__) || defined(_M_ARM64)
20- # define JAVA8_ARCH_PATH " aarch64/"
21- #elif defined(__arm__) || defined(_M_ARM)
22- # define JAVA8_ARCH_PATH " arm/"
23- #else
24- # define JAVA8_ARCH_PATH " /"
25- #endif
13+ #include < time.h>
14+ #include < dirent.h>
15+ #include < string.h>
2616
2717#ifdef __linux__
2818const char profiler_lib_path[] = " build/lib/libasyncProfiler.so" ;
29- const char jvm_lib_path[] = " lib/server/libjvm.so" ;
30- const char jvm8_lib_path[] = " lib/" JAVA8_ARCH_PATH " server/libjvm.so" ;
19+ const char jvm_lib_path[] = " server/libjvm.so" ;
3120#else
3221const char profiler_lib_path[] = " build/lib/libasyncProfiler.dylib" ;
33- const char jvm_lib_path[] = " lib/server/libjvm.dylib" ;
34- const char jvm8_lib_path[] = " lib/server/libjvm.dylib" ;
22+ const char jvm_lib_path[] = " server/libjvm.dylib" ;
3523#endif
3624
3725typedef jint (*CreateJvm)(JavaVM **, void **, void *);
@@ -45,8 +33,6 @@ JNIEnv* _env;
4533
4634void * _jvm_lib;
4735
48- jint java_version = 0 ;
49-
5036void outputCallback (const char * buffer, size_t size) {
5137 fwrite (buffer, sizeof (char ), size, stderr);
5238}
@@ -84,30 +70,49 @@ void stopProfiler(char* outputFile) {
8470}
8571
8672void loadJvmLib () {
73+ char lib_path[PATH_MAX];
74+
8775 // Get Java home
8876 char * java_home = getenv (" TEST_JAVA_HOME" );
8977 if (java_home == NULL ) {
9078 std::cerr << " TEST_JAVA_HOME is not set" << std::endl;
9179 exit (1 );
9280 }
93- // Get Java version
94- char * version = getenv ( " TEST_JAVA_VERSION " );
95- if (version == NULL ) {
96- std::cerr << " TEST_JAVA_VERSION is not set " << std::endl;
97- exit ( 1 ) ;
81+
82+ // check that libjvm is found under the standard path
83+ snprintf (lib_path, sizeof (lib_path), " %s/%s/%s " , java_home, " lib " , jvm_lib_path);
84+ if ((_jvm_lib = dlopen (lib_path, RTLD_LOCAL | RTLD_NOW)) != NULL ) {
85+ return ;
9886 }
99- // Java 8 or higher
100- java_version = std::stoi (version);
101- if (java_version < 8 ) {
102- std::cerr << " Unsupported Java version: " << version << std::endl;
87+
88+ char java_lib_home[PATH_MAX];
89+ struct dirent * entry;
90+ DIR* dir;
91+
92+ // libjvm wasn't found under standard path, this could happen in JDK 8 where the path is formated like:
93+ // ${TEST_JAVA_HOME}/lib/${ARCH}/server/libjvm.(so|dylib)
94+ snprintf (java_lib_home, sizeof (java_lib_home), " %s/lib" , java_home);
95+ dir = opendir (java_lib_home);
96+ if (dir == NULL ) {
97+ std::cerr << " Error opening directory: " << java_lib_home << std::endl;
10398 exit (1 );
10499 }
105100
106- char lib_path[PATH_MAX];
107- snprintf (lib_path, sizeof (lib_path), " %s/%s" , java_home, java_version == 8 ? jvm8_lib_path : jvm_lib_path);
108- _jvm_lib = dlopen (lib_path, RTLD_LOCAL | RTLD_NOW);
101+ while ((entry = readdir (dir)) != NULL ) {
102+ // Skip .. & .
103+ if (strcmp (entry->d_name , " .." ) == 0 || strcmp (entry->d_name , " ." ) == 0 ) {
104+ continue ;
105+ }
106+
107+ snprintf (lib_path, sizeof (lib_path), " %s/%s/%s" , java_lib_home, entry->d_name , jvm_lib_path);
108+ if ((_jvm_lib = dlopen (lib_path, RTLD_LOCAL | RTLD_NOW)) != NULL ) {
109+ break ;
110+ }
111+ }
112+
113+ // libjvm was never found
109114 if (_jvm_lib == NULL ) {
110- std::cerr << " Unable to find: " << lib_path << " , Error: " << dlerror () << std::endl;
115+ std::cerr << " Unable to find: libjvm " << std::endl;
111116 exit (1 );
112117 }
113118}
@@ -317,6 +322,8 @@ The profiler should be able to profile the JVM task
317322The JVM is loaded and started before the profiling session is started so it's attached correctly at the session start
318323*/
319324void testFlow4 (int argc, char ** argv) {
325+ struct timespec wait_time = {(time_t )(2 ), 0L };
326+
320327 validateArgsCount (argc, 3 , " Minimum Arguments is 2" );
321328
322329 loadProfiler ();
@@ -325,12 +332,12 @@ void testFlow4(int argc, char** argv) {
325332 pthread_create (&thread, NULL , jvmThreadWrapper, NULL );
326333
327334 // busy wait for JVM to start on the thread
328- for ( int i = 0 ; i < 3 ; i++) for ( int i = 0 ; i < 1000000000 ; ++i) {}
335+ nanosleep (&wait_time, NULL );
329336
330337 startProfiler ();
331338
332339 // busy wait for profiler to sample JVM
333- for ( int i = 0 ; i < 3 ; i++) for ( int i = 0 ; i < 1000000000 ; ++i) {}
340+ nanosleep (&wait_time, NULL );
334341
335342 stopProfiler (argv[2 ]);
336343}
0 commit comments