PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Papi_add_env_event.c
Go to the documentation of this file.
1 /*
2  * This example shows how to use PAPI_library_init, PAPI_create_eventset,
3  * PAPI_add_event, * PAPI_start and PAPI_stop. These 5 functions
4  * will allow a user to do most of the performance information gathering
5  * that they would need. PAPI_read could also be used if you don't want
6  * to stop the EventSet from running but only check the counts.
7  *
8  * Also, we will use PAPI_perror for * error information.
9  *
10  * In addition, a new call was created called PAPI_add_env_event
11  * that allows a user to setup environment variable to read
12  * which event should be monitored this allows different events
13  * to be monitored at runtime without recompiling, the syntax
14  * is as follows:
15  * PAPI_add_env_event(int *EventSet, int *Event, char *env_variable);
16  * EventSet is the same as in PAPI_add_event
17  * Event is the default event to monitor if the environment variable
18  * does not exist and differs from PAPI_add_event as it is
19  * a pointer.
20  * env_varialbe is the name of the environment variable to look for
21  * the event code, this can be a name, number or hex, for example
22  * PAPI_L1_DCM could be defined in the environment variable as
23  * all of the following: PAPI_L1_DCM, 0x80000000, or -2147483648
24  *
25  * To use only add_event you would change the calls to
26  * PAPI_add_env_event(int *EventSet, int *Event, char *env_variable);
27  * to PAPI_add_event(int *EventSet, int Event);
28  *
29  * We will also use PAPI_event_code_to_name since the event may have
30  * changed.
31  * Author: Kevin London
32  * email: london@cs.utk.edu
33  */
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include "papi.h" /* This needs to be included anytime you use PAPI */
37 
38 int PAPI_add_env_event(int *EventSet, int *Event, char *env_variable);
39 
40 
41 int main(){
42  int retval,i;
43  int EventSet=PAPI_NULL;
44  int event_code=PAPI_TOT_INS; /* By default monitor total instructions */
45  char errstring[PAPI_MAX_STR_LEN];
47  float a[1000],b[1000],c[1000];
48  long long values;
49 
50 
51  /* This initializes the library and checks the version number of the
52  * header file, to the version of the library, if these don't match
53  * then it is likely that PAPI won't work correctly.
54  */
56  /* This call loads up what the error means into errstring
57  * if retval == PAPI_ESYS then it might be beneficial
58  * to call perror as well to see what system call failed
59  */
60  PAPI_perror("PAPI_library_init");
61  exit(-1);
62  }
63  /* Create space for the EventSet */
64  if ( (retval=PAPI_create_eventset( &EventSet ))!=PAPI_OK){
65  PAPI_perror(retval, errstring, PAPI_MAX_STR_LEN);
66  exit(-1);
67  }
68 
69  /* After this call if the environment variable PAPI_EVENT is set,
70  * event_code may contain something different than total instructions.
71  */
72  if ( (retval=PAPI_add_env_event(&EventSet, &event_code, "PAPI_EVENT"))!=PAPI_OK){
73  PAPI_perror("PAPI_add_env_event");
74  exit(-1);
75  }
76  /* Now lets start counting */
77  if ( (retval = PAPI_start(EventSet)) != PAPI_OK ){
78  PAPI_perror("PAPI_start");
79  exit(-1);
80  }
81 
82  /* Some work to take up some time, the PAPI_start/PAPI_stop (and/or
83  * PAPI_read) should surround what you want to monitor.
84  */
85  for ( i=0;i<1000;i++){
86  a[i] = b[i]-c[i];
87  c[i] = a[i]*1.2;
88  }
89 
90  if ( (retval = PAPI_stop(EventSet, &values) ) != PAPI_OK ){
91  PAPI_perror("PAPI_stop");
92  exit(-1);
93  }
94 
95  if ( (retval=PAPI_event_code_to_name( event_code, event_name))!=PAPI_OK){
96  PAPI_perror("PAPI_event_code_to_name");
97  exit(-1);
98  }
99 
100  printf("Ending values for %s: %lld\n", event_name,values);
101  /* Remove PAPI instrumentation, this is necessary on platforms
102  * that need to release shared memory segments and is always
103  * good practice.
104  */
105  PAPI_shutdown();
106  exit(0);
107 }
108 
109 
110 
111 int PAPI_add_env_event(int *EventSet, int *EventCode, char *env_variable){
112  int real_event=*EventCode;
113  char *eventname;
114  int retval;
115 
116  if ( env_variable != NULL ){
117  if ( (eventname=getenv(env_variable)) ) {
118  if ( eventname[0] == 'P' ) { /* Use the PAPI name */
119  retval=PAPI_event_name_to_code(eventname, &real_event );
120  if ( retval != PAPI_OK ) real_event = *EventCode;
121  }
122  else{
123  if ( strlen(eventname)>1 && eventname[1]=='x')
124  sscanf(eventname, "%#x", &real_event);
125  else
126  real_event = atoi(eventname);
127  }
128  }
129  }
130  if ( (retval = PAPI_add_event( *EventSet, real_event))!= PAPI_OK ){
131  if ( real_event != *EventCode ) {
132  if ( (retval = PAPI_add_event( *EventSet, *EventCode)) == PAPI_OK
133 ){
134  real_event = *EventCode;
135  }
136  }
137  }
138  *EventCode = real_event;
139  return retval;
140 }
141 
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
int atoi()
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
char * getenv()
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1604
void PAPI_perror(char *msg)
Definition: papi.c:4533
#define PAPI_NULL
Definition: fpapi.h:13
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
#define PAPI_TOT_INS
Definition: fpapi.h:185
return PAPI_OK
Definition: linux-nvml.c:458
int PAPI_add_env_event(int *EventSet, int *Event, char *env_variable)
#define printf
Definition: papi_test.h:125
double c
Definition: multiplex.c:22
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:37
Return codes and api definitions.
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
void PAPI_shutdown(void)
Definition: papi.c:4357
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1406
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:924
int PAPI_event_name_to_code(char *in, int *out)
Definition: papi.c:1008
int EventSet
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:38
sscanf(mnc->m_child_port,"%d",&mc.m_child_port)
int Event[MAX_EVENTS]
int PAPI_start(int EventSet)
Definition: papi.c:2026
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
int main(int argc, char **argv)
List all appio events codes and names.
void exit()