PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PAPI_add_remove_events.c
Go to the documentation of this file.
1 /******************************************************************************
2  * This is a simple low level function demonstration on using PAPI_add_events *
3  * to add an array of events to a created eventset, we are going to use these *
4  * events to monitor a set of instructions, start the counters, read the *
5  * counters and then cleanup the eventset when done. In this example we use *
6  * the presets PAPI_TOT_INS and PAPI_TOT_CYC. PAPI_add_events,PAPI_start, *
7  * PAPI_stop, PAPI_clean_eventset, PAPI_destroy_eventset and *
8  * PAPI_create_eventset all return PAPI_OK(which is 0) when succesful. *
9  ******************************************************************************/
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include "papi.h" /* This needs to be included every time you use PAPI */
14 
15 #define NUM_EVENT 2
16 #define THRESHOLD 100000
17 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
18 
19 
20 int main(){
21 
22  int i,retval,tmp;
23  int EventSet = PAPI_NULL;
24  /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
25 
26  int event_codes[NUM_EVENT]={PAPI_TOT_INS,PAPI_TOT_CYC};
27  char errstring[PAPI_MAX_STR_LEN];
28  long long values[NUM_EVENT];
29 
30  /***************************************************************************
31  * This part initializes the library and compares the version number of the *
32  * header file, to the version of the library, if these don't match then it *
33  * is likely that PAPI won't work correctly.If there is an error, retval *
34  * keeps track of the version number. *
35  ****************************************************************************/
36 
38  {
39  fprintf(stderr, "Error: %s\n", errstring);
40  exit(1);
41  }
42 
43 
44  /* Creating event set */
45  if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK)
46  ERROR_RETURN(retval);
47 
48 
49  /* Add the array of events PAPI_TOT_INS and PAPI_TOT_CYC to the eventset*/
50  if ((retval=PAPI_add_events(EventSet, event_codes, NUM_EVENT)) != PAPI_OK)
51  ERROR_RETURN(retval);
52 
53 
54  /* Start counting */
55  if ( (retval=PAPI_start(EventSet)) != PAPI_OK)
56  ERROR_RETURN(retval);
57 
58  /*** this is where your computation goes *********/
59  for(i=0;i<1000;i++)
60  {
61  tmp = tmp+i;
62  }
63 
64  /* Stop counting, this reads from the counter as well as stop it. */
65  if ( (retval=PAPI_stop(EventSet,values)) != PAPI_OK)
66  ERROR_RETURN(retval);
67 
68  printf("\nThe total instructions executed are %lld, total cycles %lld\n",
69  values[0],values[1]);
70 
71 
72  if ( (retval=PAPI_remove_events(EventSet,event_codes, NUM_EVENT))!=PAPI_OK)
73  ERROR_RETURN(retval);
74 
75  /* Free all memory and data structures, EventSet must be empty. */
76  if ( (retval=PAPI_destroy_eventset(&EventSet)) != PAPI_OK)
77  ERROR_RETURN(retval);
78 
79  /* free the resources used by PAPI */
80  PAPI_shutdown();
81 
82  exit(0);
83 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
#define PAPI_NULL
Definition: papi.h:290
int EventSet
Definition: data_range.c:25
#define NUM_EVENT
#define PAPI_TOT_INS
return PAPI_OK
Definition: linux-nvml.c:458
int PAPI_remove_events(int EventSet, int *Events, int number)
Definition: papi.c:5857
#define printf
Definition: papi_test.h:125
int PAPI_add_events(int EventSet, int *Events, int number)
Definition: papi.c:5769
Return codes and api definitions.
#define ERROR_RETURN(retval)
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
void PAPI_shutdown(void)
Definition: papi.c:4400
#define PAPI_TOT_CYC
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1971
#define PAPI_MAX_STR_LEN
Definition: papi.h:463
int PAPI_start(int EventSet)
Definition: papi.c:2053
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.
void exit()