141 * Prepare data:
142 * - clean threads list
143 * - get all live threads
144 * - get threads name
145 * - find tested threads
146 * - make global refs
147 */
148 static int prepare() {
149 jthread *allThreadsList = NULL;
150 jint allThreadsCount = 0;
151 int notfound = 0;
152 int i, j;
153
154 NSK_DISPLAY1("Prepare: find tested threads: %d kinds\n", THREADS_KINDS);
155
156 /* allocate and clean threads list */
157 for (i = 0; i < THREADS_KINDS; i++) {
158 threadsCounts[i] = 0;
159 threadsList[i] = NULL;
160
161 if (!NSK_JVMTI_VERIFY(
162 NSK_CPP_STUB3(Allocate, jvmti, (threadsCount * sizeof(jthread)),
163 (unsigned char**)&threadsList[i])))
164 return NSK_FALSE;
165
166 for (j = 0; j < threadsCount; j++) {
167 threadsList[i][j] = NULL;
168 }
169 }
170
171 /* get all live threads */
172 if (!NSK_JVMTI_VERIFY(
173 NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList)))
174 return NSK_FALSE;
175
176 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
177 return NSK_FALSE;
178
179 /* find tested threads */
180 for (i = 0; i < allThreadsCount; i++) {
181 jvmtiThreadInfo threadInfo;
182
183 if (!NSK_VERIFY(allThreadsList[i] != NULL))
184 return NSK_FALSE;
185
186 /* get thread name (info) */
187 if (!NSK_JVMTI_VERIFY(
188 NSK_CPP_STUB3(GetThreadInfo, jvmti, 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(
206 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList)))
207 return NSK_FALSE;
208
209 /* check if all tested threads found */
210 notfound = 0;
211 for (i = 0; i < THREADS_KINDS; i++) {
212 if (threadsCounts[i] != threadsCount) {
213 NSK_COMPLAIN3("Found unexpected number of tested threads (%s):\n"
214 "# found: %d\n"
215 "# expected: %d\n",
216 threadsName[i], threadsCounts[i], threadsCount);
217 nsk_jvmti_setFailStatus();
218 notfound++;
219 }
220 }
221
222 if (notfound > 0)
223 return NSK_FALSE;
224
225 /* make global refs */
226 for (i = 0; i < THREADS_KINDS; i++) {
227 for (j = 0; j < threadsCount; j++) {
228 if (!NSK_JNI_VERIFY(jni, (threadsList[i][j] =
229 NSK_CPP_STUB2(NewGlobalRef, jni, threadsList[i][j])) != NULL))
230 return NSK_FALSE;
231 }
232 }
233
234 return NSK_TRUE;
235 }
236
237 /**
238 * Suspend or resume tested threads list.
239 */
240 static int suspendThreadsList(int suspend) {
241 jlong resultsSize = threadsCount * sizeof(jvmtiError);
242 jvmtiError* results = NULL;
243 const char* kind = (suspend ? "suspending" : "resuming");
244 int i, j;
245
246 /* allocate results array */
247 if (!NSK_JVMTI_VERIFY(
248 NSK_CPP_STUB3(Allocate, jvmti, resultsSize, (unsigned char**)&results))) {
249 nsk_jvmti_setFailStatus();
250 return NSK_FALSE;
251 }
252
253 for (i = 0; i < THREADS_KINDS; i++) {
254 /* suspend or resume threads list */
255 if (suspend) {
256 if (!NSK_JVMTI_VERIFY(
257 NSK_CPP_STUB4(SuspendThreadList, jvmti, threadsCount,
258 threadsList[i], results)))
259 nsk_jvmti_setFailStatus();
260 } else {
261 if (!NSK_JVMTI_VERIFY(
262 NSK_CPP_STUB4(ResumeThreadList, jvmti, threadsCount,
263 threadsList[i], results)))
264 nsk_jvmti_setFailStatus();
265 }
266
267 /* check results */
268 for (j = 0; j < threadsCount; j++) {
269 if (results[j] != JVMTI_ERROR_NONE) {
270 NSK_COMPLAIN5("Unexpected result of %s thread #%d (%s):\n"
271 "# got result: %s (%d)\n",
272 kind, j, threadsName[i],
273 TranslateError(results[j]), (int)results[j]);
274 nsk_jvmti_setFailStatus();
275 }
276 }
277 }
278
279 /* deallocate results array */
280 if (!NSK_JVMTI_VERIFY(
281 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)results))) {
282 nsk_jvmti_setFailStatus();
283 }
284
285 return NSK_TRUE;
286 }
287
288 /**
289 * Suspend or resume tested threads individually.
290 */
291 static int suspendThreadsIndividually(int suspend) {
292 int i, j;
293
294 for (i = 0; i < THREADS_KINDS; i++) {
295 for (j = 0; j < threadsCount; j++) {
296 if (suspend) {
297 NSK_DISPLAY2(" suspend thread #%d (%s)\n", j, threadsName[i]);
298 if (!NSK_JVMTI_VERIFY(
299 NSK_CPP_STUB2(SuspendThread, jvmti, threadsList[i][j])))
300 nsk_jvmti_setFailStatus();
301 } else {
302 NSK_DISPLAY2(" resume thread #%d (%s)\n", j, threadsName[i]);
303 if (!NSK_JVMTI_VERIFY(
304 NSK_CPP_STUB2(ResumeThread, jvmti, threadsList[i][j])))
305 nsk_jvmti_setFailStatus();
306 }
307 }
308 }
309 return NSK_TRUE;
310 }
311
312 /**
313 * Testcase: check tested threads
314 * - get thread state
315 * - wait for WAITIME if state is not expected
316 * - check if thread state is as expected
317 *
318 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
319 */
320 static int checkThreads(int suspended, const char* kind, jlong timeout) {
321 int i, j;
322
323 /* check each thread */
324 for (i = 0; i < THREADS_KINDS; i++) {
325 for (j = 0; j < threadsCount; j++) {
326 jint state = JVMTI_THREAD_STATE_NOT_STARTED;
327 jlong t = 0;
328
329 NSK_DISPLAY2(" thread #%d (%s):\n", j, threadsName[i]);
330
331 /* get thread state */
332 if (!NSK_JVMTI_VERIFY(
333 NSK_CPP_STUB3(GetThreadState, jvmti, threadsList[i][j], &state))) {
334 nsk_jvmti_setFailStatus();
335 return NSK_FALSE;
336 }
337
338 NSK_DISPLAY2(" state = %s (%d)\n",
339 TranslateState(state), (int)state);
340
341 /* check SUSPENDED state */
342 if (suspended) {
343 if (!(state & JVMTI_THREAD_STATE_SUSPENDED)) {
344 NSK_COMPLAIN5("No SUSPENDED state for %s thread #%d (%s):\n"
345 "# got flags: %s (%d)\n",
346 kind, j, threadsName[i],
347 TranslateState(state), (int)state);
348 nsk_jvmti_setFailStatus();
349 }
350 } else {
351 if (state & JVMTI_THREAD_STATE_SUSPENDED) {
352 NSK_COMPLAIN5("Unexpected SUSPENDED state for %s thread #%d (%s):\n"
353 "# got flags: %s (%d)\n",
356 nsk_jvmti_setFailStatus();
357 }
358 }
359 }
360 }
361
362 /* test may continue */
363 return NSK_TRUE;
364 }
365
366 /**
367 * Clean data:
368 * - dispose global references to tested threads
369 */
370 static int clean() {
371 int i, j;
372
373 /* dispose global references to threads */
374 for (i = 0; i < THREADS_KINDS; i++) {
375 for (j = 0; j < threadsCount; j++) {
376 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsList[i][j]));
377 }
378 }
379
380 /* deallocate memory */
381 for (i = 0; i < THREADS_KINDS; i++) {
382 if (!NSK_JVMTI_VERIFY(
383 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threadsList[i])))
384 return NSK_FALSE;
385 threadsList[i] = NULL;
386 }
387
388 return NSK_TRUE;
389 }
390
391 /* ============================================================================= */
392
393 static volatile int testedThreadRunning = NSK_FALSE;
394 static volatile int testedThreadShouldFinish = NSK_FALSE;
395
396 /** Native running method in tested thread. */
397 JNIEXPORT void JNICALL
398 Java_nsk_jvmti_scenarios_sampling_SP04_sp04t001ThreadRunningNative_nativeMethod(JNIEnv* jni,
399 jobject obj) {
400 volatile int i = 0, n = 1000;
401
402 /* run in a loop */
403 testedThreadRunning = NSK_TRUE;
448 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
449 return JNI_ERR;
450
451 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
452
453 /* get number of threads for each kind */
454 threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_NUMBER);
455 if (!NSK_VERIFY(threadsCount > 0))
456 return JNI_ERR;
457
458 /* create JVMTI environment */
459 if (!NSK_VERIFY((jvmti =
460 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
461 return JNI_ERR;
462
463 /* add specific capabilities for suspending thread */
464 {
465 jvmtiCapabilities suspendCaps;
466 memset(&suspendCaps, 0, sizeof(suspendCaps));
467 suspendCaps.can_suspend = 1;
468 if (!NSK_JVMTI_VERIFY(
469 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
470 return JNI_ERR;
471 }
472
473 /* register agent proc and arg */
474 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
475 return JNI_ERR;
476
477 return JNI_OK;
478 }
479
480 /* ============================================================================= */
481
482 }
|
141 * Prepare data:
142 * - clean threads list
143 * - get all live threads
144 * - get threads name
145 * - find tested threads
146 * - make global refs
147 */
148 static int prepare() {
149 jthread *allThreadsList = NULL;
150 jint allThreadsCount = 0;
151 int notfound = 0;
152 int i, j;
153
154 NSK_DISPLAY1("Prepare: find tested threads: %d kinds\n", THREADS_KINDS);
155
156 /* allocate and clean threads list */
157 for (i = 0; i < THREADS_KINDS; i++) {
158 threadsCounts[i] = 0;
159 threadsList[i] = NULL;
160
161 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(threadsCount * sizeof(jthread),
162 (unsigned char**)&threadsList[i])))
163 return NSK_FALSE;
164
165 for (j = 0; j < threadsCount; j++) {
166 threadsList[i][j] = NULL;
167 }
168 }
169
170 /* get all live threads */
171 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&allThreadsCount, &allThreadsList)))
172 return NSK_FALSE;
173
174 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
175 return NSK_FALSE;
176
177 /* find tested threads */
178 for (i = 0; i < allThreadsCount; i++) {
179 jvmtiThreadInfo threadInfo;
180
181 if (!NSK_VERIFY(allThreadsList[i] != NULL))
182 return NSK_FALSE;
183
184 /* get thread name (info) */
185 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(allThreadsList[i], &threadInfo)))
186 return NSK_FALSE;
187
188 /* find by name */
189 if (threadInfo.name != NULL) {
190 for (j = 0; j < THREADS_KINDS; j++) {
191 if (strcmp(threadInfo.name, threadsName[j]) == 0) {
192 int k = threadsCounts[j];
193 if (k < threadsCount)
194 threadsList[j][k] = allThreadsList[i];
195 threadsCounts[j]++;
196 }
197 }
198 }
199 }
200
201 /* deallocate all threads list */
202 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)allThreadsList)))
203 return NSK_FALSE;
204
205 /* check if all tested threads found */
206 notfound = 0;
207 for (i = 0; i < THREADS_KINDS; i++) {
208 if (threadsCounts[i] != threadsCount) {
209 NSK_COMPLAIN3("Found unexpected number of tested threads (%s):\n"
210 "# found: %d\n"
211 "# expected: %d\n",
212 threadsName[i], threadsCounts[i], threadsCount);
213 nsk_jvmti_setFailStatus();
214 notfound++;
215 }
216 }
217
218 if (notfound > 0)
219 return NSK_FALSE;
220
221 /* make global refs */
222 for (i = 0; i < THREADS_KINDS; i++) {
223 for (j = 0; j < threadsCount; j++) {
224 if (!NSK_JNI_VERIFY(jni, (threadsList[i][j] =
225 jni->NewGlobalRef(threadsList[i][j])) != NULL))
226 return NSK_FALSE;
227 }
228 }
229
230 return NSK_TRUE;
231 }
232
233 /**
234 * Suspend or resume tested threads list.
235 */
236 static int suspendThreadsList(int suspend) {
237 jlong resultsSize = threadsCount * sizeof(jvmtiError);
238 jvmtiError* results = NULL;
239 const char* kind = (suspend ? "suspending" : "resuming");
240 int i, j;
241
242 /* allocate results array */
243 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(resultsSize, (unsigned char**)&results))) {
244 nsk_jvmti_setFailStatus();
245 return NSK_FALSE;
246 }
247
248 for (i = 0; i < THREADS_KINDS; i++) {
249 /* suspend or resume threads list */
250 if (suspend) {
251 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(threadsCount, threadsList[i], results)))
252 nsk_jvmti_setFailStatus();
253 } else {
254 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(threadsCount, threadsList[i], results)))
255 nsk_jvmti_setFailStatus();
256 }
257
258 /* check results */
259 for (j = 0; j < threadsCount; j++) {
260 if (results[j] != JVMTI_ERROR_NONE) {
261 NSK_COMPLAIN5("Unexpected result of %s thread #%d (%s):\n"
262 "# got result: %s (%d)\n",
263 kind, j, threadsName[i],
264 TranslateError(results[j]), (int)results[j]);
265 nsk_jvmti_setFailStatus();
266 }
267 }
268 }
269
270 /* deallocate results array */
271 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)results))) {
272 nsk_jvmti_setFailStatus();
273 }
274
275 return NSK_TRUE;
276 }
277
278 /**
279 * Suspend or resume tested threads individually.
280 */
281 static int suspendThreadsIndividually(int suspend) {
282 int i, j;
283
284 for (i = 0; i < THREADS_KINDS; i++) {
285 for (j = 0; j < threadsCount; j++) {
286 if (suspend) {
287 NSK_DISPLAY2(" suspend thread #%d (%s)\n", j, threadsName[i]);
288 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(threadsList[i][j])))
289 nsk_jvmti_setFailStatus();
290 } else {
291 NSK_DISPLAY2(" resume thread #%d (%s)\n", j, threadsName[i]);
292 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(threadsList[i][j])))
293 nsk_jvmti_setFailStatus();
294 }
295 }
296 }
297 return NSK_TRUE;
298 }
299
300 /**
301 * Testcase: check tested threads
302 * - get thread state
303 * - wait for WAITIME if state is not expected
304 * - check if thread state is as expected
305 *
306 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
307 */
308 static int checkThreads(int suspended, const char* kind, jlong timeout) {
309 int i, j;
310
311 /* check each thread */
312 for (i = 0; i < THREADS_KINDS; i++) {
313 for (j = 0; j < threadsCount; j++) {
314 jint state = JVMTI_THREAD_STATE_NOT_STARTED;
315 jlong t = 0;
316
317 NSK_DISPLAY2(" thread #%d (%s):\n", j, threadsName[i]);
318
319 /* get thread state */
320 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threadsList[i][j], &state))) {
321 nsk_jvmti_setFailStatus();
322 return NSK_FALSE;
323 }
324
325 NSK_DISPLAY2(" state = %s (%d)\n",
326 TranslateState(state), (int)state);
327
328 /* check SUSPENDED state */
329 if (suspended) {
330 if (!(state & JVMTI_THREAD_STATE_SUSPENDED)) {
331 NSK_COMPLAIN5("No SUSPENDED state for %s thread #%d (%s):\n"
332 "# got flags: %s (%d)\n",
333 kind, j, threadsName[i],
334 TranslateState(state), (int)state);
335 nsk_jvmti_setFailStatus();
336 }
337 } else {
338 if (state & JVMTI_THREAD_STATE_SUSPENDED) {
339 NSK_COMPLAIN5("Unexpected SUSPENDED state for %s thread #%d (%s):\n"
340 "# got flags: %s (%d)\n",
343 nsk_jvmti_setFailStatus();
344 }
345 }
346 }
347 }
348
349 /* test may continue */
350 return NSK_TRUE;
351 }
352
353 /**
354 * Clean data:
355 * - dispose global references to tested threads
356 */
357 static int clean() {
358 int i, j;
359
360 /* dispose global references to threads */
361 for (i = 0; i < THREADS_KINDS; i++) {
362 for (j = 0; j < threadsCount; j++) {
363 NSK_TRACE(jni->DeleteGlobalRef(threadsList[i][j]));
364 }
365 }
366
367 /* deallocate memory */
368 for (i = 0; i < THREADS_KINDS; i++) {
369 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threadsList[i])))
370 return NSK_FALSE;
371 threadsList[i] = NULL;
372 }
373
374 return NSK_TRUE;
375 }
376
377 /* ============================================================================= */
378
379 static volatile int testedThreadRunning = NSK_FALSE;
380 static volatile int testedThreadShouldFinish = NSK_FALSE;
381
382 /** Native running method in tested thread. */
383 JNIEXPORT void JNICALL
384 Java_nsk_jvmti_scenarios_sampling_SP04_sp04t001ThreadRunningNative_nativeMethod(JNIEnv* jni,
385 jobject obj) {
386 volatile int i = 0, n = 1000;
387
388 /* run in a loop */
389 testedThreadRunning = NSK_TRUE;
434 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
435 return JNI_ERR;
436
437 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
438
439 /* get number of threads for each kind */
440 threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_NUMBER);
441 if (!NSK_VERIFY(threadsCount > 0))
442 return JNI_ERR;
443
444 /* create JVMTI environment */
445 if (!NSK_VERIFY((jvmti =
446 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
447 return JNI_ERR;
448
449 /* add specific capabilities for suspending thread */
450 {
451 jvmtiCapabilities suspendCaps;
452 memset(&suspendCaps, 0, sizeof(suspendCaps));
453 suspendCaps.can_suspend = 1;
454 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))
455 return JNI_ERR;
456 }
457
458 /* register agent proc and arg */
459 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
460 return JNI_ERR;
461
462 return JNI_OK;
463 }
464
465 /* ============================================================================= */
466
467 }
|