PAPI 7.1.0.0
Loading...
Searching...
No Matches
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
9static int total = 0; /* total overflows */
10
11static long long values[2];
12static long long rapl_values[2];
13static long long old_rapl_values[2] = {0,0};
14static int rapl_backward=0;
15static long long before_time, after_time;
16
18
19int quiet=0;
20
21void handler( int EventSet, void *address,
22 long long overflow_vector, void *context ) {
23
24 ( void ) context;
25 ( void ) address;
26 ( void ) overflow_vector;
27
28#if 0
29 fprintf( stderr, "handler(%d ) Overflow at %p! bit=%#llx \n",
30 EventSet, address, overflow_vector );
31#endif
32
36 double elapsed_time=((double)(after_time-before_time))/1.0e9;
37
38 if (!quiet) printf("%15lld %15lld %18lld %15lld %.3fms\n",
39 values[0],values[1],
40 rapl_values[0], rapl_values[1], elapsed_time*1000.);
41
42 if ((rapl_values[0]<old_rapl_values[0]) ||
44 if (!quiet) printf("RAPL decreased!\n");
46 }
49
50 total++;
51}
52
53
54void do_ints(int n,int quiet)
55{
56 int i,c=n;
57
58 for(i=0;i<n;i++) {
59 c+=c*i*n;
60 }
61 if (!quiet) printf("do_ints result: %d\n",c);
62}
63
64
65
66int
67main( int argc, char **argv )
68{
69 int EventSet = PAPI_NULL;
70 long long values0[2],values1[2],values2[2];
71 int num_flops = 30000000, retval;
72 int mythreshold;
73 char event_name1[PAPI_MAX_STR_LEN];
74 int PAPI_event;
75 int cid,numcmp,rapl_cid;
76 const PAPI_component_info_t *cmpinfo = NULL;
77 int i;
78
79 /* Set TESTS_QUIET variable */
80 quiet=tests_quiet( argc, argv );
81
82 /* Init PAPI */
84 if ( retval != PAPI_VER_CURRENT ) {
85 test_fail(__FILE__, __LINE__,"PAPI_library_init",retval);
86 }
87
88 numcmp = PAPI_num_components();
89
90 for(cid=0; cid<numcmp; cid++) {
91
92 if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
93 test_fail(__FILE__, __LINE__,"PAPI_get_component_info failed\n", 0);
94 }
95
96 if (strstr(cmpinfo->name,"rapl")) {
97 rapl_cid=cid;
98 if (!TESTS_QUIET) printf("Found rapl component at cid %d\n",
99 rapl_cid);
100 if (cmpinfo->num_native_events==0) {
101 test_skip(__FILE__,__LINE__,"No rapl events found",0);
102 }
103 break;
104 }
105 }
106
107 /* Component not found */
108 if (cid==numcmp) {
109 test_skip(__FILE__,__LINE__,"No rapl component found\n",0);
110 }
111
112
113 /* add PAPI_TOT_CYC and PAPI_TOT_INS to EventSet */
115 if ( retval != PAPI_OK ) {
116 test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
117 }
118
120 if ( retval != PAPI_OK ) {
121 test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
122 }
123
125 if ( retval != PAPI_OK ) {
126 test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
127 }
128
129 /* Add some RAPL events */
131 if ( retval != PAPI_OK ) {
132 test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
133 }
134
135 /* Add to eventSet2 for each package 0-n */
136 /* We use PACKAGE_ENERGY_CNT because it is an integer counter. */
137 char raplEventBase[]="rapl:::PACKAGE_ENERGY_CNT:PACKAGE";
138 i = 0;
139 do {
140 char buffer[80];
141 sprintf(buffer, "%s%d", raplEventBase, i);
143 ++i;
144 /* protect against insane PAPI library, the value 64 is the same value as
145 * RAPL_MAX_COUNTERS in linux-rapl.c, and feels reasonable. */
146 } while ( 0 < retval && i < 64 );
147
149
150 /* arbitrary, period of reporting, in total cycles. */
151 /* our test routine is ~16 cycles per flop, get ~50 reports. */
152 mythreshold = (num_flops/50)<<4;
153 if (!TESTS_QUIET) {
154 printf("Using %#x for the overflow event on PAPI_TOT_CYC, threshold %d\n",
156 }
157
158 /* Start the run calibration run */
160 if ( retval != PAPI_OK ) {
161 test_fail(__FILE__, __LINE__,"PAPI_start",retval);
162 }
163
164 do_ints(num_flops,TESTS_QUIET);
165 do_flops( 3000000 );
166
167 /* stop the calibration run */
168 retval = PAPI_stop( EventSet, values0 );
169 if ( retval != PAPI_OK ) {
170 test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
171 }
172
173 /* report header */
174 if (!TESTS_QUIET) {
175 printf("%15s %15s %18s %15s Elapsed\n", "PAPI_TOT_CYC", "PAPI_TOT_INS",
176 "PACKAGE_ENERGY_CNT", "--UNUSED--");
177 }
178
179 /* set up overflow handler */
181 if ( retval != PAPI_OK ) {
182 test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
183 }
184
185 /* Start overflow run */
188 if ( retval != PAPI_OK ) {
189 test_fail(__FILE__, __LINE__,"PAPI_start",retval);
190 }
192 if ( retval != PAPI_OK ) {
193 test_fail(__FILE__, __LINE__,"PAPI_start",retval);
194 }
195
196 do_ints(num_flops,TESTS_QUIET);
197 do_flops( num_flops );
198
199 /* stop overflow run */
200 retval = PAPI_stop( EventSet, values1 );
201 if ( retval != PAPI_OK ) {
202 test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
203 }
204
205 retval = PAPI_stop( EventSet2, values2 );
206 if ( retval != PAPI_OK ) {
207 test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
208 }
209
211 if ( retval != PAPI_OK ) {
212 test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
213 }
214
215 retval = PAPI_event_code_to_name( PAPI_event, event_name1 );
216 if (retval != PAPI_OK) {
217 test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n", retval);
218 }
219
220 if (!TESTS_QUIET) {
221 printf("%s: %lld(Calibration) %lld(OverflowRun)\n",event_name1,values0[0],values1[0]);
222 }
223
225 if (retval != PAPI_OK) {
226 test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n",retval);
227 }
228
229 if (!TESTS_QUIET) {
230 printf("%s: %lld(Calibration) %lld(OverflowRun)\n",event_name1,values0[1],values1[1]);
231 }
232
234 if ( retval != PAPI_OK ) {
235 test_fail(__FILE__, __LINE__,"PAPI_cleanup_eventset",retval);
236 }
237
239 if ( retval != PAPI_OK ) {
240 test_fail(__FILE__, __LINE__,"PAPI_destroy_eventset",retval);
241 }
242
243 if (rapl_backward) {
244 test_fail(__FILE__, __LINE__,"RAPL counts went backward!",0);
245 }
246
247 test_pass( __FILE__ );
248
249 return 0;
250}
int i
add PAPI preset or native hardware event to an event set
add PAPI preset or native hardware event by name to an EventSet
Empty and destroy an EventSet.
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
Convert a numeric hardware event code to a name.
get information about a specific software component
Get real time counter value in nanoseconds.
initialize the PAPI library.
Get the number of components available on the system.
Set up an event set to begin registering overflows.
Read hardware counters from an event set.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
static long long mythreshold
int PAPI_event[2]
Definition: data_range.c:30
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_TOT_CYC
Definition: f90papi.h:308
#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 double c[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:40
void do_flops(int n)
Definition: multiplex.c:23
int TESTS_QUIET
Definition: test_utils.c:18
Return codes and api definitions.
FILE * stderr
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
void PAPI_NORETURN test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:584
int main()
Definition: pernode.c:20
static long long old_rapl_values[2]
Definition: rapl_overflow.c:13
void do_ints(int n, int quiet)
Definition: rapl_overflow.c:54
static long long values[2]
Definition: rapl_overflow.c:11
int quiet
Definition: rapl_overflow.c:19
static int rapl_backward
Definition: rapl_overflow.c:14
static int total
Definition: rapl_overflow.c:9
int EventSet2
Definition: rapl_overflow.c:17
void handler(int EventSet, void *address, long long overflow_vector, void *context)
Definition: rapl_overflow.c:21
static long long after_time
Definition: rapl_overflow.c:15
static long long rapl_values[2]
Definition: rapl_overflow.c:12
static long long before_time
Definition: rapl_overflow.c:15
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627
int retval
Definition: zero_fork.c:53