ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/network_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/network_stats.c (file contents):
Revision 1.54 by tdb, Mon Mar 8 13:48:00 2004 UTC vs.
Revision 1.59 by ats, Sun Apr 4 22:52:16 2004 UTC

# Line 28 | Line 28
28   #include <stdlib.h>
29   #include <string.h>
30   #include "statgrab.h"
31 + #include "vector.h"
32   #include <time.h>
33   #ifdef SOLARIS
34   #include <kstat.h>
# Line 66 | Line 67 | typedef __uint64_t u64;
67   #include <net/if.h>
68   #include <net/if_media.h>
69   #include <sys/ioctl.h>
70 + #include <unistd.h>
71   #endif
72  
73 < static network_stat_t *network_stats=NULL;
74 < static int interfaces=0;
75 <
76 < void network_stat_init(int start, int end, network_stat_t *net_stats){
77 <
78 <        for(net_stats+=start; start<end; start++){
79 <                net_stats->interface_name=NULL;
80 <                net_stats->tx=0;
81 <                net_stats->rx=0;
80 <                net_stats->ipackets=0;
81 <                net_stats->opackets=0;
82 <                net_stats->ierrors=0;
83 <                net_stats->oerrors=0;
84 <                net_stats->collisions=0;
85 <                net_stats++;
86 <        }
73 > static void network_stat_init(network_stat_t *s) {
74 >        s->interface_name = NULL;
75 >        s->tx = 0;
76 >        s->rx = 0;
77 >        s->ipackets = 0;
78 >        s->opackets = 0;
79 >        s->ierrors = 0;
80 >        s->oerrors = 0;
81 >        s->collisions = 0;
82   }
83  
84 < network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
85 <
91 <        if(net_stats==NULL){
92 <
93 <                if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
94 <                        return NULL;
95 <                }
96 <                network_stat_init(0, needed_entries, net_stats);
97 <                *cur_entries=needed_entries;
98 <
99 <                return net_stats;
100 <        }
101 <
102 <
103 <        if(*cur_entries<needed_entries){
104 <                net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
105 <                if(net_stats==NULL){
106 <                        return NULL;
107 <                }
108 <                network_stat_init(*cur_entries, needed_entries, net_stats);
109 <                *cur_entries=needed_entries;
110 <        }
111 <
112 <        return net_stats;
84 > static void network_stat_destroy(network_stat_t *s) {
85 >        free(s->interface_name);
86   }
87  
88 + VECTOR_DECLARE_STATIC(network_stats, network_stat_t, 5,
89 +                      network_stat_init, network_stat_destroy);
90  
91   network_stat_t *get_network_stats(int *entries){
92 <
118 <        static int sizeof_network_stats=0;      
92 >        int interfaces;
93          network_stat_t *network_stat_ptr;
94  
95   #ifdef SOLARIS
# Line 145 | Line 119 | network_stat_t *get_network_stats(int *entries){
119          
120          for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){
121                  if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
122 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
123 <                if(network_stats==NULL){
122 >
123 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
124                          return NULL;
125                  }
126                  network_stat_ptr=network_stats+interfaces;
# Line 199 | Line 173 | network_stat_t *get_network_stats(int *entries){
173                                   * have the rbytes/obytes values; for instance,
174                                   * the loopback devices have this behaviour
175                                   * (although they do track packets in/out). */
176 +                                /* FIXME: Show packet counts when byte counts
177 +                                 * not available. */
178                                  continue;
179                          }
180  
181                          /* Create new network_stats */
182 <                        network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
207 <                        if(network_stats==NULL){
182 >                        if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
183                                  return NULL;
184                          }
185                          network_stat_ptr=network_stats+interfaces;
# Line 283 | Line 258 | network_stat_t *get_network_stats(int *entries){
258                  if((regexec(&regex, line, 9, line_match, 0))!=0){
259                          continue;
260                  }
261 <                network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
262 <                if(network_stats==NULL){
263 <                        return NULL;
261 >
262 >                if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
263 >                        return NULL;
264                  }
265                  network_stat_ptr=network_stats+interfaces;
266  
# Line 320 | Line 295 | network_stat_t *get_network_stats(int *entries){
295   }
296  
297   long long transfer_diff(long long new, long long old){
298 < #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD)
299 < #define MAXVAL 0xffffffffLL
298 > #if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD)
299 >        /* 32-bit quantities, so we must explicitly deal with wraparound. */
300 > #define MAXVAL 0x100000000LL
301 >        if (new >= old) {
302 >                return new - old;
303 >        } else {
304 >                return MAXVAL + new - old;
305 >        }
306   #else
307 < #define MAXVAL 0xffffffffffffffffLL
307 >        /* 64-bit quantities, so plain subtraction works. */
308 >        return new - old;
309   #endif
328        long long result;
329        if(new>=old){
330                result = (new-old);
331        }else{
332                result = (MAXVAL+(new-old));
333        }
334
335        return result;
336
310   }
311  
312   network_stat_t *get_network_stats_diff(int *entries) {
313 <        static network_stat_t *diff = NULL;
314 <        static int diff_count = 0;
315 <        network_stat_t *src, *dest;
316 <        int i, j, new_count;
313 >        VECTOR_DECLARE_STATIC(diff, network_stat_t, 1,
314 >                              network_stat_init, network_stat_destroy);
315 >        network_stat_t *src = NULL, *dest;
316 >        int i, j, diff_count, new_count;
317  
318          if (network_stats == NULL) {
319                  /* No previous stats, so we can't calculate a difference. */
# Line 348 | Line 321 | network_stat_t *get_network_stats_diff(int *entries) {
321          }
322  
323          /* Resize the results array to match the previous stats. */
324 <        diff = network_stat_malloc(interfaces, &diff_count, diff);
325 <        if (diff == NULL) {
324 >        diff_count = VECTOR_SIZE(network_stats);
325 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
326                  return NULL;
327          }
328  
# Line 410 | Line 383 | network_stat_t *get_network_stats_diff(int *entries) {
383          *entries = diff_count;
384          return diff;
385   }
386 +
387   /* NETWORK INTERFACE STATS */
388  
389 < void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){
390 <
391 <        for(net_stats+=start; start<end; start++){
392 <                net_stats->interface_name=NULL;
419 <                net_stats->speed=0;
420 <                net_stats->dup=UNKNOWN_DUPLEX;
421 <                net_stats++;
422 <        }
389 > static void network_iface_stat_init(network_iface_stat_t *s) {
390 >        s->interface_name = NULL;
391 >        s->speed = 0;
392 >        s->dup = UNKNOWN_DUPLEX;
393   }
394  
395 < network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){
396 <
427 <        if(net_stats==NULL){
428 <
429 <                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
430 <                        return NULL;
431 <                }
432 <                network_iface_stat_init(0, needed_entries, net_stats);
433 <                *cur_entries=needed_entries;
434 <
435 <                return net_stats;
436 <        }
437 <
438 <
439 <        if(*cur_entries<needed_entries){
440 <                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
441 <                if(net_stats==NULL){
442 <                        return NULL;
443 <                }
444 <                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
445 <                *cur_entries=needed_entries;
446 <        }
447 <
448 <        return net_stats;
395 > static void network_iface_stat_destroy(network_iface_stat_t *s) {
396 >        free(s->interface_name);
397   }
398  
399   network_iface_stat_t *get_network_iface_stats(int *entries){
400 <        static network_iface_stat_t *network_iface_stats;
400 >        VECTOR_DECLARE_STATIC(network_iface_stats, network_iface_stat_t, 5,
401 >                              network_iface_stat_init, network_iface_stat_destroy);
402          network_iface_stat_t *network_iface_stat_ptr;
454        static int sizeof_network_iface_stats=0;        
403          int ifaces = 0;
404  
405   #ifdef SOLARIS
# Line 483 | Line 431 | network_iface_stat_t *get_network_iface_stats(int *ent
431  
432          for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){
433                  if(net_ptr->ifa_addr->sa_family != AF_LINK) continue;
434 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
435 <                if(network_iface_stats==NULL){
434 >
435 >                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
436                          return NULL;
437                  }
438                  network_iface_stat_ptr = network_iface_stats + ifaces;
# Line 596 | Line 544 | network_iface_stat_t *get_network_iface_stats(int *ent
544                                  continue;
545                          }
546  
547 <                        network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats);
600 <                        if (network_iface_stats == NULL) {
547 >                        if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
548                                  return NULL;
549                          }
550                          network_iface_stat_ptr = network_iface_stats + ifaces;
# Line 674 | Line 621 | network_iface_stat_t *get_network_iface_stats(int *ent
621                  }
622  
623                  /* We have a good interface to add */
624 <                network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats);
678 <                if(network_iface_stats==NULL){
624 >                if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) {
625                          return NULL;
626                  }
627                  network_iface_stat_ptr = network_iface_stats + ifaces;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines