getThreadCpuTime trouble

My target is getting informations about the cpu used by all the threads running in the VM. Printing out I always see the some value for the cpu-time... and it's equal to the max_value of the cpuTimeInfo struct...

I can't understand what is wrong... can you help me please?

here is some parts of code:

void JNICALL VMInitCallback(jvmtiEnv *jvmti_env,JNIEnv* jni_env,jthread thread){

pid_routine=fork();

if (!pid_routine)

routine_cpu();

}

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved){

(*vm)->GetEnv(vm, (void**)&env, JVMTI_VERSION_1_0);

memset(&clbk,0,sizeof(clbk));

memset(&cap,0,sizeof(cap));

cap.can_get_thread_cpu_time=1;

clbk.VMInit=&VMInitCallback;

(*env)->AddCapabilities(env,&cap);

(*env)->SetEventCallbacks(env,&clbk,sizeof(clbk));

(*env)->SetEventNotificationMode(env,(jint)JVMTI_ENABLE,JVMTI_EVENT_VM_INIT,NULL);

return JNI_OK;

}

void routine_cpu(){

.....................

while(count){

sleep(INT);

(*env)->GetAllThreads(env,&thread_count,&thread_list);

errors = malloc(thread_count*sizeof(jvmtiError));

memset(errors,0,thread_count*sizeof(jvmtiError));

(*env)->SuspendThreadList(env,thread_count,thread_list,&errors);

for (i=0;i<thread_count;i++)

if(errors!=JVMTI_ERROR_NONE)

printf("error occurred on suspending thread#%d %d\n",i,(jvmtiError)errors);

if(thread_list != NULL)

for(i=0;i<thread_count;i++){

(*env)->GetThreadInfo(env,thread_list,&info);

retval=(*env)->GetThreadCpuTime(env,thread_list,&value);

printf("thread %s\tvalue %Lu\n",info,(jlong)val);

}

(*env)->ResumeThreadList(env,thread_count,thread_list,errors);

count--;

}

exit(0);

}

-

and this is the output running a simple test program:

thread Reference Handlervalue 18446744073709551615state 401

thread mainvalue 18446744073709551615state 5

thread Finalizervalue 18446744073709551615state 401

thread Reference Handlervalue 18446744073709551615state 401

thread mainvalue 18446744073709551615state 5

thread Finalizervalue 18446744073709551615state 401

thread Reference Handlervalue 18446744073709551615state 401

thread mainvalue 18446744073709551615state 5

thread Finalizervalue 18446744073709551615state 401

...... and so on

--

[2564 byte] By [skioppettoa] at [2007-10-3 5:52:40]
# 1

Hello,

You provided just a part of your JVMTI agent, so it is hard

to understand the whole picture but anyway...

It looks like your code has at least two errors.

(1) The declaration of the GetThreadCpuTime() function is:

jvmtiError

GetThreadCpuTime(jvmtiEnv* env, jthread thread, jlong* nanos_ptr);

But the thread_list is passed to the function instead of a jthread id:

retval=(*env)->GetThreadCpuTime(env,thread_list,&value);

Probably, you forgot to dereference the thread_list array pointer.

With a correction it could look like this:

retval=(*env)->GetThreadCpuTime(env,*thread_list,&value);

(2) Also the value of the "val" variable is printed instead of the

"value" variable:

printf("thread %s\tvalue %Lu\n",info,(jlong)val);

I would expect it to be:

printf("thread %s\tvalue %Lu\n", info, value);

I don't see where the "value" variable is defined.

It probably makes sense to define it as jlong.

Then there is no need to make a cast: "(jlong)value".

Please, let me know if you still have questions.

Regards,

Serguei

ss45998a at 2007-7-15 0:32:58 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Correction:Probably, you need something like this:retval=(*env)->GetThreadCpuTime(env,thread_list[thread_count],&value);Regards,Serguei
ss45998a at 2007-7-15 0:32:58 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...