PAPI  5.0.1.0
net_values_by_name.c
Go to the documentation of this file.
00001 /****************************/
00002 /* THIS IS OPEN SOURCE CODE */
00003 /****************************/
00004 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include "papi_test.h"
00017 
00018 /*
00019 #define IFNAME     "eth0"
00020 */
00021 #define IFNAME     "lo"
00022 #define PINGADDR   "127.0.0.1"
00023 
00024 #define NUM_EVENTS 4
00025 
00026 int main (int argc, char **argv)
00027 {
00028     int i, retval;
00029     int EventSet = PAPI_NULL;
00030     char *event_name[NUM_EVENTS] = {
00031         IFNAME ":rx:bytes",
00032         IFNAME ":rx:packets",
00033         IFNAME ":tx:bytes",
00034         IFNAME ":tx:packets",
00035     };
00036     int event_code[NUM_EVENTS] = { 0, 0, 0, 0};
00037     long long event_value[NUM_EVENTS];
00038     int total_events=0;
00039 
00040     /* Set TESTS_QUIET variable */
00041     tests_quiet( argc, argv );
00042 
00043     /* PAPI Initialization */
00044     retval = PAPI_library_init( PAPI_VER_CURRENT );
00045     if ( retval != PAPI_VER_CURRENT ) {
00046         test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
00047     }
00048 
00049     if (!TESTS_QUIET) {
00050         printf("Net events by name\n");
00051     }
00052 
00053     /* Map names to codes */
00054     for ( i=0; i<NUM_EVENTS; i++ ) {
00055         retval = PAPI_event_name_to_code( event_name[i], &event_code[i]);
00056         if ( retval != PAPI_OK ) {
00057             test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code", retval );
00058         }
00059 
00060         total_events++;
00061     }
00062 
00063     /* Create and populate the EventSet */
00064     EventSet = PAPI_NULL;
00065 
00066     retval = PAPI_create_eventset( &EventSet );
00067     if (retval != PAPI_OK) {
00068         test_fail(__FILE__, __LINE__, "PAPI_create_eventset()", retval);
00069     }
00070 
00071     retval = PAPI_add_events( EventSet, event_code, NUM_EVENTS);
00072     if (retval != PAPI_OK) {
00073         test_fail(__FILE__, __LINE__, "PAPI_add_events()", retval);
00074     }
00075 
00076     retval = PAPI_start( EventSet );
00077     if (retval != PAPI_OK) {
00078         test_fail(__FILE__, __LINE__, "PAPI_start()", retval);
00079     }
00080 
00081     /* generate some traffic
00082      * the operation should take more than one second in order
00083      * to guarantee that the network counters are updated */
00084     retval = system("ping -c 4 " PINGADDR " > /dev/null");
00085     if (retval < 0) {
00086         test_fail(__FILE__, __LINE__, "Unable to start ping", retval);
00087     }
00088 
00089     retval = PAPI_stop( EventSet, event_value );
00090     if (retval != PAPI_OK) {
00091         test_fail(__FILE__, __LINE__, "PAPI_start()", retval);
00092     }
00093 
00094     if (!TESTS_QUIET) {
00095         for ( i=0; i<NUM_EVENTS; i++ ) {
00096             printf("0x%x %-24s = %lld\n",
00097                 event_code[i], event_name[i], event_value[i]);
00098         }
00099     }
00100 
00101     retval = PAPI_cleanup_eventset( EventSet );
00102     if (retval != PAPI_OK) {
00103         test_fail(__FILE__, __LINE__, "PAPI_cleanup_eventset()", retval);
00104     }
00105 
00106     retval = PAPI_destroy_eventset( &EventSet );
00107     if (retval != PAPI_OK) {
00108         test_fail(__FILE__, __LINE__, "PAPI_destroy_eventset()", retval);
00109     }
00110 
00111     if (total_events==0) {
00112         test_skip(__FILE__,__LINE__,"No net events found", 0);
00113     }
00114 
00115     test_pass( __FILE__, NULL, 0 );
00116 
00117     return 0;
00118 }
00119 
00120 // vim:set ai ts=4 sw=4 sts=4 et:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines