138 * - clean threads list
139 * - get all live threads
140 * - get threads name
141 * - find tested threads
142 * - make global refs
143 */
144 static int prepare() {
145 jthread *allThreadsList = NULL;
146 jint allThreadsCount = 0;
147 int found = 0;
148 int i;
149
150 NSK_DISPLAY1("Prepare: find tested threads: %d\n", THREADS_COUNT);
151
152 /* clean threads list */
153 for (i = 0; i < THREADS_COUNT; i++) {
154 threadsList[i] = NULL;
155 }
156
157 /* get all live threads */
158 if (!NSK_JVMTI_VERIFY(
159 NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList)))
160 return NSK_FALSE;
161
162 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
163 return NSK_FALSE;
164
165 /* find tested threads */
166 for (i = 0; i < allThreadsCount; i++) {
167 jvmtiThreadInfo threadInfo;
168
169 if (!NSK_VERIFY(allThreadsList[i] != NULL))
170 return NSK_FALSE;
171
172 /* get thread name (info) */
173 if (!NSK_JVMTI_VERIFY(
174 NSK_CPP_STUB3(GetThreadInfo, jvmti, allThreadsList[i], &threadInfo)))
175 return NSK_FALSE;
176
177 /* find by name */
178 if (threadInfo.name != NULL) {
179 int j;
180
181 for (j = 0; j < THREADS_COUNT; j++) {
182 if (strcmp(threadInfo.name, threadsName[j]) == 0) {
183 threadsList[j] = allThreadsList[i];
184 NSK_DISPLAY3(" thread #%d (%s): %p\n",
185 j, threadInfo.name, (void*)threadsList[j]);
186 }
187 }
188 }
189 }
190
191 /* deallocate all threads list */
192 if (!NSK_JVMTI_VERIFY(
193 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList)))
194 return NSK_FALSE;
195
196 /* check if all tested threads found */
197 found = 0;
198 for (i = 0; i < THREADS_COUNT; i++) {
199 if (threadsList[i] == NULL) {
200 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsName[i]);
201 } else {
202 found++;
203 }
204 }
205
206 if (found < THREADS_COUNT)
207 return NSK_FALSE;
208
209 /* make global refs */
210 for (i = 0; i < THREADS_COUNT; i++) {
211 if (!NSK_JNI_VERIFY(jni, (threadsList[i] =
212 NSK_CPP_STUB2(NewGlobalRef, jni, threadsList[i])) != NULL))
213 return NSK_FALSE;
214 }
215
216 return NSK_TRUE;
217 }
218
219 /**
220 * Suspend or resume tested threads.
221 */
222 static int suspendThreadsList(int suspend) {
223 jvmtiError results[THREADS_COUNT];
224 const char* kind = (suspend ? "suspending" : "resuming");
225 int i;
226
227 /* suspend or resume threads list */
228 if (suspend) {
229 if (!NSK_JVMTI_VERIFY(
230 NSK_CPP_STUB4(SuspendThreadList, jvmti, THREADS_COUNT,
231 threadsList, results)))
232 nsk_jvmti_setFailStatus();
233 } else {
234 if (!NSK_JVMTI_VERIFY(
235 NSK_CPP_STUB4(ResumeThreadList, jvmti, THREADS_COUNT,
236 threadsList, results)))
237 nsk_jvmti_setFailStatus();
238 }
239
240 /* check results */
241 for (i = 0; i < THREADS_COUNT; i++) {
242 if (results[i] != JVMTI_ERROR_NONE) {
243 NSK_COMPLAIN5("Unexpected result of %s thread #%d (%s):\n"
244 "# got result: %s (%d)\n",
245 kind, i, threadsName[i],
246 TranslateError(results[i]), (int)results[i]);
247 nsk_jvmti_setFailStatus();
248 }
249 }
250
251 return NSK_TRUE;
252 }
253
254 /**
255 * Testcase: check tested threads
256 * - get thread state and state flag
257 * - wait for WAITIME if state is not expected
258 * - check if thread state is as expected
259 *
260 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
261 */
262 static int checkThreads(int suspended, const char* kind, jlong timeout) {
263 int i;
264
265 /* check each thread */
266 for (i = 0; i < THREADS_COUNT; i++) {
267 jthread thread = threadsList[i];
268 jint state = JVMTI_THREAD_STATE_NOT_STARTED;
269 jlong t = 0;
270
271 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsName[i]);
272
273 /* wait for WAITTIME for thread to reach expected state */
274 do {
275 /* get thread status */
276 if (!NSK_JVMTI_VERIFY(
277 NSK_CPP_STUB3(GetThreadState, jvmti, threadsList[i], &state))) {
278 nsk_jvmti_setFailStatus();
279 return NSK_TRUE;
280 }
281
282 /* break if state is NOT_STARTED as expected */
283 if (threadsState[i] == JVMTI_THREAD_STATE_NOT_STARTED
284 && state == threadsState[i])
285 break;
286
287 /* break if state is as expected */
288 if ((state & threadsState[i]) != 0)
289 break;
290
291 /* wait for one second */
292 nsk_jvmti_sleep(1000);
293 t += 1000;
294
295 } while (t < timeout);
296
297 /* display thread status */
367 kind, i, threadsName[i],
368 TranslateState(state), (int)state);
369 nsk_jvmti_setFailStatus();
370 }
371 }
372 }
373
374 /* test may continue */
375 return NSK_TRUE;
376 }
377
378 /**
379 * Clean data:
380 * - dispose global references to tested threads
381 */
382 static int clean() {
383 int i;
384
385 /* dispose global references to threads */
386 for (i = 0; i < THREADS_COUNT; i++) {
387 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsList[i]));
388 }
389
390 return NSK_TRUE;
391 }
392
393 /* ============================================================================= */
394
395 static volatile int testedThreadRunning = NSK_FALSE;
396 static volatile int testedThreadShouldFinish = NSK_FALSE;
397
398 /** Native running method in tested thread. */
399 JNIEXPORT void JNICALL
400 Java_nsk_jvmti_scenarios_sampling_SP01_sp01t003ThreadRunningNative_nativeMethod(JNIEnv* jni,
401 jobject obj) {
402 volatile int i = 0, n = 1000;
403
404 /* run in a loop */
405 testedThreadRunning = NSK_TRUE;
406 while (!testedThreadShouldFinish) {
407 if (n <= 0)
445 }
446 #endif
447 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
448
449 /* init framework and parse options */
450 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
451 return JNI_ERR;
452
453 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
454
455 /* create JVMTI environment */
456 if (!NSK_VERIFY((jvmti =
457 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
458 return JNI_ERR;
459
460 /* add specific capabilities for suspending thread */
461 {
462 jvmtiCapabilities suspendCaps;
463 memset(&suspendCaps, 0, sizeof(suspendCaps));
464 suspendCaps.can_suspend = 1;
465 if (!NSK_JVMTI_VERIFY(
466 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
467 return JNI_ERR;
468 }
469
470 /* register agent proc and arg */
471 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
472 return JNI_ERR;
473
474 return JNI_OK;
475 }
476
477 /* ============================================================================= */
478
479 }
|
138 * - clean threads list
139 * - get all live threads
140 * - get threads name
141 * - find tested threads
142 * - make global refs
143 */
144 static int prepare() {
145 jthread *allThreadsList = NULL;
146 jint allThreadsCount = 0;
147 int found = 0;
148 int i;
149
150 NSK_DISPLAY1("Prepare: find tested threads: %d\n", THREADS_COUNT);
151
152 /* clean threads list */
153 for (i = 0; i < THREADS_COUNT; i++) {
154 threadsList[i] = NULL;
155 }
156
157 /* get all live threads */
158 if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&allThreadsCount, &allThreadsList)))
159 return NSK_FALSE;
160
161 if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
162 return NSK_FALSE;
163
164 /* find tested threads */
165 for (i = 0; i < allThreadsCount; i++) {
166 jvmtiThreadInfo threadInfo;
167
168 if (!NSK_VERIFY(allThreadsList[i] != NULL))
169 return NSK_FALSE;
170
171 /* get thread name (info) */
172 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(allThreadsList[i], &threadInfo)))
173 return NSK_FALSE;
174
175 /* find by name */
176 if (threadInfo.name != NULL) {
177 int j;
178
179 for (j = 0; j < THREADS_COUNT; j++) {
180 if (strcmp(threadInfo.name, threadsName[j]) == 0) {
181 threadsList[j] = allThreadsList[i];
182 NSK_DISPLAY3(" thread #%d (%s): %p\n",
183 j, threadInfo.name, (void*)threadsList[j]);
184 }
185 }
186 }
187 }
188
189 /* deallocate all threads list */
190 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)allThreadsList)))
191 return NSK_FALSE;
192
193 /* check if all tested threads found */
194 found = 0;
195 for (i = 0; i < THREADS_COUNT; i++) {
196 if (threadsList[i] == NULL) {
197 NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsName[i]);
198 } else {
199 found++;
200 }
201 }
202
203 if (found < THREADS_COUNT)
204 return NSK_FALSE;
205
206 /* make global refs */
207 for (i = 0; i < THREADS_COUNT; i++) {
208 if (!NSK_JNI_VERIFY(jni, (threadsList[i] = jni->NewGlobalRef(threadsList[i])) != NULL))
209 return NSK_FALSE;
210 }
211
212 return NSK_TRUE;
213 }
214
215 /**
216 * Suspend or resume tested threads.
217 */
218 static int suspendThreadsList(int suspend) {
219 jvmtiError results[THREADS_COUNT];
220 const char* kind = (suspend ? "suspending" : "resuming");
221 int i;
222
223 /* suspend or resume threads list */
224 if (suspend) {
225 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(THREADS_COUNT, threadsList, results)))
226 nsk_jvmti_setFailStatus();
227 } else {
228 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(THREADS_COUNT, threadsList, results)))
229 nsk_jvmti_setFailStatus();
230 }
231
232 /* check results */
233 for (i = 0; i < THREADS_COUNT; i++) {
234 if (results[i] != JVMTI_ERROR_NONE) {
235 NSK_COMPLAIN5("Unexpected result of %s thread #%d (%s):\n"
236 "# got result: %s (%d)\n",
237 kind, i, threadsName[i],
238 TranslateError(results[i]), (int)results[i]);
239 nsk_jvmti_setFailStatus();
240 }
241 }
242
243 return NSK_TRUE;
244 }
245
246 /**
247 * Testcase: check tested threads
248 * - get thread state and state flag
249 * - wait for WAITIME if state is not expected
250 * - check if thread state is as expected
251 *
252 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
253 */
254 static int checkThreads(int suspended, const char* kind, jlong timeout) {
255 int i;
256
257 /* check each thread */
258 for (i = 0; i < THREADS_COUNT; i++) {
259 jthread thread = threadsList[i];
260 jint state = JVMTI_THREAD_STATE_NOT_STARTED;
261 jlong t = 0;
262
263 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsName[i]);
264
265 /* wait for WAITTIME for thread to reach expected state */
266 do {
267 /* get thread status */
268 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threadsList[i], &state))) {
269 nsk_jvmti_setFailStatus();
270 return NSK_TRUE;
271 }
272
273 /* break if state is NOT_STARTED as expected */
274 if (threadsState[i] == JVMTI_THREAD_STATE_NOT_STARTED
275 && state == threadsState[i])
276 break;
277
278 /* break if state is as expected */
279 if ((state & threadsState[i]) != 0)
280 break;
281
282 /* wait for one second */
283 nsk_jvmti_sleep(1000);
284 t += 1000;
285
286 } while (t < timeout);
287
288 /* display thread status */
358 kind, i, threadsName[i],
359 TranslateState(state), (int)state);
360 nsk_jvmti_setFailStatus();
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;
375
376 /* dispose global references to threads */
377 for (i = 0; i < THREADS_COUNT; i++) {
378 NSK_TRACE(jni->DeleteGlobalRef(threadsList[i]));
379 }
380
381 return NSK_TRUE;
382 }
383
384 /* ============================================================================= */
385
386 static volatile int testedThreadRunning = NSK_FALSE;
387 static volatile int testedThreadShouldFinish = NSK_FALSE;
388
389 /** Native running method in tested thread. */
390 JNIEXPORT void JNICALL
391 Java_nsk_jvmti_scenarios_sampling_SP01_sp01t003ThreadRunningNative_nativeMethod(JNIEnv* jni,
392 jobject obj) {
393 volatile int i = 0, n = 1000;
394
395 /* run in a loop */
396 testedThreadRunning = NSK_TRUE;
397 while (!testedThreadShouldFinish) {
398 if (n <= 0)
436 }
437 #endif
438 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
439
440 /* init framework and parse options */
441 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
442 return JNI_ERR;
443
444 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
445
446 /* create JVMTI environment */
447 if (!NSK_VERIFY((jvmti =
448 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
449 return JNI_ERR;
450
451 /* add specific capabilities for suspending thread */
452 {
453 jvmtiCapabilities suspendCaps;
454 memset(&suspendCaps, 0, sizeof(suspendCaps));
455 suspendCaps.can_suspend = 1;
456 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))
457 return JNI_ERR;
458 }
459
460 /* register agent proc and arg */
461 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
462 return JNI_ERR;
463
464 return JNI_OK;
465 }
466
467 /* ============================================================================= */
468
469 }
|