PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pe_libpfm4_events.c
Go to the documentation of this file.
1 /*
2 * File: pe_libpfm4_events.c
3 * Author: Vince Weaver vincent.weaver @ maine.edu
4 * Mods: Gary Mohr
5 * gary.mohr@bull.com
6 * Modified the perf_event component to use PFM_OS_PERF_EVENT_EXT mode in libpfm4.
7 * This adds several new event masks, including cpu=, u=, and k= which give the user
8 * the ability to set cpu number to use or control the domain (user, kernel, or both)
9 * in which the counter should be incremented. These are event masks so it is now
10 * possible to have multiple events in the same event set that count activity from
11 * differennt cpu's or count activity in different domains.
12 *
13 * Handle the libpfm4 event interface for the perf_event component
14 */
15 
16 #include <string.h>
17 
18 #include "papi.h"
19 #include "papi_internal.h"
20 #include "papi_vector.h"
21 
22 #include "papi_libpfm4_events.h"
23 #include "pe_libpfm4_events.h"
24 #include "perf_event_lib.h"
25 
26 #include "perfmon/pfmlib.h"
27 #include "perfmon/pfmlib_perf_event.h"
28 #include "libpfm4/lib/pfmlib_priv.h"
29 
30 #define NATIVE_EVENT_CHUNK 1024
31 
32 // used to step through the attributes when enumerating events
33 static int attr_idx;
34 
47 static int find_existing_event(char *name,
48  struct native_event_table_t *event_table) {
49  SUBDBG("Entry: name: %s, event_table: %p, num_native_events: %d\n", name, event_table, event_table->num_native_events);
50 
51  int i,event=PAPI_ENOEVNT;
52 
54 
55  for(i=0;i<event_table->num_native_events;i++) {
56  // Most names passed in will contain the pmu name, so first we compare to the allocated name (it has pmu name on front)
57  if (!strcmp(name,event_table->native_events[i].allocated_name)) {
58  SUBDBG("Found allocated_name: %s, libpfm4_idx: %#x, papi_event_code: %#x\n",
59  event_table->native_events[i].allocated_name, event_table->native_events[i].libpfm4_idx, event_table->native_events[i].papi_event_code);
60  event=i;
61  break;
62  }
63  // some callers have an event name without the pmu name on the front, so we also compare to the base name (just the event name part)
64  if (!strcmp(name,event_table->native_events[i].base_name)) {
65  SUBDBG("Found base_name: %s, libpfm4_idx: %#x, papi_event_code: %#x\n",
66  event_table->native_events[i].base_name, event_table->native_events[i].libpfm4_idx, event_table->native_events[i].papi_event_code);
67  event=i;
68  break;
69  }
70  }
72 
73  SUBDBG("EXIT: returned: %#x\n", event);
74  return event;
75 }
76 
77 
78 static int pmu_is_present_and_right_type(pfm_pmu_info_t *pinfo, int type) {
79 // SUBDBG("ENTER: pinfo: %p, pinfo->is_present: %d, pinfo->type: %#x, type: %#x\n", pinfo, pinfo->is_present, pinfo->type, type);
80  if (!pinfo->is_present) {
81 // SUBDBG("EXIT: not present\n");
82  return 0;
83  }
84 
85  if ((pinfo->type==PFM_PMU_TYPE_UNCORE) && (type&PMU_TYPE_UNCORE)) {
86 // SUBDBG("EXIT: found PFM_PMU_TYPE_UNCORE\n");
87  return 1;
88  }
89  if ((pinfo->type==PFM_PMU_TYPE_CORE) && (type&PMU_TYPE_CORE)) {
90 // SUBDBG("EXIT: found PFM_PMU_TYPE_CORE\n");
91  return 1;
92  }
93  if ((pinfo->type==PFM_PMU_TYPE_OS_GENERIC) && (type&PMU_TYPE_OS)) {
94 // SUBDBG("EXIT: found PFM_PMU_TYPE_OS_GENERIC\n");
95  return 1;
96  }
97 
98 // SUBDBG("EXIT: not right type\n");
99  return 0;
100 }
101 
102 
117 static struct native_event_t *allocate_native_event(char *name, int libpfm4_index,
118  struct native_event_table_t *event_table) {
119 
120  SUBDBG("ENTER: name: %s, libpfm4_index: %#x, event_table: %p, event_table->pmu_type: %d\n", name, libpfm4_index, event_table, event_table->pmu_type);
121 
122  int nevt_idx;
123  int event_num;
124  int encode_failed=0;
125 
126  pfm_err_t ret;
127  char *event_string=NULL;
128  char *pmu_name;
129  char *event;
130  char *masks;
131  char fullname[BUFSIZ];
132  struct native_event_t *ntv_evt;
133 
134  pfm_perf_encode_arg_t perf_arg;
135  pfm_event_info_t einfo;
136  pfm_event_attr_info_t ainfo;
137  pfm_pmu_info_t pinfo;
138 
139  // if no place to put native events, report that allocate failed
140  if (event_table->native_events==NULL) {
141  SUBDBG("EXIT: no place to put native events\n");
142  return NULL;
143  }
144 
145  // find out if this event is already known
146  event_num=find_existing_event(name, event_table);
147 
148  /* add the event to our event table */
150 
151  // if we already know this event name, it was created as part of setting up the preset tables
152  // we need to use the event table which is already created
153  if (event_num >= 0) {
154  nevt_idx = event_num;
155  ntv_evt = &(event_table->native_events[event_num]);
156  } else {
157  // set to use a new event table (count of used events not bumped until we are sure setting it up does not get an errro)
158  nevt_idx = event_table->num_native_events;
159  ntv_evt = &(event_table->native_events[nevt_idx]);
160  }
161 
162  SUBDBG("event_num: %d, nevt_idx: %d, ntv_evt: %p\n", event_num, nevt_idx, ntv_evt);
163 
164  /* clear the argument and attribute structures */
165  memset(&perf_arg,0,sizeof(pfm_perf_encode_arg_t));
166  memset(&(ntv_evt->attr),0,sizeof(struct perf_event_attr));
167 
168  // set argument structure fields so the encode function can give us what we need
169  perf_arg.attr=&ntv_evt->attr;
170  perf_arg.fstr=&event_string;
171 
172  /* use user provided name of the event to get the perf_event encoding and a fully qualified event string */
173  ret = pfm_get_os_event_encoding(name,
174  PFM_PLM0 | PFM_PLM3,
175  PFM_OS_PERF_EVENT_EXT,
176  &perf_arg);
177 
178  // If the encode function failed, skip processing of the event_string
179  if ((ret != PFM_SUCCESS) || (event_string == NULL)) {
180  SUBDBG("encode failed for event: %s, returned: %d\n", name, ret);
181 
182  // we need to remember that this event encoding failed but still create the native event table
183  // the event table is used by the list so we put what we can get into it
184  // but the failure doing the encode causes us to return null to our caller
185  encode_failed = 1;
186 
187  // Noting the encode_failed error in the attr.config allows
188  // any later validate attempts to return an error value
189  ntv_evt->attr.config = 0xFFFFFF;
190 
191  // we also want to make it look like a cpu number was not provided as an event mask
192  perf_arg.cpu = -1;
193  }
194 
195  // get a copy of the event name and break it up into its parts
196  event_string = strdup(name);
197 
198  SUBDBG("event_string: %s\n", event_string);
199 
200  // get the pmu name, event name and mask list pointers from the event string
201  event = strstr (event_string, "::");
202  if (event != NULL) {
203  *event = 0; // null terminate pmu name
204  event += 2; // event name follows '::'
205  pmu_name = strdup(event_string);
206  } else {
207  // no pmu name in event string
208  pmu_name = malloc(2);
209  pmu_name[0] = 0;
210  event = event_string;
211  }
212  masks = strstr (event, ":");
213  if (masks != NULL) {
214  *masks = 0; // null terminate event name
215  masks += 1; // masks follow :
216  } else {
217  masks = "";
218  }
219 
220  // build event name to find, put a pmu name on it if we have one
221  if(strlen(pmu_name) == 0) {
222  sprintf(fullname,"%s", event);
223  } else {
224  sprintf(fullname,"%s::%s", pmu_name, event);
225  }
226  SUBDBG("pmu_name: %s, event: %s, masks: %s, fullname: %s\n", pmu_name, event, masks, fullname);
227 
228  // if the libpfm4 index was not provided, try to get one based on the event name passed in.
229  if (libpfm4_index == -1) {
230  libpfm4_index = pfm_find_event(fullname);
231  if (libpfm4_index < 0) {
232  free(event_string);
233  free(pmu_name);
235  SUBDBG("EXIT: error from libpfm4 find event\n");
236  return NULL;
237  }
238  SUBDBG("libpfm4_index: %#x\n", libpfm4_index);
239  }
240 
241  // get this events information from libpfm4, if unavailable return event not found (structure be zeroed)
242  memset( &einfo, 0, sizeof( pfm_event_info_t ));
243  einfo.size = sizeof(pfm_event_info_t);
244  if ((ret = pfm_get_event_info(libpfm4_index, PFM_OS_PERF_EVENT_EXT, &einfo)) != PFM_SUCCESS) {
245  free(event_string);
246  free(pmu_name);
248  SUBDBG("EXIT: pfm_get_event_info failed with %d\n", ret);
249  return NULL;
250  }
251 
252  // if pmu type is not one supported by this component, return event not found (structure be zeroed)
253  memset(&pinfo,0,sizeof(pfm_pmu_info_t));
254  pinfo.size = sizeof(pfm_pmu_info_t);
255  pfm_get_pmu_info(einfo.pmu, &pinfo);
256  if (pmu_is_present_and_right_type(&pinfo, event_table->pmu_type) == 0) {
257  free(event_string);
258  free(pmu_name);
260  SUBDBG("EXIT: PMU not supported by this component: einfo.pmu: %d, PFM_PMU_TYPE_CORE: %d\n", einfo.pmu, PFM_PMU_TYPE_CORE);
261  return NULL;
262  }
263 
264  ntv_evt->allocated_name=strdup(name);
265  ntv_evt->mask_string=strdup(masks);
266  ntv_evt->component=_pe_libpfm4_get_cidx();
267  ntv_evt->pmu=pmu_name;
268  ntv_evt->base_name=strdup(event);
269  ntv_evt->pmu_plus_name=strdup(fullname);
270  ntv_evt->libpfm4_idx=libpfm4_index;
271  ntv_evt->event_description=strdup(einfo.desc);
272  ntv_evt->users=0; /* is this needed? */
273  ntv_evt->cpu=perf_arg.cpu;
274 
275  SUBDBG("ntv_evt->mask_string: %p (%s)\n", ntv_evt->mask_string, ntv_evt->mask_string);
276  char *msk_ptr = strdup(masks); // get a work copy of the mask string before we free the space it was in
277  free(event_string);
278 
279  // if there is any mask data, collect their descriptions
280  if ((msk_ptr != NULL) && (strlen(msk_ptr) > 0)) {
281  // go get the descriptions for each of the masks provided with this event
282  char mask_desc[PAPI_HUGE_STR_LEN] = "";
283  char *ptr = msk_ptr;
284  SUBDBG("ptr: %p (%s)\n", ptr, ptr);
285  while (ptr != NULL) {
286  char *ptrm = strstr(ptr, ":");
287  if (ptrm != NULL) {
288  *ptrm = '\0';
289  ptrm++;
290  }
291 
292  // get the length of the mask name
293  char *wrk = strchr(ptr, '=');
294  unsigned int msk_name_len;
295  if (wrk != NULL) {
296  msk_name_len = wrk - ptr;
297  } else {
298  msk_name_len = strlen (ptr);
299  }
300 
301  int i;
302  for (i=0 ; i<einfo.nattrs ; i++) {
303  // get this events attribute information from libpfm4, if unavailable return event not found (table must be cleared)
304  memset (&ainfo, 0, sizeof(pfm_event_attr_info_t));
305  ainfo.size = sizeof(pfm_event_attr_info_t);
306  ret = pfm_get_event_attr_info(libpfm4_index, i, PFM_OS_PERF_EVENT_EXT, &ainfo);
307  if (ret != PFM_SUCCESS) {
308  free (msk_ptr);
309  SUBDBG("EXIT: Attribute info not found, libpfm4_index: %#x, ret: %d\n", libpfm4_index, _papi_libpfm4_error(ret));
310  return NULL;
311  }
312 
313  // if this is the one we want, append its description
314  if ((msk_name_len == strlen(ainfo.name)) && (strncmp(ptr, ainfo.name, msk_name_len) == 0)) {
315  SUBDBG("Found mask: %s, i: %d\n", ainfo.name, i);
316  // find out how much space is left in the mask description work buffer we are building
317  unsigned int mskleft = sizeof(mask_desc) - strlen(mask_desc);
318  // if no space left, just discard this mask description
319  if (mskleft <= 1) {
320  SUBDBG("EXIT: Attribute description discarded: %s\n", ainfo.desc);
321  break;
322  }
323  // if description buffer is not empty, put in mask description separator
324  if (strlen(mask_desc) > 0) {
325  strcat (mask_desc, ":");
326  mskleft--;
327  }
328  // if new description will not all fit in buffer, report truncation
329  if (mskleft < (strlen(ainfo.desc) + 1)) {
330  SUBDBG("EXIT: Attribute description truncated: %s\n", ainfo.desc);
331  }
332  // move as much of this description as will fit
333  strncat (mask_desc, ainfo.desc, mskleft-1);
334  mask_desc[mskleft-1] = '\0';
335  break;
336  }
337  }
338 
339  // if we have filled the work buffer, we can quit now
340  if ( (sizeof(mask_desc) - strlen(mask_desc)) <= 1) {
341  break;
342  }
343  ptr = ptrm;
344  }
345  ntv_evt->mask_description=strdup(mask_desc);
346  SUBDBG("ntv_evt->mask_description: %p (%s)\n", ntv_evt->mask_description, ntv_evt->mask_description);
347  }
348 
349  // give back space if we got any
350  if (msk_ptr != NULL) {
351  free (msk_ptr);
352  }
353 
354  // create a papi table for this native event, put the index into the event sets array of native events into the papi table
355  int new_event_code = _papi_hwi_native_to_eventcode(_pe_libpfm4_get_cidx(), libpfm4_index, nevt_idx, ntv_evt->allocated_name);
356  _papi_hwi_set_papi_event_string((const char *)ntv_evt->allocated_name);
357  _papi_hwi_set_papi_event_code(new_event_code, 1);
358 
359  ntv_evt->papi_event_code=new_event_code;
360 
361  SUBDBG("Using %#x as index for %s\n", ntv_evt->libpfm4_idx, fullname);
362  SUBDBG("num_native_events: %d, allocated_native_events: %d\n", event_table->num_native_events, event_table->allocated_native_events);
363  SUBDBG("Native Event: papi_event_code: %#x, libpfm4_idx: %#x, pmu: %s, base_name: %s, mask_string: %s, allocated_name: %s\n",
364  ntv_evt->papi_event_code, ntv_evt->libpfm4_idx, ntv_evt->pmu, ntv_evt->base_name, ntv_evt->mask_string, ntv_evt->allocated_name);
365  SUBDBG("event_table->native_events[%d]: %p, cpu: %d, attr.config: 0x%"PRIx64", attr.config1: 0x%"PRIx64", attr.config2: 0x%"PRIx64", attr.type: 0x%"PRIx32", attr.exclude_user: %d, attr.exclude_kernel: %d, attr.exclude_guest: %d\n",
366  nevt_idx, &(event_table->native_events[nevt_idx]), ntv_evt->cpu, ntv_evt->attr.config,
367  ntv_evt->attr.config1, ntv_evt->attr.config2, ntv_evt->attr.type,
368  ntv_evt->attr.exclude_user, ntv_evt->attr.exclude_kernel, ntv_evt->attr.exclude_guest);
369 
370  /* If we've used all of the allocated native events, then allocate more room */
371  if (event_table->num_native_events >=
372  event_table->allocated_native_events-1) {
373 
374  SUBDBG("Allocating more room for native events (%d %ld)\n",
376  (long)sizeof(struct native_event_t) *
378 
379  event_table->native_events=realloc(event_table->native_events,
380  sizeof(struct native_event_t) *
383 
384  // we got new space so we need to reset the pointer to the correct native event in the new space
385  ntv_evt = &(event_table->native_events[nevt_idx]);
386  }
387 
389 
390  // if getting more space for native events failed, report that allocate failed
391  if (event_table->native_events==NULL) {
392  SUBDBG("EXIT: attempt to get more space for native events failed\n");
393  return NULL;
394  }
395 
396  // if we created a new event, bump the number used
397  if (event_num < 0) {
398  event_table->num_native_events++;
399  }
400 
401  if (encode_failed != 0) {
402  SUBDBG("EXIT: encoding event failed\n");
403  return NULL;
404  }
405 
406  SUBDBG("EXIT: new_event: %p\n", ntv_evt);
407  return ntv_evt;
408 }
409 
410 
419 static int
420 get_first_event_next_pmu(int pmu_idx, int pmu_type)
421 {
422  SUBDBG("ENTER: pmu_idx: %d, pmu_type: %d\n", pmu_idx, pmu_type);
423  int pidx, ret;
424 
425  pfm_pmu_info_t pinfo;
426 
427  // start looking at the next pmu in the list
428  pmu_idx++;
429 
430  while(pmu_idx<PFM_PMU_MAX) {
431 
432  /* clear the PMU structure (required by libpfm4) */
433  memset(&pinfo,0,sizeof(pfm_pmu_info_t));
434  pinfo.size = sizeof(pfm_pmu_info_t);
435  ret=pfm_get_pmu_info(pmu_idx, &pinfo);
436 
437  if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) {
438 
439  pidx=pinfo.first_event;
440  SUBDBG("First event in pmu: %s is %#x\n", pinfo.name, pidx);
441 
442  if (pidx<0) {
443  /* For some reason no events available */
444  /* despite the PMU being active. */
445  /* This can happen, for example with ix86arch */
446  /* inside of VMware */
447  }
448  else {
449  SUBDBG("EXIT: pidx: %#x\n", pidx);
450  return pidx;
451  }
452  }
453 
454  pmu_idx++;
455 
456  }
457 
458  SUBDBG("EXIT: PAPI_ENOEVNT\n");
459  return PAPI_ENOEVNT;
460 
461 }
462 
463 
464 /***********************************************************/
465 /* Exported functions */
466 /***********************************************************/
467 
482 int
483 _pe_libpfm4_ntv_name_to_code( char *name, unsigned int *event_code,
484  struct native_event_table_t *event_table)
485 {
486  SUBDBG( "ENTER: name: %s, event_code: %p, *event_code: %#x, event_table: %p\n", name, event_code, *event_code, event_table);
487 
488  struct native_event_t *our_event;
489  int event_num;
490 
491  // if we already know this event name, just return its native code
492  event_num=find_existing_event(name, event_table);
493  if (event_num >= 0) {
494  *event_code=event_table->native_events[event_num].libpfm4_idx;
495  // the following call needs to happen to prevent the internal layer from creating a new papi native event table
496  _papi_hwi_set_papi_event_code(event_table->native_events[event_num].papi_event_code, 1);
497  SUBDBG("EXIT: Found papi_event_code: %#x, libpfm4_idx: %#x\n", event_table->native_events[event_num].papi_event_code, event_table->native_events[event_num].libpfm4_idx);
498  return PAPI_OK;
499  }
500 
501  // Try to allocate this event to see if it is known by libpfm4, if allocate fails tell the caller it is not valid
502  our_event=allocate_native_event(name, -1, event_table);
503  if (our_event==NULL) {
504  SUBDBG("EXIT: Allocating event: '%s' failed\n", name);
505  return PAPI_ENOEVNT;
506  }
507 
508  *event_code = our_event->libpfm4_idx;
509  SUBDBG("EXIT: Found code: %#x\n",*event_code);
510  return PAPI_OK;
511 }
512 
513 
531 int
532 _pe_libpfm4_ntv_code_to_name(unsigned int EventCode,
533  char *ntv_name, int len,
534  struct native_event_table_t *event_table)
535 {
536  SUBDBG("ENTER: EventCode: %#x, ntv_name: %p, len: %d, event_table: %p\n", EventCode, ntv_name, len, event_table);
537 
538  int eidx;
539  int papi_event_code;
540 
541  // get the attribute index for this papi event
542  papi_event_code = _papi_hwi_get_papi_event_code();
543 
544  // a papi event code less than 0 is invalid, return error
545  if (papi_event_code <= 0) {
546  SUBDBG("EXIT: PAPI_ENOEVNT\n");
547  return PAPI_ENOEVNT;
548  }
549 
550  // find our native event table for this papi event code (search list backwards because it improves chances of finding it quickly)
551  for (eidx=event_table->num_native_events-1 ; eidx>=0 ; eidx--) {
552  if ((papi_event_code == event_table->native_events[eidx].papi_event_code) && (EventCode == ((unsigned)event_table->native_events[eidx].libpfm4_idx))) {
553  SUBDBG("Found native_event[%d]: papi_event_code: %#x, libpfm4_idx: %#x\n", eidx, event_table->native_events[eidx].papi_event_code, event_table->native_events[eidx].libpfm4_idx);
554  break;
555  }
556  }
557 
558  // if we did not find a match, return an error
559  if (eidx < 0) {
560  // If we did not find a match in our native event table, then the code passed in has not been
561  // allocated yet It should not be possible to get to this code. The user has to call the papi
562  // code_to_name api with a papi event code for a native event. But the only way to get one of
563  // those is to call either name_to_code or enum_cmp_events first. When one of these calls is
564  // done we allocate the event so it should always be there.
565 
566  SUBDBG("EXIT: PAPI_ENOEVNT\n");
567  return PAPI_ENOEVNT;
568  }
569 
570  // if this event is defined by the default pmu, then use only the event name
571  // if it is not defined by the default pmu, then use both the pmu name and event name
572  char *ename;
573  if (strcmp(event_table->default_pmu.name, event_table->native_events[eidx].pmu) == 0) {
574  ename = event_table->native_events[eidx].base_name;
575  } else {
576  ename = event_table->native_events[eidx].pmu_plus_name;
577  }
578 
579  // if it will not fit, return error
580  if (strlen (ename) >= (unsigned)len) {
581  SUBDBG("EXIT: event name %s will not fit in buffer provided\n", ename);
582  return PAPI_EBUF;
583  }
584  strcpy (ntv_name, ename);
585 
586  // if this event had masks, also add their names
587  char *mname = event_table->native_events[eidx].mask_string;
588  if ((mname != NULL) && (strlen(mname) > 0)) {
589  if ((strlen(ename) + 8 + strlen(mname)) >= (unsigned)len) {
590  SUBDBG("EXIT: Not enough room for event and mask descriptions: need: %u, have: %u", (unsigned)(strlen(ename) + 8 + strlen(mname)), (unsigned)len);
591  return PAPI_EBUF;
592  }
593  strcat (ntv_name, ":");
594  strcat (ntv_name, mname);
595  }
596 
597  SUBDBG("EXIT: event name: %s\n", ntv_name);
598  return PAPI_OK;
599 }
600 
601 
624 int
625 _pe_libpfm4_ntv_code_to_descr( unsigned int EventCode,
626  char *ntv_descr, int len,
627  struct native_event_table_t *event_table)
628 {
629  SUBDBG("ENTER: EventCode: %#x, ntv_descr: %p, len: %d: event_table: %p\n", EventCode, ntv_descr, len, event_table);
630 
631  int eidx;
632  int papi_event_code;
633 
634  // get the attribute index for this papi event
635  papi_event_code = _papi_hwi_get_papi_event_code();
636 
637  // a papi event code less than 0 is invalid, return error
638  if (papi_event_code <= 0) {
639  SUBDBG("EXIT: PAPI_ENOEVNT\n");
640  return PAPI_ENOEVNT;
641  }
642 
643  // find our native event table for this papi event code (search list backwards because it improves chances of finding it quickly)
644  for (eidx=event_table->num_native_events-1 ; eidx>=0 ; eidx--) {
645  SUBDBG("native_event[%d]: papi_event_code: %#x, libpfm4_idx: %#x\n", eidx, event_table->native_events[eidx].papi_event_code, event_table->native_events[eidx].libpfm4_idx);
646  if ((papi_event_code == event_table->native_events[eidx].papi_event_code) && (EventCode == ((unsigned)event_table->native_events[eidx].libpfm4_idx))) {
647  break;
648  }
649  }
650 
651  // if we did not find a match, return an error
652  if (eidx < 0) {
653  // If we did not find a match in our native event table, then the code passed in has not been
654  // allocated yet It should not be possible to get to this code. The user has to call the papi
655  // code_to_name api with a papi event code for a native event. But the only way to get one of
656  // those is to call either name_to_code or enum_cmp_events first. When one of these calls is
657  // done we allocate the event so it should always be there.
658 
659  SUBDBG("EXIT: PAPI_ENOEVNT\n");
660  return PAPI_ENOEVNT;
661  }
662 
663  char *edesc = event_table->native_events[eidx].event_description;
664 
665  // if it will not fit, return error
666  if (strlen (edesc) >= (unsigned)len) {
667  SUBDBG("EXIT: event name %s will not fit in buffer provided\n", edesc);
668  return PAPI_EBUF;
669  }
670  strcpy (ntv_descr, edesc);
671 
672  // if this event had masks, also add their descriptions
673  char *mdesc = event_table->native_events[eidx].mask_description;
674  if ((mdesc != NULL) && (strlen(mdesc) > 0)) {
675  if ((strlen(edesc) + 8 + strlen(mdesc)) >= (unsigned)len) {
676  SUBDBG("EXIT: Not enough room for event and mask descriptions: need: %u, have: %u", (unsigned)(strlen(edesc) + 8 + strlen(mdesc)), (unsigned)len);
677  return PAPI_EBUF;
678  }
679  strcat (ntv_descr, ", masks:");
680  strcat (ntv_descr, mdesc);
681  }
682 
683  SUBDBG("EXIT: event description: %s\n", ntv_descr);
684  return PAPI_OK;
685 }
686 
687 
688 int
689 _pe_libpfm4_ntv_code_to_info(unsigned int EventCode,
690  PAPI_event_info_t *info,
691  struct native_event_table_t *event_table)
692 {
693  SUBDBG("ENTER: EventCode: %#x, info: %p, event_table: %p\n", EventCode, info, event_table);
694 
695  int ret;
696 
697  // get the event name first
698  if ((ret = _pe_libpfm4_ntv_code_to_name(EventCode, info->symbol, sizeof(info->symbol), event_table)) != PAPI_OK) {
699  SUBDBG("EXIT: _pe_libpfm4_ntv_code_to_name returned: %d\n", ret);
700  return PAPI_ENOEVNT;
701  }
702 
703  if ((ret = _pe_libpfm4_ntv_code_to_descr(EventCode, info->long_descr, sizeof(info->long_descr), event_table)) != PAPI_OK) {
704  SUBDBG("EXIT: _pe_libpfm4_ntv_code_to_descr returned: %d\n", ret);
705  return PAPI_ENOEVNT;
706  }
707 
708  SUBDBG("EXIT: EventCode: %#x, name: %s, desc: %s\n", EventCode, info->symbol, info->long_descr);
709  return PAPI_OK;
710 }
711 
712 
729 int
730 _pe_libpfm4_ntv_enum_events( unsigned int *PapiEventCode,
731  int modifier,
732  struct native_event_table_t *event_table) {
733 
734  SUBDBG("ENTER: PapiEventCode: %p, *PapiEventCode: %#x, modifier: %d, event_table: %p\n", PapiEventCode, *PapiEventCode, modifier, event_table);
735 
736  int code,ret, pnum;
737  int max_umasks;
738  char event_string[BUFSIZ];
739  pfm_pmu_info_t pinfo;
740  pfm_event_info_t einfo;
741  struct native_event_t *our_event;
742 
743  /* return first event if so specified */
744  if ( modifier == PAPI_ENUM_FIRST ) {
745  attr_idx = 0; // set so if they want attribute information, it will start with the first attribute
746  code=get_first_event_next_pmu(-1, event_table->pmu_type);
747  if (code < 0 ) {
748  SUBDBG("EXIT: Invalid component first event code: %d\n", code);
749  return code;
750  }
751 
752  // get the event information from libpfm4 (must zero structure)
753  memset( &einfo, 0, sizeof( pfm_event_info_t ));
754  einfo.size = sizeof(pfm_event_info_t);
755  if ((ret = pfm_get_event_info(code, PFM_OS_PERF_EVENT_EXT, &einfo)) != PFM_SUCCESS) {
756  SUBDBG("EXIT: pfm_get_event_info returned: %d\n", ret);
757  return PAPI_ENOIMPL;
758  }
759 
760  // get the pmu information from libpfm4 (must zero structure)
761  memset( &pinfo, 0, sizeof(pfm_pmu_info_t) );
762  pinfo.size = sizeof(pfm_pmu_info_t);
763  ret=pfm_get_pmu_info(einfo.pmu, &pinfo);
764  if (ret!=PFM_SUCCESS) {
765  SUBDBG("EXIT: pfm_get_pmu_info returned: %d\n", ret);
766  return ret;
767  }
768 
769  // build full event name
770  sprintf (event_string, "%s::%s", pinfo.name, einfo.name);
771  SUBDBG("code: %#x, pmu: %s, event: %s, event_string: %s\n", code, pinfo.name, einfo.name, event_string);
772 
773  // go allocate this event, need to create tables used by the get event info call that will probably follow
774  if ((our_event = allocate_native_event(event_string, code, event_table)) == NULL) {
775  // allocate may have created the event table but returned NULL to tell the caller the event string was invalid (attempt to encode it failed).
776  // if the caller wants to use this event to count something, it will report an error
777  // but if the caller is just interested in listing the event, then we need an event table with an event name and libpfm4 index
778  int evt_idx;
779  if ((evt_idx = find_existing_event(event_string, event_table)) < 0) {
780  SUBDBG("EXIT: Allocating event: '%s' failed\n", event_string);
781  return PAPI_ENOEVNT;
782  }
783 
784  // give back the new event code
785  *PapiEventCode = event_table->native_events[evt_idx].libpfm4_idx;
786  SUBDBG("EXIT: event code: %#x\n", *PapiEventCode);
787  return PAPI_OK;
788  }
789 
790  *PapiEventCode = our_event->libpfm4_idx;
791 
792  SUBDBG("EXIT: *PapiEventCode: %#x\n", *PapiEventCode);
793  return PAPI_OK;
794  }
795 
796  /* Handle looking for the next event */
797  if ( modifier == PAPI_ENUM_EVENTS ) {
798  attr_idx = 0; // set so if they want attribute information, it will start with the first attribute
799 
800  // get the next event code from libpfm4, if there are no more in this pmu find first event in next pmu
801  if ((code = pfm_get_event_next(*PapiEventCode)) < 0) {
802 
803  // get this events information from libpfm4, we need the pmu number of the last event we processed (table must be cleared)
804  memset( &einfo, 0, sizeof( pfm_event_info_t ));
805  einfo.size = sizeof(pfm_event_info_t);
806  if ((ret = pfm_get_event_info(*PapiEventCode, PFM_OS_PERF_EVENT_EXT, &einfo)) != PFM_SUCCESS) {
807  SUBDBG("EXIT: pfm_get_event_info returned: %d\n", ret);
808  return PAPI_ENOIMPL;
809  }
810  SUBDBG("*PapiEventCode: %#x, event: %s\n", *PapiEventCode, einfo.name);
811 
812  // get the pmu number of the last event
813  pnum = einfo.pmu;
814 
815  while ( pnum<PFM_PMU_MAX) {
816  SUBDBG("pnum: %d\n", pnum);
817  code=get_first_event_next_pmu(pnum, event_table->pmu_type);
818  if (code < 0) {
819  SUBDBG("EXIT: No more pmu's to list, returning: %d\n", code);
820  return code;
821  }
822  break;
823  }
824  }
825 
826 
827  // get the event information from libpfm4 (must zero structure)
828  memset( &einfo, 0, sizeof( pfm_event_info_t ));
829  einfo.size = sizeof(pfm_event_info_t);
830  if ((ret = pfm_get_event_info(code, PFM_OS_PERF_EVENT_EXT, &einfo)) != PFM_SUCCESS) {
831  SUBDBG("EXIT: pfm_get_event_info returned: %d\n", ret);
832  return PAPI_ENOIMPL;
833  }
834 
835  // get the pmu information from libpfm4 (must zero structure)
836  memset( &pinfo, 0, sizeof(pfm_pmu_info_t) );
837  pinfo.size = sizeof(pfm_pmu_info_t);
838  ret=pfm_get_pmu_info(einfo.pmu, &pinfo);
839  if (ret!=PFM_SUCCESS) {
840  SUBDBG("EXIT: pfm_get_pmu_info returned: %d\n", ret);
841  return ret;
842  }
843 
844  // build full event name
845  sprintf (event_string, "%s::%s", pinfo.name, einfo.name);
846  SUBDBG("code: %#x, pmu: %s, event: %s, event_string: %s\n", code, pinfo.name, einfo.name, event_string);
847 
848  // go allocate this event, need to create tables used by the get event info call that will follow
849  if ((our_event = allocate_native_event(event_string, code, event_table)) == NULL) {
850  // allocate may have created the event table but returned NULL to tell the caller the event string was invalid (attempt to encode it failed).
851  // if the caller wants to use this event to count something, it will report an error
852  // but if the caller is just interested in listing the event, then we need an event table with an event name and libpfm4 index
853  int evt_idx;
854  if ((evt_idx = find_existing_event(event_string, event_table)) < 0) {
855  SUBDBG("EXIT: Allocating event: '%s' failed\n", event_string);
856  return PAPI_ENOEVNT;
857  }
858 
859  // give back the new event code
860  *PapiEventCode = event_table->native_events[evt_idx].libpfm4_idx;
861  SUBDBG("EXIT: event code: %#x\n", *PapiEventCode);
862  return PAPI_OK;
863  }
864 
865  // give back the new event code
866  *PapiEventCode = our_event->libpfm4_idx;
867 
868  SUBDBG("EXIT: *PapiEventCode: %#x\n", *PapiEventCode);
869  return PAPI_OK;
870  }
871 
872  /* We don't handle PAPI_NTV_ENUM_UMASK_COMBOS */
873  if ( modifier == PAPI_NTV_ENUM_UMASK_COMBOS ) {
874  SUBDBG("EXIT: do not support umask combos yet\n");
875  return PAPI_ENOIMPL;
876  }
877 
878  /* Enumerate PAPI_NTV_ENUM_UMASKS (umasks on an event) */
879  if ( modifier == PAPI_NTV_ENUM_UMASKS ) {
880  // get this events information from libpfm4, we need the number of masks this event knows about (table must be cleared)
881  memset( &einfo, 0, sizeof( pfm_event_info_t ));
882  einfo.size = sizeof(pfm_event_info_t);
883  if ((ret = pfm_get_event_info(*PapiEventCode, PFM_OS_PERF_EVENT_EXT, &einfo)) != PFM_SUCCESS) {
884  SUBDBG("EXIT: pfm_get_event_info returned: %d\n", ret);
885  return PAPI_ENOIMPL;
886  }
887 // SUBDBG("*PapiEventCode: %#x, einfo.name: %s, einfo.code: %#x, einfo.nattrs: %d\n", *PapiEventCode, einfo.name, einfo.code, einfo.nattrs);
888 
889  // set max number of masks
890  max_umasks = einfo.nattrs;
891 
892  // if we reached last attribute, return error to show we are done with this events masks
893  if (attr_idx == max_umasks) {
894  SUBDBG("EXIT: already processed all umasks: attr_idx: %d\n", attr_idx);
895  return PAPI_ENOEVNT;
896  }
897 
898  // find the event table for this event, we need the pmu name and event name without any masks
900  if (ntv_idx < 0) {
901  SUBDBG("EXIT: _papi_hwi_get_ntv_idx returned: %d\n", ntv_idx);
902  return ntv_idx;
903  }
904  char *ename = event_table->native_events[ntv_idx].pmu_plus_name;
905  if ((ename == NULL) || (strlen(ename) >= sizeof(event_string))) {
906  SUBDBG("EXIT: Event name will not fit into buffer\n");
907  return PAPI_EBUF;
908  }
909  strcpy (event_string, ename);
910  SUBDBG("event_string: %s\n", event_string);
911 
912  // go get the attribute information for this event
913  // libpfm4 likes the table cleared
914  pfm_event_attr_info_t ainfo;
915  memset (&ainfo, 0, sizeof(pfm_event_attr_info_t));
916  ainfo.size = sizeof(pfm_event_attr_info_t);
917  ret = pfm_get_event_attr_info(*PapiEventCode, attr_idx, PFM_OS_PERF_EVENT_EXT, &ainfo);
918  if (ret != PFM_SUCCESS) {
919  SUBDBG("EXIT: Attribute info not found, EventCode: %#x, attr_idx: %d, ret: %d\n", *PapiEventCode, attr_idx, _papi_libpfm4_error(ret));
920  return _papi_libpfm4_error(ret);
921  }
922  SUBDBG("*PapiEventCode: %#x, attr_idx: %d, type: %d, name: %s, description: %s\n", *PapiEventCode, attr_idx, ainfo.type, ainfo.name, ainfo.desc);
923 
924  if (strlen(event_string) + strlen(ainfo.name) + 35 > sizeof(event_string)) {
925  SUBDBG("EXIT: Event name and mask will not fit into buffer\n");
926  return PAPI_EBUF;
927  }
928 
929  strcat (event_string, ":");
930  strcat (event_string, ainfo.name);
931  switch (ainfo.type) {
932  case PFM_ATTR_UMASK:
933  break;
934  case PFM_ATTR_MOD_BOOL:
935  case PFM_ATTR_MOD_INTEGER:
936  // a few attributes require a non-zero value to encode correctly (most would accept zero here)
937  strcat(event_string,"=0");
938  break;
939  default:
940  SUBDBG("EXIT: Unsupported attribute type: %d", ainfo.type);
941  return PAPI_EATTR;
942  }
943 
944  // go allocate this event, need to create tables used by the get event info call that will follow
945  if ((our_event = allocate_native_event(event_string, *PapiEventCode, event_table)) == NULL) {
946  // allocate may have created the event table but returned NULL to tell the caller the event string was invalid.
947  // if the caller wants to use this event to count something, it must report the error
948  // but if the caller is just interested in listing the event (like this code), then find the table that was created and return its libpfm4 index
949  int evt_idx;
950  if ((evt_idx = find_existing_event(event_string, event_table)) < 0) {
951  SUBDBG("EXIT: Allocating event: '%s' failed\n", event_string);
952  return PAPI_ENOEVNT;
953  }
954  // bump so next time we will use next attribute
955  attr_idx++;
956  // give back the new event code
957  *PapiEventCode = event_table->native_events[evt_idx].libpfm4_idx;
958  SUBDBG("EXIT: event code: %#x\n", *PapiEventCode);
959  return PAPI_OK;
960  }
961 
962  // bump so next time we will use next attribute
963  attr_idx++;
964 
965  // give back the new event code
966  *PapiEventCode = our_event->libpfm4_idx;
967 
968  SUBDBG("EXIT: event code: %#x\n", *PapiEventCode);
969  return PAPI_OK;
970  }
971 
972  /* Enumerate PAPI_NTV_ENUM_GROUPS (groups on an event) */
973  if ( modifier == PAPI_NTV_ENUM_GROUPS ) {
974  SUBDBG("EXIT: do not support enumerating groups in this component\n");
975  return PAPI_ENOIMPL;
976  }
977 
978  /* An unknown enumeration method was indicated */
979 
980  SUBDBG("EXIT: Invalid modifier argument provided\n");
981  return PAPI_ENOIMPL;
982 }
983 
984 
995 int
997  struct native_event_table_t *event_table) {
998  SUBDBG("ENTER: event_table: %p\n", event_table);
999 
1000  int i;
1001 
1002  for (i=0 ; i<PAPI_PMU_MAX ; i++) {
1003  if (my_vector->cmp_info.pmu_names[i] != NULL) {
1004  free (my_vector->cmp_info.pmu_names[i]);
1005  }
1006  }
1007 
1008  /* clean out and free the native events structure */
1010 
1011  /* free memory allocated with strdup or malloc */
1012  for( i=0; i<event_table->num_native_events; i++) {
1013  free(event_table->native_events[i].base_name);
1014  free(event_table->native_events[i].pmu_plus_name);
1015  free(event_table->native_events[i].pmu);
1016  free(event_table->native_events[i].allocated_name);
1017  free(event_table->native_events[i].mask_string);
1018  free(event_table->native_events[i].event_description);
1019  if (event_table->native_events[i].mask_description != NULL) {
1020  free(event_table->native_events[i].mask_description);
1021  }
1022  }
1023 
1024  free(event_table->native_events);
1025 
1027 
1028  SUBDBG("EXIT: PAPI_OK\n");
1029  return PAPI_OK;
1030 }
1031 
1032 
1044 int
1046  struct native_event_table_t *event_table,
1047  int pmu_type) {
1048 
1049  int detected_pmus=0, found_default=0;
1050  int i;
1051  int j=0;
1052  pfm_err_t retval = PFM_SUCCESS;
1053  unsigned int ncnt;
1054  pfm_pmu_info_t pinfo;
1055 
1056  /* allocate the native event structure */
1057 
1058  event_table->num_native_events=0;
1059  event_table->pmu_type=pmu_type;
1060 
1061  event_table->native_events=calloc(NATIVE_EVENT_CHUNK,
1062  sizeof(struct native_event_t));
1063  if (event_table->native_events==NULL) {
1064  return PAPI_ENOMEM;
1065  }
1067 
1068  /* Count number of present PMUs */
1069  detected_pmus=0;
1070  ncnt=0;
1071 
1072  /* init default pmu */
1073  /* need to init pinfo or pfmlib might complain */
1074  memset(&(event_table->default_pmu), 0, sizeof(pfm_pmu_info_t));
1075  event_table->default_pmu.size = sizeof(pfm_pmu_info_t);
1076  retval=pfm_get_pmu_info(0, &(event_table->default_pmu));
1077 
1078  SUBDBG("Detected pmus:\n");
1079  for(i=0;i<PFM_PMU_MAX;i++) {
1080  memset(&pinfo,0,sizeof(pfm_pmu_info_t));
1081  pinfo.size = sizeof(pfm_pmu_info_t);
1082  retval=pfm_get_pmu_info(i, &pinfo);
1083  if ((retval!=PFM_SUCCESS) || (pinfo.name == NULL)) {
1084  continue;
1085  }
1086 
1087  if (pmu_is_present_and_right_type(&pinfo,pmu_type)) {
1088  SUBDBG("\t%d %s %s %d\n",i,pinfo.name,pinfo.desc,pinfo.type);
1089 
1090  detected_pmus++;
1091  ncnt+=pinfo.nevents;
1092 
1093  if (j < PAPI_PMU_MAX) {
1094  my_vector->cmp_info.pmu_names[j++] = strdup(pinfo.name);
1095  }
1096 
1097  if (pmu_type&PMU_TYPE_CORE) {
1098 
1099  /* Hack to have "default" PMU */
1100  if ( (pinfo.type==PFM_PMU_TYPE_CORE) &&
1101  strcmp(pinfo.name,"ix86arch")) {
1102 
1103  SUBDBG("\t %s is default\n",pinfo.name);
1104  memcpy(&(event_table->default_pmu),
1105  &pinfo,sizeof(pfm_pmu_info_t));
1106  found_default++;
1107  }
1108  }
1109  if (pmu_type==PMU_TYPE_UNCORE) {
1110  /* To avoid confusion, no "default" CPU for uncore */
1111  found_default=1;
1112  }
1113  }
1114  }
1115  SUBDBG("%d native events detected on %d pmus\n",ncnt,detected_pmus);
1116 
1117  if (!found_default) {
1118  SUBDBG("Could not find default PMU\n");
1119  return PAPI_ECMP;
1120  }
1121 
1122  if (found_default>1) {
1123  PAPIERROR("Found too many default PMUs!\n");
1124  return PAPI_ECMP;
1125  }
1126 
1127  my_vector->cmp_info.num_native_events = ncnt;
1128 
1129  my_vector->cmp_info.num_cntrs = event_table->default_pmu.num_cntrs+
1130  event_table->default_pmu.num_fixed_cntrs;
1131 
1132  SUBDBG( "num_counters: %d\n", my_vector->cmp_info.num_cntrs );
1133 
1134  /* Setup presets, only if Component 0 */
1135  if (cidx==0) {
1136  retval = _papi_load_preset_table( (char *)event_table->default_pmu.name,
1137  event_table->default_pmu.pmu, cidx );
1138  if ( retval ) {
1139  return retval;
1140  }
1141  }
1142 
1143  return PAPI_OK;
1144 }
1145 
#define PAPI_ENOEVNT
Definition: papi.h:258
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
memset(eventId, 0, size)
static int get_first_event_next_pmu(int pmu_idx, int pmu_type)
int _papi_hwi_get_ntv_idx(unsigned int papi_evt_code)
static unsigned int papi_event_code
unsigned int _papi_hwi_get_papi_event_code()
int _pe_libpfm4_ntv_enum_events(unsigned int *PapiEventCode, int modifier, struct native_event_table_t *event_table)
struct native_event_t * native_events
int _pe_libpfm4_ntv_name_to_code(char *name, unsigned int *event_code, struct native_event_table_t *event_table)
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:966
#define NATIVE_EVENT_CHUNK
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:963
device[deviceId] domain[domainId] event
Definition: linux-cuda.c:306
return PAPI_OK
Definition: linux-nvml.c:458
#define PAPI_EBUF
Definition: papi.h:271
#define PMU_TYPE_OS
int _pe_libpfm4_get_cidx(void)
Definition: perf_event.c:71
int _pe_libpfm4_init(papi_vector_t *my_vector, int cidx, struct native_event_table_t *event_table, int pmu_type)
PAPI_component_info_t cmp_info
Definition: papi_vector.h:20
int _papi_libpfm4_error(int pfm_error)
#define PAPI_HUGE_STR_LEN
Definition: papi.h:465
Return codes and api definitions.
static int attr_idx
long long ret
Definition: iozone.c:1346
int _pe_libpfm4_ntv_code_to_name(unsigned int EventCode, char *ntv_name, int len, struct native_event_table_t *event_table)
int i
Definition: fileop.c:140
inline_static int _papi_hwi_lock(int lck)
Definition: threads.h:64
int _papi_hwi_native_to_eventcode(int cidx, int event_code, int ntv_idx, const char *event_name)
#define PAPI_PMU_MAX
Definition: papi.h:467
static struct native_event_t * allocate_native_event(char *name, int libpfm4_index, struct native_event_table_t *event_table)
static int find_existing_event(char *name, struct native_event_table_t *event_table)
free(dummyfile[xx])
#define PAPI_EATTR
Definition: papi.h:273
static int cidx
Definition: event_info.c:40
char * pmu_names[PAPI_PMU_MAX]
Definition: papi.h:647
inline_static int _papi_hwi_unlock(int lck)
Definition: threads.h:78
#define SUBDBG(format, args...)
Definition: papi_debug.h:63
void PAPIERROR(char *format,...)
#define PAPI_ECMP
Definition: papi.h:254
strcat(command, mountname)
#define PMU_TYPE_CORE
int _papi_load_preset_table(char *pmu_str, int pmu_type, int cidx)
Definition: papi_preset.c:299
#define PAPI_ENOMEM
Definition: papi.h:252
#define NAMELIB_LOCK
Definition: papi_internal.h:93
#define PAPI_ENOIMPL
Definition: papi.h:270
strcpy(filename, default_filename)
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 pmu_is_present_and_right_type(pfm_pmu_info_t *pinfo, int type)
void _papi_hwi_set_papi_event_code(unsigned int event_code, int update_flag)
#define PMU_TYPE_UNCORE
int _pe_libpfm4_shutdown(papi_vector_t *my_vector, struct native_event_table_t *event_table)
perf_event_attr_t attr
char * name
Definition: iozone.c:23648
pfm_pmu_info_t default_pmu
long j
Definition: iozone.c:19135
ssize_t retval
Definition: libasync.c:338
void _papi_hwi_set_papi_event_string(const char *event_string)
Definition: papi_internal.c:89
char * ptr
Definition: iozone.c:23586