< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/sampling/SP02/sp02t003/sp02t003.cpp

Print this page
rev 52100 : 8212082: Remove the NSK_CPP_STUB macros for remaining vmTestbase/jvmti/[sS]*
Summary:
Reviewed-by:


 142  *    - find tested threads
 143  *    - find tested methos
 144  *    - make global refs
 145  */
 146 static int prepare() {
 147     jthread *allThreadsList = NULL;
 148     jint allThreadsCount = 0;
 149     int found = 0;
 150     int i;
 151 
 152     NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT);
 153 
 154     /* clean threads list */
 155     for (i = 0; i < THREADS_COUNT; i++) {
 156         threadsDesc[i].thread = (jthread)NULL;
 157         threadsDesc[i].method = (jmethodID)NULL;
 158         threadsDesc[i].location = NSK_JVMTI_INVALID_JLOCATION;
 159     }
 160 
 161     /* get all live threads */
 162     if (!NSK_JVMTI_VERIFY(
 163             NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList)))
 164         return NSK_FALSE;
 165 
 166     if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
 167         return NSK_FALSE;
 168 
 169     /* find tested threads */
 170     for (i = 0; i < allThreadsCount; i++) {
 171         jvmtiThreadInfo threadInfo;
 172 
 173         if (!NSK_VERIFY(allThreadsList[i] != NULL))
 174             return NSK_FALSE;
 175 
 176         /* get thread name (info) */
 177         if (!NSK_JVMTI_VERIFY(
 178                 NSK_CPP_STUB3(GetThreadInfo, jvmti, allThreadsList[i], &threadInfo)))
 179             return NSK_FALSE;
 180 
 181         /* find by name */
 182         if (threadInfo.name != NULL) {
 183             int j;
 184 
 185             for (j = 0; j < THREADS_COUNT; j++) {
 186                 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) {
 187                     threadsDesc[j].thread = allThreadsList[i];
 188                     NSK_DISPLAY3("    thread #%d (%s): %p\n",
 189                                             j, threadInfo.name, (void*)threadsDesc[j].thread);
 190                 }
 191             }
 192         }
 193     }
 194 
 195     /* deallocate all threads list */
 196     if (!NSK_JVMTI_VERIFY(
 197             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList)))
 198         return NSK_FALSE;
 199 
 200     /* check if all tested threads found */
 201     found = 0;
 202     for (i = 0; i < THREADS_COUNT; i++) {
 203         if (threadsDesc[i].thread == NULL) {
 204             NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName);
 205         } else {
 206             found++;
 207         }
 208     }
 209 
 210     if (found < THREADS_COUNT)
 211         return NSK_FALSE;
 212 
 213     /* get threads class and frame method */
 214     NSK_DISPLAY0("Find tested methods:\n");
 215     for (i = 0; i < THREADS_COUNT; i++) {
 216         /* get thread class */
 217         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls =
 218                 NSK_CPP_STUB2(GetObjectClass, jni, threadsDesc[i].thread)) != NULL))
 219             return NSK_FALSE;
 220         /* get frame method */
 221         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method =
 222                 NSK_CPP_STUB4(GetMethodID, jni, threadsDesc[i].cls,
 223                             threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL))
 224             return NSK_FALSE;
 225 
 226         NSK_DISPLAY4("    thread #%d (%s): %p (%s)\n",
 227                                 i, threadsDesc[i].threadName,
 228                                 (void*)threadsDesc[i].method,
 229                                 threadsDesc[i].methodName);
 230     }
 231 
 232     /* make global refs */
 233     for (i = 0; i < THREADS_COUNT; i++) {
 234         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread)
 235                 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].thread)) != NULL))
 236             return NSK_FALSE;
 237         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass)
 238                 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].cls)) != NULL))
 239             return NSK_FALSE;
 240     }
 241 
 242     return NSK_TRUE;
 243 }
 244 
 245 /**
 246  * Suspend or resume tested threads.
 247  */
 248 static int suspendThreadsIndividually(int suspend) {
 249     int i;
 250 
 251     for (i = 0; i < THREADS_COUNT; i++) {
 252         if (suspend) {
 253             NSK_DISPLAY2("    suspend thread #%d (%s)\n", i, threadsDesc[i].threadName);
 254             if (!NSK_JVMTI_VERIFY(
 255                     NSK_CPP_STUB2(SuspendThread, jvmti, threadsDesc[i].thread)))
 256                 nsk_jvmti_setFailStatus();
 257         } else {
 258             NSK_DISPLAY2("    resume thread #%d (%s)\n", i, threadsDesc[i].threadName);
 259             if (!NSK_JVMTI_VERIFY(
 260                     NSK_CPP_STUB2(ResumeThread, jvmti, threadsDesc[i].thread)))
 261                 nsk_jvmti_setFailStatus();
 262         }
 263     }
 264     return NSK_TRUE;
 265 }
 266 
 267 /**
 268  * Testcase: check tested threads
 269  *    - call GetFrameCount() and then GetStackTrace()
 270  *    - for each stack frame of common depth GetFrameLocation()
 271  *    - compare frame ifno returned by GetFrameLocation() and GetStackTrace()
 272  *    - find expected frame for tested method
 273  *
 274  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 275  */
 276 static int checkThreads(int suspended, const char* kind) {
 277     int i;
 278 
 279     /* check each thread */
 280     for (i = 0; i < THREADS_COUNT; i++) {
 281         jint frameCount = 0;
 282         jint frameStackSize = 0;
 283         jvmtiFrameInfo frameStack[MAX_STACK_SIZE];
 284         int commonDepth = 0;
 285         int found = 0;
 286         int j;
 287 
 288         NSK_DISPLAY2("  thread #%d (%s):\n", i, threadsDesc[i].threadName);
 289 
 290         /* get frame count */
 291         if (!NSK_JVMTI_VERIFY(
 292                 NSK_CPP_STUB3(GetFrameCount, jvmti,
 293                                     threadsDesc[i].thread, &frameCount))) {
 294             nsk_jvmti_setFailStatus();
 295             return NSK_TRUE;
 296         }
 297         NSK_DISPLAY1("    frameCount:  %d\n", (int)frameCount);
 298 
 299         /* get stack trace */
 300         if (!NSK_JVMTI_VERIFY(
 301                 NSK_CPP_STUB6(GetStackTrace, jvmti, threadsDesc[i].thread,
 302                                     0, MAX_STACK_SIZE, frameStack, &frameStackSize))) {
 303             nsk_jvmti_setFailStatus();
 304             return NSK_TRUE;
 305         }
 306         NSK_DISPLAY1("    stack depth: %d\n", (int)frameStackSize);
 307 
 308         commonDepth = (frameCount < frameStackSize ? frameCount : frameStackSize);
 309         NSK_DISPLAY1("         common: %d\n", (int)commonDepth);
 310 
 311         /* check first commonDepth frames and find expected method there */
 312         found = 0;
 313         for (j = 0; j < commonDepth; j++) {
 314             jmethodID qMethod = (jmethodID)NULL;
 315             jlocation qLocation = NSK_JVMTI_INVALID_JLOCATION;
 316 
 317             NSK_DISPLAY3("      %d frame: method: %p, location: %ld\n",
 318                                         j, (void*)frameStack[j].method,
 319                                         (long)frameStack[j].location);
 320             /* query frame location */
 321             if (!NSK_JVMTI_VERIFY(
 322                     NSK_CPP_STUB5(GetFrameLocation, jvmti, threadsDesc[i].thread,
 323                                                         j, &qMethod, &qLocation))) {
 324                 nsk_jvmti_setFailStatus();
 325                 continue;
 326             }
 327 
 328             NSK_DISPLAY2("      queried: method: %p, location: %ld\n",
 329                                         (void*)qMethod, (long)qLocation);
 330 
 331             /* check frame equalaty */
 332             if (frameStack[j].method != qMethod) {
 333                 NSK_COMPLAIN6("Different method in stack frame #%d for %s thread #%d (%s):\n"
 334                             "#   GetStackTrace():    %p\n"
 335                             "#   GetFrameLocation(): %p\n",
 336                             j, kind, i, threadsDesc[i].threadName,
 337                             (void*)frameStack[j].method, (void*)qMethod);
 338                 nsk_jvmti_setFailStatus();
 339             }
 340             if ( (suspended == NSK_TRUE) && (frameStack[j].location != qLocation) ) {
 341                 NSK_COMPLAIN6("Different location in stack frame #%d for %s thread #%d (%s):\n"
 342                             "#   GetStackTrace():    %ld\n"
 343                             "#   GetFrameLocation(): %ld\n",


 358         if (found <= 0) {
 359             NSK_COMPLAIN3("No expected method frame for %s thread #%d (%s)\n",
 360                                 kind, i, threadsDesc[i].threadName);
 361             nsk_jvmti_setFailStatus();
 362         }
 363     }
 364 
 365     /* test may continue */
 366     return NSK_TRUE;
 367 }
 368 
 369 /**
 370  * Clean data:
 371  *   - dispose global references to tested threads
 372  */
 373 static int clean() {
 374     int i;
 375 
 376     /* dispose global references to threads */
 377     for (i = 0; i < THREADS_COUNT; i++) {
 378         NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].thread));
 379         NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].cls));
 380     }
 381 
 382     return NSK_TRUE;
 383 }
 384 
 385 /* ============================================================================= */
 386 
 387 static volatile int testedThreadRunning = NSK_FALSE;
 388 static volatile int testedThreadShouldFinish = NSK_FALSE;
 389 
 390 /** Native running method in tested thread. */
 391 JNIEXPORT void JNICALL
 392 Java_nsk_jvmti_scenarios_sampling_SP02_sp02t003ThreadRunningNative_testedMethod(JNIEnv* jni,
 393                                                                                 jobject obj) {
 394     volatile int i = 0, n = 1000;
 395 
 396     /* run in a loop */
 397     testedThreadRunning = NSK_TRUE;
 398     while (!testedThreadShouldFinish) {
 399         if (n <= 0)


 436     return JNI_VERSION_1_8;
 437 }
 438 #endif
 439 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 440 
 441     /** Init framework and parse options. */
 442     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 443         return JNI_ERR;
 444 
 445     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 446 
 447     /* create JVMTI environment */
 448     if (!NSK_VERIFY((jvmti =
 449             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 450         return JNI_ERR;
 451 
 452     /* add specific capabilities for suspending thread */    {
 453         jvmtiCapabilities suspendCaps;
 454         memset(&suspendCaps, 0, sizeof(suspendCaps));
 455         suspendCaps.can_suspend = 1;
 456         if (!NSK_JVMTI_VERIFY(
 457                 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
 458             return JNI_ERR;
 459     }
 460 
 461     /* register agent proc and arg */
 462     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 463         return JNI_ERR;
 464 
 465     return JNI_OK;
 466 }
 467 
 468 /* ============================================================================= */
 469 
 470 }


 142  *    - find tested threads
 143  *    - find tested methos
 144  *    - make global refs
 145  */
 146 static int prepare() {
 147     jthread *allThreadsList = NULL;
 148     jint allThreadsCount = 0;
 149     int found = 0;
 150     int i;
 151 
 152     NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT);
 153 
 154     /* clean threads list */
 155     for (i = 0; i < THREADS_COUNT; i++) {
 156         threadsDesc[i].thread = (jthread)NULL;
 157         threadsDesc[i].method = (jmethodID)NULL;
 158         threadsDesc[i].location = NSK_JVMTI_INVALID_JLOCATION;
 159     }
 160 
 161     /* get all live threads */
 162     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&allThreadsCount, &allThreadsList)))

 163         return NSK_FALSE;
 164 
 165     if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
 166         return NSK_FALSE;
 167 
 168     /* find tested threads */
 169     for (i = 0; i < allThreadsCount; i++) {
 170         jvmtiThreadInfo threadInfo;
 171 
 172         if (!NSK_VERIFY(allThreadsList[i] != NULL))
 173             return NSK_FALSE;
 174 
 175         /* get thread name (info) */
 176         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(allThreadsList[i], &threadInfo)))

 177             return NSK_FALSE;
 178 
 179         /* find by name */
 180         if (threadInfo.name != NULL) {
 181             int j;
 182 
 183             for (j = 0; j < THREADS_COUNT; j++) {
 184                 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) {
 185                     threadsDesc[j].thread = allThreadsList[i];
 186                     NSK_DISPLAY3("    thread #%d (%s): %p\n",
 187                                             j, threadInfo.name, (void*)threadsDesc[j].thread);
 188                 }
 189             }
 190         }
 191     }
 192 
 193     /* deallocate all threads list */
 194     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)allThreadsList)))

 195         return NSK_FALSE;
 196 
 197     /* check if all tested threads found */
 198     found = 0;
 199     for (i = 0; i < THREADS_COUNT; i++) {
 200         if (threadsDesc[i].thread == NULL) {
 201             NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName);
 202         } else {
 203             found++;
 204         }
 205     }
 206 
 207     if (found < THREADS_COUNT)
 208         return NSK_FALSE;
 209 
 210     /* get threads class and frame method */
 211     NSK_DISPLAY0("Find tested methods:\n");
 212     for (i = 0; i < THREADS_COUNT; i++) {
 213         /* get thread class */
 214         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls =
 215                 jni->GetObjectClass(threadsDesc[i].thread)) != NULL))
 216             return NSK_FALSE;
 217         /* get frame method */
 218         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].method =
 219                 jni->GetMethodID(threadsDesc[i].cls, threadsDesc[i].methodName, threadsDesc[i].methodSig)) != NULL))

 220             return NSK_FALSE;
 221 
 222         NSK_DISPLAY4("    thread #%d (%s): %p (%s)\n",
 223                                 i, threadsDesc[i].threadName,
 224                                 (void*)threadsDesc[i].method,
 225                                 threadsDesc[i].methodName);
 226     }
 227 
 228     /* make global refs */
 229     for (i = 0; i < THREADS_COUNT; i++) {
 230         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread)
 231                 jni->NewGlobalRef(threadsDesc[i].thread)) != NULL))
 232             return NSK_FALSE;
 233         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].cls = (jclass)
 234                 jni->NewGlobalRef(threadsDesc[i].cls)) != NULL))
 235             return NSK_FALSE;
 236     }
 237 
 238     return NSK_TRUE;
 239 }
 240 
 241 /**
 242  * Suspend or resume tested threads.
 243  */
 244 static int suspendThreadsIndividually(int suspend) {
 245     int i;
 246 
 247     for (i = 0; i < THREADS_COUNT; i++) {
 248         if (suspend) {
 249             NSK_DISPLAY2("    suspend thread #%d (%s)\n", i, threadsDesc[i].threadName);
 250             if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(threadsDesc[i].thread)))

 251                 nsk_jvmti_setFailStatus();
 252         } else {
 253             NSK_DISPLAY2("    resume thread #%d (%s)\n", i, threadsDesc[i].threadName);
 254             if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(threadsDesc[i].thread)))

 255                 nsk_jvmti_setFailStatus();
 256         }
 257     }
 258     return NSK_TRUE;
 259 }
 260 
 261 /**
 262  * Testcase: check tested threads
 263  *    - call GetFrameCount() and then GetStackTrace()
 264  *    - for each stack frame of common depth GetFrameLocation()
 265  *    - compare frame ifno returned by GetFrameLocation() and GetStackTrace()
 266  *    - find expected frame for tested method
 267  *
 268  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 269  */
 270 static int checkThreads(int suspended, const char* kind) {
 271     int i;
 272 
 273     /* check each thread */
 274     for (i = 0; i < THREADS_COUNT; i++) {
 275         jint frameCount = 0;
 276         jint frameStackSize = 0;
 277         jvmtiFrameInfo frameStack[MAX_STACK_SIZE];
 278         int commonDepth = 0;
 279         int found = 0;
 280         int j;
 281 
 282         NSK_DISPLAY2("  thread #%d (%s):\n", i, threadsDesc[i].threadName);
 283 
 284         /* get frame count */
 285         if (!NSK_JVMTI_VERIFY(jvmti->GetFrameCount(threadsDesc[i].thread, &frameCount))) {


 286             nsk_jvmti_setFailStatus();
 287             return NSK_TRUE;
 288         }
 289         NSK_DISPLAY1("    frameCount:  %d\n", (int)frameCount);
 290 
 291         /* get stack trace */
 292         if (!NSK_JVMTI_VERIFY(
 293                 jvmti->GetStackTrace(threadsDesc[i].thread, 0, MAX_STACK_SIZE, frameStack, &frameStackSize))) {

 294             nsk_jvmti_setFailStatus();
 295             return NSK_TRUE;
 296         }
 297         NSK_DISPLAY1("    stack depth: %d\n", (int)frameStackSize);
 298 
 299         commonDepth = (frameCount < frameStackSize ? frameCount : frameStackSize);
 300         NSK_DISPLAY1("         common: %d\n", (int)commonDepth);
 301 
 302         /* check first commonDepth frames and find expected method there */
 303         found = 0;
 304         for (j = 0; j < commonDepth; j++) {
 305             jmethodID qMethod = (jmethodID)NULL;
 306             jlocation qLocation = NSK_JVMTI_INVALID_JLOCATION;
 307 
 308             NSK_DISPLAY3("      %d frame: method: %p, location: %ld\n",
 309                                         j, (void*)frameStack[j].method,
 310                                         (long)frameStack[j].location);
 311             /* query frame location */
 312             if (!NSK_JVMTI_VERIFY(
 313                     jvmti->GetFrameLocation(threadsDesc[i].thread, j, &qMethod, &qLocation))) {

 314                 nsk_jvmti_setFailStatus();
 315                 continue;
 316             }
 317 
 318             NSK_DISPLAY2("      queried: method: %p, location: %ld\n",
 319                                         (void*)qMethod, (long)qLocation);
 320 
 321             /* check frame equalaty */
 322             if (frameStack[j].method != qMethod) {
 323                 NSK_COMPLAIN6("Different method in stack frame #%d for %s thread #%d (%s):\n"
 324                             "#   GetStackTrace():    %p\n"
 325                             "#   GetFrameLocation(): %p\n",
 326                             j, kind, i, threadsDesc[i].threadName,
 327                             (void*)frameStack[j].method, (void*)qMethod);
 328                 nsk_jvmti_setFailStatus();
 329             }
 330             if ( (suspended == NSK_TRUE) && (frameStack[j].location != qLocation) ) {
 331                 NSK_COMPLAIN6("Different location in stack frame #%d for %s thread #%d (%s):\n"
 332                             "#   GetStackTrace():    %ld\n"
 333                             "#   GetFrameLocation(): %ld\n",


 348         if (found <= 0) {
 349             NSK_COMPLAIN3("No expected method frame for %s thread #%d (%s)\n",
 350                                 kind, i, threadsDesc[i].threadName);
 351             nsk_jvmti_setFailStatus();
 352         }
 353     }
 354 
 355     /* test may continue */
 356     return NSK_TRUE;
 357 }
 358 
 359 /**
 360  * Clean data:
 361  *   - dispose global references to tested threads
 362  */
 363 static int clean() {
 364     int i;
 365 
 366     /* dispose global references to threads */
 367     for (i = 0; i < THREADS_COUNT; i++) {
 368         NSK_TRACE(jni->DeleteGlobalRef(threadsDesc[i].thread));
 369         NSK_TRACE(jni->DeleteGlobalRef(threadsDesc[i].cls));
 370     }
 371 
 372     return NSK_TRUE;
 373 }
 374 
 375 /* ============================================================================= */
 376 
 377 static volatile int testedThreadRunning = NSK_FALSE;
 378 static volatile int testedThreadShouldFinish = NSK_FALSE;
 379 
 380 /** Native running method in tested thread. */
 381 JNIEXPORT void JNICALL
 382 Java_nsk_jvmti_scenarios_sampling_SP02_sp02t003ThreadRunningNative_testedMethod(JNIEnv* jni,
 383                                                                                 jobject obj) {
 384     volatile int i = 0, n = 1000;
 385 
 386     /* run in a loop */
 387     testedThreadRunning = NSK_TRUE;
 388     while (!testedThreadShouldFinish) {
 389         if (n <= 0)


 426     return JNI_VERSION_1_8;
 427 }
 428 #endif
 429 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 430 
 431     /** Init framework and parse options. */
 432     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 433         return JNI_ERR;
 434 
 435     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 436 
 437     /* create JVMTI environment */
 438     if (!NSK_VERIFY((jvmti =
 439             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 440         return JNI_ERR;
 441 
 442     /* add specific capabilities for suspending thread */    {
 443         jvmtiCapabilities suspendCaps;
 444         memset(&suspendCaps, 0, sizeof(suspendCaps));
 445         suspendCaps.can_suspend = 1;
 446         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))

 447             return JNI_ERR;
 448     }
 449 
 450     /* register agent proc and arg */
 451     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 452         return JNI_ERR;
 453 
 454     return JNI_OK;
 455 }
 456 
 457 /* ============================================================================= */
 458 
 459 }
< prev index next >