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@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..e0a76cf 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 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..eebcfc3 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 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 efe7d1c..ca353d3 100644 --- a/src/detstats.c +++ b/src/detstats.c @@ -270,13 +270,11 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter)
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;
@@ -381,6 +379,7 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter)
gettimeofday(&tv, NULL); start_tv = tv; + updtime = tv; starttime = startlog = statbegin = tv.tv_sec;
leaveok(statwin, TRUE); @@ -406,7 +405,6 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter) while (!exitloop) { gettimeofday(&tv, NULL); now = tv.tv_sec; - unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 1) { char buf[64]; @@ -483,15 +481,12 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter) } }
- if (((options.updrate == 0) - && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY)) - || ((options.updrate != 0) - && (now - updtime >= options.updrate))) { + if (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 341a4a2..6ad9340 100644 --- a/src/hostmon.c +++ b/src/hostmon.c @@ -761,11 +761,9 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter) 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;
@@ -858,6 +856,7 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter) exitloop = 0; gettimeofday(&tv, NULL); tv_rate = tv; + updtime = tv; statbegin = startlog = tv.tv_sec;
PACKET_INIT(pkt); @@ -865,7 +864,6 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter) 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) { @@ -882,14 +880,11 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter) startlog = now; } } - if (((options.updrate != 0) - && (now - updtime >= options.updrate)) - || ((options.updrate == 0) - && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) { + if (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 35a5e18..a66cab8 100644 --- a/src/ifstats.c +++ b/src/ifstats.c @@ -449,10 +449,8 @@ void ifstats(struct filterstate *ofilter, 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;
@@ -518,6 +516,7 @@ void ifstats(struct filterstate *ofilter, time_t facilitytime) exitloop = 0; gettimeofday(&tv, NULL); start_tv = tv; + updtime = tv; starttime = startlog = statbegin = tv.tv_sec;
PACKET_INIT(pkt); @@ -525,7 +524,6 @@ void ifstats(struct filterstate *ofilter, 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; @@ -546,14 +544,11 @@ void ifstats(struct filterstate *ofilter, time_t facilitytime) startlog = now; } } - if (((options.updrate != 0) - && (now - updtime >= options.updrate)) - || ((options.updrate == 0) - && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) { + if (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 d902407..531315e 100644 --- a/src/itrafmon.c +++ b/src/itrafmon.c @@ -561,9 +561,7 @@ void ipmon(struct filterstate *ofilter, 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; @@ -723,6 +721,7 @@ void ipmon(struct filterstate *ofilter, time_t facilitytime, char *ifptr) exitloop = 0; gettimeofday(&tv, NULL); tv_rate = tv; + updtime = tv; starttime = timeint = closedint = tv.tv_sec;
PACKET_INIT(pkt); @@ -732,7 +731,6 @@ void ipmon(struct filterstate *ofilter, 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 @@ -760,14 +758,11 @@ void ipmon(struct filterstate *ofilter, 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 (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 ef76925..65cf352 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 ff5604d..218ef68 100644 --- a/src/pktsize.c +++ b/src/pktsize.c @@ -158,9 +158,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; @@ -242,6 +240,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) { @@ -268,16 +267,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 (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 1491ae5..4fdc96b 100644 --- a/src/serv.c +++ b/src/serv.c @@ -769,9 +769,7 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter) 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;
@@ -867,6 +865,7 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter) exitloop = 0; gettimeofday(&tv, NULL); tv_rate = tv; + updtime = tv; starttime = startlog = timeint = tv.tv_sec;
wattrset(statwin, IPSTATATTR); @@ -889,7 +888,6 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter) 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, @@ -917,14 +915,11 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter) tv_rate = tv; }
- if (((options.updrate != 0) - && (now - updtime >= options.updrate)) - || ((options.updrate == 0) - && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) { + if (update_needed(&tv, &updtime)) { update_panels(); doupdate(); - updtime = now; - updtime_usec = unow; + + updtime = tv; }
if ((facilitytime != 0)
Vitezslav Samel vitezslav@samel.cz writes:
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@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..e0a76cf 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 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;
- }
+}
I'm not sure if that is right name for that function. update_needed doesn't give me any hit, what will be updated, or will it 'something' update inside itself.
On Tue, Aug 28, 2012 at 02:20:23PM +0200, Nikola Pajkovsky wrote:
Vitezslav Samel vitezslav@samel.cz writes:
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@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..e0a76cf 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 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;
- }
+}
I'm not sure if that is right name for that function. update_needed doesn't give me any hit, what will be updated, or will it 'something' update inside itself.
Any recomendations? screen_update_needed() ?
Vita
Vitezslav Samel vitezslav@samel.cz writes:
On Tue, Aug 28, 2012 at 02:20:23PM +0200, Nikola Pajkovsky wrote:
Vitezslav Samel vitezslav@samel.cz writes:
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@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..e0a76cf 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 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;
- }
+}
I'm not sure if that is right name for that function. update_needed doesn't give me any hit, what will be updated, or will it 'something' update inside itself.
Any recomendations? screen_update_needed() ?
Vita
You can have, need_update_screen(...) which will return 0/1 then
int do_update_screen(...) that really update screen according to need_update_screen.
the biggest pain in the ass is that part
- if (((options.updrate == 0) - && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY)) - || ((options.updrate != 0) - && (now - updtime >= options.updrate))) { + if (update_needed(&tv, &updtime)) { printdetails(&ifcounts, statwin); update_panels(); doupdate(); - updtime_usec = unow; - updtime = now; + + updtime = tv; } which do printdetails. Others simply calls update_panels and doupdate.
On Tue, Aug 28, 2012 at 03:00:16PM +0200, Nikola Pajkovsky wrote:
Vitezslav Samel vitezslav@samel.cz writes:
On Tue, Aug 28, 2012 at 02:20:23PM +0200, Nikola Pajkovsky wrote:
Vitezslav Samel vitezslav@samel.cz writes:
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@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..e0a76cf 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 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;
- }
+}
I'm not sure if that is right name for that function. update_needed doesn't give me any hit, what will be updated, or will it 'something' update inside itself.
Any recomendations? screen_update_needed() ?
Vita
You can have, need_update_screen(...) which will return 0/1 then
int do_update_screen(...) that really update screen according to need_update_screen.
I'll rename it to screen_update_needed() and leave the rest as is, if you are OK with it.
Vita
iptraf-ng@lists.fedorahosted.org