PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
example_basic.c File Reference

This file is a very simple example test and Makefile that acat as a guideline on how to add tests to components. The papi configure and papi Makefile will take care of the compilation of the component tests (if all tests are added to a directory named 'tests' in the specific component dir). See components/README for more details. More...

Include dependency graph for example_basic.c:

Go to the source code of this file.

Macros

#define NUM_EVENTS   3
 

Functions

int main (int argc, char **argv)
 

Detailed Description

Author
Vince Weaver vweav.nosp@m.er1@.nosp@m.eecs..nosp@m.utk..nosp@m.edu test case for Example component

Definition in file example_basic.c.

Macro Definition Documentation

#define NUM_EVENTS   3

Definition at line 28 of file example_basic.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 30 of file example_basic.c.

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 ) {
98  if (PAPI_event_code_to_name( code, event_name )!=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 
136  retval = PAPI_create_eventset( &EventSet );
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 
148  retval = PAPI_add_event( EventSet, code);
149  if ( retval != PAPI_OK ) {
150  test_fail( __FILE__, __LINE__,
151  "PAPI_add_events failed\n", retval );
152  }
153 
154  retval = PAPI_start( EventSet );
155  if ( retval != PAPI_OK ) {
156  test_fail( __FILE__, __LINE__,
157  "PAPI_start failed\n",retval );
158  }
159 
160  retval = PAPI_stop( EventSet, values );
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 
171  retval = PAPI_cleanup_eventset(EventSet);
172  if (retval != PAPI_OK) {
173  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
174  }
175 
176  retval = PAPI_destroy_eventset(&EventSet);
177  if (retval != PAPI_OK) {
178  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
179  }
180 
181  EventSet=PAPI_NULL;
182 
183 
184  /***********************************/
185  /* Test the EXAMPLE_CONSTANT event */
186  /***********************************/
187 
188  retval = PAPI_create_eventset( &EventSet );
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 
200  retval = PAPI_add_event( EventSet, code);
201  if ( retval != PAPI_OK ) {
202  test_fail( __FILE__, __LINE__,
203  "PAPI_add_events failed\n", retval );
204  }
205 
206  retval = PAPI_start( EventSet );
207  if ( retval != PAPI_OK ) {
208  test_fail( __FILE__, __LINE__,
209  "PAPI_start failed\n",retval );
210  }
211 
212  retval = PAPI_stop( EventSet, values );
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 
223  retval = PAPI_cleanup_eventset(EventSet);
224  if (retval != PAPI_OK) {
225  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
226  }
227 
228  retval = PAPI_destroy_eventset(&EventSet);
229  if (retval != PAPI_OK) {
230  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
231  }
232 
233  EventSet=PAPI_NULL;
234 
235 
236 
237  /***********************************/
238  /* Test the EXAMPLE_AUTOINC event */
239  /***********************************/
240 
241  retval = PAPI_create_eventset( &EventSet );
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 
253  retval = PAPI_add_event( EventSet, code);
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 
263  retval = PAPI_start( EventSet );
264  if ( retval != PAPI_OK ) {
265  test_fail( __FILE__, __LINE__,
266  "PAPI_start failed\n",retval );
267  }
268 
269  retval = PAPI_stop( EventSet, values );
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 
288  retval = PAPI_start( EventSet );
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 
296  retval=PAPI_read( EventSet, values);
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 
303  retval = PAPI_stop( EventSet, values );
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 
317  retval = PAPI_reset( EventSet);
318  if ( retval != PAPI_OK ) {
319  test_fail( __FILE__, __LINE__,
320  "PAPI_reset() failed\n",retval );
321  }
322 
323  retval = PAPI_start( EventSet );
324  if ( retval != PAPI_OK ) {
325  test_fail( __FILE__, __LINE__,
326  "PAPI_start failed\n",retval );
327  }
328 
329  retval = PAPI_reset( EventSet);
330  if ( retval != PAPI_OK ) {
331  test_fail( __FILE__, __LINE__,
332  "PAPI_reset() failed\n",retval );
333  }
334 
335  retval = PAPI_stop( EventSet, values );
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 
348  retval = PAPI_cleanup_eventset(EventSet);
349  if (retval != PAPI_OK) {
350  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
351  }
352 
353  retval = PAPI_destroy_eventset(&EventSet);
354  if (retval != PAPI_OK) {
355  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
356  }
357 
358  EventSet=PAPI_NULL;
359 
360 
361  /***********************************/
362  /* Test multiple events */
363  /***********************************/
364 
365  if (!quiet) printf("Testing Multiple Events: ");
366 
367  retval = PAPI_create_eventset( &EventSet );
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 
379  retval = PAPI_add_event( EventSet, code);
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 
391  retval = PAPI_add_event( EventSet, code);
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 
403  retval = PAPI_add_event( EventSet, code);
404  if ( retval != PAPI_OK ) {
405  test_fail( __FILE__, __LINE__,
406  "PAPI_add_events failed\n", retval );
407  }
408 
409 
410  retval = PAPI_start( EventSet );
411  if ( retval != PAPI_OK ) {
412  test_fail( __FILE__, __LINE__,
413  "PAPI_start failed\n",retval );
414  }
415 
416  retval = PAPI_stop( EventSet, values );
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 
436  retval = PAPI_cleanup_eventset(EventSet);
437  if (retval != PAPI_OK) {
438  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
439  }
440 
441  retval = PAPI_destroy_eventset(&EventSet);
442  if (retval != PAPI_OK) {
443  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
444  }
445 
446  EventSet=PAPI_NULL;
447 
448  /***********************************/
449  /* Test writing to an event */
450  /***********************************/
451 
452  if (!quiet) printf("Testing Write\n");
453 
454  retval = PAPI_create_eventset( &EventSet );
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 
466  retval = PAPI_add_event( EventSet, code);
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 
478  retval = PAPI_add_event( EventSet, code);
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 
490  retval = PAPI_add_event( EventSet, code);
491  if ( retval != PAPI_OK ) {
492  test_fail( __FILE__, __LINE__,
493  "PAPI_add_events failed\n", retval );
494  }
495 
496 
497  retval = PAPI_start( EventSet );
498  if ( retval != PAPI_OK ) {
499  test_fail( __FILE__, __LINE__,
500  "PAPI_start failed\n",retval );
501  }
502 
503  retval = PAPI_read ( EventSet, values );
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 
521  retval = PAPI_write ( EventSet, values );
522  if ( retval != PAPI_OK ) {
523  test_fail( __FILE__, __LINE__,
524  "PAPI_write failed\n",retval );
525  }
526 
527  retval = PAPI_stop( EventSet, values );
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 
553  retval = PAPI_cleanup_eventset(EventSet);
554  if (retval != PAPI_OK) {
555  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
556  }
557 
558  retval = PAPI_destroy_eventset(&EventSet);
559  if (retval != PAPI_OK) {
560  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
561  }
562 
563  EventSet=PAPI_NULL;
564 
565 
566  /************/
567  /* All Done */
568  /************/
569 
570  if (!quiet) printf("\n");
571 
572  test_pass( __FILE__ );
573 
574  return 0;
575 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:629
#define PAPI_ENOEVNT
Definition: papi.h:260
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define PAPI_NATIVE_MASK
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
int PAPI_reset(int EventSet)
Definition: papi.c:2459
int PAPI_write(int EventSet, long long *values)
Definition: papi.c:2813
#define PAPI_NULL
Definition: papi.h:292
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:969
int PAPI_num_components(void)
Definition: papi.c:4387
int EventSet
return PAPI_OK
Definition: linux-nvml.c:497
int PAPI_event_name_to_code(const char *in, int *out)
Definition: papi.c:1004
int int argc
Definition: iozone.c:1609
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
char ** argv
Definition: iozone.c:1610
static struct counter_info * event_info
int PAPI_library_init(int version)
Definition: papi.c:500
int i
Definition: fileop.c:140
#define PAPI_VERSION_MAJOR(x)
Definition: papi.h:217
int quiet
Definition: rapl_overflow.c:18
#define PAPI_VERSION_REVISION(x)
Definition: papi.h:219
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1357
#define PAPI_VERSION_MINOR(x)
Definition: papi.h:218
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
printf("\tTry: -i 0 -i 1 \n\n")
#define PAPI_VERSION
Definition: papi.h:224
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
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
#define NUM_EVENTS
Definition: example_basic.c:28

Here is the call graph for this function: