254 |
|
|
255 |
|
} |
256 |
|
|
257 |
< |
network_stat_t *get_network_stats_diff(int *entries){ |
258 |
< |
static network_stat_t *network_stats_diff=NULL; |
259 |
< |
static int sizeof_net_stats_diff=0; |
260 |
< |
network_stat_t *network_stats_ptr, *network_stats_diff_ptr; |
261 |
< |
int ifaces, x, y; |
257 |
> |
network_stat_t *get_network_stats_diff(int *entries) { |
258 |
> |
static network_stat_t *diff = NULL; |
259 |
> |
static int diff_count = 0; |
260 |
> |
network_stat_t *src, *dest; |
261 |
> |
int i, j, new_count; |
262 |
|
|
263 |
< |
if(network_stats==NULL){ |
264 |
< |
network_stats_ptr=get_network_stats(&ifaces); |
265 |
< |
*entries=ifaces; |
266 |
< |
return network_stats_ptr; |
263 |
> |
if (network_stats == NULL) { |
264 |
> |
/* No previous stats, so we can't calculate a difference. */ |
265 |
> |
return get_network_stats(entries); |
266 |
|
} |
267 |
|
|
268 |
< |
network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff); |
269 |
< |
if(network_stats_diff==NULL){ |
268 |
> |
/* Resize the results array to match the previous stats. */ |
269 |
> |
diff = network_stat_malloc(interfaces, &diff_count, diff); |
270 |
> |
if (diff == NULL) { |
271 |
|
return NULL; |
272 |
|
} |
273 |
|
|
274 |
< |
network_stats_ptr=network_stats; |
275 |
< |
network_stats_diff_ptr=network_stats_diff; |
274 |
> |
/* Copy the previous stats into the result. */ |
275 |
> |
for (i = 0; i < diff_count; i++) { |
276 |
> |
src = &network_stats[i]; |
277 |
> |
dest = &diff[i]; |
278 |
|
|
279 |
< |
for(ifaces=0;ifaces<interfaces;ifaces++){ |
280 |
< |
if(network_stats_diff_ptr->interface_name!=NULL){ |
279 |
< |
free(network_stats_diff_ptr->interface_name); |
279 |
> |
if (dest->interface_name != NULL) { |
280 |
> |
free(dest->interface_name); |
281 |
|
} |
282 |
< |
network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name); |
283 |
< |
network_stats_diff_ptr->tx=network_stats_ptr->tx; |
284 |
< |
network_stats_diff_ptr->rx=network_stats_ptr->rx; |
285 |
< |
network_stats_diff_ptr->systime=network_stats->systime; |
285 |
< |
|
286 |
< |
network_stats_ptr++; |
287 |
< |
network_stats_diff_ptr++; |
282 |
> |
dest->interface_name = strdup(src->interface_name); |
283 |
> |
dest->rx = src->rx; |
284 |
> |
dest->tx = src->tx; |
285 |
> |
dest->systime = src->systime; |
286 |
|
} |
287 |
< |
network_stats_ptr=get_network_stats(&ifaces); |
288 |
< |
if (network_stats_ptr == NULL) { |
287 |
> |
|
288 |
> |
/* Get a new set of stats. */ |
289 |
> |
if (get_network_stats(&new_count) == NULL) { |
290 |
|
return NULL; |
291 |
|
} |
293 |
– |
network_stats_diff_ptr=network_stats_diff; |
292 |
|
|
293 |
< |
for(x=0;x<sizeof_net_stats_diff;x++){ |
293 |
> |
/* For each previous stat... */ |
294 |
> |
for (i = 0; i < diff_count; i++) { |
295 |
> |
dest = &diff[i]; |
296 |
|
|
297 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
298 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
299 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
300 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
301 |
< |
}else{ |
302 |
< |
|
303 |
< |
network_stats_ptr=network_stats; |
304 |
< |
for(y=0;y<ifaces;y++){ |
305 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
306 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
307 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
308 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
309 |
< |
break; |
310 |
< |
} |
311 |
< |
|
312 |
< |
network_stats_ptr++; |
313 |
< |
} |
297 |
> |
/* ... find the corresponding new stat ... */ |
298 |
> |
for (j = 0; j < new_count; j++) { |
299 |
> |
/* Try the new stat in the same position first, |
300 |
> |
since that's most likely to be it. */ |
301 |
> |
src = &network_stats[(i + j) % new_count]; |
302 |
> |
if (strcmp(src->interface_name, dest->interface_name) == 0) { |
303 |
> |
break; |
304 |
> |
} |
305 |
|
} |
306 |
+ |
if (j == new_count) { |
307 |
+ |
/* No match found. */ |
308 |
+ |
continue; |
309 |
+ |
} |
310 |
|
|
311 |
< |
network_stats_ptr++; |
312 |
< |
network_stats_diff_ptr++; |
311 |
> |
/* ... and subtract the previous stat from it to get the |
312 |
> |
difference. */ |
313 |
> |
dest->rx = transfer_diff(src->rx, dest->rx); |
314 |
> |
dest->tx = transfer_diff(src->tx, dest->tx); |
315 |
> |
dest->systime = src->systime - dest->systime; |
316 |
|
} |
317 |
|
|
318 |
< |
*entries=sizeof_net_stats_diff; |
319 |
< |
return network_stats_diff; |
320 |
< |
} |
323 |
< |
|
318 |
> |
*entries = diff_count; |
319 |
> |
return diff; |
320 |
> |
} |