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