The test if we need to update the display is the same in 5 files;
make this a function: code is more readable, code is smaller,
code is (marginally) faster (I eliminated 64-bit multiply).
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/deskman.c | 17 +++++++++++++++++
src/deskman.h | 1 +
src/detstats.c | 15 +++++----------
src/hostmon.c | 15 +++++----------
src/ifstats.c | 15 +++++----------
src/iptraf-ng-compat.h | 3 +++
src/itrafmon.c | 15 +++++----------
src/options.h | 2 --
src/packet.c | 2 +-
src/pktsize.c | 15 +++++----------
src/serv.c | 15 +++++----------
11 files changed, 52 insertions(+), 63 deletions(-)
diff --git a/src/deskman.c b/src/deskman.c
index 25940dc..e180ea4 100644
--- a/src/deskman.c
+++ b/src/deskman.c
@@ -14,6 +14,7 @@ deskman.c - desktop management routines
#include "tui/winops.h"
#include "deskman.h"
+#include "options.h"
/* Attribute variables */
@@ -194,6 +195,22 @@ void printlargenum(unsigned long long i, WINDOW * win)
wprintw(win, "%8lluT", i / 1000000000000ULL);
}
+int screen_update_needed(const struct timeval *now, const struct timeval *last)
+{
+ unsigned long msecs = timeval_diff_msec(now, last);
+ if (options.updrate == 0) {
+ if (msecs >= DEFAULT_UPDATE_DELAY)
+ return 1;
+ else
+ return 0;
+ } else {
+ if (msecs >= (options.updrate * 1000UL))
+ return 1;
+ else
+ return 0;
+ }
+}
+
void standardcolors(int color)
{
if ((color) && (has_colors())) {
diff --git a/src/deskman.h b/src/deskman.h
index a745f0c..7536459 100644
--- a/src/deskman.h
+++ b/src/deskman.h
@@ -17,6 +17,7 @@ void scrollkeyhelp(void);
void stdexitkeyhelp(void);
void indicate(char *message);
void printlargenum(unsigned long long i, WINDOW * win);
+int screen_update_needed(const struct timeval *now, const struct timeval *last);
void infobox(char *text, char *prompt);
void standardcolors(int color);
void show_sort_statwin(WINDOW **, PANEL **);
diff --git a/src/detstats.c b/src/detstats.c
index 6fbee28..c1e415f 100644
--- a/src/detstats.c
+++ b/src/detstats.c
@@ -269,13 +269,11 @@ void detstats(char *iface, time_t facilitytime)
struct timeval tv;
struct timeval start_tv;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
time_t starttime;
time_t now;
time_t statbegin;
time_t startlog;
- unsigned long long unow;
struct proto_counter span;
@@ -380,6 +378,7 @@ void detstats(char *iface, time_t facilitytime)
gettimeofday(&tv, NULL);
start_tv = tv;
+ updtime = tv;
starttime = startlog = statbegin = tv.tv_sec;
leaveok(statwin, TRUE);
@@ -405,7 +404,6 @@ void detstats(char *iface, time_t facilitytime)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 1) {
char buf[64];
@@ -482,15 +480,12 @@ void detstats(char *iface, time_t facilitytime)
}
}
- if (((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))
- || ((options.updrate != 0)
- && (now - updtime >= options.updrate))) {
+ if (screen_update_needed(&tv, &updtime)) {
printdetails(&ifcounts, statwin);
update_panels();
doupdate();
- updtime_usec = unow;
- updtime = now;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/hostmon.c b/src/hostmon.c
index 5ba7f3c..21b1797 100644
--- a/src/hostmon.c
+++ b/src/hostmon.c
@@ -760,11 +760,9 @@ void hostmon(time_t facilitytime, char *ifptr)
struct timeval tv;
struct timeval tv_rate;
time_t now = 0;
- unsigned long long unow = 0;
time_t statbegin = 0;
time_t startlog = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
struct eth_desc *list = NULL;
@@ -857,6 +855,7 @@ void hostmon(time_t facilitytime, char *ifptr)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
statbegin = startlog = tv.tv_sec;
PACKET_INIT(pkt);
@@ -864,7 +863,6 @@ void hostmon(time_t facilitytime, char *ifptr)
do {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
unsigned long msecs = timeval_diff_msec(&tv, &tv_rate);
if (msecs >= 1000) {
@@ -881,14 +879,11 @@ void hostmon(time_t facilitytime, char *ifptr)
startlog = now;
}
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/ifstats.c b/src/ifstats.c
index fa64619..8214838 100644
--- a/src/ifstats.c
+++ b/src/ifstats.c
@@ -448,10 +448,8 @@ void ifstats(time_t facilitytime)
time_t statbegin = 0;
time_t now = 0;
struct timeval start_tv;
- unsigned long long unow = 0;
time_t startlog = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
struct promisc_states *promisc_list;
@@ -517,6 +515,7 @@ void ifstats(time_t facilitytime)
exitloop = 0;
gettimeofday(&tv, NULL);
start_tv = tv;
+ updtime = tv;
starttime = startlog = statbegin = tv.tv_sec;
PACKET_INIT(pkt);
@@ -524,7 +523,6 @@ void ifstats(time_t facilitytime)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 1) {
unsigned long msecs;
@@ -545,14 +543,11 @@ void ifstats(time_t facilitytime)
startlog = now;
}
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/iptraf-ng-compat.h b/src/iptraf-ng-compat.h
index 8aa0be0..64b3db0 100644
--- a/src/iptraf-ng-compat.h
+++ b/src/iptraf-ng-compat.h
@@ -61,6 +61,9 @@
#define __unused __attribute__((unused))
#define __printf(x, y) __attribute__((format(printf, (x), (y))))
+/* screen delay (in msecs) if update rate == 0 */
+#define DEFAULT_UPDATE_DELAY 50
+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define alloc_nr(x) (((x)+16)*3/2)
diff --git a/src/itrafmon.c b/src/itrafmon.c
index 55cb52c..597920c 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -560,9 +560,7 @@ void ipmon(time_t facilitytime, char *ifptr)
time_t starttime = 0;
time_t now = 0;
time_t timeint = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
- unsigned long long unow = 0;
+ struct timeval updtime;
time_t closedint = 0;
WINDOW *statwin;
@@ -722,6 +720,7 @@ void ipmon(time_t facilitytime, char *ifptr)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
starttime = timeint = closedint = tv.tv_sec;
PACKET_INIT(pkt);
@@ -731,7 +730,6 @@ void ipmon(time_t facilitytime, char *ifptr)
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
/*
* Print timer at bottom of screen
@@ -759,14 +757,11 @@ void ipmon(time_t facilitytime, char *ifptr)
* Update screen at configured intervals.
*/
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
/*
diff --git a/src/options.h b/src/options.h
index b47c243..2304600 100644
--- a/src/options.h
+++ b/src/options.h
@@ -10,8 +10,6 @@ struct OPTIONS {
time_t closedint;
};
-#define DEFAULT_UPDATE_DELAY 50000 /* usec screen delay if update rate 0 */
-
extern struct OPTIONS options;
void setoptions(void);
diff --git a/src/packet.c b/src/packet.c
index c831848..d881e14 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -141,7 +141,7 @@ int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win)
nfds++;
}
do {
- ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY / 1000);
+ ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY);
} while ((ss == -1) && (errno == EINTR));
PACKET_INIT_STRUCT(pkt);
diff --git a/src/pktsize.c b/src/pktsize.c
index 296b8fa..44bc61f 100644
--- a/src/pktsize.c
+++ b/src/pktsize.c
@@ -156,9 +156,7 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
struct timeval tv;
time_t starttime, startlog, timeint;
time_t now;
- unsigned long long unow;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
int logging = options.logging;
FILE *logfile = NULL;
@@ -240,6 +238,7 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
exitloop = 0;
gettimeofday(&tv, NULL);
+ updtime = tv;
now = starttime = startlog = timeint = tv.tv_sec;
if (first_active_facility() && options.promisc) {
@@ -266,16 +265,12 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
do {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if (now - timeint >= 5) {
printelapsedtime(starttime, now, LINES - 3, 1,
diff --git a/src/serv.c b/src/serv.c
index 1fbf877..fdadf03 100644
--- a/src/serv.c
+++ b/src/serv.c
@@ -768,9 +768,7 @@ void servmon(char *ifname, time_t facilitytime)
struct timeval tv_rate;
time_t starttime, startlog, timeint;
time_t now;
- unsigned long long unow;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
unsigned int tot_br;
@@ -866,6 +864,7 @@ void servmon(char *ifname, time_t facilitytime)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
starttime = startlog = timeint = tv.tv_sec;
wattrset(statwin, IPSTATATTR);
@@ -888,7 +887,6 @@ void servmon(char *ifname, time_t facilitytime)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if (now - timeint >= 5) {
printelapsedtime(starttime, now, LINES - 4, 20,
@@ -916,14 +914,11 @@ void servmon(char *ifname, time_t facilitytime)
tv_rate = tv;
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
--
1.7.8.4