22 #include <sys/utsname.h> 24 #include <sys/ioctl.h> 36 #include "perfmon/pfmlib.h" 60 #define PERF_EVENTS_OPENED 0x01 61 #define PERF_EVENTS_RUNNING 0x02 80 unsigned int format = 0;
84 format |= PERF_FORMAT_TOTAL_TIME_ENABLED;
85 format |= PERF_FORMAT_TOTAL_TIME_RUNNING;
91 format |= PERF_FORMAT_GROUP;
95 SUBDBG(
"multiplex: %d, inherit: %d, group_leader: %d, format: %#x\n",
110 #ifndef __NR_perf_event_open 113 #define __NR_perf_event_open 319 114 #elif defined(__x86_64__) 115 #define __NR_perf_event_open 298 116 #elif defined(__i386__) 117 #define __NR_perf_event_open 336 118 #elif defined(__arm__) 366+0x900000 119 #define __NR_perf_event_open 126 int group_fd,
unsigned long flags )
130 SUBDBG(
"sys_perf_event_open(hw_event: %p, pid: %d, cpu: %d, group_fd: %d, flags: %lx\n",hw_event,
pid,cpu,group_fd,flags);
132 SUBDBG(
" size: %d\n",hw_event->size);
133 SUBDBG(
" config: %#"PRIx64
" (%"PRIu64
")\n",hw_event->
config,
135 SUBDBG(
" sample_period: %"PRIu64
"\n",hw_event->sample_period);
136 SUBDBG(
" sample_type: %"PRIu64
"\n",hw_event->sample_type);
137 SUBDBG(
" read_format: %"PRIu64
"\n",hw_event->read_format);
138 SUBDBG(
" disabled: %d\n",hw_event->disabled);
139 SUBDBG(
" inherit: %d\n",hw_event->inherit);
140 SUBDBG(
" pinned: %d\n",hw_event->pinned);
141 SUBDBG(
" exclusive: %d\n",hw_event->exclusive);
142 SUBDBG(
" exclude_user: %d\n",hw_event->exclude_user);
143 SUBDBG(
" exclude_kernel: %d\n",hw_event->exclude_kernel);
144 SUBDBG(
" exclude_hv: %d\n",hw_event->exclude_hv);
145 SUBDBG(
" exclude_idle: %d\n",hw_event->exclude_idle);
146 SUBDBG(
" mmap: %d\n",hw_event->mmap);
147 SUBDBG(
" comm: %d\n",hw_event->comm);
148 SUBDBG(
" freq: %d\n",hw_event->freq);
149 SUBDBG(
" inherit_stat: %d\n",hw_event->inherit_stat);
150 SUBDBG(
" enable_on_exec: %d\n",hw_event->enable_on_exec);
151 SUBDBG(
" task: %d\n",hw_event->task);
152 SUBDBG(
" watermark: %d\n",hw_event->watermark);
153 SUBDBG(
" precise_ip: %d\n",hw_event->precise_ip);
154 SUBDBG(
" mmap_data: %d\n",hw_event->mmap_data);
155 SUBDBG(
" sample_id_all: %d\n",hw_event->sample_id_all);
156 SUBDBG(
" exclude_host: %d\n",hw_event->exclude_host);
157 SUBDBG(
" exclude_guest: %d\n",hw_event->exclude_guest);
158 SUBDBG(
" exclude_callchain_kernel: %d\n",hw_event->exclude_callchain_kernel);
159 SUBDBG(
" exclude_callchain_user: %d\n",hw_event->exclude_callchain_user);
160 SUBDBG(
" wakeup_watermark: %d\n",hw_event->wakeup_watermark);
161 SUBDBG(
" bp_type: %d\n",hw_event->bp_type);
162 SUBDBG(
" config1: %#lx (%lu)\n",hw_event->config1,hw_event->config1);
163 SUBDBG(
" config2: %#lx (%lu)\n",hw_event->config2,hw_event->config2);
164 SUBDBG(
" branch_sample_type: %lu\n",hw_event->branch_sample_type);
165 SUBDBG(
" sample_regs_user: %lu\n",hw_event->sample_regs_user);
166 SUBDBG(
" sample_stack_user: %d\n",hw_event->sample_stack_user);
168 ret = syscall( __NR_perf_event_open, hw_event,
pid, cpu, group_fd, flags );
182 switch(perf_event_error) {
217 #define READ_BUFFER_SIZE (3 + (2 * PERF_EVENT_MAX_MPX_COUNTERS)) 229 SUBDBG(
"ENTER: ctx: %p, ctl: %p\n", ctx, ctl);
243 SUBDBG(
"EXIT: Enable failed event index: %d, num_events: %d, return PAPI_ESYS\n",
i, ctl->
num_events);
252 SUBDBG(
"EXIT: Disable failed: event index: %d, num_events: %d, return PAPI_ESYS\n",
i, ctl->
num_events);
261 SUBDBG(
"EXIT: read failed: event index: %d, num_events: %d, return PAPI_ESYS. Should never happen.\n",
i, ctl->
num_events);
271 SUBDBG(
"EXIT: read returned 0: event index: %d, num_events: %d, return PAPI_ECNFLCT.\n",
i, ctl->
num_events);
291 SUBDBG(
"EXIT: Reset failed: event index: %d, num_events: %d, return PAPI_ESYS\n",
i, ctl->
num_events);
295 SUBDBG(
"EXIT: return PAPI_OK\n");
382 SUBDBG(
"sys_perf_event_open returned error on event #%d." 387 goto open_peu_cleanup;
390 SUBDBG (
"sys_perf_event_open: tid: %ld, cpu_num: %d," 391 " group_leader/fd: %d, event_fd: %d," 392 " read_format: %"PRIu64
"\n",
414 goto open_peu_cleanup;
453 int events_not_opened=0;
457 SUBDBG(
"Closing without stopping first\n");
469 PAPIERROR(
"munmap of fd = %d returned error: %s",
476 PAPIERROR(
"close of fd = %d returned error: %s",
499 PAPIERROR(
"munmap of fd = %d returned error: %s",
507 PAPIERROR(
"close of fd = %d returned error: %s",
520 if (ctl->
num_events!=(num_closed+events_not_opened)) {
522 "Closed %d Not Opened: %d Expected %d\n",
523 num_closed,events_not_opened,ctl->
num_events);
602 fff=fopen(
"/proc/sys/kernel/perf_event_paranoid",
"r");
609 if (
retval!=1) fprintf(stderr,
"Error reading paranoid level\n");
652 if ((paranoid_level>0) && (getuid()!=0)) {
654 "Insufficient permissions for uncore access. Set /proc/sys/kernel/perf_event_paranoid to 0 or run as root.",
690 int skipped_events=0;
703 SUBDBG(
"Called with count == 0\n" );
713 SUBDBG(
"papi_event_code: %#x known by papi but not by the component\n",
native[
i].ni_papi_code);
728 SUBDBG(
"papi_event_code: %#x not found in native event tables\n",
native[
i].ni_papi_code);
735 SUBDBG(
"ntv_evt: %p\n", ntv_evt);
740 memcpy (&pe_ctl->
events[
i].
attr, &ntv_evt->
attr,
sizeof(perf_event_attr_t));
747 if ((aName == NULL) || (strstr(aName,
":u=") == NULL)) {
751 if ((aName == NULL) || (strstr(aName,
":k=") == NULL)) {
774 SUBDBG(
"&native[%d]: %p, ni_papi_code: %#x, ni_event: %#x, ni_position: %d, ni_owners: %d\n",
779 if (
count <= skipped_events) {
780 SUBDBG(
"EXIT: No events to count, they all contained invalid umasks\n");
790 SUBDBG(
"open_pe_events failed\n");
795 SUBDBG(
"EXIT: PAPI_OK\n" );
813 SUBDBG(
"old control domain %d, new domain %d\n",
847 PAPIERROR(
"ioctl(%d, PERF_EVENT_IOC_RESET, NULL) " 848 "returned error, Linux says: %s",
892 long long **
events,
int flags )
894 SUBDBG(
"ENTER: ctx: %p, ctl: %p, events: %p, flags: %#x\n", ctx, ctl,
events, flags);
902 long long tot_time_running, tot_time_enabled, scale;
913 sizeof ( papi_pe_buffer ) );
916 SUBDBG(
"EXIT: PAPI_ESYS\n");
921 if (
ret<(
signed)(3*
sizeof(
long long))) {
923 SUBDBG(
"EXIT: PAPI_ESYS\n");
927 SUBDBG(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
930 SUBDBG(
"read: %lld %lld %lld\n",papi_pe_buffer[0],
931 papi_pe_buffer[1],papi_pe_buffer[2]);
933 tot_time_enabled = papi_pe_buffer[1];
934 tot_time_running = papi_pe_buffer[2];
936 SUBDBG(
"count[%d] = (papi_pe_buffer[%d] %lld * " 937 "tot_time_enabled %lld) / tot_time_running %lld\n",
938 i, 0,papi_pe_buffer[0],
939 tot_time_enabled,tot_time_running);
941 if (tot_time_running == tot_time_enabled) {
943 pe_ctl->
counts[
i] = papi_pe_buffer[0];
944 }
else if (tot_time_running && tot_time_enabled) {
948 scale = (tot_time_enabled * 100LL) / tot_time_running;
949 scale = scale * papi_pe_buffer[0];
950 scale = scale / 100LL;
954 SUBDBG(
"perf_event kernel bug(?) count, enabled, " 955 "running: %lld, %lld, %lld\n",
956 papi_pe_buffer[0],tot_time_enabled,
959 pe_ctl->
counts[
i] = papi_pe_buffer[0];
971 sizeof ( papi_pe_buffer ) );
974 SUBDBG(
"EXIT: PAPI_ESYS\n");
979 if (
ret!=
sizeof(
long long)) {
981 PAPIERROR(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
984 SUBDBG(
"EXIT: PAPI_ESYS\n");
988 SUBDBG(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
991 SUBDBG(
"read: %lld\n",papi_pe_buffer[0]);
993 pe_ctl->
counts[
i] = papi_pe_buffer[0];
1003 PAPIERROR(
"Was expecting group leader!\n");
1007 sizeof ( papi_pe_buffer ) );
1011 SUBDBG(
"EXIT: PAPI_ESYS\n");
1017 if (
ret<(
signed)((1+pe_ctl->
num_events)*
sizeof(
long long))) {
1019 SUBDBG(
"EXIT: PAPI_ESYS\n");
1023 SUBDBG(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
1028 for(j=0;j<
ret/8;j++) {
1029 SUBDBG(
"read %d: %lld\n",j,papi_pe_buffer[j]);
1035 PAPIERROR(
"Error! Wrong number of events!\n");
1036 SUBDBG(
"EXIT: PAPI_ESYS\n");
1042 pe_ctl->
counts[
i] = papi_pe_buffer[1+
i];
1049 SUBDBG(
"EXIT: PAPI_OK\n");
1059 int did_something = 0;
1078 PAPIERROR(
"ioctl(PERF_EVENT_IOC_ENABLE) failed.\n");
1086 if (!did_something) {
1087 PAPIERROR(
"Did not enable any counters.\n");
1112 PAPIERROR(
"ioctl(%d, PERF_EVENT_IOC_DISABLE, NULL) " 1113 "returned error, Linux says: %s",
1263 char *ntv_name,
int len) {
1274 char *ntv_descr,
int len) {
1297 .
name =
"perf_event_uncore",
1298 .short_name =
"peu",
1300 .description =
"Linux perf_event CPU uncore and northbridge",
1310 .fast_virtual_timer = 0,
1312 .attach_must_ptrace = 1,
1323 .reg_value =
sizeof ( int ),
1324 .reg_alloc =
sizeof (
int ),
ssize_t read(int fd, void *buf, size_t count)
static int open_pe_events(pe_context_t *ctx, pe_control_t *ctl)
long long counts[PERF_EVENT_MAX_MPX_COUNTERS]
int _papi_hwi_get_ntv_idx(unsigned int papi_evt_code)
_papi_int_inherit_t inherit
static int _peu_init_thread(hwd_context_t *hwd_ctx)
#define PERF_EVENT_MAX_MPX_COUNTERS
struct native_event_t * native_events
#define PAPI_DEF_ITIMER_NS
int _papi_libpfm4_init(papi_vector_t *my_vector)
static int _peu_shutdown_thread(hwd_context_t *ctx)
static int _peu_ntv_code_to_descr(unsigned int EventCode, char *ntv_descr, int len)
static int map_perf_event_errors_to_papi(int perf_event_error)
#define PAPI_INSTR_ADDRESS
static int _peu_shutdown_component(void)
#define PAPI_DATA_ADDRESS
papi_vector_t _perf_event_uncore_vector
#define PERF_EVENTS_OPENED
static int _peu_write(hwd_context_t *ctx, hwd_control_state_t *ctl, long long *from)
pe_event_info_t events[PERF_EVENT_MAX_MPX_COUNTERS]
int _pe_libpfm4_ntv_enum_events(unsigned int *PapiEventCode, int modifier, int cidx, struct native_event_table_t *event_table)
static int close_pe_events(pe_context_t *ctx, pe_control_t *ctl)
int _pe_libpfm4_ntv_name_to_code(const char *name, unsigned int *event_code, int cidx, struct native_event_table_t *event_table)
static int _peu_ctl(hwd_context_t *ctx, int code, _papi_int_option_t *option)
PAPI_component_info_t cmp_info
static int _peu_stop(hwd_context_t *ctx, hwd_control_state_t *ctl)
Return codes and api definitions.
static int check_scheduability(pe_context_t *ctx, pe_control_t *ctl)
char events[MAX_EVENTS][BUFSIZ]
_papi_int_attach_t attach
static int _peu_start(hwd_context_t *ctx, hwd_control_state_t *ctl)
int _pe_libpfm4_ntv_code_to_name(unsigned int EventCode, char *ntv_name, int len, struct native_event_table_t *event_table)
#define PAPI_DOM_SUPERVISOR
struct native_event_table_t uncore_native_event_table
static int _peu_init_control_state(hwd_control_state_t *ctl)
static int _peu_init_component(int cidx)
#define SUBDBG(format, args...)
_papi_int_granularity_t granularity
void PAPIERROR(char *format,...)
struct native_event_table_t * event_table
char name[PAPI_MAX_STR_LEN]
int _papi_libpfm4_shutdown(papi_vector_t *my_vector)
int _peu_libpfm4_init(papi_vector_t *my_vector, int cidx, struct native_event_table_t *event_table, int pmu_type)
static int _peu_ntv_name_to_code(const char *name, unsigned int *event_code)
int cnt[ctr_pcp_ntv_code_to_info+1]
_papi_int_multiplex_t multiplex
#define PERF_EVENTS_RUNNING
int _pe_libpfm4_ntv_code_to_info(unsigned int EventCode, PAPI_event_info_t *info, struct native_event_table_t *event_table)
int _pe_libpfm4_ntv_code_to_descr(unsigned int EventCode, char *ntv_descr, int len, struct native_event_table_t *event_table)
static int _peu_ntv_enum_events(unsigned int *PapiEventCode, int modifier)
int _pe_libpfm4_shutdown(papi_vector_t *my_vector, struct native_event_table_t *event_table)
static int _peu_ntv_code_to_name(unsigned int EventCode, char *ntv_name, int len)
static int _peu_set_domain(hwd_control_state_t *ctl, int domain)
struct perf_event_attr attr
static unsigned int get_read_format(unsigned int multiplex, unsigned int inherit, int format_group)
struct papi_vectors * _papi_hwd[]
static int _peu_ntv_code_to_info(unsigned int EventCode, PAPI_event_info_t *info)
_papi_int_domain_t domain
static long sys_perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
hwd_control_state_t * ctl_state
int _peu_update_control_state(hwd_control_state_t *ctl, NativeInfo_t *native, int count, hwd_context_t *ctx)
static int _peu_read(hwd_context_t *ctx, hwd_control_state_t *ctl, long long **events, int flags)
static int _peu_reset(hwd_context_t *ctx, hwd_control_state_t *ctl)