agent/qtcreator/src/agent.c
/*
 * Periodically samples the current state of all JVM threads we know about.
 */
Periodically samples the current state of all JVM threads we know about.
Periodically samples the current state of all JVM threads we know about .
static void* sampleThreadStates(void *ptr) 
{   
   jint err;
   JNIEnv *env;
   JavaVMAttachArgs attach_args = {JNI_VERSION_1_2, SAMPLING_THREAD_NAME ,NULL};
   usleep(1000*1000); 
   startWriterThread(sampleBuffer,configuration.outputFile);
#ifdef DEBUG     
   printf("*** attaching sampling thread.\n"); 
#endif
   err = (*currentVM)->AttachCurrentThreadAsDaemon(currentVM,(void **) &env, &attach_args);
   if ( err != 0 ) {
        fprintf(stderr,"ERROR: FAILED to attach sampling thread.\n");        
        abort();
   } 
   if ( configuration.verboseMode ) {
    printf("INFO: Ready to sample thread states, ring buffer size: %d events \n",SAMPLE_RINGBUFFER_SIZE);
   }
  (*currentVM)->GetEnv(currentVM, (void**) &samplingThreadJvmti, JVMTI_VERSION_1_0);   
   while( ! terminateSamplingThread ) 
   {
     visitThreadList( &queryThreadState ); 
     __sync_synchronize(); 
     delayLoop( &configuration );
   }
#ifdef DEBUG   
   printf("*** Sampling thread detached, terminating.\n");    
#endif
   (*currentVM)->DetachCurrentThread(currentVM);   
   return NULL;
}
static void * sampleThreadStates ( void * ptr ) { jint err ; JNIEnv * env ; JavaVMAttachArgs attach_args = { JNI_VERSION_1_2 , SAMPLING_THREAD_NAME , NULL } ; usleep ( 1000 * 1000 ) ; startWriterThread ( sampleBuffer , configuration . outputFile ) ; #ifdef DEBUG printf ( " \n " ) ; #endif err = ( * currentVM ) -> AttachCurrentThreadAsDaemon ( currentVM , ( void * * ) & env , & attach_args ) ; if ( err != 0 ) { fprintf ( stderr , " \n " ) ; abort ( ) ; } if ( configuration . verboseMode ) { printf ( " \n " , SAMPLE_RINGBUFFER_SIZE ) ; } ( * currentVM ) -> GetEnv ( currentVM , ( void * * ) & samplingThreadJvmti , JVMTI_VERSION_1_0 ) ; while ( ! terminateSamplingThread ) { visitThreadList ( & queryThreadState ) ; __sync_synchronize ( ) ; delayLoop ( & configuration ) ; } #ifdef DEBUG printf ( " \n " ) ; #endif ( * currentVM ) -> DetachCurrentThread ( currentVM ) ; return NULL ; }
Periodically samples the current state of all JVM threads we know about.
Periodically samples the current state of all JVM threads we know about .
// the next line is actually a hack to work around a race-condition // when this thread starts to run although the thread that spawned it // has not yet left the onVMStart() method // TODO: Get rid of using a Java native monitor , use a pthread_mutex to protected JNI callback methods // 1 second