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 suspendThreadsIndividually(int suspend) {
223 int i;
224
225 for (i = 0; i < THREADS_COUNT; i++) {
226 if (suspend) {
227 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsName[i]);
228 if (!NSK_JVMTI_VERIFY(
229 NSK_CPP_STUB2(SuspendThread, jvmti, threadsList[i])))
230 nsk_jvmti_setFailStatus();
231 } else {
232 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsName[i]);
233 if (!NSK_JVMTI_VERIFY(
234 NSK_CPP_STUB2(ResumeThread, jvmti, threadsList[i])))
235 nsk_jvmti_setFailStatus();
236 }
237 }
238 return NSK_TRUE;
239 }
240
241 /**
242 * Testcase: check tested threads
243 * - get thread state
244 * - wait for WAITIME if state is not expected
245 * - check if thread state is as expected
246 *
247 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
248 */
249 static int checkThreads(int suspended, const char* kind, jlong timeout) {
250 int i;
251
252 /* check each thread */
253 for (i = 0; i < THREADS_COUNT; i++) {
254 jthread thread = threadsList[i];
255 jint state = JVMTI_THREAD_STATE_NOT_STARTED;
256 jlong t = 0;
257
258 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsName[i]);
259
260 /* wait for WAITTIME for thread to reach expected state */
261 do {
262 /* get thread state */
263 if (!NSK_JVMTI_VERIFY(
264 NSK_CPP_STUB3(GetThreadState, jvmti, threadsList[i], &state))) {
265 nsk_jvmti_setFailStatus();
266 return NSK_TRUE;
267 }
268
269 /* break if state is NOT_STARTED as expected */
270 if (threadsState[i] == JVMTI_THREAD_STATE_NOT_STARTED
271 && state == threadsState[i])
272 break;
273
274 /* break if state is as expected */
275 if ((state & threadsState[i]) != 0)
276 break;
277
278 /* wait for one second */
279 nsk_jvmti_sleep(1000);
280 t += 1000;
281
282 } while (t < timeout);
283
284 /* display thread state */
354 kind, i, threadsName[i],
355 TranslateState(state), (int)state);
356 nsk_jvmti_setFailStatus();
357 }
358 }
359 }
360
361 /* test may continue */
362 return NSK_TRUE;
363 }
364
365 /**
366 * Clean data:
367 * - dispose global references to tested threads
368 */
369 static int clean() {
370 int i;
371
372 /* dispose global references to threads */
373 for (i = 0; i < THREADS_COUNT; i++) {
374 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsList[i]));
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_SP01_sp01t002ThreadRunningNative_nativeMethod(JNIEnv* jni,
388 jobject obj) {
389 volatile int i = 0, n = 1000;
390
391 /* run in a loop */
392 testedThreadRunning = NSK_TRUE;
393 while (!testedThreadShouldFinish) {
394 if (n <= 0)
432 }
433 #endif
434 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
435
436 /* init framework and parse options */
437 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
438 return JNI_ERR;
439
440 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
441
442 /* create JVMTI environment */
443 if (!NSK_VERIFY((jvmti =
444 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
445 return JNI_ERR;
446
447 /* add specific capabilities for suspending thread */
448 {
449 jvmtiCapabilities suspendCaps;
450 memset(&suspendCaps, 0, sizeof(suspendCaps));
451 suspendCaps.can_suspend = 1;
452 if (!NSK_JVMTI_VERIFY(
453 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
454 return JNI_ERR;
455 }
456
457 /* register agent proc and arg */
458 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
459 return JNI_ERR;
460
461 return JNI_OK;
462 }
463
464 /* ============================================================================= */
465
466 }
|
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 suspendThreadsIndividually(int suspend) {
219 int i;
220
221 for (i = 0; i < THREADS_COUNT; i++) {
222 if (suspend) {
223 NSK_DISPLAY2(" suspend thread #%d (%s)\n", i, threadsName[i]);
224 if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(threadsList[i])))
225 nsk_jvmti_setFailStatus();
226 } else {
227 NSK_DISPLAY2(" resume thread #%d (%s)\n", i, threadsName[i]);
228 if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(threadsList[i])))
229 nsk_jvmti_setFailStatus();
230 }
231 }
232 return NSK_TRUE;
233 }
234
235 /**
236 * Testcase: check tested threads
237 * - get thread state
238 * - wait for WAITIME if state is not expected
239 * - check if thread state is as expected
240 *
241 * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
242 */
243 static int checkThreads(int suspended, const char* kind, jlong timeout) {
244 int i;
245
246 /* check each thread */
247 for (i = 0; i < THREADS_COUNT; i++) {
248 jthread thread = threadsList[i];
249 jint state = JVMTI_THREAD_STATE_NOT_STARTED;
250 jlong t = 0;
251
252 NSK_DISPLAY2(" thread #%d (%s):\n", i, threadsName[i]);
253
254 /* wait for WAITTIME for thread to reach expected state */
255 do {
256 /* get thread state */
257 if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threadsList[i], &state))) {
258 nsk_jvmti_setFailStatus();
259 return NSK_TRUE;
260 }
261
262 /* break if state is NOT_STARTED as expected */
263 if (threadsState[i] == JVMTI_THREAD_STATE_NOT_STARTED
264 && state == threadsState[i])
265 break;
266
267 /* break if state is as expected */
268 if ((state & threadsState[i]) != 0)
269 break;
270
271 /* wait for one second */
272 nsk_jvmti_sleep(1000);
273 t += 1000;
274
275 } while (t < timeout);
276
277 /* display thread state */
347 kind, i, threadsName[i],
348 TranslateState(state), (int)state);
349 nsk_jvmti_setFailStatus();
350 }
351 }
352 }
353
354 /* test may continue */
355 return NSK_TRUE;
356 }
357
358 /**
359 * Clean data:
360 * - dispose global references to tested threads
361 */
362 static int clean() {
363 int i;
364
365 /* dispose global references to threads */
366 for (i = 0; i < THREADS_COUNT; i++) {
367 NSK_TRACE(jni->DeleteGlobalRef(threadsList[i]));
368 }
369
370 return NSK_TRUE;
371 }
372
373 /* ============================================================================= */
374
375 static volatile int testedThreadRunning = NSK_FALSE;
376 static volatile int testedThreadShouldFinish = NSK_FALSE;
377
378 /** Native running method in tested thread. */
379 JNIEXPORT void JNICALL
380 Java_nsk_jvmti_scenarios_sampling_SP01_sp01t002ThreadRunningNative_nativeMethod(JNIEnv* jni,
381 jobject obj) {
382 volatile int i = 0, n = 1000;
383
384 /* run in a loop */
385 testedThreadRunning = NSK_TRUE;
386 while (!testedThreadShouldFinish) {
387 if (n <= 0)
425 }
426 #endif
427 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
428
429 /* init framework and parse options */
430 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
431 return JNI_ERR;
432
433 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
434
435 /* create JVMTI environment */
436 if (!NSK_VERIFY((jvmti =
437 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
438 return JNI_ERR;
439
440 /* add specific capabilities for suspending thread */
441 {
442 jvmtiCapabilities suspendCaps;
443 memset(&suspendCaps, 0, sizeof(suspendCaps));
444 suspendCaps.can_suspend = 1;
445 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))
446 return JNI_ERR;
447 }
448
449 /* register agent proc and arg */
450 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
451 return JNI_ERR;
452
453 return JNI_OK;
454 }
455
456 /* ============================================================================= */
457
458 }
|