PAPI 7.1.0.0
Loading...
Searching...
No Matches
linux-net.c File Reference

net component This file contains the source code for a component that enables PAPI-C to access network statistics through the /proc file system. This component will dynamically create a native events table for all the interfaces listed in /proc/net/dev (16 entries for each interface). More...

Include dependency graph for linux-net.c:

Go to the source code of this file.

Data Structures

struct  temp_event
 
struct  net_counters
 

Macros

#define NET_REFRESH_LATENCY   1000000
 
#define NET_PROC_FILE   "/proc/net/dev"
 
#define NET_PROC_MAX_LINE   (IFNAMSIZ + 16 * (20 + 1) + 16)
 
#define NET_INVALID_RESULT   -1
 
#define HANDLE_STRING_ERROR   {fprintf(stderr,"%s:%i unexpected string function error.\n",__FILE__,__LINE__); exit(-1);}
 
#define NET_INTERFACE_COUNTERS   16
 

Functions

static int generateNetEventList (void)
 
static int getInterfaceBaseIndex (const char *ifname)
 
static int read_net_counters (long long *values)
 
static int _net_init_thread (hwd_context_t *ctx)
 
static int _net_init_component (int cidx)
 
static int _net_init_control_state (hwd_control_state_t *ctl)
 
static int _net_start (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
static int _net_read (hwd_context_t *ctx, hwd_control_state_t *ctl, long long **events, int flags)
 
static int _net_stop (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
static int _net_shutdown_thread (hwd_context_t *ctx)
 
static int _net_shutdown_component (void)
 
static int _net_ctl (hwd_context_t *ctx, int code, _papi_int_option_t *option)
 
static int _net_update_control_state (hwd_control_state_t *ctl, NativeInfo_t *native, int count, hwd_context_t *ctx)
 
static int _net_set_domain (hwd_control_state_t *ctl, int domain)
 
int _net_reset (hwd_context_t *ctx, hwd_control_state_t *ctl)
 
static int _net_ntv_enum_events (unsigned int *EventCode, int modifier)
 
static int _net_ntv_name_to_code (const char *name, unsigned int *EventCode)
 
static int _net_ntv_code_to_name (unsigned int EventCode, char *name, int len)
 
static int _net_ntv_code_to_descr (unsigned int EventCode, char *name, int len)
 
static int _net_ntv_code_to_bits (unsigned int EventCode, hwd_register_t *bits)
 

Variables

papi_vector_t _net_vector
 
static NET_native_event_entry_t_net_native_events =NULL
 
static int num_events = 0
 
static int is_initialized = 0
 
static long long _net_register_start [NET_MAX_COUNTERS]
 
static long long _net_register_current [NET_MAX_COUNTERS]
 
static struct temp_eventroot = NULL
 
static const struct net_counters _net_counter_info [NET_INTERFACE_COUNTERS]
 

Detailed Description

Author
Haihang You you@c.nosp@m.s.ut.nosp@m.k.edu
Jose Pedro Oliveira jpo@d.nosp@m.i.um.nosp@m.inho..nosp@m.pt

Definition in file linux-net.c.

Macro Definition Documentation

◆ HANDLE_STRING_ERROR

#define HANDLE_STRING_ERROR   {fprintf(stderr,"%s:%i unexpected string function error.\n",__FILE__,__LINE__); exit(-1);}

Definition at line 57 of file linux-net.c.

◆ NET_INTERFACE_COUNTERS

#define NET_INTERFACE_COUNTERS   16

Definition at line 76 of file linux-net.c.

◆ NET_INVALID_RESULT

#define NET_INVALID_RESULT   -1

Definition at line 51 of file linux-net.c.

◆ NET_PROC_FILE

#define NET_PROC_FILE   "/proc/net/dev"

Definition at line 44 of file linux-net.c.

◆ NET_PROC_MAX_LINE

#define NET_PROC_MAX_LINE   (IFNAMSIZ + 16 * (20 + 1) + 16)

Definition at line 49 of file linux-net.c.

◆ NET_REFRESH_LATENCY

#define NET_REFRESH_LATENCY   1000000

Definition at line 42 of file linux-net.c.

Function Documentation

◆ _net_ctl()

static int _net_ctl ( hwd_context_t ctx,
int  code,
_papi_int_option_t option 
)
static

Definition at line 498 of file linux-net.c.

499{
500 ( void ) ctx;
501 ( void ) code;
502 ( void ) option;
503
504 return PAPI_OK;
505}
#define PAPI_OK
Definition: f90papi.h:73

◆ _net_init_component()

static int _net_init_component ( int  cidx)
static

Definition at line 320 of file linux-net.c.

321{
322 int retval = PAPI_OK;
323 int i = 0;
324 struct temp_event *t, *last;
325
326 if ( is_initialized )
327 goto fn_exit;
328
329 memset(_net_register_start, 0,
331 memset(_net_register_current, 0,
333
334 is_initialized = 1;
335
336 /* The network interfaces are listed in /proc/net/dev */
338
339 if ( num_events < 0 ) { /* disabled_reason already set */
341 goto fn_fail;
342 }
343
344 if ( num_events == 0 ) /* No network interfaces found */
345 goto fn_exit;
346
347 t = root;
350 do {
351 int retlen;
352 retlen = snprintf(_net_native_events[i].name, PAPI_MAX_STR_LEN, "%s", t->name);
353 if (retlen <= 0 || retlen >= PAPI_MAX_STR_LEN) HANDLE_STRING_ERROR;
354 retlen = snprintf(_net_native_events[i].description, PAPI_MAX_STR_LEN, "%s", t->description);
355 if (retlen <= 0 || retlen >= PAPI_MAX_STR_LEN) HANDLE_STRING_ERROR;
357 last = t;
358 t = t->next;
360 i++;
361 } while (t != NULL);
362 root = NULL;
363
364 /* Export the total number of events available */
366
367 /* Export the component id */
369
370 fn_exit:
371 _papi_hwd[cidx]->cmp_info.disabled = retval;
372 return retval;
373 fn_fail:
374 goto fn_exit;
375}
int i
struct papi_vectors * _papi_hwd[]
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
#define PAPI_ECMP
Definition: f90papi.h:214
static struct temp_event * last
static int num_events
Definition: linux-net.c:61
static long long _net_register_current[NET_MAX_COUNTERS]
Definition: linux-net.c:65
static struct temp_event * root
Definition: linux-net.c:73
static int is_initialized
Definition: linux-net.c:62
static int generateNetEventList(void)
Definition: linux-net.c:111
papi_vector_t _net_vector
Definition: linux-net.c:35
static long long _net_register_start[NET_MAX_COUNTERS]
Definition: linux-net.c:64
#define HANDLE_STRING_ERROR
Definition: linux-net.c:57
static NET_native_event_entry_t * _net_native_events
Definition: linux-net.c:59
#define NET_MAX_COUNTERS
Definition: linux-net.h:34
#define papi_free(a)
Definition: papi_memory.h:35
#define papi_malloc(a)
Definition: papi_memory.h:34
static int cidx
const char * name
Definition: rocs.c:225
Definition: linux-net.h:53
NET_register_t resources
Definition: linux-net.h:54
unsigned int selector
Definition: linux-net.h:40
PAPI_component_info_t cmp_info
Definition: papi_vector.h:20
char description[PAPI_MAX_STR_LEN]
struct temp_event * next
char name[PAPI_MAX_STR_LEN]
int retval
Definition: zero_fork.c:53
Here is the call graph for this function:

◆ _net_init_control_state()

static int _net_init_control_state ( hwd_control_state_t ctl)
static

Definition at line 383 of file linux-net.c.

384{
385 ( void ) ctl;
386
387 return PAPI_OK;
388}

◆ _net_init_thread()

static int _net_init_thread ( hwd_context_t ctx)
static

Definition at line 307 of file linux-net.c.

308{
309 ( void ) ctx;
310
311 return PAPI_OK;
312}

◆ _net_ntv_code_to_bits()

static int _net_ntv_code_to_bits ( unsigned int  EventCode,
hwd_register_t bits 
)
static

Definition at line 650 of file linux-net.c.

651{
652 int index = EventCode;
653
654 if ( index >= 0 && index < num_events ) {
655 memcpy( ( NET_register_t * ) bits,
656 &( _net_native_events[index].resources ),
657 sizeof ( NET_register_t ) );
658 return PAPI_OK;
659 }
660
661 return PAPI_ENOEVNT;
662}
#define PAPI_ENOEVNT
Definition: f90papi.h:139

◆ _net_ntv_code_to_descr()

static int _net_ntv_code_to_descr ( unsigned int  EventCode,
char *  name,
int  len 
)
static

Definition at line 633 of file linux-net.c.

634{
635 int index = EventCode;
636
637 if ( index >= 0 && index < num_events ) {
638 strncpy( name, _net_native_events[index].description, len );
639 return PAPI_OK;
640 }
641
642 return PAPI_ENOEVNT;
643}

◆ _net_ntv_code_to_name()

static int _net_ntv_code_to_name ( unsigned int  EventCode,
char *  name,
int  len 
)
static

Definition at line 616 of file linux-net.c.

617{
618 int index = EventCode;
619
620 if ( index >= 0 && index < num_events ) {
621 strncpy( name, _net_native_events[index].name, len );
622 return PAPI_OK;
623 }
624
625 return PAPI_ENOEVNT;
626}

◆ _net_ntv_enum_events()

static int _net_ntv_enum_events ( unsigned int EventCode,
int  modifier 
)
static

Definition at line 561 of file linux-net.c.

562{
563 int index;
564
565 switch ( modifier ) {
566 case PAPI_ENUM_FIRST:
567 if (num_events==0) {
568 return PAPI_ENOEVNT;
569 }
570 *EventCode = 0;
571 return PAPI_OK;
572 break;
573
574 case PAPI_ENUM_EVENTS:
575 index = *EventCode;
576 if ( index < num_events - 1 ) {
577 *EventCode = *EventCode + 1;
578 return PAPI_OK;
579 } else {
580 return PAPI_ENOEVNT;
581 }
582 break;
583
584 default:
585 return PAPI_EINVAL;
586 break;
587 }
588 return PAPI_EINVAL;
589}
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_EINVAL
Definition: f90papi.h:115

◆ _net_ntv_name_to_code()

static int _net_ntv_name_to_code ( const char *  name,
unsigned int EventCode 
)
static

Definition at line 596 of file linux-net.c.

597{
598 int i;
599
600 for ( i=0; i<num_events; i++) {
601 if (strcmp(name, _net_native_events[i].name) == 0) {
602 *EventCode = i;
603
604 return PAPI_OK;
605 }
606 }
607
608 return PAPI_ENOEVNT;
609}

◆ _net_read()

static int _net_read ( hwd_context_t ctx,
hwd_control_state_t ctl,
long long **  events,
int  flags 
)
static

Definition at line 414 of file linux-net.c.

416{
417 (void) flags;
418 (void) ctx;
419
420 NET_control_state_t *net_ctl = (NET_control_state_t *) ctl;
421 long long now = PAPI_get_real_usec();
422 int i;
423
424 /* Caching
425 * Only read new values from /proc if enough time has passed
426 * since the last read.
427 */
428 if ( now - net_ctl->lastupdate > NET_REFRESH_LATENCY ) {
430 for ( i=0; i<NET_MAX_COUNTERS; i++ ) {
432 }
433 net_ctl->lastupdate = now;
434 }
435 *events = net_ctl->values;
436
437 return PAPI_OK;
438}
get real time counter value in microseconds
char events[MAX_EVENTS][BUFSIZ]
#define NET_REFRESH_LATENCY
Definition: linux-net.c:42
static int read_net_counters(long long *values)
Definition: linux-net.c:221
long long lastupdate
Definition: linux-net.h:69
long long values[NET_MAX_COUNTERS]
Definition: linux-net.h:68
Here is the call graph for this function:

◆ _net_reset()

int _net_reset ( hwd_context_t ctx,
hwd_control_state_t ctl 
)

Definition at line 548 of file linux-net.c.

549{
550 ( void ) ctx;
551 ( void ) ctl;
552
553 return PAPI_OK;
554}

◆ _net_set_domain()

static int _net_set_domain ( hwd_control_state_t ctl,
int  domain 
)
static

Definition at line 537 of file linux-net.c.

538{
539 ( void ) ctl;
540 if ( PAPI_DOM_ALL != domain )
541 return PAPI_EINVAL;
542
543 return PAPI_OK;
544}
#define PAPI_DOM_ALL
Definition: f90papi.h:261

◆ _net_shutdown_component()

static int _net_shutdown_component ( void  )
static

Definition at line 476 of file linux-net.c.

477{
478 if ( is_initialized )
479 {
480 is_initialized = 0;
481 if (_net_native_events != NULL)
482 {
484 _net_native_events = NULL;
485 }
486 }
487
488 return PAPI_OK;
489}

◆ _net_shutdown_thread()

static int _net_shutdown_thread ( hwd_context_t ctx)
static

Definition at line 464 of file linux-net.c.

465{
466 ( void ) ctx;
467
468 return PAPI_OK;
469}

◆ _net_start()

static int _net_start ( hwd_context_t ctx,
hwd_control_state_t ctl 
)
static

Definition at line 392 of file linux-net.c.

393{
394 ( void ) ctx;
395
396 NET_control_state_t *net_ctl = (NET_control_state_t *) ctl;
397 long long now = PAPI_get_real_usec();
398
402
403 /* set initial values to 0 */
404 memset(net_ctl->values, 0, NET_MAX_COUNTERS*sizeof(net_ctl->values[0]));
405
406 /* Set last access time for caching purposes */
407 net_ctl->lastupdate = now;
408
409 return PAPI_OK;
410}
Here is the call graph for this function:

◆ _net_stop()

static int _net_stop ( hwd_context_t ctx,
hwd_control_state_t ctl 
)
static

Definition at line 442 of file linux-net.c.

443{
444 (void) ctx;
445
446 NET_control_state_t *net_ctl = (NET_control_state_t *) ctl;
447 long long now = PAPI_get_real_usec();
448 int i;
449
451 for ( i=0; i<NET_MAX_COUNTERS; i++ ) {
453 }
454 net_ctl->lastupdate = now;
455
456 return PAPI_OK;
457}
Here is the call graph for this function:

◆ _net_update_control_state()

static int _net_update_control_state ( hwd_control_state_t ctl,
NativeInfo_t native,
int  count,
hwd_context_t ctx 
)
static

Definition at line 509 of file linux-net.c.

511{
512 ( void ) ctx;
513 ( void ) ctl;
514
515 int i, index;
516
517 for ( i = 0; i < count; i++ ) {
518 index = native[i].ni_event;
519 native[i].ni_position = _net_native_events[index].resources.selector - 1;
520 }
521
522 return PAPI_OK;
523}
static long count
static int native

◆ generateNetEventList()

static int generateNetEventList ( void  )
static

Definition at line 111 of file linux-net.c.

112{
113 FILE *fin;
114 char line[NET_PROC_MAX_LINE];
115 char *retval, *ifname;
116 int count = 0;
117 struct temp_event *temp;
118 struct temp_event *last = NULL;
119 int i, j;
120
121 fin = fopen(NET_PROC_FILE, "r");
122 if (fin == NULL) {
123 SUBDBG("Can't find %s, are you sure the /proc file-system is mounted?\n",
126 "Failed to find /proc file-system.");
127 _net_vector.cmp_info.disabled_reason[PAPI_MAX_STR_LEN-1]=0; // force null termination.
128 return 0;
129 }
130
131 /* skip the 2 header lines */
132 for (i=0; i<2; i++) {
133 retval = fgets (line, NET_PROC_MAX_LINE, fin);
134 if (retval == NULL) {
135 fclose(fin);
136 SUBDBG("Not enough lines in %s\n", NET_PROC_FILE);
138 "Not enough lines in '%s'.", NET_PROC_FILE);
139 _net_vector.cmp_info.disabled_reason[PAPI_MAX_STR_LEN-1]=0; // force null termination.
140 return 0;
141 }
142 }
143
144 while ((fgets (line, NET_PROC_MAX_LINE, fin)) == line) {
145
146 /* split the interface name from the 16 counters */
147 retval = strstr(line, ":");
148 if (retval == NULL) {
149 SUBDBG("Wrong line format <%s>\n", line);
150 continue;
151 }
152
153 *retval = '\0';
154 ifname = line;
155 while (isspace(*ifname)) { ifname++; }
156
157 for (j=0; j<NET_INTERFACE_COUNTERS; j++) {
158
159 /* keep the interface name around */
160 temp = (struct temp_event *)papi_malloc(sizeof(struct temp_event));
161 if (!temp) {
162 PAPIERROR("out of memory!");
163 fclose(fin);
165 "%s failed to allocate %lu bytes for temp_event.", __func__, sizeof(struct temp_event));
166 _net_vector.cmp_info.disabled_reason[PAPI_MAX_STR_LEN-1]=0; // force null termination.
167 return PAPI_ENOMEM;
168 }
169 temp->next = NULL;
170
171 if (root == NULL) {
172 root = temp;
173 } else if (last) {
174 last->next = temp;
175 } else {
176 free(temp);
177 fclose(fin);
178 PAPIERROR("This shouldn't be possible\n");
180 "%s Error navigating linked list.", __func__);
181 _net_vector.cmp_info.disabled_reason[PAPI_MAX_STR_LEN-1]=0; // force null termination.
182 return PAPI_ECMP;
183 }
184 last = temp;
185
186 size_t str_len = strlen(ifname) + strlen(_net_counter_info[j].name) + 1;
187 str_len = (str_len > PAPI_MAX_STR_LEN - 1) ? PAPI_MAX_STR_LEN - 1 : str_len;
188 snprintf(temp->name, str_len, "%s:%s",
189 ifname, _net_counter_info[j].name);
190 str_len = strlen(ifname) + strlen(_net_counter_info[j].description) + 1;
191 str_len = (str_len > PAPI_MAX_STR_LEN - 1) ? PAPI_MAX_STR_LEN - 1 : str_len;
192 snprintf(temp->description, str_len, "%s %s",
193 ifname, _net_counter_info[j].description);
194
195 count++;
196 }
197 }
198
199 fclose(fin);
200
201 return count;
202}
#define PAPI_ENOMEM
Definition: f90papi.h:16
#define NET_INTERFACE_COUNTERS
Definition: linux-net.c:76
static const struct net_counters _net_counter_info[NET_INTERFACE_COUNTERS]
#define NET_PROC_FILE
Definition: linux-net.c:44
#define NET_PROC_MAX_LINE
Definition: linux-net.c:49
#define SUBDBG(format, args...)
Definition: papi_debug.h:64
int fclose(FILE *__stream)
void PAPIERROR(char *format,...)
char disabled_reason[PAPI_HUGE_STR_LEN]
Definition: papi.h:634
char * description
Definition: linux-net.c:80
char * name
Definition: linux-net.c:79
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getInterfaceBaseIndex()

static int getInterfaceBaseIndex ( const char *  ifname)
static

Definition at line 206 of file linux-net.c.

207{
208 int i;
209
210 for ( i=0; i<num_events; i+=NET_INTERFACE_COUNTERS ) {
211 if (strncmp(_net_native_events[i].name, ifname, strlen(ifname)) == 0) {
212 return i;
213 }
214 }
215
216 return -1; /* Not found */
217}
Here is the caller graph for this function:

◆ read_net_counters()

static int read_net_counters ( long long values)
static

Definition at line 221 of file linux-net.c.

222{
223 FILE *fin;
224 char line[NET_PROC_MAX_LINE];
225 char *retval, *ifname, *data;
226 int i, nf, if_bidx;
227
228 fin = fopen(NET_PROC_FILE, "r");
229 if (fin == NULL) {
230 SUBDBG("Can't find %s, are you sure the /proc file-system is mounted?\n",
232 return NET_INVALID_RESULT;
233 }
234
235 /* skip the 2 header lines */
236 for (i=0; i<2; i++) {
237 retval = fgets (line, NET_PROC_MAX_LINE, fin);
238 if (retval == NULL) {
239 SUBDBG("Not enough lines in %s\n", NET_PROC_FILE);
240 fclose(fin);
241 return 0;
242 }
243 }
244
245 while ((fgets (line, NET_PROC_MAX_LINE, fin)) == line) {
246
247 /* split the interface name from its 16 counters */
248 retval = strstr(line, ":");
249 if (retval == NULL) {
250 SUBDBG("Wrong line format <%s>\n", line);
251 continue;
252 }
253
254 *retval = '\0';
255 data = retval + 1;
256 ifname = line;
257 while (isspace(*ifname)) { ifname++; }
258
259 if_bidx = getInterfaceBaseIndex(ifname);
260 if (if_bidx < 0) {
261 SUBDBG("Interface <%s> not found\n", ifname);
262 } else {
263 nf = sscanf( data,
264 "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\n",
265 &values[if_bidx + 0], &values[if_bidx + 1],
266 &values[if_bidx + 2], &values[if_bidx + 3],
267 &values[if_bidx + 4], &values[if_bidx + 5],
268 &values[if_bidx + 6], &values[if_bidx + 7],
269 &values[if_bidx + 8], &values[if_bidx + 9],
270 &values[if_bidx + 10], &values[if_bidx + 11],
271 &values[if_bidx + 12], &values[if_bidx + 13],
272 &values[if_bidx + 14], &values[if_bidx + 15]);
273
274 SUBDBG("\nRead "
275 "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\n",
276 values[if_bidx + 0], values[if_bidx + 1],
277 values[if_bidx + 2], values[if_bidx + 3],
278 values[if_bidx + 4], values[if_bidx + 5],
279 values[if_bidx + 6], values[if_bidx + 7],
280 values[if_bidx + 8], values[if_bidx + 9],
281 values[if_bidx + 10], values[if_bidx + 11],
282 values[if_bidx + 12], values[if_bidx + 13],
283 values[if_bidx + 14], values[if_bidx + 15]);
284
285 if ( nf != NET_INTERFACE_COUNTERS ) {
286 /* This shouldn't happen */
287 SUBDBG("/proc line with wrong number of fields\n");
288 }
289 }
290
291 }
292
293 fclose(fin);
294
295 return 0;
296}
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
#define NET_INVALID_RESULT
Definition: linux-net.c:51
static int getInterfaceBaseIndex(const char *ifname)
Definition: linux-net.c:206
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ _net_counter_info

const struct net_counters _net_counter_info[NET_INTERFACE_COUNTERS]
static
Initial value:
= {
{ "rx:bytes", "receive bytes"},
{ "rx:packets", "receive packets"},
{ "rx:errors", "receive errors"},
{ "rx:dropped", "receive dropped"},
{ "rx:fifo", "receive fifo"},
{ "rx:frame", "receive frame"},
{ "rx:compressed", "receive compressed"},
{ "rx:multicast", "receive multicast"},
{ "tx:bytes", "transmit bytes"},
{ "tx:packets", "transmit packets"},
{ "tx:errors", "transmit errors"},
{ "tx:dropped", "transmit dropped"},
{ "tx:fifo", "transmit fifo"},
{ "tx:colls", "transmit colls"},
{ "tx:carrier", "transmit carrier"},
{ "tx:compressed", "transmit compressed"},
}

◆ _net_native_events

NET_native_event_entry_t* _net_native_events =NULL
static

Definition at line 59 of file linux-net.c.

◆ _net_register_current

long long _net_register_current[NET_MAX_COUNTERS]
static

Definition at line 65 of file linux-net.c.

◆ _net_register_start

long long _net_register_start[NET_MAX_COUNTERS]
static

Definition at line 64 of file linux-net.c.

◆ _net_vector

papi_vector_t _net_vector

Definition at line 35 of file linux-net.c.

◆ is_initialized

int is_initialized = 0
static

Definition at line 62 of file linux-net.c.

◆ num_events

int num_events = 0
static

Definition at line 61 of file linux-net.c.

◆ root

struct temp_event* root = NULL
static

Definition at line 73 of file linux-net.c.