Hardware Performance Counters, libcpc, and Solaris 10
By User12610965-Oracle on Feb 10, 2011
I recently created a tutorial on using libcpc under Solaris 10. During my research I came across http://developers.sun.com/solaris/articles/optimizing_apps.html
This is an excellent article, but the sample application needed to be updated for Solaris 10. For my tutorial I updated the sample application.
The first interesting news is PAPI. see
PAPI proposes to solve one of the usability issues related to hw performance counters. Historically, one needed the chip architecture or programmers manual to determine what the available counters actually measured. With PAPI, generic events are defined that are valid across architectures. In OpenSolaris, there is a man page on generic_events. This man page explains the meaning of each generic event and maps generic events to chip specific events. This man page is useful for determining the chip specific events to use under Solaris 10, even though PAPI is not available under Solaris 10.
Here is an outline on how to use libcpc under Solaris 10 and OpenSolaris.
Open the library
rcw_cpc = cpc_open(cpuver);
If the open fails either hardware performance counters are not available or there is a library version mismatch.
create a cpc set
my_cpc_set = cpc_set_create(rcw_cpc);
define two events (Your hardware may support more)
my_event0 = "PAPI_tot_cyc";
my_event1 = "PAPI_tot_ins";
Add an event to my cpc set
i=cpc_set_add_request(rcw_cpc, my_cpc_set, my_event0, my_preset,CPC_COUNT_USER|CPC_COUNT_SYSTEM, 0, NULL);
create buffers to hold my results
diff = cpc_buf_create(rcw_cpc, my_cpc_set)
bind my set to my lwp and any threads it creates
i=cpc_bind_curlwp(rcw_cpc, my_cpc_set, CPC_BIND_LWP_INHERIT);
run my code
doit will take samples
if (cpc_set_sample(rcw_cpc, my_cpc_set, before) == -1)
if (cpc_set_sample(rcw_cpc, my_cpc_set, after) == -1)
calculate the delta
cpc_buf_sub(rcw_cpc, diff, after, before);
retrieve the value
cpc_buf_get(rcw_cpc, diff, my_index, &pic0);
destroy the set prior to using new events
since the set is destroyed, the buffers must be destroyed also