< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/sampling/SP03/sp03t002/sp03t002.cpp

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


 144  * Prepare data:
 145  *    - clean threads list
 146  *    - get all live threads
 147  *    - get threads name
 148  *    - find tested threads
 149  *    - make global refs
 150  */
 151 static int prepare() {
 152     jthread *allThreadsList = NULL;
 153     jint allThreadsCount = 0;
 154     int notfound = 0;
 155     int i, j;
 156 
 157     NSK_DISPLAY1("Prepare: find tested threads: %d kinds\n", THREADS_KINDS);
 158 
 159     /* allocate and clean threads list */
 160     for (i = 0; i < THREADS_KINDS; i++) {
 161         threadsCounts[i] = 0;
 162         threadsList[i] = NULL;
 163 
 164         if (!NSK_JVMTI_VERIFY(
 165                 NSK_CPP_STUB3(Allocate, jvmti, (threadsCount * sizeof(jthread)),
 166                                                     (unsigned char**)&threadsList[i])))
 167             return NSK_FALSE;
 168 
 169         for (j = 0; j < threadsCount; j++) {
 170             threadsList[i][j] = NULL;
 171         }
 172     }
 173 
 174     /* get all live threads */
 175     if (!NSK_JVMTI_VERIFY(
 176             NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList)))
 177         return NSK_FALSE;
 178 
 179     if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
 180         return NSK_FALSE;
 181 
 182     /* find tested threads */
 183     for (i = 0; i < allThreadsCount; i++) {
 184         jvmtiThreadInfo threadInfo;
 185 
 186         if (!NSK_VERIFY(allThreadsList[i] != NULL))
 187             return NSK_FALSE;
 188 
 189         /* get thread name (info) */
 190         if (!NSK_JVMTI_VERIFY(
 191                 NSK_CPP_STUB3(GetThreadInfo, jvmti, allThreadsList[i], &threadInfo)))
 192             return NSK_FALSE;
 193 
 194         /* find by name */
 195         if (threadInfo.name != NULL) {
 196             for (j = 0; j < THREADS_KINDS; j++) {
 197                 if (strcmp(threadInfo.name, threadsName[j]) == 0) {
 198                     int k = threadsCounts[j];
 199                     if (k < threadsCount)
 200                         threadsList[j][k] = allThreadsList[i];
 201                     threadsCounts[j]++;
 202                 }
 203             }
 204         }
 205     }
 206 
 207     /* deallocate all threads list */
 208     if (!NSK_JVMTI_VERIFY(
 209             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList)))
 210         return NSK_FALSE;
 211 
 212     /* check if all tested threads found */
 213     notfound = 0;
 214     for (i = 0; i < THREADS_KINDS; i++) {
 215         if (threadsCounts[i] != threadsCount) {
 216             NSK_COMPLAIN3("Found unexpected number of tested threads (%s):\n"
 217                             "#   found:    %d\n"
 218                             "#   expected: %d\n",
 219                             threadsName[i], threadsCounts[i], threadsCount);
 220             nsk_jvmti_setFailStatus();
 221             notfound++;
 222         }
 223     }
 224 
 225     if (notfound > 0)
 226         return NSK_FALSE;
 227 
 228     /* make global refs */
 229     for (i = 0; i < THREADS_KINDS; i++) {
 230         for (j = 0; j < threadsCount; j++) {
 231             if (!NSK_JNI_VERIFY(jni, (threadsList[i][j] =
 232                     NSK_CPP_STUB2(NewGlobalRef, jni, threadsList[i][j])) != NULL))
 233                 return NSK_FALSE;
 234         }
 235     }
 236 
 237     return NSK_TRUE;
 238 }
 239 
 240 /**
 241  * Suspend or resume tested threads list.
 242  */
 243 static int suspendThreadsList(int suspend) {
 244     jlong resultsSize = threadsCount * sizeof(jvmtiError);
 245     jvmtiError* results = NULL;
 246     const char* kind = (suspend ? "suspending" : "resuming");
 247     int i, j;
 248 
 249     /* allocate results array */
 250     if (!NSK_JVMTI_VERIFY(
 251             NSK_CPP_STUB3(Allocate, jvmti, resultsSize, (unsigned char**)&results))) {
 252         nsk_jvmti_setFailStatus();
 253         return NSK_FALSE;
 254     }
 255 
 256     for (i = 0; i < THREADS_KINDS; i++) {
 257         /* suspend or resume threads list */
 258         if (suspend) {
 259             if (!NSK_JVMTI_VERIFY(
 260                     NSK_CPP_STUB4(SuspendThreadList, jvmti, threadsCount,
 261                                                         threadsList[i], results)))
 262                 nsk_jvmti_setFailStatus();
 263         } else {
 264             if (!NSK_JVMTI_VERIFY(
 265                     NSK_CPP_STUB4(ResumeThreadList, jvmti, threadsCount,
 266                                                         threadsList[i], results)))
 267                 nsk_jvmti_setFailStatus();
 268         }
 269 
 270         /* check results */
 271         for (j = 0; j < threadsCount; j++) {
 272             if (results[j] != JVMTI_ERROR_NONE) {
 273                 NSK_COMPLAIN5("Unexpected result of %s thread #%d (%s):\n"
 274                                 "#   got result: %s (%d)\n",
 275                                 kind, j, threadsName[i],
 276                                 TranslateError(results[j]), (int)results[j]);
 277                 nsk_jvmti_setFailStatus();
 278             }
 279         }
 280     }
 281 
 282     /* deallocate results array */
 283     if (!NSK_JVMTI_VERIFY(
 284             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)results))) {
 285         nsk_jvmti_setFailStatus();
 286     }
 287 
 288     return NSK_TRUE;
 289 }
 290 
 291 /**
 292  * Suspend or resume tested threads individually.
 293  */
 294 static int suspendThreadsIndividually(int suspend) {
 295     int i, j;
 296 
 297     for (i = 0; i < THREADS_KINDS; i++) {
 298         for (j = 0; j < threadsCount; j++) {
 299             if (suspend) {
 300                 NSK_DISPLAY2("    suspend thread #%d (%s)\n", j, threadsName[i]);
 301                 if (!NSK_JVMTI_VERIFY(
 302                         NSK_CPP_STUB2(SuspendThread, jvmti, threadsList[i][j])))
 303                     nsk_jvmti_setFailStatus();
 304             } else {
 305                 NSK_DISPLAY2("    resume thread #%d (%s)\n", j, threadsName[i]);
 306                 if (!NSK_JVMTI_VERIFY(
 307                         NSK_CPP_STUB2(ResumeThread, jvmti, threadsList[i][j])))
 308                     nsk_jvmti_setFailStatus();
 309             }
 310         }
 311     }
 312     return NSK_TRUE;
 313 }
 314 
 315 /**
 316  * Testcase: check tested threads
 317  *    - get thread state and state
 318  *    - wait for WAITIME if state is not expected
 319  *    - check if thread state is as expected
 320  *
 321  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 322  */
 323 static int checkThreads(int suspended, const char* kind, jlong timeout) {
 324     int i, j;
 325 
 326     /* check each thread */
 327     for (i = 0; i < THREADS_KINDS; i++) {
 328         for (j = 0; j < threadsCount; j++) {
 329             jint state = JVMTI_THREAD_STATE_NOT_STARTED;
 330             jlong t = 0;
 331 
 332             NSK_DISPLAY2("    thread #%d (%s):\n", j, threadsName[i]);
 333 
 334             /* get thread state */
 335             if (!NSK_JVMTI_VERIFY(
 336                     NSK_CPP_STUB3(GetThreadState, jvmti, threadsList[i][j], &state))) {
 337                 nsk_jvmti_setFailStatus();
 338                 return NSK_FALSE;
 339             }
 340 
 341             NSK_DISPLAY2("        state  = %s (%d)\n",
 342                                         TranslateState(state), (int)state);
 343 
 344             /* check SUSPENDED state */
 345             if (suspended) {
 346                 if (!(state & JVMTI_THREAD_STATE_SUSPENDED)) {
 347                     NSK_COMPLAIN5("No SUSPENDED state for %s thread #%d (%s):\n"
 348                                     "#    got flags: %s (%d)\n",
 349                                     kind, j, threadsName[i],
 350                                     TranslateState(state), (int)state);
 351                     nsk_jvmti_setFailStatus();
 352                 }
 353             } else {
 354                 if (state & JVMTI_THREAD_STATE_SUSPENDED) {
 355                     NSK_COMPLAIN5("Unexpected SUSPENDED state for %s thread #%d (%s):\n"
 356                                     "#   got flags: %s (%d)\n",


 359                     nsk_jvmti_setFailStatus();
 360                 }
 361             }
 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, j;
 375 
 376     /* dispose global references to threads */
 377     for (i = 0; i < THREADS_KINDS; i++) {
 378         for (j = 0; j < threadsCount; j++) {
 379             NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsList[i][j]));
 380         }
 381     }
 382 
 383     /* deallocate memory */
 384     for (i = 0; i < THREADS_KINDS; i++) {
 385         if (!NSK_JVMTI_VERIFY(
 386                 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threadsList[i])))
 387             return NSK_FALSE;
 388         threadsList[i] = NULL;
 389     }
 390 
 391     return NSK_TRUE;
 392 }
 393 
 394 /* ============================================================================= */
 395 
 396 static volatile int testedThreadRunning = NSK_FALSE;
 397 static volatile int testedThreadShouldFinish = NSK_FALSE;
 398 
 399 /** Native running method in tested thread. */
 400 JNIEXPORT void JNICALL
 401 Java_nsk_jvmti_scenarios_sampling_SP03_sp03t002ThreadRunningNative_nativeMethod(JNIEnv* jni,
 402                                                                                      jobject obj) {
 403     volatile int i = 0, n = 1000;
 404 
 405     /* run in a loop */
 406     testedThreadRunning = NSK_TRUE;


 451     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 452         return JNI_ERR;
 453 
 454     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 455 
 456     /* get number of threads for each kind */
 457     threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_NUMBER);
 458     if (!NSK_VERIFY(threadsCount > 0))
 459         return JNI_ERR;
 460 
 461     /* create JVMTI environment */
 462     if (!NSK_VERIFY((jvmti =
 463             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 464         return JNI_ERR;
 465 
 466     /* add specific capabilities for suspending thread */
 467     {
 468         jvmtiCapabilities suspendCaps;
 469         memset(&suspendCaps, 0, sizeof(suspendCaps));
 470         suspendCaps.can_suspend = 1;
 471         if (!NSK_JVMTI_VERIFY(
 472                 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
 473             return JNI_ERR;
 474     }
 475 
 476     /* register agent proc and arg */
 477     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 478         return JNI_ERR;
 479 
 480     return JNI_OK;
 481 }
 482 
 483 /* ============================================================================= */
 484 
 485 }


 144  * Prepare data:
 145  *    - clean threads list
 146  *    - get all live threads
 147  *    - get threads name
 148  *    - find tested threads
 149  *    - make global refs
 150  */
 151 static int prepare() {
 152     jthread *allThreadsList = NULL;
 153     jint allThreadsCount = 0;
 154     int notfound = 0;
 155     int i, j;
 156 
 157     NSK_DISPLAY1("Prepare: find tested threads: %d kinds\n", THREADS_KINDS);
 158 
 159     /* allocate and clean threads list */
 160     for (i = 0; i < THREADS_KINDS; i++) {
 161         threadsCounts[i] = 0;
 162         threadsList[i] = NULL;
 163 
 164         if (!NSK_JVMTI_VERIFY(jvmti->Allocate(threadsCount * sizeof(jthread),

 165                                               (unsigned char**)&threadsList[i])))
 166             return NSK_FALSE;
 167 
 168         for (j = 0; j < threadsCount; j++) {
 169             threadsList[i][j] = NULL;
 170         }
 171     }
 172 
 173     /* get all live threads */
 174     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&allThreadsCount, &allThreadsList)))

 175         return NSK_FALSE;
 176 
 177     if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
 178         return NSK_FALSE;
 179 
 180     /* find tested threads */
 181     for (i = 0; i < allThreadsCount; i++) {
 182         jvmtiThreadInfo threadInfo;
 183 
 184         if (!NSK_VERIFY(allThreadsList[i] != NULL))
 185             return NSK_FALSE;
 186 
 187         /* get thread name (info) */
 188         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(allThreadsList[i], &threadInfo)))

 189             return NSK_FALSE;
 190 
 191         /* find by name */
 192         if (threadInfo.name != NULL) {
 193             for (j = 0; j < THREADS_KINDS; j++) {
 194                 if (strcmp(threadInfo.name, threadsName[j]) == 0) {
 195                     int k = threadsCounts[j];
 196                     if (k < threadsCount)
 197                         threadsList[j][k] = allThreadsList[i];
 198                     threadsCounts[j]++;
 199                 }
 200             }
 201         }
 202     }
 203 
 204     /* deallocate all threads list */
 205     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)allThreadsList)))

 206         return NSK_FALSE;
 207 
 208     /* check if all tested threads found */
 209     notfound = 0;
 210     for (i = 0; i < THREADS_KINDS; i++) {
 211         if (threadsCounts[i] != threadsCount) {
 212             NSK_COMPLAIN3("Found unexpected number of tested threads (%s):\n"
 213                             "#   found:    %d\n"
 214                             "#   expected: %d\n",
 215                             threadsName[i], threadsCounts[i], threadsCount);
 216             nsk_jvmti_setFailStatus();
 217             notfound++;
 218         }
 219     }
 220 
 221     if (notfound > 0)
 222         return NSK_FALSE;
 223 
 224     /* make global refs */
 225     for (i = 0; i < THREADS_KINDS; i++) {
 226         for (j = 0; j < threadsCount; j++) {
 227             if (!NSK_JNI_VERIFY(jni, (threadsList[i][j] =
 228                     jni->NewGlobalRef(threadsList[i][j])) != NULL))
 229                 return NSK_FALSE;
 230         }
 231     }
 232 
 233     return NSK_TRUE;
 234 }
 235 
 236 /**
 237  * Suspend or resume tested threads list.
 238  */
 239 static int suspendThreadsList(int suspend) {
 240     jlong resultsSize = threadsCount * sizeof(jvmtiError);
 241     jvmtiError* results = NULL;
 242     const char* kind = (suspend ? "suspending" : "resuming");
 243     int i, j;
 244 
 245     /* allocate results array */
 246     if (!NSK_JVMTI_VERIFY(jvmti->Allocate(resultsSize, (unsigned char**)&results))) {

 247         nsk_jvmti_setFailStatus();
 248         return NSK_FALSE;
 249     }
 250 
 251     for (i = 0; i < THREADS_KINDS; i++) {
 252         /* suspend or resume threads list */
 253         if (suspend) {
 254             if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(threadsCount, threadsList[i], results)))


 255                 nsk_jvmti_setFailStatus();
 256         } else {
 257             if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(threadsCount, threadsList[i], results)))


 258                 nsk_jvmti_setFailStatus();
 259         }
 260 
 261         /* check results */
 262         for (j = 0; j < threadsCount; j++) {
 263             if (results[j] != JVMTI_ERROR_NONE) {
 264                 NSK_COMPLAIN5("Unexpected result of %s thread #%d (%s):\n"
 265                                 "#   got result: %s (%d)\n",
 266                                 kind, j, threadsName[i],
 267                                 TranslateError(results[j]), (int)results[j]);
 268                 nsk_jvmti_setFailStatus();
 269             }
 270         }
 271     }
 272 
 273     /* deallocate results array */
 274     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)results))) {

 275         nsk_jvmti_setFailStatus();
 276     }
 277 
 278     return NSK_TRUE;
 279 }
 280 
 281 /**
 282  * Suspend or resume tested threads individually.
 283  */
 284 static int suspendThreadsIndividually(int suspend) {
 285     int i, j;
 286 
 287     for (i = 0; i < THREADS_KINDS; i++) {
 288         for (j = 0; j < threadsCount; j++) {
 289             if (suspend) {
 290                 NSK_DISPLAY2("    suspend thread #%d (%s)\n", j, threadsName[i]);
 291                 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(threadsList[i][j])))

 292                     nsk_jvmti_setFailStatus();
 293             } else {
 294                 NSK_DISPLAY2("    resume thread #%d (%s)\n", j, threadsName[i]);
 295                 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(threadsList[i][j])))

 296                     nsk_jvmti_setFailStatus();
 297             }
 298         }
 299     }
 300     return NSK_TRUE;
 301 }
 302 
 303 /**
 304  * Testcase: check tested threads
 305  *    - get thread state and state
 306  *    - wait for WAITIME if state is not expected
 307  *    - check if thread state is as expected
 308  *
 309  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 310  */
 311 static int checkThreads(int suspended, const char* kind, jlong timeout) {
 312     int i, j;
 313 
 314     /* check each thread */
 315     for (i = 0; i < THREADS_KINDS; i++) {
 316         for (j = 0; j < threadsCount; j++) {
 317             jint state = JVMTI_THREAD_STATE_NOT_STARTED;
 318             jlong t = 0;
 319 
 320             NSK_DISPLAY2("    thread #%d (%s):\n", j, threadsName[i]);
 321 
 322             /* get thread state */
 323             if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threadsList[i][j], &state))) {

 324                 nsk_jvmti_setFailStatus();
 325                 return NSK_FALSE;
 326             }
 327 
 328             NSK_DISPLAY2("        state  = %s (%d)\n",
 329                                         TranslateState(state), (int)state);
 330 
 331             /* check SUSPENDED state */
 332             if (suspended) {
 333                 if (!(state & JVMTI_THREAD_STATE_SUSPENDED)) {
 334                     NSK_COMPLAIN5("No SUSPENDED state for %s thread #%d (%s):\n"
 335                                     "#    got flags: %s (%d)\n",
 336                                     kind, j, threadsName[i],
 337                                     TranslateState(state), (int)state);
 338                     nsk_jvmti_setFailStatus();
 339                 }
 340             } else {
 341                 if (state & JVMTI_THREAD_STATE_SUSPENDED) {
 342                     NSK_COMPLAIN5("Unexpected SUSPENDED state for %s thread #%d (%s):\n"
 343                                     "#   got flags: %s (%d)\n",


 346                     nsk_jvmti_setFailStatus();
 347                 }
 348             }
 349         }
 350     }
 351 
 352     /* test may continue */
 353     return NSK_TRUE;
 354 }
 355 
 356 /**
 357  * Clean data:
 358  *   - dispose global references to tested threads
 359  */
 360 static int clean() {
 361     int i, j;
 362 
 363     /* dispose global references to threads */
 364     for (i = 0; i < THREADS_KINDS; i++) {
 365         for (j = 0; j < threadsCount; j++) {
 366             NSK_TRACE(jni->DeleteGlobalRef(threadsList[i][j]));
 367         }
 368     }
 369 
 370     /* deallocate memory */
 371     for (i = 0; i < THREADS_KINDS; i++) {
 372         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threadsList[i])))

 373             return NSK_FALSE;
 374         threadsList[i] = NULL;
 375     }
 376 
 377     return NSK_TRUE;
 378 }
 379 
 380 /* ============================================================================= */
 381 
 382 static volatile int testedThreadRunning = NSK_FALSE;
 383 static volatile int testedThreadShouldFinish = NSK_FALSE;
 384 
 385 /** Native running method in tested thread. */
 386 JNIEXPORT void JNICALL
 387 Java_nsk_jvmti_scenarios_sampling_SP03_sp03t002ThreadRunningNative_nativeMethod(JNIEnv* jni,
 388                                                                                      jobject obj) {
 389     volatile int i = 0, n = 1000;
 390 
 391     /* run in a loop */
 392     testedThreadRunning = NSK_TRUE;


 437     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 438         return JNI_ERR;
 439 
 440     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 441 
 442     /* get number of threads for each kind */
 443     threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_NUMBER);
 444     if (!NSK_VERIFY(threadsCount > 0))
 445         return JNI_ERR;
 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     {
 454         jvmtiCapabilities suspendCaps;
 455         memset(&suspendCaps, 0, sizeof(suspendCaps));
 456         suspendCaps.can_suspend = 1;
 457         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&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 }
< prev index next >