diff -dru xfce4-netload-plugin-0.1.3/panel-plugin/net.h xfce4-netload-plugin-0.1.3-fjo/panel-plugin/net.h --- xfce4-netload-plugin-0.1.3/panel-plugin/net.h Wed Aug 13 20:16:19 2003 +++ xfce4-netload-plugin-0.1.3-fjo/panel-plugin/net.h Sun Aug 17 12:16:09 2003 @@ -27,6 +27,7 @@ #define __NET_H #include -void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *tot); +void get_current_netload (const gchar* device, gint64 *in, gint64 *out, + gint64 *tot, guint64 *prev_in, guint64 *prev_out, struct timeval *prev_time); #endif /* ndef __NET_H */ diff -dru xfce4-netload-plugin-0.1.3/panel-plugin/net_linux.c xfce4-netload-plugin-0.1.3-fjo/panel-plugin/net_linux.c --- xfce4-netload-plugin-0.1.3/panel-plugin/net_linux.c Wed Aug 13 20:16:26 2003 +++ xfce4-netload-plugin-0.1.3-fjo/panel-plugin/net_linux.c Sun Aug 17 12:26:07 2003 @@ -48,17 +48,15 @@ * (c) Marko Macek and others, GNU General Public License * * Converted from C++ to C (using glib data types) by Bernhard Walle + * Rewritten to be reentrant by Felix J. Ogris */ -void get_current_netload (const gchar* device, gint64 *in, gint64 *out, gint64 *tot) +void get_current_netload (const gchar* device, gint64 *in, gint64 *out, + gint64 *tot, guint64 *prev_in, guint64 *prev_out, struct timeval *prev_time) { - static guint64 prev_ibytes = 0, cur_ibytes = 0, offset_ibytes = 0; - static guint64 prev_obytes = 0, cur_obytes = 0, offset_obytes = 0; - static struct timeval prev_time; - static gboolean first = TRUE; + guint64 cur_ibytes = 0, cur_obytes = 0; struct timeval curr_time; gdouble delta_t; - gint64 ni, no; gchar buf[BUFSIZ]; gchar *p; @@ -67,20 +65,12 @@ FILE *fp = NULL; - if (in != NULL && out != NULL && tot != NULL) + if (!device || !in || !out || !tot || !prev_in || !prev_out || !prev_time) { - *in = *out = *tot = 0; + return; } - if (first) - { - gettimeofday(&prev_time, NULL); - } - gettimeofday(&curr_time, NULL); - - delta_t = (gdouble) ((curr_time.tv_sec - prev_time.tv_sec) * 1000000L - + (curr_time.tv_usec - prev_time.tv_usec)) / 1000000.0; - + *in = *out = *tot = 0; if ( (fp = fopen("/proc/net/dev", "r")) == NULL) { @@ -116,39 +106,51 @@ } fclose(fp); - cur_ibytes += offset_ibytes; - cur_obytes += offset_obytes; - - if (cur_ibytes < prev_ibytes) - // har, har, overflow. Use the recent prev_ibytes value as offset this time - cur_ibytes = offset_ibytes = prev_ibytes; - - if (cur_obytes < prev_obytes) - // har, har, overflow. Use the recent prev_obytes value as offset this time - cur_obytes = offset_obytes = prev_obytes; - - + if (!prev_time->tv_sec) + { + // first call + gettimeofday(prev_time, NULL); + *prev_in = cur_ibytes; + *prev_out = cur_obytes; + return; + } - ni = (gint64)((cur_ibytes - prev_ibytes) / delta_t); - no = (gint64)((cur_obytes - prev_obytes) / delta_t); + gettimeofday(&curr_time, NULL); + + delta_t = (gdouble) ((curr_time.tv_sec - prev_time->tv_sec) * 1000000L + + (curr_time.tv_usec - prev_time->tv_usec)) / 1000000.0; + + if (cur_ibytes < *prev_in) + { + // har, har, overflow. + *in = (guint64)(0xffffffffffffffff - *prev_in + cur_ibytes); + } + else + { + if (delta_t) + { + *in = (gint64)((cur_ibytes - *prev_in) / delta_t); + } + } - if (in != NULL && out != NULL && tot != NULL) + if (cur_obytes < *prev_out) { - *in = ni; - *out = no; - *tot = *in + *out; + // har, har, overflow. + *out = (guint64)(0xffffffffffffffff - *prev_out + cur_obytes); + } + else + { + if (delta_t) + { + *out = (gint64)((cur_obytes - *prev_out) / delta_t); + } } - prev_time.tv_sec = curr_time.tv_sec; - prev_time.tv_usec = curr_time.tv_usec; + *tot = *in + *out; - prev_ibytes = cur_ibytes; - prev_obytes = cur_obytes; + prev_time->tv_sec = curr_time.tv_sec; + prev_time->tv_usec = curr_time.tv_usec; - if (first) - { - *in = *out = *tot = 0; - first = FALSE; - } + *prev_in = cur_ibytes; + *prev_out = cur_obytes; } - diff -dru xfce4-netload-plugin-0.1.3/panel-plugin/netload.c xfce4-netload-plugin-0.1.3-fjo/panel-plugin/netload.c --- xfce4-netload-plugin-0.1.3/panel-plugin/netload.c Wed Aug 13 20:16:23 2003 +++ xfce4-netload-plugin-0.1.3-fjo/panel-plugin/netload.c Sun Aug 17 12:31:55 2003 @@ -64,6 +64,8 @@ GdkColor color[SUM]; gchar *label_text; gchar *network_device; + guint64 prev_net[SUM+1]; + struct timeval prev_time; } t_monitor_options; @@ -111,8 +113,10 @@ gint i; get_current_netload( global->monitor->options.network_device, - &(net[IN]), &(net[OUT]), &(net[TOT]) ); - + &(net[IN]), &(net[OUT]), &(net[TOT]), + &(global->monitor->options.prev_net[IN]), + &(global->monitor->options.prev_net[OUT]), + &(global->monitor->options.prev_time) ); for (i = 0; i < SUM; i++) { @@ -175,6 +179,7 @@ global->monitor->options.label_text = g_strdup(DEFAULT_TEXT); global->monitor->options.network_device = g_strdup(DEFAULT_DEVICE); global->monitor->options.use_label = TRUE; + global->monitor->options.prev_time.tv_sec = 0; for (i = 0; i < SUM; i++) {