PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
net_values_by_name.c
Go to the documentation of this file.
1 /****************************/
2 /* THIS IS OPEN SOURCE CODE */
3 /****************************/
4 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "papi.h"
19 #include "papi_test.h"
20 
21 /*
22 #define IFNAME "eth0"
23 */
24 #define IFNAME "lo"
25 #define PINGADDR "127.0.0.1"
26 
27 #define NUM_EVENTS 4
28 
29 int main (int argc, char **argv)
30 {
31  int i, retval;
32  int EventSet = PAPI_NULL;
33  char *event_name[NUM_EVENTS] = {
34  IFNAME ":rx:bytes",
35  IFNAME ":rx:packets",
36  IFNAME ":tx:bytes",
37  IFNAME ":tx:packets",
38  };
39  int event_code[NUM_EVENTS] = { 0, 0, 0, 0};
40  long long event_value[NUM_EVENTS];
41  int total_events=0;
42 
43  /* Set TESTS_QUIET variable */
44  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 (!TESTS_QUIET) {
53  printf("Net events by name\n");
54  }
55 
56  /* Map names to codes */
57  for ( i=0; i<NUM_EVENTS; i++ ) {
58  retval = PAPI_event_name_to_code( event_name[i], &event_code[i]);
59  if ( retval != PAPI_OK ) {
60  test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code", retval );
61  }
62 
63  total_events++;
64  }
65 
66  /* Create and populate the EventSet */
67  EventSet = PAPI_NULL;
68 
69  retval = PAPI_create_eventset( &EventSet );
70  if (retval != PAPI_OK) {
71  test_fail(__FILE__, __LINE__, "PAPI_create_eventset()", retval);
72  }
73 
74  retval = PAPI_add_events( EventSet, event_code, NUM_EVENTS);
75  if (retval != PAPI_OK) {
76  test_fail(__FILE__, __LINE__, "PAPI_add_events()", retval);
77  }
78 
79  retval = PAPI_start( EventSet );
80  if (retval != PAPI_OK) {
81  test_fail(__FILE__, __LINE__, "PAPI_start()", retval);
82  }
83 
84  /* generate some traffic
85  * the operation should take more than one second in order
86  * to guarantee that the network counters are updated */
87  retval = system("ping -c 4 " PINGADDR " > /dev/null");
88  if (retval < 0) {
89  test_fail(__FILE__, __LINE__, "Unable to start ping", retval);
90  }
91 
92  retval = PAPI_stop( EventSet, event_value );
93  if (retval != PAPI_OK) {
94  test_fail(__FILE__, __LINE__, "PAPI_start()", retval);
95  }
96 
97  if (!TESTS_QUIET) {
98  for ( i=0; i<NUM_EVENTS; i++ ) {
99  printf("%#x %-24s = %lld\n",
100  event_code[i], event_name[i], event_value[i]);
101  }
102  }
103 
104  retval = PAPI_cleanup_eventset( EventSet );
105  if (retval != PAPI_OK) {
106  test_fail(__FILE__, __LINE__, "PAPI_cleanup_eventset()", retval);
107  }
108 
109  retval = PAPI_destroy_eventset( &EventSet );
110  if (retval != PAPI_OK) {
111  test_fail(__FILE__, __LINE__, "PAPI_destroy_eventset()", retval);
112  }
113 
114  test_pass( __FILE__ );
115 
116  return 0;
117 }
118 
119 // vim:set ai ts=4 sw=4 sts=4 et:
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#define IFNAME
Prints the values of several net events specified by names.
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
void test_pass(const char *filename)
Definition: test_utils.c:432
#define PAPI_NULL
Definition: papi.h:292
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
#define PINGADDR
#define NUM_EVENTS
int int argc
Definition: iozone.c:1609
int PAPI_add_events(int EventSet, int *Events, int number)
Definition: papi.c:5843
Return codes and api definitions.
char ** argv
Definition: iozone.c:1610
int PAPI_library_init(int version)
Definition: papi.c:500
int i
Definition: fileop.c:140
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
printf("\tTry: -i 0 -i 1 \n\n")
int TESTS_QUIET
Definition: test_utils.c:18
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
int PAPI_start(int EventSet)
Definition: papi.c:2096
int system()
ssize_t retval
Definition: libasync.c:338
#define PAPI_VER_CURRENT
Definition: papi.h:225
int main(int argc, char **argv)
List all appio events codes and names.