Intel PCM: Obtaining the Same BasicCounterType each time
I am trying to call the PCM library version 1.7 from java using JNI. The idea is simply as following:
1. Call the PCM to get BaiscCounterType(CoreCounter, Socketcounter,SystemCounter) 2. Run some Java Code 3. Call the PCM again to get the BasicCounter 4. Call the PCM to measure the diffrence. (getCyclesLostDueL2CacheMisses,getCyclesLostDueL3CacheMisses)
Now, the problem is that I get the same BasicCounterType in step 1 and 3 which causes invalid results in step 4.
The JNI C++ code methods are as following:
1. Here is the method I use to get the BasicCounterType (CoreCounter). This method calls the getSystemCounterState() in cpucounters.cpp. Please note that I return the pointer address of the counter only.
by "return (long)&getSystemCounterState();" you are returning address of a temporary object (SystemCounterState) returned by getSystemCounterState() function. In C++ using adresses of temporary objects is a bad idea: the object basically does not exist after your "return" anymore. You see the same pointer address value because the same memory region is used to store the temporary object by the compiler/runtime system.
Instead, you should copy not the address but the whole content of returned SystemCounterState object to Java and use these copies as getCyclesLostDueL3CacheMisses parameters (pointers to them).
thanks for your help. I reinstalled the msr again and it worked fine with me.
I do have another question about the program() method can I ask it here or open a new thread?
I noticed that the number of PCM instance is increasing with each call. my question is should I call the program() method only one time, or call it any time then cleanup() after, or there is no need to call program() at all?
you should call program() to initialize PCM once in the beginning of your program runtime and call cleanup() when you do not need PCM anymore in your program (e.g. before your program exits). Then the count of PCM instances should not increase each time.In thepcm.x example, theprogram() call is calledoncein the very beginning and cleanup() is called once on exit (in the kill and Ctrl-C handler).