22#include <sys/utsname.h>
60#define PERF_EVENTS_OPENED 0x01
61#define PERF_EVENTS_RUNNING 0x02
66#define HANDLE_STRING_ERROR {fprintf(stderr,"%s:%i unexpected string function error.\n",__FILE__,__LINE__); exit(-1);}
85 unsigned int format = 0;
89 format |= PERF_FORMAT_TOTAL_TIME_ENABLED;
90 format |= PERF_FORMAT_TOTAL_TIME_RUNNING;
96 format |= PERF_FORMAT_GROUP;
100 SUBDBG(
"multiplex: %d, inherit: %d, group_leader: %d, format: %#x\n",
115#ifndef __NR_perf_event_open
118#define __NR_perf_event_open 319
119#elif defined(__x86_64__)
120#define __NR_perf_event_open 298
121#elif defined(__i386__)
122#define __NR_perf_event_open 336
123#elif defined(__arm__) 366+0x900000
124#define __NR_perf_event_open
131 int group_fd,
unsigned long flags )
135 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);
137 SUBDBG(
" size: %d\n",hw_event->size);
138 SUBDBG(
" config: %#"PRIx64
" (%"PRIu64
")\n",hw_event->
config,
140 SUBDBG(
" sample_period: %"PRIu64
"\n",hw_event->sample_period);
141 SUBDBG(
" sample_type: %"PRIu64
"\n",hw_event->sample_type);
142 SUBDBG(
" read_format: %"PRIu64
"\n",hw_event->read_format);
143 SUBDBG(
" disabled: %d\n",hw_event->disabled);
144 SUBDBG(
" inherit: %d\n",hw_event->inherit);
145 SUBDBG(
" pinned: %d\n",hw_event->pinned);
146 SUBDBG(
" exclusive: %d\n",hw_event->exclusive);
147 SUBDBG(
" exclude_user: %d\n",hw_event->exclude_user);
148 SUBDBG(
" exclude_kernel: %d\n",hw_event->exclude_kernel);
149 SUBDBG(
" exclude_hv: %d\n",hw_event->exclude_hv);
150 SUBDBG(
" exclude_idle: %d\n",hw_event->exclude_idle);
151 SUBDBG(
" mmap: %d\n",hw_event->mmap);
152 SUBDBG(
" comm: %d\n",hw_event->comm);
153 SUBDBG(
" freq: %d\n",hw_event->freq);
154 SUBDBG(
" inherit_stat: %d\n",hw_event->inherit_stat);
155 SUBDBG(
" enable_on_exec: %d\n",hw_event->enable_on_exec);
156 SUBDBG(
" task: %d\n",hw_event->task);
157 SUBDBG(
" watermark: %d\n",hw_event->watermark);
158 SUBDBG(
" precise_ip: %d\n",hw_event->precise_ip);
159 SUBDBG(
" mmap_data: %d\n",hw_event->mmap_data);
160 SUBDBG(
" sample_id_all: %d\n",hw_event->sample_id_all);
161 SUBDBG(
" exclude_host: %d\n",hw_event->exclude_host);
162 SUBDBG(
" exclude_guest: %d\n",hw_event->exclude_guest);
163 SUBDBG(
" exclude_callchain_kernel: %d\n",hw_event->exclude_callchain_kernel);
164 SUBDBG(
" exclude_callchain_user: %d\n",hw_event->exclude_callchain_user);
165 SUBDBG(
" wakeup_watermark: %d\n",hw_event->wakeup_watermark);
166 SUBDBG(
" bp_type: %d\n",hw_event->bp_type);
167 SUBDBG(
" config1: %#lx (%lu)\n",hw_event->config1,hw_event->config1);
168 SUBDBG(
" config2: %#lx (%lu)\n",hw_event->config2,hw_event->config2);
169 SUBDBG(
" branch_sample_type: %lu\n",hw_event->branch_sample_type);
170 SUBDBG(
" sample_regs_user: %lu\n",hw_event->sample_regs_user);
171 SUBDBG(
" sample_stack_user: %d\n",hw_event->sample_stack_user);
173 ret = syscall( __NR_perf_event_open, hw_event,
pid, cpu, group_fd, flags );
174 SUBDBG(
"Returned %d %d %s\n",ret,
176 ret<0?strerror(
errno):
" ");
187 switch(perf_event_error) {
222#define READ_BUFFER_SIZE (3 + (2 * PERF_EVENT_MAX_MPX_COUNTERS))
234 SUBDBG(
"ENTER: ctx: %p, ctl: %p\n", ctx, ctl);
248 SUBDBG(
"EXIT: Enable failed event index: %d, num_events: %d, return PAPI_ESYS\n",
i, ctl->
num_events);
257 SUBDBG(
"EXIT: Disable failed: event index: %d, num_events: %d, return PAPI_ESYS\n",
i, ctl->
num_events);
266 SUBDBG(
"EXIT: read failed: event index: %d, num_events: %d, return PAPI_ESYS. Should never happen.\n",
i, ctl->
num_events);
276 SUBDBG(
"EXIT: read returned 0: event index: %d, num_events: %d, return PAPI_ECNFLCT.\n",
i, ctl->
num_events);
296 SUBDBG(
"EXIT: Reset failed: event index: %d, num_events: %d, return PAPI_ESYS\n",
i, ctl->
num_events);
300 SUBDBG(
"EXIT: return PAPI_OK\n");
387 SUBDBG(
"sys_perf_event_open returned error on event #%d."
392 goto open_peu_cleanup;
395 SUBDBG (
"sys_perf_event_open: tid: %ld, cpu_num: %d,"
396 " group_leader/fd: %d, event_fd: %d,"
397 " read_format: %"PRIu64
"\n",
419 goto open_peu_cleanup;
458 int events_not_opened=0;
462 SUBDBG(
"Closing without stopping first\n");
474 PAPIERROR(
"munmap of fd = %d returned error: %s",
481 PAPIERROR(
"close of fd = %d returned error: %s",
504 PAPIERROR(
"munmap of fd = %d returned error: %s",
512 PAPIERROR(
"close of fd = %d returned error: %s",
525 if (ctl->
num_events!=(num_closed+events_not_opened)) {
527 "Closed %d Not Opened: %d Expected %d\n",
528 num_closed,events_not_opened,ctl->
num_events);
535 ctx->
state &= ~PERF_EVENTS_OPENED;
608 fff=fopen(
"/proc/sys/kernel/perf_event_paranoid",
"r");
617 if (
retval!=1) fprintf(
stderr,
"Error reading paranoid level\n");
666 if ((paranoid_level>0) && (getuid()!=0)) {
668 "Insufficient permissions for uncore access. Set /proc/sys/kernel/perf_event_paranoid to 0 or run as root.",
710 int skipped_events=0;
723 SUBDBG(
"Called with count == 0\n" );
733 SUBDBG(
"papi_event_code: %#x known by papi but not by the component\n",
native[
i].ni_papi_code);
748 SUBDBG(
"papi_event_code: %#x not found in native event tables\n",
native[
i].ni_papi_code);
755 SUBDBG(
"ntv_evt: %p\n", ntv_evt);
760 memcpy (&pe_ctl->
events[
i].
attr, &ntv_evt->
attr,
sizeof(perf_event_attr_t));
767 if ((aName == NULL) || (strstr(aName,
":u=") == NULL)) {
771 if ((aName == NULL) || (strstr(aName,
":k=") == NULL)) {
794 SUBDBG(
"&native[%d]: %p, ni_papi_code: %#x, ni_event: %#x, ni_position: %d, ni_owners: %d\n",
799 if (
count <= skipped_events) {
800 SUBDBG(
"EXIT: No events to count, they all contained invalid umasks\n");
810 SUBDBG(
"open_pe_events failed\n");
815 SUBDBG(
"EXIT: PAPI_OK\n" );
833 SUBDBG(
"old control domain %d, new domain %d\n",
867 PAPIERROR(
"ioctl(%d, PERF_EVENT_IOC_RESET, NULL) "
868 "returned error, Linux says: %s",
912 long long **
events,
int flags )
914 SUBDBG(
"ENTER: ctx: %p, ctl: %p, events: %p, flags: %#x\n", ctx, ctl,
events, flags);
922 long long tot_time_running, tot_time_enabled, scale;
933 sizeof ( papi_pe_buffer ) );
936 SUBDBG(
"EXIT: PAPI_ESYS\n");
941 if (ret<(
signed)(3*
sizeof(
long long))) {
943 SUBDBG(
"EXIT: PAPI_ESYS\n");
947 SUBDBG(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
950 SUBDBG(
"read: %lld %lld %lld\n",papi_pe_buffer[0],
951 papi_pe_buffer[1],papi_pe_buffer[2]);
953 tot_time_enabled = papi_pe_buffer[1];
954 tot_time_running = papi_pe_buffer[2];
956 SUBDBG(
"count[%d] = (papi_pe_buffer[%d] %lld * "
957 "tot_time_enabled %lld) / tot_time_running %lld\n",
958 i, 0,papi_pe_buffer[0],
959 tot_time_enabled,tot_time_running);
961 if (tot_time_running == tot_time_enabled) {
963 pe_ctl->
counts[
i] = papi_pe_buffer[0];
964 }
else if (tot_time_running && tot_time_enabled) {
968 scale = (tot_time_enabled * 100LL) / tot_time_running;
969 scale = scale * papi_pe_buffer[0];
970 scale = scale / 100LL;
974 SUBDBG(
"perf_event kernel bug(?) count, enabled, "
975 "running: %lld, %lld, %lld\n",
976 papi_pe_buffer[0],tot_time_enabled,
979 pe_ctl->
counts[
i] = papi_pe_buffer[0];
991 sizeof ( papi_pe_buffer ) );
994 SUBDBG(
"EXIT: PAPI_ESYS\n");
999 if (ret!=
sizeof(
long long)) {
1001 PAPIERROR(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
1004 SUBDBG(
"EXIT: PAPI_ESYS\n");
1008 SUBDBG(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
1011 SUBDBG(
"read: %lld\n",papi_pe_buffer[0]);
1013 pe_ctl->
counts[
i] = papi_pe_buffer[0];
1023 PAPIERROR(
"Was expecting group leader!\n");
1027 sizeof ( papi_pe_buffer ) );
1031 SUBDBG(
"EXIT: PAPI_ESYS\n");
1037 if (ret<(
signed)((1+pe_ctl->
num_events)*
sizeof(
long long))) {
1039 SUBDBG(
"EXIT: PAPI_ESYS\n");
1043 SUBDBG(
"read: fd: %2d, tid: %ld, cpu: %d, ret: %d\n",
1048 for(j=0;j<ret/8;j++) {
1049 SUBDBG(
"read %d: %lld\n",j,papi_pe_buffer[j]);
1055 PAPIERROR(
"Error! Wrong number of events!\n");
1056 SUBDBG(
"EXIT: PAPI_ESYS\n");
1062 pe_ctl->
counts[
i] = papi_pe_buffer[1+
i];
1069 SUBDBG(
"EXIT: PAPI_OK\n");
1079 int did_something = 0;
1098 PAPIERROR(
"ioctl(PERF_EVENT_IOC_ENABLE) failed.\n");
1106 if (!did_something) {
1107 PAPIERROR(
"Did not enable any counters.\n");
1132 PAPIERROR(
"ioctl(%d, PERF_EVENT_IOC_DISABLE, NULL) "
1133 "returned error, Linux says: %s",
1140 pe_ctx->
state &= ~PERF_EVENTS_RUNNING;
1283 char *ntv_name,
int len) {
1294 char *ntv_descr,
int len) {
1317 .
name =
"perf_event_uncore",
1318 .short_name =
"peu",
1320 .description =
"Linux perf_event CPU uncore and northbridge",
1330 .fast_virtual_timer = 0,
1332 .attach_must_ptrace = 1,
1343 .reg_value =
sizeof (
int ),
1344 .reg_alloc =
sizeof (
int ),
ssize_t read(int fd, void *buf, size_t count)
Initialize the libpfm4 code.
Shutdown any initialization done by the libpfm4 code.
Take an event code and convert it to a description.
Take an event code and convert it to a name.
Walk through all events in a pre-defined order.
Take an event name and convert it to an event code.
Shutdown any initialization done by the libpfm4 code.
Initialize the libpfm4 code.
struct papi_vectors * _papi_hwd[]
#define PAPI_INSTR_ADDRESS
#define PAPI_DOM_SUPERVISOR
#define PAPI_DATA_ADDRESS
#define PAPI_DEF_ITIMER_NS
char events[MAX_EVENTS][BUFSIZ]
Return codes and api definitions.
#define SUBDBG(format, args...)
int fclose(FILE *__stream)
void PAPIERROR(char *format,...)
int _papi_hwi_get_ntv_idx(unsigned int papi_evt_code)
int _pe_libpfm4_ntv_code_to_info(unsigned int EventCode, PAPI_event_info_t *info, struct native_event_table_t *event_table)
#define PERF_EVENT_MAX_MPX_COUNTERS
static int close_pe_events(pe_context_t *ctx, pe_control_t *ctl)
static int _peu_shutdown_thread(hwd_context_t *ctx)
papi_vector_t _perf_event_uncore_vector
static int _peu_ntv_name_to_code(const char *name, unsigned int *event_code)
struct native_event_table_t uncore_native_event_table
static int _peu_reset(hwd_context_t *ctx, hwd_control_state_t *ctl)
static int _peu_init_thread(hwd_context_t *hwd_ctx)
static int check_scheduability(pe_context_t *ctx, pe_control_t *ctl)
#define PERF_EVENTS_OPENED
static int _peu_ntv_code_to_info(unsigned int EventCode, PAPI_event_info_t *info)
static long sys_perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
static int _peu_stop(hwd_context_t *ctx, hwd_control_state_t *ctl)
static int _peu_start(hwd_context_t *ctx, hwd_control_state_t *ctl)
static int _peu_ntv_code_to_name(unsigned int EventCode, char *ntv_name, int len)
static int _peu_ntv_code_to_descr(unsigned int EventCode, char *ntv_descr, int len)
static int _peu_shutdown_component(void)
int _peu_update_control_state(hwd_control_state_t *ctl, NativeInfo_t *native, int count, hwd_context_t *ctx)
static int map_perf_event_errors_to_papi(int perf_event_error)
static int _peu_init_control_state(hwd_control_state_t *ctl)
static int _peu_read(hwd_context_t *ctx, hwd_control_state_t *ctl, long long **events, int flags)
static int open_pe_events(pe_context_t *ctx, pe_control_t *ctl)
static unsigned int get_read_format(unsigned int multiplex, unsigned int inherit, int format_group)
static int _peu_write(hwd_context_t *ctx, hwd_control_state_t *ctl, long long *from)
static int _peu_ntv_enum_events(unsigned int *PapiEventCode, int modifier)
static int _peu_set_domain(hwd_control_state_t *ctl, int domain)
#define PERF_EVENTS_RUNNING
static int _peu_init_component(int cidx)
static int _peu_ctl(hwd_context_t *ctx, int code, _papi_int_option_t *option)
#define HANDLE_STRING_ERROR
hwd_control_state_t * ctl_state
char name[PAPI_MAX_STR_LEN]
struct native_event_t * native_events
PAPI_component_info_t cmp_info
struct native_event_table_t * event_table
long long counts[PERF_EVENT_MAX_MPX_COUNTERS]
pe_event_info_t events[PERF_EVENT_MAX_MPX_COUNTERS]
struct perf_event_attr attr
_papi_int_domain_t domain
_papi_int_multiplex_t multiplex
_papi_int_granularity_t granularity
_papi_int_attach_t attach
_papi_int_inherit_t inherit