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.50 by ats, Sat Mar 6 22:30:54 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 180 | Line 154 | network_stat_t *get_network_stats(int *entries){
154                          kstat_read(kc, ksp, NULL);
155  
156   #ifdef SOL7
157 < #define RLOOKUP "rbytes"
158 < #define WLOOKUP "obytes"
157 > #define LRX "rbytes"
158 > #define LTX "obytes"
159 > #define LIPACKETS "ipackets"
160 > #define LOPACKETS "opackets"
161   #define VALTYPE value.ui32
162   #else
163 < #define RLOOKUP "rbytes64"
164 < #define WLOOKUP "obytes64"
163 > #define LRX "rbytes64"
164 > #define LTX "obytes64"
165 > #define LIPACKETS "ipackets64"
166 > #define LOPACKETS "opackets64"
167   #define VALTYPE value.ui64
168   #endif
169  
170 <                        if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){
170 >                        /* Read rx */
171 >                        if((knp=kstat_data_lookup(ksp, LRX))==NULL){
172                                  /* This is a network interface, but it doesn't
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 <                        network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
182 <                        if(network_stats==NULL){
181 >                        /* Create new network_stats */
182 >                        if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) {
183                                  return NULL;
184                          }
185                          network_stat_ptr=network_stats+interfaces;
186 +
187 +                        /* Finish reading rx */
188                          network_stat_ptr->rx=knp->VALTYPE;
189  
190 <                        if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){
190 >                        /* Read tx */
191 >                        if((knp=kstat_data_lookup(ksp, LTX))==NULL){
192                                  continue;
193                          }
194                          network_stat_ptr->tx=knp->VALTYPE;
195 +
196 +                        /* Read ipackets */
197 +                        if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){
198 +                                continue;
199 +                        }
200 +                        network_stat_ptr->ipackets=knp->VALTYPE;
201 +
202 +                        /* Read opackets */
203 +                        if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){
204 +                                continue;
205 +                        }
206 +                        network_stat_ptr->opackets=knp->VALTYPE;
207 +
208 +                        /* Read ierrors */
209 +                        if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){
210 +                                continue;
211 +                        }
212 +                        network_stat_ptr->ierrors=knp->value.ui32;
213 +
214 +                        /* Read oerrors */
215 +                        if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){
216 +                                continue;
217 +                        }
218 +                        network_stat_ptr->oerrors=knp->value.ui32;
219 +
220 +                        /* Read collisions */
221 +                        if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){
222 +                                continue;
223 +                        }
224 +                        network_stat_ptr->collisions=knp->value.ui32;
225 +
226 +                        /* Read interface name */
227                          if(network_stat_ptr->interface_name!=NULL){
228                                  free(network_stat_ptr->interface_name);
229                          }
230                          network_stat_ptr->interface_name=strdup(ksp->ks_name);
231  
232 +                        /* Store systime */
233                          network_stat_ptr->systime=time(NULL);
234 +
235                          interfaces++;
236                  }
237          }
# Line 237 | Line 255 | network_stat_t *get_network_stats(int *entries){
255          interfaces=0;
256  
257          while((fgets(line, sizeof(line), f)) != NULL){
258 <                if((regexec(&regex, line, 4, line_match, 0))!=0){
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 277 | 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 4294967296LL
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 18446744073709551616LL
307 >        /* 64-bit quantities, so plain subtraction works. */
308 >        return new - old;
309   #endif
285        long long result;
286        if(new>=old){
287                result = (new-old);
288        }else{
289                result = (MAXVAL+(new-old));
290        }
291
292        return result;
293
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 305 | 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 367 | 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;
376 <                net_stats->speed=0;
377 <                net_stats->dup=UNKNOWN_DUPLEX;
378 <                net_stats++;
379 <        }
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 <
384 <        if(net_stats==NULL){
385 <
386 <                if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){
387 <                        return NULL;
388 <                }
389 <                network_iface_stat_init(0, needed_entries, net_stats);
390 <                *cur_entries=needed_entries;
391 <
392 <                return net_stats;
393 <        }
394 <
395 <
396 <        if(*cur_entries<needed_entries){
397 <                net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries));
398 <                if(net_stats==NULL){
399 <                        return NULL;
400 <                }
401 <                network_iface_stat_init(*cur_entries, needed_entries, net_stats);
402 <                *cur_entries=needed_entries;
403 <        }
404 <
405 <        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;
411        static int sizeof_network_iface_stats=0;        
403          int ifaces = 0;
404  
405   #ifdef SOLARIS
# Line 440 | 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 502 | Line 493 | network_iface_stat_t *get_network_iface_stats(int *ent
493                          case(IFM_1000_SX):
494                          case(IFM_1000_LX):
495                          case(IFM_1000_CX):
496 < #ifdef IFM_1000_TX
497 <                        case(IFM_1000_TX): /* FreeBSD 4 and others? */
496 > #if defined(IFM_1000_TX) && !defined(OPENBSD)
497 >                        case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */
498   #endif
499   #ifdef IFM_1000_FX
500                          case(IFM_1000_FX): /* FreeBSD 4 */
# Line 553 | 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);
557 <                        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 631 | 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);
635 <                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