PAPI 7.1.0.0
Loading...
Searching...
No Matches
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
38int PAPI_add_env_event(int *EventSet, int *Event, char *env_variable);
39
40
41int main(){
42 int retval,i;
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 */
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 */
106 exit(0);
107}
108
109
110
111int 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 */
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
int i
int PAPI_add_env_event(int *EventSet, int *Event, char *env_variable)
int main()
static int Event[MAX_EVENTS]
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Convert a numeric hardware event code to a name.
Convert a name to a numeric hardware event code.
initialize the PAPI library.
Produces a string on standard error, describing the last library error.
Finish using PAPI and free all related resources.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
char * eventname
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
#define PAPI_TOT_INS
Definition: f90papi.h:317
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:38
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:39
static double c[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:40
Return codes and api definitions.
int retval
Definition: zero_fork.c:53