PAPI  5.0.1.0
appio_values_by_name.c
Go to the documentation of this file.
00001 /****************************/
00002 /* THIS IS OPEN SOURCE CODE */
00003 /****************************/
00004 
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include "papi_test.h"
00018 #include <errno.h>
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021 #include <fcntl.h>
00022 #include <unistd.h>
00023 
00024 
00025 #define NUM_EVENTS 11
00026 
00027 int main (int argc, char **argv)
00028 {
00029     int i, retval;
00030     int EventSet = PAPI_NULL;
00031     char *event_name[NUM_EVENTS] = {
00032         "READ_BYTES",
00033         "READ_CALLS",
00034         "READ_USEC",
00035         "READ_EOF",
00036         "READ_SHORT",
00037         "READ_ERR",
00038         "WRITE_BYTES",
00039         "WRITE_CALLS",
00040         "WRITE_USEC",
00041         "WRITE_ERR",
00042         "WRITE_SHORT"
00043     };
00044     int event_code[NUM_EVENTS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0};
00045     long long event_value[NUM_EVENTS];
00046     int total_events=0;
00047 
00048     /* Set TESTS_QUIET variable */
00049     tests_quiet( argc, argv );
00050 
00051     /* PAPI Initialization */
00052     retval = PAPI_library_init( PAPI_VER_CURRENT );
00053     if ( retval != PAPI_VER_CURRENT ) {
00054         test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
00055     }
00056 
00057     if (!TESTS_QUIET) {
00058         printf("Appio events by name\n");
00059     }
00060 
00061     /* Map names to codes */
00062     for ( i=0; i<NUM_EVENTS; i++ ) {
00063         retval = PAPI_event_name_to_code( event_name[i], &event_code[i]);
00064         if ( retval != PAPI_OK ) {
00065             test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code", retval );
00066         }
00067 
00068         total_events++;
00069     }
00070 
00071     int fdin,fdout;
00072     const char* infile = "/etc/group";
00073     if (!TESTS_QUIET) printf("This program will read %s and write it to /dev/null\n", infile);
00074     int bytes = 0;
00075     char buf[1024];
00076 
00077     /* Create and populate the EventSet */
00078     EventSet = PAPI_NULL;
00079 
00080     retval = PAPI_create_eventset( &EventSet );
00081     if (retval != PAPI_OK) {
00082         test_fail(__FILE__, __LINE__, "PAPI_create_eventset()", retval);
00083     }
00084 
00085     retval = PAPI_add_events( EventSet, event_code, NUM_EVENTS);
00086     if (retval != PAPI_OK) {
00087         test_fail(__FILE__, __LINE__, "PAPI_add_events()", retval);
00088     }
00089 
00090     retval = PAPI_start( EventSet );
00091     if (retval != PAPI_OK) {
00092         test_fail(__FILE__, __LINE__, "PAPI_start()", retval);
00093     }
00094 
00095     fdin=open(infile, O_RDONLY);
00096     if (fdin < 0) perror("Could not open file for reading: \n");
00097     fdout=open("/dev/null", O_WRONLY);
00098     if (fdout < 0) perror("Could not open /dev/null for writing: \n");
00099 
00100     while ((bytes = read(fdin, buf, 1024)) > 0) {
00101       write(fdout, buf, bytes);
00102     }
00103     close(fdin);
00104     close(fdout);
00105 
00106     retval = PAPI_stop( EventSet, event_value );
00107     if (retval != PAPI_OK) {
00108         test_fail(__FILE__, __LINE__, "PAPI_start()", retval);
00109     }
00110 
00111     if (!TESTS_QUIET) {
00112         for ( i=0; i<NUM_EVENTS; i++ ) {
00113             printf("0x%x %-24s = %lld\n",
00114                 event_code[i], event_name[i], event_value[i]);
00115         }
00116     }
00117 
00118     retval = PAPI_cleanup_eventset( EventSet );
00119     if (retval != PAPI_OK) {
00120         test_fail(__FILE__, __LINE__, "PAPI_cleanup_eventset()", retval);
00121     }
00122 
00123     retval = PAPI_destroy_eventset( &EventSet );
00124     if (retval != PAPI_OK) {
00125         test_fail(__FILE__, __LINE__, "PAPI_destroy_eventset()", retval);
00126     }
00127 
00128     if (total_events==0) {
00129         test_skip(__FILE__,__LINE__,"No appio events found", 0);
00130     }
00131 
00132     test_pass( __FILE__, NULL, 0 );
00133 
00134     return 0;
00135 }
00136 
00137 // vim:set ai ts=4 sw=4 sts=4 et:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines