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.2 by pajs, Tue Feb 18 23:23:36 2003 UTC vs.
Revision 1.3 by pajs, Wed Feb 19 17:13:24 2003 UTC

# Line 31 | Line 31
31   #include <string.h>
32   #endif
33  
34 < #define START_VAL 1
34 > static network_stat_t *network_stats=NULL;
35 > static int interfaces=0;
36  
37 < network_stat_t *network_stat_init(int num_iface, int *wmark, network_stat_t *net_stats){
37 <        int x;
38 <        network_stat_t *net_stats_ptr;
37 > void network_stat_init(int start, int end, network_stat_t *net_stats){
38  
39 <        if(*wmark==-1){
40 <                printf("new malloc\n");
41 <                if((net_stats=malloc(START_VAL * sizeof(network_stat_t)))==NULL){
39 >        for(net_stats+=start; start<end; start++){
40 >                net_stats->interface_name=NULL;
41 >                net_stats->tx=0;
42 >                net_stats->rx=0;
43 >                net_stats++;
44 >        }
45 > }
46 >
47 > network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){
48 >
49 >        if(net_stats==NULL){
50 >
51 >                if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){
52                          return NULL;
53                  }
54 <                *wmark=START_VAL;
55 <                net_stats_ptr=net_stats;
56 <                for(x=0;x<(*wmark);x++){
48 <                        net_stats_ptr->interface_name=NULL;
49 <                        net_stats_ptr++;
50 <                }
54 >                network_stat_init(0, needed_entries, net_stats);
55 >                *cur_entries=needed_entries;
56 >
57                  return net_stats;
58          }
59 <        if(num_iface>(*wmark-1)){
60 <                if((net_stats=realloc(net_stats, (*wmark)*2 * sizeof(network_stat_t)))==NULL){
59 >
60 >
61 >        if(*cur_entries<needed_entries){
62 >                net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries));
63 >                if(net_stats==NULL){
64                          return NULL;
65                  }
66 <
67 <                *wmark=(*wmark)*2;
59 <                net_stats_ptr=net_stats+num_iface;
60 <                for(x=num_iface;x<(*wmark);x++){
61 <                        net_stats_ptr->interface_name=NULL;
62 <                        net_stats_ptr++;
63 <                }
64 <                return net_stats;
66 >                network_stat_init(*cur_entries, needed_entries, net_stats);
67 >                *cur_entries=needed_entries;
68          }
69  
70          return net_stats;
71   }
72  
73 +
74   network_stat_t *get_network_stats(int *entries){
75          kstat_ctl_t *kc;
76          kstat_t *ksp;
77          kstat_named_t *knp;
78 +
79 +        static int sizeof_network_stats=0;      
80          
75        int interfaces=0;      
81          network_stat_t *network_stat_ptr;
77        static network_stat_t *network_stats=NULL;
78        static int watermark=-1;
82  
83          if ((kc = kstat_open()) == NULL) {
84                  return NULL;
85          }
86  
87 +        interfaces=0;  
88 +
89          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
90                  if (!strcmp(ksp->ks_class, "net")) {
91                          kstat_read(kc, ksp, NULL);
# Line 89 | Line 94 | network_stat_t *get_network_stats(int *entries){
94                                  /* Not a network interface, so skip to the next entry */
95                                  continue;
96                          }
97 <                        network_stats=network_stat_init(interfaces, &watermark, network_stats);
97 >
98 >                        network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats);
99                          if(network_stats==NULL){
100                                  return NULL;
101                          }
# Line 105 | Line 111 | network_stat_t *get_network_stats(int *entries){
111                                  free(network_stat_ptr->interface_name);
112                          }
113                          network_stat_ptr->interface_name=strdup(ksp->ks_name);
114 +
115 +                        network_stat_ptr->systime=time(NULL);
116                          interfaces++;
117                  }
118          }
# Line 114 | Line 122 | network_stat_t *get_network_stats(int *entries){
122          *entries=interfaces;
123  
124          return network_stats;  
117
125   }
126 +
127 + network_stat_t *get_network_stats_diff(int *entries){
128 +        static network_stat_t *network_stats_diff=NULL;
129 +        static int sizeof_net_stats_diff=0;
130 +        network_stat_t *network_stats_ptr, *network_stats_diff_ptr;
131 +        int ifaces, x, y;
132 +
133 +        if(network_stats==NULL){
134 +                network_stats_ptr=get_network_stats(&ifaces);
135 +                *entries=ifaces;
136 +                return network_stats_ptr;
137 +        }
138 +
139 +        network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff);
140 +        if(network_stats_diff==NULL){
141 +                return NULL;
142 +        }
143 +
144 +        network_stats_ptr=network_stats;
145 +        network_stats_diff_ptr=network_stats_diff;
146 +
147 +        for(ifaces=0;ifaces<interfaces;ifaces++){
148 +                if(network_stats_diff_ptr->interface_name!=NULL){
149 +                        free(network_stats_diff_ptr->interface_name);
150 +                }
151 +                network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name);
152 +                network_stats_diff_ptr->tx=network_stats_ptr->tx;
153 +                network_stats_diff_ptr->rx=network_stats_ptr->rx;
154 +                network_stats_diff_ptr->systime=network_stats->systime;
155 +
156 +                network_stats_ptr++;
157 +                network_stats_diff_ptr++;
158 +        }
159 +        network_stats_ptr=get_network_stats(&ifaces);          
160 +        network_stats_diff_ptr=network_stats_diff;
161 +
162 +        for(x=0;x<sizeof_net_stats_diff;x++){
163 +
164 +                if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
165 +                        network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx;
166 +                        network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx;        
167 +                        network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
168 +                }else{
169 +                        
170 +                        network_stats_ptr=network_stats;
171 +                        for(y=0;y<ifaces;y++){
172 +                                if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){
173 +                                        network_stats_diff_ptr->tx = network_stats_ptr->tx - network_stats_diff_ptr->tx;
174 +                                        network_stats_diff_ptr->rx = network_stats_ptr->rx - network_stats_diff_ptr->rx;        
175 +                                        network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime;
176 +                                        break;
177 +                                }
178 +
179 +                                network_stats_ptr++;
180 +                        }      
181 +                }
182 +
183 +                network_stats_ptr++;
184 +                network_stats_diff_ptr++;
185 +        }
186 +
187 +        return network_stats_diff;
188 + }      
189 +
190 +
191  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines