PAPI  5.4.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 25 of file example_basic.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 27 of file example_basic.c.

28 {
29 
30  int retval,i;
31  int EventSet = PAPI_NULL;
32  long long values[NUM_EVENTS];
33  const PAPI_component_info_t *cmpinfo = NULL;
34  int numcmp,cid,example_cid=-1;
35  int code,maximum_code=0;
38 
39  /* Set TESTS_QUIET variable */
40  tests_quiet( argc, argv );
41 
42  /* PAPI Initialization */
44  if ( retval != PAPI_VER_CURRENT ) {
45  test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
46  }
47 
48  if (!TESTS_QUIET) {
49  printf( "Testing example component with PAPI %d.%d.%d\n",
53  }
54 
55  /* Find our component */
56 
57  numcmp = PAPI_num_components();
58  for( cid=0; cid<numcmp; cid++) {
59  if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
60  test_fail(__FILE__, __LINE__,
61  "PAPI_get_component_info failed\n", 0);
62  }
63  if (!TESTS_QUIET) {
64  printf("\tComponent %d - %d events - %s\n", cid,
65  cmpinfo->num_native_events,
66  cmpinfo->name);
67  }
68  if (strstr(cmpinfo->name,"example")) {
69  /* FOUND! */
70  example_cid=cid;
71  }
72  }
73 
74 
75  if (example_cid<0) {
76  test_skip(__FILE__, __LINE__,
77  "Example component not found\n", 0);
78  }
79 
80  if (!TESTS_QUIET) {
81  printf("\nFound Example Component at id %d\n",example_cid);
82  printf("\nListing all events in this component:\n");
83  }
84 
85  /**************************************************/
86  /* Listing all available events in this component */
87  /* Along with descriptions */
88  /**************************************************/
89  code = PAPI_NATIVE_MASK;
90 
91  retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, example_cid );
92 
93  while ( retval == PAPI_OK ) {
94  if (PAPI_event_code_to_name( code, event_name )!=PAPI_OK) {
95  printf("Error translating %#x\n",code);
96  test_fail( __FILE__, __LINE__,
97  "PAPI_event_code_to_name", retval );
98  }
99 
100  if (PAPI_get_event_info( code, &event_info)!=PAPI_OK) {
101  printf("Error getting info for event %#x\n",code);
102  test_fail( __FILE__, __LINE__,
103  "PAPI_get_event_info()", retval );
104  }
105 
106  if (!TESTS_QUIET) {
107  printf("\tEvent %#x: %s -- %s\n",
108  code,event_name,event_info.long_descr);
109  }
110 
111  maximum_code=code;
112 
113  retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, example_cid );
114 
115  }
116  if (!TESTS_QUIET) printf("\n");
117 
118  /**********************************/
119  /* Try accessing an invalid event */
120  /**********************************/
121 
122  retval=PAPI_event_code_to_name( maximum_code+10, event_name );
123  if (retval!=PAPI_ENOEVNT) {
124  test_fail( __FILE__, __LINE__,
125  "Failed to return PAPI_ENOEVNT on invalid event", retval );
126  }
127 
128  /***********************************/
129  /* Test the EXAMPLE_ZERO event */
130  /***********************************/
131 
132  retval = PAPI_create_eventset( &EventSet );
133  if ( retval != PAPI_OK ) {
134  test_fail( __FILE__, __LINE__,
135  "PAPI_create_eventset() failed\n", retval );
136  }
137 
138  retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code);
139  if ( retval != PAPI_OK ) {
140  test_fail( __FILE__, __LINE__,
141  "EXAMPLE_ZERO not found\n",retval );
142  }
143 
144  retval = PAPI_add_event( EventSet, code);
145  if ( retval != PAPI_OK ) {
146  test_fail( __FILE__, __LINE__,
147  "PAPI_add_events failed\n", retval );
148  }
149 
150  retval = PAPI_start( EventSet );
151  if ( retval != PAPI_OK ) {
152  test_fail( __FILE__, __LINE__,
153  "PAPI_start failed\n",retval );
154  }
155 
156  retval = PAPI_stop( EventSet, values );
157  if ( retval != PAPI_OK ) {
158  test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
159  }
160 
161  if (!TESTS_QUIET) printf("Testing EXAMPLE_ZERO: %lld\n",values[0]);
162 
163  if (values[0]!=0) {
164  test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0);
165  }
166 
167  retval = PAPI_cleanup_eventset(EventSet);
168  if (retval != PAPI_OK) {
169  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
170  }
171 
172  retval = PAPI_destroy_eventset(&EventSet);
173  if (retval != PAPI_OK) {
174  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
175  }
176 
177  EventSet=PAPI_NULL;
178 
179 
180  /***********************************/
181  /* Test the EXAMPLE_CONSTANT event */
182  /***********************************/
183 
184  retval = PAPI_create_eventset( &EventSet );
185  if ( retval != PAPI_OK ) {
186  test_fail( __FILE__, __LINE__,
187  "PAPI_create_eventset() failed\n", retval );
188  }
189 
190  retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code);
191  if ( retval != PAPI_OK ) {
192  test_fail( __FILE__, __LINE__,
193  "EXAMPLE_CONSTANT not found\n",retval );
194  }
195 
196  retval = PAPI_add_event( EventSet, code);
197  if ( retval != PAPI_OK ) {
198  test_fail( __FILE__, __LINE__,
199  "PAPI_add_events failed\n", retval );
200  }
201 
202  retval = PAPI_start( EventSet );
203  if ( retval != PAPI_OK ) {
204  test_fail( __FILE__, __LINE__,
205  "PAPI_start failed\n",retval );
206  }
207 
208  retval = PAPI_stop( EventSet, values );
209  if ( retval != PAPI_OK ) {
210  test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
211  }
212 
213  if (!TESTS_QUIET) printf("Testing EXAMPLE_CONSTANT: %lld\n",values[0]);
214 
215  if (values[0]!=42) {
216  test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0);
217  }
218 
219  retval = PAPI_cleanup_eventset(EventSet);
220  if (retval != PAPI_OK) {
221  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
222  }
223 
224  retval = PAPI_destroy_eventset(&EventSet);
225  if (retval != PAPI_OK) {
226  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
227  }
228 
229  EventSet=PAPI_NULL;
230 
231 
232 
233  /***********************************/
234  /* Test the EXAMPLE_AUTOINC event */
235  /***********************************/
236 
237  retval = PAPI_create_eventset( &EventSet );
238  if ( retval != PAPI_OK ) {
239  test_fail( __FILE__, __LINE__,
240  "PAPI_create_eventset() failed\n", retval );
241  }
242 
243  retval = PAPI_event_name_to_code("EXAMPLE_AUTOINC", &code);
244  if ( retval != PAPI_OK ) {
245  test_fail( __FILE__, __LINE__,
246  "EXAMPLE_AUTOINC not found\n",retval );
247  }
248 
249  retval = PAPI_add_event( EventSet, code);
250  if ( retval != PAPI_OK ) {
251  test_fail( __FILE__, __LINE__,
252  "PAPI_add_events failed\n", retval );
253  }
254 
255  if (!TESTS_QUIET) printf("Testing EXAMPLE_AUTOINC: ");
256 
257  for(i=0;i<10;i++) {
258 
259  retval = PAPI_start( EventSet );
260  if ( retval != PAPI_OK ) {
261  test_fail( __FILE__, __LINE__,
262  "PAPI_start failed\n",retval );
263  }
264 
265  retval = PAPI_stop( EventSet, values );
266  if ( retval != PAPI_OK ) {
267  test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
268  }
269 
270  if (!TESTS_QUIET) printf("%lld ",values[0]);
271 
272  if (values[0]!=i) {
273  test_fail( __FILE__, __LINE__, "Result wrong!\n", 0);
274  }
275  }
276 
277  if (!TESTS_QUIET) printf("\n");
278 
279 
280  /***********************************/
281  /* Test multiple reads */
282  /***********************************/
283 
284  retval = PAPI_start( EventSet );
285  if ( retval != PAPI_OK ) {
286  test_fail( __FILE__, __LINE__,
287  "PAPI_start failed\n",retval );
288  }
289 
290  for(i=0;i<10;i++) {
291 
292  retval=PAPI_read( EventSet, values);
293  if ( retval != PAPI_OK ) {
294  test_fail( __FILE__, __LINE__, "PAPI_read failed\n", retval);
295  }
296  if (!TESTS_QUIET) printf("%lld ",values[0]);
297  }
298 
299  retval = PAPI_stop( EventSet, values );
300  if ( retval != PAPI_OK ) {
301  test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
302  }
303  if (!TESTS_QUIET) printf("%lld\n",values[0]);
304 
305  // if (values[0]!=i) {
306  // test_fail( __FILE__, __LINE__, "Result wrong!\n", 0);
307  //}
308 
309  /***********************************/
310  /* Test PAPI_reset() */
311  /***********************************/
312 
313  retval = PAPI_reset( EventSet);
314  if ( retval != PAPI_OK ) {
315  test_fail( __FILE__, __LINE__,
316  "PAPI_reset() failed\n",retval );
317  }
318 
319  retval = PAPI_start( EventSet );
320  if ( retval != PAPI_OK ) {
321  test_fail( __FILE__, __LINE__,
322  "PAPI_start failed\n",retval );
323  }
324 
325  retval = PAPI_reset( EventSet);
326  if ( retval != PAPI_OK ) {
327  test_fail( __FILE__, __LINE__,
328  "PAPI_reset() failed\n",retval );
329  }
330 
331  retval = PAPI_stop( EventSet, values );
332  if ( retval != PAPI_OK ) {
333  test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
334  }
335 
336 
337  if (!TESTS_QUIET) printf("Testing EXAMPLE_AUTOINC after PAPI_reset(): %lld\n",
338  values[0]);
339 
340  if (values[0]!=0) {
341  test_fail( __FILE__, __LINE__, "Result not zero!\n", 0);
342  }
343 
344  retval = PAPI_cleanup_eventset(EventSet);
345  if (retval != PAPI_OK) {
346  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
347  }
348 
349  retval = PAPI_destroy_eventset(&EventSet);
350  if (retval != PAPI_OK) {
351  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
352  }
353 
354  EventSet=PAPI_NULL;
355 
356 
357  /***********************************/
358  /* Test multiple events */
359  /***********************************/
360 
361  if (!TESTS_QUIET) printf("Testing Multiple Events: ");
362 
363  retval = PAPI_create_eventset( &EventSet );
364  if ( retval != PAPI_OK ) {
365  test_fail( __FILE__, __LINE__,
366  "PAPI_create_eventset() failed\n", retval );
367  }
368 
369  retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code);
370  if ( retval != PAPI_OK ) {
371  test_fail( __FILE__, __LINE__,
372  "EXAMPLE_CONSTANT not found\n",retval );
373  }
374 
375  retval = PAPI_add_event( EventSet, code);
376  if ( retval != PAPI_OK ) {
377  test_fail( __FILE__, __LINE__,
378  "PAPI_add_events failed\n", retval );
379  }
380 
381  retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code);
382  if ( retval != PAPI_OK ) {
383  test_fail( __FILE__, __LINE__,
384  "EXAMPLE_GLOBAL_AUTOINC not found\n",retval );
385  }
386 
387  retval = PAPI_add_event( EventSet, code);
388  if ( retval != PAPI_OK ) {
389  test_fail( __FILE__, __LINE__,
390  "PAPI_add_events failed\n", retval );
391  }
392 
393  retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code);
394  if ( retval != PAPI_OK ) {
395  test_fail( __FILE__, __LINE__,
396  "EXAMPLE_ZERO not found\n",retval );
397  }
398 
399  retval = PAPI_add_event( EventSet, code);
400  if ( retval != PAPI_OK ) {
401  test_fail( __FILE__, __LINE__,
402  "PAPI_add_events failed\n", retval );
403  }
404 
405 
406  retval = PAPI_start( EventSet );
407  if ( retval != PAPI_OK ) {
408  test_fail( __FILE__, __LINE__,
409  "PAPI_start failed\n",retval );
410  }
411 
412  retval = PAPI_stop( EventSet, values );
413  if ( retval != PAPI_OK ) {
414  test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
415  }
416 
417  if (!TESTS_QUIET) {
418  for(i=0;i<3;i++) {
419  printf("%lld ",values[i]);
420  }
421  printf("\n");
422  }
423 
424  if (values[0]!=42) {
425  test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0);
426  }
427 
428  if (values[2]!=0) {
429  test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0);
430  }
431 
432  retval = PAPI_cleanup_eventset(EventSet);
433  if (retval != PAPI_OK) {
434  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
435  }
436 
437  retval = PAPI_destroy_eventset(&EventSet);
438  if (retval != PAPI_OK) {
439  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
440  }
441 
442  EventSet=PAPI_NULL;
443 
444  /***********************************/
445  /* Test writing to an event */
446  /***********************************/
447 
448  if (!TESTS_QUIET) printf("Testing Write\n");
449 
450  retval = PAPI_create_eventset( &EventSet );
451  if ( retval != PAPI_OK ) {
452  test_fail( __FILE__, __LINE__,
453  "PAPI_create_eventset() failed\n", retval );
454  }
455 
456  retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code);
457  if ( retval != PAPI_OK ) {
458  test_fail( __FILE__, __LINE__,
459  "EXAMPLE_CONSTANT not found\n",retval );
460  }
461 
462  retval = PAPI_add_event( EventSet, code);
463  if ( retval != PAPI_OK ) {
464  test_fail( __FILE__, __LINE__,
465  "PAPI_add_events failed\n", retval );
466  }
467 
468  retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code);
469  if ( retval != PAPI_OK ) {
470  test_fail( __FILE__, __LINE__,
471  "EXAMPLE_GLOBAL_AUTOINC not found\n",retval );
472  }
473 
474  retval = PAPI_add_event( EventSet, code);
475  if ( retval != PAPI_OK ) {
476  test_fail( __FILE__, __LINE__,
477  "PAPI_add_events failed\n", retval );
478  }
479 
480  retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code);
481  if ( retval != PAPI_OK ) {
482  test_fail( __FILE__, __LINE__,
483  "EXAMPLE_ZERO not found\n",retval );
484  }
485 
486  retval = PAPI_add_event( EventSet, code);
487  if ( retval != PAPI_OK ) {
488  test_fail( __FILE__, __LINE__,
489  "PAPI_add_events failed\n", retval );
490  }
491 
492 
493  retval = PAPI_start( EventSet );
494  if ( retval != PAPI_OK ) {
495  test_fail( __FILE__, __LINE__,
496  "PAPI_start failed\n",retval );
497  }
498 
499  retval = PAPI_read ( EventSet, values );
500  if ( retval != PAPI_OK ) {
501  test_fail( __FILE__, __LINE__,
502  "PAPI_read failed\n",retval );
503  }
504 
505  if (!TESTS_QUIET) {
506  printf("Before values: ");
507  for(i=0;i<3;i++) {
508  printf("%lld ",values[i]);
509  }
510  printf("\n");
511  }
512 
513  values[0]=100;
514  values[1]=200;
515  values[2]=300;
516 
517  retval = PAPI_write ( EventSet, values );
518  if ( retval != PAPI_OK ) {
519  test_fail( __FILE__, __LINE__,
520  "PAPI_write failed\n",retval );
521  }
522 
523  retval = PAPI_stop( EventSet, values );
524  if ( retval != PAPI_OK ) {
525  test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval);
526  }
527 
528  if (!TESTS_QUIET) {
529  printf("After values: ");
530  for(i=0;i<3;i++) {
531  printf("%lld ",values[i]);
532  }
533  printf("\n");
534  }
535 
536 
537  if (values[0]!=42) {
538  test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0);
539  }
540 
541  if (values[1]!=200) {
542  test_fail( __FILE__, __LINE__, "Result should be 200!\n", 0);
543  }
544 
545  if (values[2]!=0) {
546  test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0);
547  }
548 
549  retval = PAPI_cleanup_eventset(EventSet);
550  if (retval != PAPI_OK) {
551  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval);
552  }
553 
554  retval = PAPI_destroy_eventset(&EventSet);
555  if (retval != PAPI_OK) {
556  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval);
557  }
558 
559  EventSet=PAPI_NULL;
560 
561 
562  /************/
563  /* All Done */
564  /************/
565 
566  if (!TESTS_QUIET) printf("\n");
567 
568  test_pass( __FILE__, NULL, 0 );
569 
570  return 0;
571 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:626
#define PAPI_ENOEVNT
Definition: papi.h:258
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
#define PAPI_NATIVE_MASK
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_reset(int EventSet)
Definition: papi.c:2403
int PAPI_write(int EventSet, long long *values)
Definition: papi.c:2757
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
#define PAPI_NULL
Definition: papi.h:290
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:966
int EventSet
Definition: data_range.c:25
int PAPI_num_components(void)
Definition: papi.c:4326
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
#define printf
Definition: papi_test.h:125
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:846
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
static struct counter_info * event_info
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
#define PAPI_VERSION_MAJOR(x)
Definition: papi.h:215
#define PAPI_VERSION_REVISION(x)
Definition: papi.h:217
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1324
#define PAPI_VERSION_MINOR(x)
Definition: papi.h:216
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
int PAPI_event_name_to_code(char *in, int *out)
Definition: papi.c:1010
#define PAPI_VERSION
Definition: papi.h:222
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
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
#define NUM_EVENTS
Definition: example_basic.c:25

Here is the call graph for this function: