PAPI 7.1.0.0
Loading...
Searching...
No Matches
example_basic.c
Go to the documentation of this file.
1/****************************/
2/* THIS IS OPEN SOURCE CODE */
3/****************************/
4
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "papi.h"
26#include "papi_test.h"
27
28#define NUM_EVENTS 3
29
30int main (int argc, char **argv)
31{
32
33 int retval,i;
34 int EventSet = PAPI_NULL;
35 long long values[NUM_EVENTS];
36 const PAPI_component_info_t *cmpinfo = NULL;
37 int numcmp,cid,example_cid=-1;
38 int code,maximum_code=0;
41 int quiet=0;
42
43 /* Set TESTS_QUIET variable */
44 quiet=tests_quiet( argc, argv );
45
46 /* PAPI Initialization */
48 if ( retval != PAPI_VER_CURRENT ) {
49 test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
50 }
51
52 if (!quiet) {
53 printf( "Testing example component with PAPI %d.%d.%d\n",
57 }
58
59 /* Find our component */
60
61 numcmp = PAPI_num_components();
62 for( cid=0; cid<numcmp; cid++) {
63 if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
64 test_fail(__FILE__, __LINE__,
65 "PAPI_get_component_info failed\n", 0);
66 }
67 if (!quiet) {
68 printf("\tComponent %d - %d events - %s\n", cid,
69 cmpinfo->num_native_events,
70 cmpinfo->name);
71 }
72 if (strstr(cmpinfo->name,"example")) {
73 /* FOUND! */
74 example_cid=cid;
75 }
76 }
77
78
79 if (example_cid<0) {
80 test_skip(__FILE__, __LINE__,
81 "Example component not found\n", 0);
82 }
83
84 if (!quiet) {
85 printf("\nFound Example Component at id %d\n",example_cid);
86 printf("\nListing all events in this component:\n");
87 }
88
89 /**************************************************/
90 /* Listing all available events in this component */
91 /* Along with descriptions */
92 /**************************************************/
93 code = PAPI_NATIVE_MASK;
94
95 retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, example_cid );
96
97 while ( retval == PAPI_OK ) {
99 printf("Error translating %#x\n",code);
100 test_fail( __FILE__, __LINE__,
101 "PAPI_event_code_to_name", retval );
102 }
103
104 if (PAPI_get_event_info( code, &event_info)!=PAPI_OK) {
105 printf("Error getting info for event %#x\n",code);
106 test_fail( __FILE__, __LINE__,
107 "PAPI_get_event_info()", retval );
108 }
109
110 if (!quiet) {
111 printf("\tEvent %#x: %s -- %s\n",
112 code,event_name,event_info.long_descr);
113 }
114
115 maximum_code=code;
116
117 retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, example_cid );
118
119 }
120 if (!quiet) printf("\n");
121
122 /**********************************/
123 /* Try accessing an invalid event */
124 /**********************************/
125
126 retval=PAPI_event_code_to_name( maximum_code+10, event_name );
127 if (retval!=PAPI_ENOEVNT) {
128 test_fail( __FILE__, __LINE__,
129 "Failed to return PAPI_ENOEVNT on invalid event", retval );
130 }
131
132 /***********************************/
133 /* Test the EXAMPLE_ZERO event */
134 /***********************************/
135
137 if ( retval != PAPI_OK ) {
138 test_fail( __FILE__, __LINE__,
139 "PAPI_create_eventset() failed\n", retval );
140 }
141
142 retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code);
143 if ( retval != PAPI_OK ) {
144 test_fail( __FILE__, __LINE__,
145 "EXAMPLE_ZERO not found\n",retval );
146 }
147
149 if ( retval != PAPI_OK ) {
150 test_fail( __FILE__, __LINE__,
151 "PAPI_add_events failed\n", retval );
152 }
153
155 if ( retval != PAPI_OK ) {
156 test_fail( __FILE__, __LINE__,
157 "PAPI_start failed\n",retval );
158 }
159
161 if ( retval != PAPI_OK ) {
162 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
163 }
164
165 if (!quiet) printf("Testing EXAMPLE_ZERO: %lld\n",values[0]);
166
167 if (values[0]!=0) {
168 test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0);
169 }
170
172 if (retval != PAPI_OK) {
173 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
174 }
175
177 if (retval != PAPI_OK) {
178 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
179 }
180
182
183
184 /***********************************/
185 /* Test the EXAMPLE_CONSTANT event */
186 /***********************************/
187
189 if ( retval != PAPI_OK ) {
190 test_fail( __FILE__, __LINE__,
191 "PAPI_create_eventset() failed\n", retval );
192 }
193
194 retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code);
195 if ( retval != PAPI_OK ) {
196 test_fail( __FILE__, __LINE__,
197 "EXAMPLE_CONSTANT not found\n",retval );
198 }
199
201 if ( retval != PAPI_OK ) {
202 test_fail( __FILE__, __LINE__,
203 "PAPI_add_events failed\n", retval );
204 }
205
207 if ( retval != PAPI_OK ) {
208 test_fail( __FILE__, __LINE__,
209 "PAPI_start failed\n",retval );
210 }
211
213 if ( retval != PAPI_OK ) {
214 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
215 }
216
217 if (!quiet) printf("Testing EXAMPLE_CONSTANT: %lld\n",values[0]);
218
219 if (values[0]!=42) {
220 test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0);
221 }
222
224 if (retval != PAPI_OK) {
225 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
226 }
227
229 if (retval != PAPI_OK) {
230 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
231 }
232
234
235
236
237 /***********************************/
238 /* Test the EXAMPLE_AUTOINC event */
239 /***********************************/
240
242 if ( retval != PAPI_OK ) {
243 test_fail( __FILE__, __LINE__,
244 "PAPI_create_eventset() failed\n", retval );
245 }
246
247 retval = PAPI_event_name_to_code("EXAMPLE_AUTOINC", &code);
248 if ( retval != PAPI_OK ) {
249 test_fail( __FILE__, __LINE__,
250 "EXAMPLE_AUTOINC not found\n",retval );
251 }
252
254 if ( retval != PAPI_OK ) {
255 test_fail( __FILE__, __LINE__,
256 "PAPI_add_events failed\n", retval );
257 }
258
259 if (!quiet) printf("Testing EXAMPLE_AUTOINC: ");
260
261 for(i=0;i<10;i++) {
262
264 if ( retval != PAPI_OK ) {
265 test_fail( __FILE__, __LINE__,
266 "PAPI_start failed\n",retval );
267 }
268
270 if ( retval != PAPI_OK ) {
271 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
272 }
273
274 if (!quiet) printf("%lld ",values[0]);
275
276 if (values[0]!=i) {
277 test_fail( __FILE__, __LINE__, "Result wrong!\n", 0);
278 }
279 }
280
281 if (!quiet) printf("\n");
282
283
284 /***********************************/
285 /* Test multiple reads */
286 /***********************************/
287
289 if ( retval != PAPI_OK ) {
290 test_fail( __FILE__, __LINE__,
291 "PAPI_start failed\n",retval );
292 }
293
294 for(i=0;i<10;i++) {
295
297 if ( retval != PAPI_OK ) {
298 test_fail( __FILE__, __LINE__, "PAPI_read failed\n", retval);
299 }
300 if (!quiet) printf("%lld ",values[0]);
301 }
302
304 if ( retval != PAPI_OK ) {
305 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
306 }
307 if (!quiet) printf("%lld\n",values[0]);
308
309 // if (values[0]!=i) {
310 // test_fail( __FILE__, __LINE__, "Result wrong!\n", 0);
311 //}
312
313 /***********************************/
314 /* Test PAPI_reset() */
315 /***********************************/
316
318 if ( retval != PAPI_OK ) {
319 test_fail( __FILE__, __LINE__,
320 "PAPI_reset() failed\n",retval );
321 }
322
324 if ( retval != PAPI_OK ) {
325 test_fail( __FILE__, __LINE__,
326 "PAPI_start failed\n",retval );
327 }
328
330 if ( retval != PAPI_OK ) {
331 test_fail( __FILE__, __LINE__,
332 "PAPI_reset() failed\n",retval );
333 }
334
336 if ( retval != PAPI_OK ) {
337 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
338 }
339
340
341 if (!quiet) printf("Testing EXAMPLE_AUTOINC after PAPI_reset(): %lld\n",
342 values[0]);
343
344 if (values[0]!=0) {
345 test_fail( __FILE__, __LINE__, "Result not zero!\n", 0);
346 }
347
349 if (retval != PAPI_OK) {
350 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
351 }
352
354 if (retval != PAPI_OK) {
355 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
356 }
357
359
360
361 /***********************************/
362 /* Test multiple events */
363 /***********************************/
364
365 if (!quiet) printf("Testing Multiple Events: ");
366
368 if ( retval != PAPI_OK ) {
369 test_fail( __FILE__, __LINE__,
370 "PAPI_create_eventset() failed\n", retval );
371 }
372
373 retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code);
374 if ( retval != PAPI_OK ) {
375 test_fail( __FILE__, __LINE__,
376 "EXAMPLE_CONSTANT not found\n",retval );
377 }
378
380 if ( retval != PAPI_OK ) {
381 test_fail( __FILE__, __LINE__,
382 "PAPI_add_events failed\n", retval );
383 }
384
385 retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code);
386 if ( retval != PAPI_OK ) {
387 test_fail( __FILE__, __LINE__,
388 "EXAMPLE_GLOBAL_AUTOINC not found\n",retval );
389 }
390
392 if ( retval != PAPI_OK ) {
393 test_fail( __FILE__, __LINE__,
394 "PAPI_add_events failed\n", retval );
395 }
396
397 retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code);
398 if ( retval != PAPI_OK ) {
399 test_fail( __FILE__, __LINE__,
400 "EXAMPLE_ZERO not found\n",retval );
401 }
402
404 if ( retval != PAPI_OK ) {
405 test_fail( __FILE__, __LINE__,
406 "PAPI_add_events failed\n", retval );
407 }
408
409
411 if ( retval != PAPI_OK ) {
412 test_fail( __FILE__, __LINE__,
413 "PAPI_start failed\n",retval );
414 }
415
417 if ( retval != PAPI_OK ) {
418 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
419 }
420
421 if (!quiet) {
422 for(i=0;i<3;i++) {
423 printf("%lld ",values[i]);
424 }
425 printf("\n");
426 }
427
428 if (values[0]!=42) {
429 test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0);
430 }
431
432 if (values[2]!=0) {
433 test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0);
434 }
435
437 if (retval != PAPI_OK) {
438 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
439 }
440
442 if (retval != PAPI_OK) {
443 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
444 }
445
447
448 /***********************************/
449 /* Test writing to an event */
450 /***********************************/
451
452 if (!quiet) printf("Testing Write\n");
453
455 if ( retval != PAPI_OK ) {
456 test_fail( __FILE__, __LINE__,
457 "PAPI_create_eventset() failed\n", retval );
458 }
459
460 retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code);
461 if ( retval != PAPI_OK ) {
462 test_fail( __FILE__, __LINE__,
463 "EXAMPLE_CONSTANT not found\n",retval );
464 }
465
467 if ( retval != PAPI_OK ) {
468 test_fail( __FILE__, __LINE__,
469 "PAPI_add_events failed\n", retval );
470 }
471
472 retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code);
473 if ( retval != PAPI_OK ) {
474 test_fail( __FILE__, __LINE__,
475 "EXAMPLE_GLOBAL_AUTOINC not found\n",retval );
476 }
477
479 if ( retval != PAPI_OK ) {
480 test_fail( __FILE__, __LINE__,
481 "PAPI_add_events failed\n", retval );
482 }
483
484 retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code);
485 if ( retval != PAPI_OK ) {
486 test_fail( __FILE__, __LINE__,
487 "EXAMPLE_ZERO not found\n",retval );
488 }
489
491 if ( retval != PAPI_OK ) {
492 test_fail( __FILE__, __LINE__,
493 "PAPI_add_events failed\n", retval );
494 }
495
496
498 if ( retval != PAPI_OK ) {
499 test_fail( __FILE__, __LINE__,
500 "PAPI_start failed\n",retval );
501 }
502
504 if ( retval != PAPI_OK ) {
505 test_fail( __FILE__, __LINE__,
506 "PAPI_read failed\n",retval );
507 }
508
509 if (!quiet) {
510 printf("Before values: ");
511 for(i=0;i<3;i++) {
512 printf("%lld ",values[i]);
513 }
514 printf("\n");
515 }
516
517 values[0]=100;
518 values[1]=200;
519 values[2]=300;
520
522 if ( retval != PAPI_OK ) {
523 test_fail( __FILE__, __LINE__,
524 "PAPI_write failed\n",retval );
525 }
526
528 if ( retval != PAPI_OK ) {
529 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
530 }
531
532 if (!quiet) {
533 printf("After values: ");
534 for(i=0;i<3;i++) {
535 printf("%lld ",values[i]);
536 }
537 printf("\n");
538 }
539
540
541 if (values[0]!=42) {
542 test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0);
543 }
544
545 if (values[1]!=200) {
546 test_fail( __FILE__, __LINE__, "Result should be 200!\n", 0);
547 }
548
549 if (values[2]!=0) {
550 test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0);
551 }
552
554 if (retval != PAPI_OK) {
555 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
556 }
557
559 if (retval != PAPI_OK) {
560 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
561 }
562
564
565
566 /************/
567 /* All Done */
568 /************/
569
570 if (!quiet) printf("\n");
571
572 test_pass( __FILE__ );
573
574 return 0;
575}
576
int i
add PAPI preset or native hardware event to an event set
Empty and destroy an EventSet.
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
Enumerate PAPI preset or native events for a given component.
Convert a numeric hardware event code to a name.
Convert a name to a numeric hardware event code.
get information about a specific software component
Get the event's name and description info.
initialize the PAPI library.
Get the number of components available on the system.
Read hardware counters from an event set.
Reset the hardware event counts in an event set.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Write counter values into counters.
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#define NUM_EVENTS
Definition: example_basic.c:28
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_VERSION
Definition: f90papi.h:193
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_ENOEVNT
Definition: f90papi.h:139
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
static struct counter_info * event_info
#define PAPI_NATIVE_MASK
Return codes and api definitions.
#define PAPI_VERSION_REVISION(x)
Definition: papi.h:221
#define PAPI_VERSION_MAJOR(x)
Definition: papi.h:219
#define PAPI_VERSION_MINOR(x)
Definition: papi.h:220
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
int quiet
Definition: rapl_overflow.c:19
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627
int retval
Definition: zero_fork.c:53