PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PAPI_overflow.c
Go to the documentation of this file.
1 /*****************************************************************************
2 * This example shows how to use PAPI_overflow to set up an event set to *
3 * begin registering overflows.
4 ******************************************************************************/
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "papi.h" /* This needs to be included every time you use PAPI */
9 #include <pthread.h>
10 
11 #define OVER_FMT "handler(%d ) Overflow at %p! bit=%#llx \n"
12 #define THRESHOLD 100000
13 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
14 
15 int total = 0; /* we use total to track the amount of overflows that occured */
16 
17 /* THis is the handler called by PAPI_overflow*/
18 void
19 handler(int EventSet, void *address, long long overflow_vector, void *context)
20 {
21  fprintf(stderr, OVER_FMT, EventSet, address, overflow_vector);
22  total++;
23 }
24 
25 
26 int main ()
27 {
28  int EventSet = PAPI_NULL;
29  /* must be set to null before calling PAPI_create_eventset */
30 
31  char errstring[PAPI_MAX_STR_LEN];
32  long long (values[2])[2];
33  int retval, i;
34  double tmp = 0;
35  int PAPI_event; /* a place holder for an event preset */
37 
38 
39  /****************************************************************************
40  * This part initializes the library and compares the version number of the *
41  * header file, to the version of the library, if these don't match then it *
42  * is likely that PAPI won't work correctly.If there is an error, retval *
43  * keeps track of the version number. *
44  ****************************************************************************/
45 
47  {
48  printf("Library initialization error! \n");
49  exit(1);
50  }
51 
52  /* Here we create the eventset */
53  if ((retval=PAPI_create_eventset (&EventSet)) != PAPI_OK)
54  ERROR_RETURN(retval);
55 
56  PAPI_event = PAPI_TOT_INS;
57 
58  /* Here we are querying for the existence of the PAPI presets */
60  {
61  PAPI_event = PAPI_TOT_CYC;
62 
63  if ((retval=PAPI_query_event (PAPI_TOT_INS)) != PAPI_OK)
64  ERROR_RETURN(retval);
65 
66  printf ("PAPI_TOT_INS not available on this platform.");
67  printf (" so subst PAPI_event with PAPI_TOT_CYC !\n\n");
68 
69  }
70 
71 
72  /* PAPI_event_code_to_name is used to convert a PAPI preset from
73  its integer value to its string name. */
74  if ((retval = PAPI_event_code_to_name (PAPI_event, event_name)) != PAPI_OK)
75  ERROR_RETURN(retval);
76 
77  /* add event to the event set */
78  if ((retval = PAPI_add_event (EventSet, PAPI_event)) != PAPI_OK)
79  ERROR_RETURN(retval);
80 
81  /* register overflow and set up threshold */
82  /* The threshold "THRESHOLD" was set to 100000 */
83  if ((retval = PAPI_overflow (EventSet, PAPI_event, THRESHOLD, 0,
84  handler)) != PAPI_OK)
85  ERROR_RETURN(retval);
86 
87  printf ("Here are the addresses at which overflows occured and overflow vectors \n");
88  printf ("--------------------------------------------------------------\n");
89 
90 
91  /* Start counting */
92 
93  if ( (retval=PAPI_start (EventSet)) != PAPI_OK)
94  ERROR_RETURN(retval);
95 
96  for (i = 0; i < 2000000; i++)
97  {
98  tmp = 1.01 + tmp;
99  tmp++;
100  }
101 
102  /* Stops the counters and reads the counter values into the values array */
103  if ( (retval=PAPI_stop (EventSet, values[0])) != PAPI_OK)
104  ERROR_RETURN(retval);
105 
106 
107  printf ("The total no of overflows was %d\n", total);
108 
109  /* clear the overflow status */
110  if ((retval = PAPI_overflow (EventSet, PAPI_event, 0, 0,
111  handler)) != PAPI_OK)
112  ERROR_RETURN(retval);
113 
114  /************************************************************************
115  * PAPI_cleanup_eventset can only be used after the counter has been *
116  * stopped then it remove all events in the eventset *
117  ************************************************************************/
118  if ( (retval=PAPI_cleanup_eventset (EventSet)) != PAPI_OK)
119  ERROR_RETURN(retval);
120 
121  /* Free all memory and data structures, EventSet must be empty. */
122  if ( (retval=PAPI_destroy_eventset(&EventSet)) != PAPI_OK)
123  ERROR_RETURN(retval);
124 
125  /* free the resources used by PAPI */
126  PAPI_shutdown();
127 
128  exit(0);
129 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
#define OVER_FMT
Definition: PAPI_overflow.c:11
int PAPI_event[2]
Definition: data_range.c:24
Empty and destroy an EventSet.
#define PAPI_NULL
Definition: papi.h:290
int EventSet
Definition: data_range.c:25
#define PAPI_TOT_INS
return PAPI_OK
Definition: linux-nvml.c:458
#define printf
Definition: papi_test.h:125
Return codes and api definitions.
Stop counting hardware events in an event set.
Create a new empty PAPI EventSet.
int i
Definition: fileop.c:140
Start counting hardware events in an event set.
void PAPI_shutdown(void)
Definition: papi.c:4400
#define PAPI_TOT_CYC
long long
Definition: iozone.c:19827
#define THRESHOLD
Definition: PAPI_overflow.c:12
void handler(int EventSet, void *address, long long overflow_vector, void *context)
Definition: rapl_overflow.c:16
Query if PAPI event exists.
static int total
Definition: rapl_overflow.c:5
add PAPI preset or native hardware event to an event set
initialize the PAPI library.
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1971
#define PAPI_MAX_STR_LEN
Definition: papi.h:463
Convert a numeric hardware event code to a name.
#define ERROR_RETURN(retval)
Definition: PAPI_overflow.c:13
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long tmp
Definition: iozone.c:12031
#define PAPI_VER_CURRENT
Definition: papi.h:223
int main(int argc, char **argv)
List all appio events codes and names.
Set up an event set to begin registering overflows.
void exit()