PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PAPI_add_remove_event.c
Go to the documentation of this file.
1 /*****************************************************************************
2 * This example shows how to use PAPI_add_event, PAPI_start, PAPI_read, *
3 * PAPI_stop and PAPI_remove_event. *
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 
10 #define NUM_EVENTS 2
11 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
12 
13 int main()
14 {
15  int EventSet = PAPI_NULL;
16  int tmp, i;
17  /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
18 
19  long long values[NUM_EVENTS];
20  /*This is where we store the values we read from the eventset */
21 
22  /* We use number to keep track of the number of events in the EventSet */
23  int retval, number;
24 
25  char errstring[PAPI_MAX_STR_LEN];
26 
27  /***************************************************************************
28  * This part initializes the library and compares the version number of the*
29  * header file, to the version of the library, if these don't match then it *
30  * is likely that PAPI won't work correctly.If there is an error, retval *
31  * keeps track of the version number. *
32  ***************************************************************************/
33 
34 
36  ERROR_RETURN(retval);
37 
38 
39  /* Creating the eventset */
40  if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
41  ERROR_RETURN(retval);
42 
43  /* Add Total Instructions Executed to the EventSet */
44  if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
45  ERROR_RETURN(retval);
46 
47  /* Add Total Cycles event to the EventSet */
48  if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
49  ERROR_RETURN(retval);
50 
51  /* get the number of events in the event set */
52  number = 0;
53  if ( (retval = PAPI_list_events(EventSet, NULL, &number)) != PAPI_OK)
54  ERROR_RETURN(retval);
55 
56  printf("There are %d events in the event set\n", number);
57 
58  /* Start counting */
59 
60  if ( (retval = PAPI_start(EventSet)) != PAPI_OK)
61  ERROR_RETURN(retval);
62 
63  /* you can replace your code here */
64  tmp=0;
65  for (i = 0; i < 2000000; i++)
66  {
67  tmp = i + tmp;
68  }
69 
70 
71  /* read the counter values and store them in the values array */
72  if ( (retval=PAPI_read(EventSet, values)) != PAPI_OK)
73  ERROR_RETURN(retval);
74 
75  printf("The total instructions executed for the first loop are %lld \n", values[0] );
76  printf("The total cycles executed for the first loop are %lld \n",values[1]);
77 
78  /* our slow code again */
79  tmp=0;
80  for (i = 0; i < 2000000; i++)
81  {
82  tmp = i + tmp;
83  }
84 
85  /* Stop counting and store the values into the array */
86  if ( (retval = PAPI_stop(EventSet, values)) != PAPI_OK)
87  ERROR_RETURN(retval);
88 
89  printf("Total instructions executed are %lld \n", values[0] );
90  printf("Total cycles executed are %lld \n",values[1]);
91 
92  /* Remove event: We are going to take the PAPI_TOT_INS from the eventset */
93  if( (retval = PAPI_remove_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
94  ERROR_RETURN(retval);
95  printf("Removing PAPI_TOT_INS from the eventset\n");
96 
97  /* Now we list how many events are left on the event set */
98  number = 0;
99  if ((retval=PAPI_list_events(EventSet, NULL, &number))!= PAPI_OK)
100  ERROR_RETURN(retval);
101 
102  printf("There is only %d event left in the eventset now\n", number);
103 
104  /* free the resources used by PAPI */
105  PAPI_shutdown();
106 
107  exit(0);
108 }
109 
110 
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2225
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1597
#define PAPI_NULL
Definition: fpapi.h:13
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1692
#define PAPI_TOT_INS
Definition: fpapi.h:185
return PAPI_OK
Definition: linux-nvml.c:458
#define ERROR_RETURN(retval)
#define printf
Definition: papi_test.h:125
Return codes and api definitions.
#define NUM_EVENTS
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
#define PAPI_TOT_CYC
Definition: fpapi.h:194
void PAPI_shutdown(void)
Definition: papi.c:4344
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1399
int EventSet
int PAPI_read(int EventSet, long long *values)
Definition: papi.c:2469
int PAPI_start(int EventSet)
Definition: papi.c:2019
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
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int PAPI_list_events(int EventSet, int *Events, int *number)
Definition: papi.c:5863