< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/events/EM02/em02t012/em02t012.cpp

Print this page
rev 52050 : [mq]: refactor


  35 /* scaffold objects */
  36 static jvmtiEnv *jvmti = NULL;
  37 static jthread testedThread;
  38 static jlong timeout = 0;
  39 static jrawMonitorID syncLock = NULL;
  40 
  41 /* constant names */
  42 #define STEP_NUMBER 3
  43 #define JVMTI_EVENT_COUNT   (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1)
  44 #define NUMBER_OF_INVOCATIONS 1000
  45 
  46 static int eventCount[JVMTI_EVENT_COUNT];
  47 static int newEventCount[JVMTI_EVENT_COUNT];
  48 
  49 /* ============================================================================= */
  50 JNIEXPORT void JNICALL
  51 Java_nsk_jvmti_scenarios_events_EM02_em02t012_setThread(JNIEnv *jni_env,
  52                         jobject o, jthread thrd) {
  53 
  54     /* make thread accessable for a long time */
  55     NSK_JNI_VERIFY(jni_env, (testedThread =
  56             NSK_CPP_STUB2(NewGlobalRef, jni_env, thrd)) != NULL);
  57 }
  58 
  59 static void
  60 showEventStatistics(int step) {
  61     int i;
  62     const char* str;
  63     int *currentCounts = (step == 1) ? &eventCount[0] : &newEventCount[0];
  64 
  65     NSK_DISPLAY0("\n");
  66     NSK_DISPLAY1("Event statistics for %d step:\n", step);
  67     NSK_DISPLAY0("-----------------------------\n");
  68     for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
  69         if (currentCounts[i] > 0) {
  70             str = TranslateEvent((jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL));
  71             NSK_DISPLAY2("%-40s %7d\n", str, currentCounts[i]);
  72         }
  73     }
  74 }
  75 
  76 /* ========================================================================== */


 134                 }
 135             }
 136 
 137         } else {
 138 
 139             if (currentCounts[i] > 0) {
 140                 NSK_COMPLAIN2("Unexpected event %s was sent %d times\n",
 141                                     TranslateEvent(curr),
 142                                     currentCounts[i]);
 143                 result = NSK_FALSE;
 144             }
 145         }
 146     }
 147 
 148     return result;
 149 }
 150 
 151 static void
 152 changeCount(jvmtiEvent event, int *currentCounts) {
 153 
 154     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock)))
 155         nsk_jvmti_setFailStatus();
 156 
 157     currentCounts[event - JVMTI_MIN_EVENT_TYPE_VAL]++;
 158 
 159     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock)))
 160         nsk_jvmti_setFailStatus();
 161 
 162 }
 163 
 164 /* ============================================================================= */
 165 
 166 /* callbacks */
 167 JNIEXPORT void JNICALL
 168 cbVMInit(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread) {
 169     changeCount(JVMTI_EVENT_VM_INIT, &eventCount[0]);
 170 }
 171 
 172 JNIEXPORT void JNICALL
 173 cbVMDeath(jvmtiEnv* jvmti, JNIEnv* jni_env) {
 174     changeCount(JVMTI_EVENT_VM_DEATH, &newEventCount[0]);
 175     showEventStatistics(STEP_NUMBER);
 176     if (!checkEvents(STEP_NUMBER))
 177         nsk_jvmti_setFailStatus();
 178 
 179     if (!NSK_JVMTI_VERIFY(
 180             NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock)))
 181         nsk_jvmti_setFailStatus();
 182 
 183 }
 184 
 185 void JNICALL
 186 cbException(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 187                 jmethodID method, jlocation location, jobject exception,
 188                 jmethodID catch_method, jlocation catch_location) {
 189 
 190     changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]);
 191 }
 192 
 193 void JNICALL
 194 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 195                 jmethodID method, jlocation location, jobject exception) {
 196 
 197     changeCount(JVMTI_EVENT_EXCEPTION_CATCH, &eventCount[0]);
 198 }
 199 
 200 void JNICALL


 314 void JNICALL
 315 cbObjectFree(jvmtiEnv *jvmti_env, jlong tag) {
 316 
 317     changeCount(JVMTI_EVENT_OBJECT_FREE, &eventCount[0]);
 318 }
 319 
 320 void JNICALL
 321 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 322                     jobject object, jclass object_klass, jlong size) {
 323 
 324     changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]);
 325 }
 326 
 327 /* ============================================================================= */
 328 
 329 static int enableEvent(jvmtiEvent event) {
 330 
 331     if (nsk_jvmti_isOptionalEvent(event)
 332             && (event != JVMTI_EVENT_FRAME_POP)) {
 333         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
 334                 NSK_CPP_STUB4(SetEventNotificationMode, jvmti,
 335                     JVMTI_ENABLE, event, NULL))) {
 336             NSK_COMPLAIN1("Unexpected error enabling %s\n",
 337                 TranslateEvent(event));
 338             return NSK_FALSE;
 339         }
 340     } else {
 341         if (!NSK_JVMTI_VERIFY(
 342                 NSK_CPP_STUB4(SetEventNotificationMode, jvmti,
 343                     JVMTI_ENABLE, event, NULL))) {
 344             NSK_COMPLAIN1("Unexpected error enabling %s\n",
 345                 TranslateEvent(event));
 346             return NSK_FALSE;
 347         }
 348     }
 349 
 350     return NSK_TRUE;
 351 }
 352 
 353 /**
 354  * Enable or disable tested events.
 355  */
 356 static int enableEventList() {
 357 
 358     int i, result;
 359 
 360     result = enableEvent(JVMTI_EVENT_VM_INIT);
 361 
 362     result = result && enableEvent(JVMTI_EVENT_VM_DEATH);
 363 


 416             eventCallbacks.VMObjectAlloc             = cbVMObjectAlloc;
 417             break;
 418 
 419         case 2:
 420             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 421                 newEventCount[i] = 0;
 422             }
 423 
 424             eventCallbacks.FramePop                  = cbNewFramePop;
 425             break;
 426 
 427         case 3:
 428             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 429                 newEventCount[i] = 0;
 430             }
 431 
 432             eventCallbacks.VMDeath                   = cbVMDeath;
 433             break;
 434 
 435     }
 436     if (!NSK_JVMTI_VERIFY(
 437             NSK_CPP_STUB3(SetEventCallbacks, jvmti,
 438                                 &eventCallbacks,
 439                                 sizeof(eventCallbacks))))
 440         return NSK_FALSE;
 441 
 442     return NSK_TRUE;
 443 }
 444 
 445 /* ============================================================================= */
 446 
 447 /** Agent algorithm. */
 448 static void JNICALL
 449 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 450 
 451     int i, j;
 452 
 453     for (i = 1; i <= STEP_NUMBER; i++) {
 454 
 455         if (!nsk_jvmti_waitForSync(timeout))
 456             return;
 457 
 458         if (!NSK_JVMTI_VERIFY(
 459                 NSK_CPP_STUB2(SuspendThread, jvmti, testedThread)))
 460             return;
 461 
 462         for (j = 2; j < 1002; j++) {
 463             if (!NSK_JVMTI_VERIFY(
 464                     NSK_CPP_STUB3(NotifyFramePop, jvmti, testedThread, j)))
 465                 return;
 466         }
 467 
 468         if (!NSK_JVMTI_VERIFY(
 469                 NSK_CPP_STUB2(ResumeThread, jvmti, testedThread)))
 470             return;
 471 
 472         if (!nsk_jvmti_resumeSync())
 473             return;
 474 
 475         if (!nsk_jvmti_waitForSync(timeout))
 476             return;
 477 
 478         if (i < STEP_NUMBER) {
 479             showEventStatistics(i);
 480             if (!checkEvents(i))
 481                 nsk_jvmti_setFailStatus();
 482 
 483             if (!setCallBacks(i + 1)) {
 484                 return;
 485             }
 486         }
 487 
 488         if (!nsk_jvmti_resumeSync())
 489             return;
 490     }
 491 
 492     NSK_CPP_STUB2(DeleteGlobalRef, agentJNI, testedThread);
 493 }
 494 
 495 /* ============================================================================= */
 496 
 497 /** Agent library initialization. */
 498 #ifdef STATIC_BUILD
 499 JNIEXPORT jint JNICALL Agent_OnLoad_em02t012(JavaVM *jvm, char *options, void *reserved) {
 500     return Agent_Initialize(jvm, options, reserved);
 501 }
 502 JNIEXPORT jint JNICALL Agent_OnAttach_em02t012(JavaVM *jvm, char *options, void *reserved) {
 503     return Agent_Initialize(jvm, options, reserved);
 504 }
 505 JNIEXPORT jint JNI_OnLoad_em02t012(JavaVM *jvm, char *options, void *reserved) {
 506     return JNI_VERSION_1_8;
 507 }
 508 #endif
 509 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 510 
 511     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 512         return JNI_ERR;
 513 
 514     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 515 
 516     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 517         return JNI_ERR;
 518 
 519     if (!NSK_JVMTI_VERIFY(
 520             NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) {
 521         nsk_jvmti_setFailStatus();
 522         return JNI_ERR;
 523     }
 524 
 525     {
 526         jvmtiCapabilities caps;
 527         memset(&caps, 0, sizeof(caps));
 528 
 529         caps.can_suspend = 1;
 530         caps.can_generate_frame_pop_events = 1;
 531         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)))
 532             return JNI_ERR;
 533     }
 534 
 535     if (!setCallBacks(1)) {
 536         return JNI_ERR;
 537     }
 538 
 539     if (!enableEventList()) {
 540         return JNI_ERR;
 541     }
 542 
 543     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 544         return JNI_ERR;
 545 
 546     return JNI_OK;
 547 }
 548 
 549 /* ============================================================================= */
 550 
 551 }


  35 /* scaffold objects */
  36 static jvmtiEnv *jvmti = NULL;
  37 static jthread testedThread;
  38 static jlong timeout = 0;
  39 static jrawMonitorID syncLock = NULL;
  40 
  41 /* constant names */
  42 #define STEP_NUMBER 3
  43 #define JVMTI_EVENT_COUNT   (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1)
  44 #define NUMBER_OF_INVOCATIONS 1000
  45 
  46 static int eventCount[JVMTI_EVENT_COUNT];
  47 static int newEventCount[JVMTI_EVENT_COUNT];
  48 
  49 /* ============================================================================= */
  50 JNIEXPORT void JNICALL
  51 Java_nsk_jvmti_scenarios_events_EM02_em02t012_setThread(JNIEnv *jni_env,
  52                         jobject o, jthread thrd) {
  53 
  54     /* make thread accessable for a long time */
  55     NSK_JNI_VERIFY(jni_env, (testedThread = jni_env->NewGlobalRef(thrd)) != NULL);

  56 }
  57 
  58 static void
  59 showEventStatistics(int step) {
  60     int i;
  61     const char* str;
  62     int *currentCounts = (step == 1) ? &eventCount[0] : &newEventCount[0];
  63 
  64     NSK_DISPLAY0("\n");
  65     NSK_DISPLAY1("Event statistics for %d step:\n", step);
  66     NSK_DISPLAY0("-----------------------------\n");
  67     for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
  68         if (currentCounts[i] > 0) {
  69             str = TranslateEvent((jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL));
  70             NSK_DISPLAY2("%-40s %7d\n", str, currentCounts[i]);
  71         }
  72     }
  73 }
  74 
  75 /* ========================================================================== */


 133                 }
 134             }
 135 
 136         } else {
 137 
 138             if (currentCounts[i] > 0) {
 139                 NSK_COMPLAIN2("Unexpected event %s was sent %d times\n",
 140                                     TranslateEvent(curr),
 141                                     currentCounts[i]);
 142                 result = NSK_FALSE;
 143             }
 144         }
 145     }
 146 
 147     return result;
 148 }
 149 
 150 static void
 151 changeCount(jvmtiEvent event, int *currentCounts) {
 152 
 153     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
 154         nsk_jvmti_setFailStatus();
 155 
 156     currentCounts[event - JVMTI_MIN_EVENT_TYPE_VAL]++;
 157 
 158     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
 159         nsk_jvmti_setFailStatus();
 160 
 161 }
 162 
 163 /* ============================================================================= */
 164 
 165 /* callbacks */
 166 JNIEXPORT void JNICALL
 167 cbVMInit(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread) {
 168     changeCount(JVMTI_EVENT_VM_INIT, &eventCount[0]);
 169 }
 170 
 171 JNIEXPORT void JNICALL
 172 cbVMDeath(jvmtiEnv* jvmti, JNIEnv* jni_env) {
 173     changeCount(JVMTI_EVENT_VM_DEATH, &newEventCount[0]);
 174     showEventStatistics(STEP_NUMBER);
 175     if (!checkEvents(STEP_NUMBER))
 176         nsk_jvmti_setFailStatus();
 177 
 178     if (!NSK_JVMTI_VERIFY(jvmti->DestroyRawMonitor(syncLock)))

 179         nsk_jvmti_setFailStatus();
 180 
 181 }
 182 
 183 void JNICALL
 184 cbException(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 185                 jmethodID method, jlocation location, jobject exception,
 186                 jmethodID catch_method, jlocation catch_location) {
 187 
 188     changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]);
 189 }
 190 
 191 void JNICALL
 192 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 193                 jmethodID method, jlocation location, jobject exception) {
 194 
 195     changeCount(JVMTI_EVENT_EXCEPTION_CATCH, &eventCount[0]);
 196 }
 197 
 198 void JNICALL


 312 void JNICALL
 313 cbObjectFree(jvmtiEnv *jvmti_env, jlong tag) {
 314 
 315     changeCount(JVMTI_EVENT_OBJECT_FREE, &eventCount[0]);
 316 }
 317 
 318 void JNICALL
 319 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 320                     jobject object, jclass object_klass, jlong size) {
 321 
 322     changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]);
 323 }
 324 
 325 /* ============================================================================= */
 326 
 327 static int enableEvent(jvmtiEvent event) {
 328 
 329     if (nsk_jvmti_isOptionalEvent(event)
 330             && (event != JVMTI_EVENT_FRAME_POP)) {
 331         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
 332                 jvmti->SetEventNotificationMode(JVMTI_ENABLE, event, NULL))) {

 333             NSK_COMPLAIN1("Unexpected error enabling %s\n",
 334                 TranslateEvent(event));
 335             return NSK_FALSE;
 336         }
 337     } else {
 338         if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, event, NULL))) {


 339             NSK_COMPLAIN1("Unexpected error enabling %s\n",
 340                 TranslateEvent(event));
 341             return NSK_FALSE;
 342         }
 343     }
 344 
 345     return NSK_TRUE;
 346 }
 347 
 348 /**
 349  * Enable or disable tested events.
 350  */
 351 static int enableEventList() {
 352 
 353     int i, result;
 354 
 355     result = enableEvent(JVMTI_EVENT_VM_INIT);
 356 
 357     result = result && enableEvent(JVMTI_EVENT_VM_DEATH);
 358 


 411             eventCallbacks.VMObjectAlloc             = cbVMObjectAlloc;
 412             break;
 413 
 414         case 2:
 415             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 416                 newEventCount[i] = 0;
 417             }
 418 
 419             eventCallbacks.FramePop                  = cbNewFramePop;
 420             break;
 421 
 422         case 3:
 423             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 424                 newEventCount[i] = 0;
 425             }
 426 
 427             eventCallbacks.VMDeath                   = cbVMDeath;
 428             break;
 429 
 430     }
 431     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))



 432         return NSK_FALSE;
 433 
 434     return NSK_TRUE;
 435 }
 436 
 437 /* ============================================================================= */
 438 
 439 /** Agent algorithm. */
 440 static void JNICALL
 441 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 442 
 443     int i, j;
 444 
 445     for (i = 1; i <= STEP_NUMBER; i++) {
 446 
 447         if (!nsk_jvmti_waitForSync(timeout))
 448             return;
 449 
 450         if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(testedThread)))

 451             return;
 452 
 453         for (j = 2; j < 1002; j++) {
 454             if (!NSK_JVMTI_VERIFY(jvmti->NotifyFramePop(testedThread, j)))

 455                 return;
 456         }
 457 
 458         if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(testedThread)))

 459             return;
 460 
 461         if (!nsk_jvmti_resumeSync())
 462             return;
 463 
 464         if (!nsk_jvmti_waitForSync(timeout))
 465             return;
 466 
 467         if (i < STEP_NUMBER) {
 468             showEventStatistics(i);
 469             if (!checkEvents(i))
 470                 nsk_jvmti_setFailStatus();
 471 
 472             if (!setCallBacks(i + 1)) {
 473                 return;
 474             }
 475         }
 476 
 477         if (!nsk_jvmti_resumeSync())
 478             return;
 479     }
 480 
 481     agentJNI->DeleteGlobalRef(testedThread);
 482 }
 483 
 484 /* ============================================================================= */
 485 
 486 /** Agent library initialization. */
 487 #ifdef STATIC_BUILD
 488 JNIEXPORT jint JNICALL Agent_OnLoad_em02t012(JavaVM *jvm, char *options, void *reserved) {
 489     return Agent_Initialize(jvm, options, reserved);
 490 }
 491 JNIEXPORT jint JNICALL Agent_OnAttach_em02t012(JavaVM *jvm, char *options, void *reserved) {
 492     return Agent_Initialize(jvm, options, reserved);
 493 }
 494 JNIEXPORT jint JNI_OnLoad_em02t012(JavaVM *jvm, char *options, void *reserved) {
 495     return JNI_VERSION_1_8;
 496 }
 497 #endif
 498 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 499 
 500     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 501         return JNI_ERR;
 502 
 503     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 504 
 505     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 506         return JNI_ERR;
 507 
 508     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_syncLock", &syncLock))) {

 509         nsk_jvmti_setFailStatus();
 510         return JNI_ERR;
 511     }
 512 
 513     {
 514         jvmtiCapabilities caps;
 515         memset(&caps, 0, sizeof(caps));
 516 
 517         caps.can_suspend = 1;
 518         caps.can_generate_frame_pop_events = 1;
 519         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 520             return JNI_ERR;
 521     }
 522 
 523     if (!setCallBacks(1)) {
 524         return JNI_ERR;
 525     }
 526 
 527     if (!enableEventList()) {
 528         return JNI_ERR;
 529     }
 530 
 531     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 532         return JNI_ERR;
 533 
 534     return JNI_OK;
 535 }
 536 
 537 /* ============================================================================= */
 538 
 539 }
< prev index next >