PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rapl_overflow.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include "papi.h"
3 #include "papi_test.h"
4 
5 static int total = 0; /* total overflows */
6 
7 static long long values[2];
8 static long long rapl_values[2];
9 static long long old_rapl_values[2] = {0,0};
10 static int rapl_backward=0;
11 
13 
14 int quiet=0;
15 
16 void handler( int EventSet, void *address,
17  long long overflow_vector, void *context ) {
18 
19  ( void ) context;
20  ( void ) address;
21  ( void ) overflow_vector;
22 
23 #if 0
24  fprintf( stderr, "handler(%d ) Overflow at %p! bit=%#llx \n",
25  EventSet, address, overflow_vector );
26 #endif
27 
28  PAPI_read(EventSet,values);
29  if (!quiet) printf("%lld %lld\t",values[0],values[1]);
31  if (!quiet) printf("RAPL: %lld %lld\n",rapl_values[0],rapl_values[1]);
32 
33  if ((rapl_values[0]<old_rapl_values[0]) ||
34  (rapl_values[1]<old_rapl_values[1])) {
35  if (!quiet) printf("RAPL decreased!\n");
36  rapl_backward=1;
37  }
40 
41 
42  total++;
43 }
44 
45 
46 void do_ints(int n,int quiet)
47 {
48  int i,c=n;
49 
50  for(i=0;i<n;i++) {
51  c+=c*i*n;
52  }
53  if (!quiet) printf("%d\n",c);
54 }
55 
56 
57 
58 int
59 main( int argc, char **argv )
60 {
61  int EventSet = PAPI_NULL;
62  long long values0[2],values1[2],values2[2];
63  int num_flops = 3000000, retval;
64  int mythreshold = 1000000;
65  char event_name1[PAPI_MAX_STR_LEN];
66  int PAPI_event;
67  int cid,numcmp,rapl_cid;
68  const PAPI_component_info_t *cmpinfo = NULL;
69  int i;
70 
71  /* Set TESTS_QUIET variable */
72  tests_quiet( argc, argv );
73 
75 
76  /* Init PAPI */
78  if ( retval != PAPI_VER_CURRENT ) {
79  test_fail(__FILE__, __LINE__,"PAPI_library_init",retval);
80  }
81 
82  numcmp = PAPI_num_components();
83 
84  for(cid=0; cid<numcmp; cid++) {
85 
86  if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
87  test_fail(__FILE__, __LINE__,"PAPI_get_component_info failed\n", 0);
88  }
89 
90  if (strstr(cmpinfo->name,"rapl")) {
91  rapl_cid=cid;
92  if (!TESTS_QUIET) printf("Found rapl component at cid %d\n",
93  rapl_cid);
94  if (cmpinfo->num_native_events==0) {
95  test_skip(__FILE__,__LINE__,"No rapl events found",0);
96  }
97  break;
98  }
99  }
100 
101  /* Component not found */
102  if (cid==numcmp) {
103  test_skip(__FILE__,__LINE__,"No rapl component found\n",0);
104  }
105 
106 
107  /* add PAPI_TOT_CYC and PAPI_TOT_INS */
108  retval=PAPI_create_eventset(&EventSet);
109  if ( retval != PAPI_OK ) {
110  test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
111  }
112 
114  if ( retval != PAPI_OK ) {
115  test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
116  }
117 
119  if ( retval != PAPI_OK ) {
120  test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
121  }
122 
123  /* Add some RAPL events */
125  if ( retval != PAPI_OK ) {
126  test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
127  }
128 
129  /* Add an event for each packages 0-n */
130  i = 0;
131  do {
132  char buffer[80];
133  sprintf(&(buffer[0]), "rapl:::PACKAGE_ENERGY:PACKAGE%d", i);
135  ++i;
136  /* protect against insane PAPI library, the value 64 is the same value as
137  * RAPL_MAX_COUNTERS in linux-rapl.c, and feels reasonable. */
138  } while ( 0 < retval && i < 64 );
139 
140  PAPI_event=PAPI_TOT_CYC;
141 
142  /* arbitrary */
143  mythreshold = 2000000;
144  if (!TESTS_QUIET) {
145  printf("Using %#x for the overflow event, threshold %d\n",
146  PAPI_event,mythreshold);
147  }
148 
149  /* Start the run calibration run */
150  retval = PAPI_start( EventSet );
151  if ( retval != PAPI_OK ) {
152  test_fail(__FILE__, __LINE__,"PAPI_start",retval);
153  }
154 
155  do_ints(num_flops,TESTS_QUIET);
156  do_flops( 3000000 );
157 
158  /* stop the calibration run */
159  retval = PAPI_stop( EventSet, values0 );
160  if ( retval != PAPI_OK ) {
161  test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
162  }
163 
164 
165  /* set up overflow handler */
166  retval = PAPI_overflow( EventSet,PAPI_event,mythreshold, 0, handler );
167  if ( retval != PAPI_OK ) {
168  test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
169  }
170 
171  /* Start overflow run */
172  retval = PAPI_start( EventSet );
173  if ( retval != PAPI_OK ) {
174  test_fail(__FILE__, __LINE__,"PAPI_start",retval);
175  }
177  if ( retval != PAPI_OK ) {
178  test_fail(__FILE__, __LINE__,"PAPI_start",retval);
179  }
180 
181  do_ints(num_flops,TESTS_QUIET);
182  do_flops( num_flops );
183 
184  /* stop overflow run */
185  retval = PAPI_stop( EventSet, values1 );
186  if ( retval != PAPI_OK ) {
187  test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
188  }
189 
190  retval = PAPI_stop( EventSet2, values2 );
191  if ( retval != PAPI_OK ) {
192  test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
193  }
194 
195  retval = PAPI_overflow( EventSet, PAPI_event, 0, 0, handler );
196  if ( retval != PAPI_OK ) {
197  test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
198  }
199 
200  retval = PAPI_event_code_to_name( PAPI_event, event_name1 );
201  if (retval != PAPI_OK) {
202  test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n", retval);
203  }
204 
205  if (!TESTS_QUIET) {
206  printf("%s: %lld %lld\n",event_name1,values0[0],values1[0]);
207  }
208 
209  retval = PAPI_event_code_to_name( PAPI_TOT_INS, event_name1 );
210  if (retval != PAPI_OK) {
211  test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n",retval);
212  }
213 
214  if (!TESTS_QUIET) {
215  printf("%s: %lld %lld\n",event_name1,values0[1],values1[1]);
216  }
217 
218  retval = PAPI_cleanup_eventset( EventSet );
219  if ( retval != PAPI_OK ) {
220  test_fail(__FILE__, __LINE__,"PAPI_cleanup_eventset",retval);
221  }
222 
223  retval = PAPI_destroy_eventset( &EventSet );
224  if ( retval != PAPI_OK ) {
225  test_fail(__FILE__, __LINE__,"PAPI_destroy_eventset",retval);
226  }
227 
228  if (rapl_backward) {
229  test_fail(__FILE__, __LINE__,"RAPL counts went backward!",0);
230  }
231 
232  test_pass( __FILE__, NULL, 0 );
233 
234 
235  return 0;
236 }
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:626
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
int PAPI_event[2]
Definition: data_range.c:24
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:807
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1622
int PAPI_add_named_event(int EventSet, char *EventName)
Definition: papi.c:1835
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
#define PAPI_NULL
Definition: papi.h:290
int EventSet
Definition: data_range.c:25
int PAPI_num_components(void)
Definition: papi.c:4326
#define PAPI_TOT_INS
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
int PAPI_overflow(int EventSet, int EventCode, int threshold, int flags, PAPI_overflow_handler_t handler)
Definition: papi.c:4714
void
Definition: iozone.c:18627
#define printf
Definition: papi_test.h:125
double c
Definition: multiplex.c:22
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
static int rapl_backward
Definition: rapl_overflow.c:10
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
int quiet
Definition: rapl_overflow.c:14
#define PAPI_TOT_CYC
int EventSet2
Definition: rapl_overflow.c:12
static long long rapl_values[2]
Definition: rapl_overflow.c:8
void handler(int EventSet, void *address, long long overflow_vector, void *context)
Definition: rapl_overflow.c:16
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2834
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:926
void do_ints(int n, int quiet)
Definition: rapl_overflow.c:46
char * buffer
Definition: iozone.c:1366
void do_flops(int n)
Definition: multiplex.c:23
static long long old_rapl_values[2]
Definition: rapl_overflow.c:9
static int total
Definition: rapl_overflow.c:5
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1971
#define PAPI_MAX_STR_LEN
Definition: papi.h:463
int PAPI_read(int EventSet, long long *values)
Definition: papi.c:2503
long long mythreshold
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
#define PAPI_VER_CURRENT
Definition: papi.h:223
int main(int argc, char **argv)
List all appio events codes and names.
int n
Definition: mendes-alt.c:164