< prev index next >

test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/libHeapMonitorTest.c

Print this page
rev 52132 : 8201655: Add thread-enabled support for the Heap Sampling
Summary:
Reviewed-by:

*** 699,801 **** #define MAX_THREADS 500 typedef struct ThreadStats { int number_threads; int counts[MAX_THREADS]; ! int not_helper_counts[MAX_THREADS]; ! int index[MAX_THREADS]; ! jthread threads[MAX_THREADS]; ! ! int method_resolution_problem; } ThreadStats; static ThreadStats thread_stats; ! static void add_thread_count(jthread thread, int lock, int helper) { int i; jvmtiThreadInfo info; - const char* name; - char* end; - int idx; int err; ! if (lock) { ! event_storage_lock(&global_event_storage); } for (i = 0; i < thread_stats.number_threads; i++) { ! if (thread_stats.threads[i] == thread) { ! if (helper) { ! thread_stats.counts[i]++; } else { ! thread_stats.not_helper_counts[i]++; ! } ! ! if (lock) { ! event_storage_unlock(&global_event_storage); ! } ! return; } } ! thread_stats.threads[thread_stats.number_threads] = thread; err = (*jvmti)->GetThreadInfo(jvmti, thread, &info); if (err != JVMTI_ERROR_NONE) { ! if (lock) { ! event_storage_unlock(&global_event_storage); ! } ! ! // Just to have it accounted as an error... ! info.name = "Allocator99"; } ! if (!strstr(info.name, "Allocator")) { ! if (lock) { event_storage_unlock(&global_event_storage); } - - // Just to have it accounted as an error... - info.name = "Allocator98"; } ! name = info.name + 9; ! end = NULL; ! idx = strtol(name, &end, 0); ! ! if (*end == '\0') { ! if (helper) { thread_stats.counts[thread_stats.number_threads]++; - } else { - thread_stats.not_helper_counts[thread_stats.number_threads]++; - } - - thread_stats.index[thread_stats.number_threads] = idx; thread_stats.number_threads++; - } else { - fprintf(stderr, "Problem with thread name...: %p %s\n", thread, name); - } - - if (PRINT_OUT) { - fprintf(stderr, "Added %s - %p - %d - lock: %d\n", info.name, thread, idx, lock); - } - - if (lock) { event_storage_unlock(&global_event_storage); ! } } static void print_thread_stats() { int i; event_storage_lock(&global_event_storage); - fprintf(stderr, "Method resolution problem: %d\n", thread_stats.method_resolution_problem); fprintf(stderr, "Thread count:\n"); for (i = 0; i < thread_stats.number_threads; i++) { ! fprintf(stderr, "\t%p: %d: %d - %d\n", thread_stats.threads[i], ! thread_stats.index[i], ! thread_stats.counts[i], ! thread_stats.not_helper_counts[i]); } event_storage_unlock(&global_event_storage); } JNIEXPORT --- 699,783 ---- #define MAX_THREADS 500 typedef struct ThreadStats { int number_threads; int counts[MAX_THREADS]; ! char* threads[MAX_THREADS]; } ThreadStats; static ThreadStats thread_stats; ! JNIEXPORT jboolean JNICALL ! Java_MyPackage_HeapMonitorThreadDisabledTest_checkThreadSamplesOnlyFrom( ! JNIEnv* env, jclass cls, jthread thread) { int i; jvmtiThreadInfo info; int err; + char* expected_name; + int found_thread = FALSE; ! err = (*jvmti)->GetThreadInfo(jvmti, thread, &info); ! expected_name = info.name; ! ! if (err != JVMTI_ERROR_NONE) { ! fprintf(stderr, "Failed to get thread information\n"); ! return FALSE; } for (i = 0; i < thread_stats.number_threads; i++) { ! if (strcmp(expected_name, thread_stats.threads[i])) { ! return FALSE; } else { ! found_thread = TRUE; } } + return found_thread; + } ! static void add_thread_count(jthread thread) { ! int i; ! jvmtiThreadInfo info; ! const char* name; ! char* end; ! int idx; ! int err; err = (*jvmti)->GetThreadInfo(jvmti, thread, &info); if (err != JVMTI_ERROR_NONE) { ! return; } ! event_storage_lock(&global_event_storage); ! for (i = 0; i < thread_stats.number_threads; i++) { ! if (!strcmp(thread_stats.threads[i], info.name)) { ! thread_stats.counts[i]++; event_storage_unlock(&global_event_storage); + return; } } ! thread_stats.threads[thread_stats.number_threads] = info.name; thread_stats.counts[thread_stats.number_threads]++; thread_stats.number_threads++; event_storage_unlock(&global_event_storage); ! } ! ! JNIEXPORT void JNICALL ! Java_MyPackage_HeapMonitorThreadDisabledTest_enableSamplingEvents( ! JNIEnv* env, jclass cls, jthread thread) { ! fprintf(stderr, "Enabling for %p\n", thread); ! check_error((*jvmti)->SetEventNotificationMode( ! jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, thread), ! "Set event notifications for a single thread"); } static void print_thread_stats() { int i; event_storage_lock(&global_event_storage); fprintf(stderr, "Thread count:\n"); for (i = 0; i < thread_stats.number_threads; i++) { ! fprintf(stderr, "\t%s: %d\n", thread_stats.threads[i], thread_stats.counts[i]); } event_storage_unlock(&global_event_storage); } JNIEXPORT
*** 803,813 **** JNIEnv* jni_env, jthread thread, jobject object, jclass object_klass, jlong size) { ! add_thread_count(thread, 1, 1); if (event_storage_get_compaction_required(&global_event_storage)) { event_storage_compact(&global_event_storage, jni_env); } --- 785,795 ---- JNIEnv* jni_env, jthread thread, jobject object, jclass object_klass, jlong size) { ! add_thread_count(thread); if (event_storage_get_compaction_required(&global_event_storage)) { event_storage_compact(&global_event_storage, jni_env); }
*** 841,873 **** return check_error((*jvmti)->SetEventNotificationMode( jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL), "Set event notifications"); } - static int enable_notifications_for_two_threads(jthread first, jthread second) { - if (check_error((*jvmti)->SetEventNotificationMode( - jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL), - "Set event notifications")) { - return 0; - } - - if (check_error((*jvmti)->SetEventNotificationMode( - jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, first), - "Set event notifications")) { - return 0; - } - - // Second thread should fail. - if (check_error((*jvmti)->SetEventNotificationMode( - jvmti, JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, second), - "Set event notifications")) { - return 0; - } - - return 1; - } - static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { jint res; jvmtiEventCallbacks callbacks; jvmtiCapabilities caps; --- 823,832 ----
*** 947,964 **** JNIEXPORT void JNICALL Java_MyPackage_HeapMonitor_enableSamplingEvents(JNIEnv* env, jclass cls) { enable_notifications(); } - JNIEXPORT jboolean JNICALL - Java_MyPackage_HeapMonitor_enableSamplingEventsForTwoThreads(JNIEnv* env, - jclass cls, - jthread first, - jthread second) { - return enable_notifications_for_two_threads(first, second); - } - JNIEXPORT void JNICALL Java_MyPackage_HeapMonitor_disableSamplingEvents(JNIEnv* env, jclass cls) { check_error((*jvmti)->SetEventNotificationMode( jvmti, JVMTI_DISABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL), "Set event notifications"); --- 906,915 ----
*** 1097,1107 **** } ThreadsFound; JNIEXPORT jboolean JNICALL Java_MyPackage_HeapMonitorThreadTest_checkSamples(JNIEnv* env, jclass cls, jint num_threads) { - print_thread_stats(); // Ensure we got stacks from at least num_threads. return thread_stats.number_threads >= num_threads; } --- 1048,1057 ----
< prev index next >