The first two patches are needed for the third patch, the last one kills now unused files.
Vitezslav Samel (4): rename rate_init() to rate_alloc() split up rate_alloc() into rate_alloc() and rate_init() ipmon(): use struct rate to print TCP flow rate remove now unused bar.c and bar.h
Makefile | 2 - src/bar.c | 32 ---------------- src/bar.h | 7 ---- src/detstats.c | 12 +++--- src/hostmon.c | 4 +- src/ifstats.c | 4 +- src/itrafmon.c | 108 +++++++++++++++++-------------------------------------- src/rate.c | 16 +++++++-- src/rate.h | 3 +- src/serv.c | 6 ++-- src/tcptable.c | 8 ++++ src/tcptable.h | 3 ++ 12 files changed, 73 insertions(+), 132 deletions(-) delete mode 100644 src/bar.c delete mode 100644 src/bar.h
Change the inappropriately chosen name to the more descriptive name.
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/detstats.c | 12 ++++++------ src/hostmon.c | 4 ++-- src/ifstats.c | 4 ++-- src/rate.c | 2 +- src/rate.h | 2 +- src/serv.c | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/src/detstats.c b/src/detstats.c index 8a8eb8c..90f64d6 100644 --- a/src/detstats.c +++ b/src/detstats.c @@ -376,13 +376,13 @@ void detstats(char *iface, const struct OPTIONS *options, time_t facilitytime, doupdate();
memset(&span, 0, sizeof(span)); - rate_init(&rate, 5); - rate_init(&rate_in, 5); - rate_init(&rate_out, 5); + rate_alloc(&rate, 5); + rate_alloc(&rate_in, 5); + rate_alloc(&rate_out, 5);
- rate_init(&pps_rate, 5); - rate_init(&pps_rate_in, 5); - rate_init(&pps_rate_out, 5); + rate_alloc(&pps_rate, 5); + rate_alloc(&pps_rate_in, 5); + rate_alloc(&pps_rate_out, 5);
gettimeofday(&tv, NULL); start_tv = tv; diff --git a/src/hostmon.c b/src/hostmon.c index b37aaa2..33dacf0 100644 --- a/src/hostmon.c +++ b/src/hostmon.c @@ -284,8 +284,8 @@ static struct ethtabent *addethentry(struct ethtab *table, ptemp->un.figs.inspanbr = ptemp->un.figs.outspanbr = 0; ptemp->un.figs.inippcount = ptemp->un.figs.outippcount = 0; ptemp->un.figs.inbcount = ptemp->un.figs.outbcount = 0; - rate_init(&ptemp->un.figs.inrate, 5); - rate_init(&ptemp->un.figs.outrate, 5); + rate_alloc(&ptemp->un.figs.inrate, 5); + rate_alloc(&ptemp->un.figs.outrate, 5);
table->entcount++;
diff --git a/src/ifstats.c b/src/ifstats.c index 01148ef..fbe7dc4 100644 --- a/src/ifstats.c +++ b/src/ifstats.c @@ -177,7 +177,7 @@ static void initiflist(struct iflist **list) struct iflist *itmp = xmallocz(sizeof(struct iflist)); strcpy(itmp->ifname, ifname); itmp->ifindex = ifindex; - rate_init(&itmp->rate, 5); + rate_alloc(&itmp->rate, 5);
/* make the linked list sorted by ifindex */ struct iflist *cur = *list, *last = NULL; @@ -688,7 +688,7 @@ void selectiface(char *ifname, int withall, int *aborted) ptmp = xmalloc(sizeof(struct iflist)); strncpy(ptmp->ifname, "All interfaces", sizeof(ptmp->ifname)); ptmp->ifindex = 0; - rate_init(&ptmp->rate, 5); /* FIXME: need iflist_entry_init() */ + rate_alloc(&ptmp->rate, 5); /* FIXME: need iflist_entry_init() */
ptmp->prev_entry = NULL; list->prev_entry = ptmp; diff --git a/src/rate.c b/src/rate.c index f996478..c60c70a 100644 --- a/src/rate.c +++ b/src/rate.c @@ -4,7 +4,7 @@ #include "iptraf-ng-compat.h" #include "rate.h"
-void rate_init(struct rate *rate, unsigned int n) +void rate_alloc(struct rate *rate, unsigned int n) { if (!rate) return; diff --git a/src/rate.h b/src/rate.h index 3cd3d8b..87f476d 100644 --- a/src/rate.h +++ b/src/rate.h @@ -23,7 +23,7 @@ struct rate { unsigned long sma; /* simple moving average */ };
-void rate_init(struct rate *rate, unsigned int n); +void rate_alloc(struct rate *rate, unsigned int n); void rate_destroy(struct rate *rate); void rate_add_rate(struct rate *rate, unsigned long bytes, unsigned long msecs); diff --git a/src/serv.c b/src/serv.c index 73dc3b7..04f964e 100644 --- a/src/serv.c +++ b/src/serv.c @@ -211,9 +211,9 @@ static struct portlistent *addtoportlist(struct portlist *list,
ptemp->protocol = protocol; ptemp->port = port; /* This is used in checks later. */ - rate_init(&ptemp->rate, 5); - rate_init(&ptemp->rate_in, 5); - rate_init(&ptemp->rate_out, 5); + rate_alloc(&ptemp->rate, 5); + rate_alloc(&ptemp->rate_in, 5); + rate_alloc(&ptemp->rate_out, 5);
/* * Obtain appropriate service name
We need to have initialization separate, so split up the rate_alloc().
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/rate.c | 16 +++++++++++++--- src/rate.h | 1 + 2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/src/rate.c b/src/rate.c index c60c70a..98c4cf2 100644 --- a/src/rate.c +++ b/src/rate.c @@ -4,15 +4,25 @@ #include "iptraf-ng-compat.h" #include "rate.h"
-void rate_alloc(struct rate *rate, unsigned int n) +void rate_init(struct rate *rate) { if (!rate) return;
- rate->n = n; rate->index = 0; rate->sma = 0; - rate->rates = xmallocz(n * sizeof(*rate->rates)); + memset(rate->rates, 0, rate->n * sizeof(rate->rates[0])); +} + +void rate_alloc(struct rate *rate, unsigned int n) +{ + if (!rate) + return; + + rate->n = n; + rate->rates = xmalloc(n * sizeof(rate->rates[0])); + + rate_init(rate); }
void rate_destroy(struct rate *rate) diff --git a/src/rate.h b/src/rate.h index 87f476d..266dd28 100644 --- a/src/rate.h +++ b/src/rate.h @@ -23,6 +23,7 @@ struct rate { unsigned long sma; /* simple moving average */ };
+void rate_init(struct rate *rate); void rate_alloc(struct rate *rate, unsigned int n); void rate_destroy(struct rate *rate); void rate_add_rate(struct rate *rate, unsigned long bytes,
This slightly changes user interface behavior: formerly the rate was computed only for the entry under the bar; now the rate is computed every second for every TCP entry in the table, so we have the (accurate) rate printed right after the bar change - there is no waittime to compute it. But this brings more CPU cycles burnt.
If this proves to be intollerable, we could rewrite it to compute the TCP flow rate only for the entry under the bar.
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- src/itrafmon.c | 108 +++++++++++++++++-------------------------------------- src/tcptable.c | 8 ++++ src/tcptable.h | 3 ++ 3 files changed, 45 insertions(+), 74 deletions(-)
diff --git a/src/itrafmon.c b/src/itrafmon.c index d00d917..9f23453 100644 --- a/src/itrafmon.c +++ b/src/itrafmon.c @@ -31,7 +31,6 @@ itrafmon.c - the IP traffic monitor module #include "ipfrag.h" #include "instances.h" #include "logvars.h" -#include "bar.h" #include "itrafmon.h"
#define SCROLLUP 0 @@ -524,27 +523,24 @@ static int checkrvnamed(void) return 1; }
-static void update_flowrate(WINDOW * win, struct tcptableent *entry, time_t now, - int *cleared, int mode) +static void update_flowrate(struct tcptable *table, unsigned long msecs) { - float rate = 0; + struct tcptableent *entry; + for (entry = table->head; entry != NULL; entry = entry->next_entry) { + rate_add_rate(&entry->rate, entry->spanbr, msecs); + entry->spanbr = 0; + } +}
+static void print_flowrate(struct tcptableent *entry, WINDOW *win, int mode) +{ wattrset(win, IPSTATLABELATTR); mvwprintw(win, 0, COLS * 47 / 80, "TCP flow rate: "); wattrset(win, IPSTATATTR); - if (mode == KBITS) { - rate = - (float) (entry->spanbr * 8 / 1000) / (float) (now - - entry-> - starttime); - } else { - rate = - (float) (entry->spanbr / 1024) / (float) (now - - entry->starttime); - } - mvwprintw(win, 0, COLS * 53 / 80 + 13, "%8.2f %s", rate, dispmode(mode)); - entry->spanbr = 0; - *cleared = 0; + + char buf[32]; + rate_print(rate_get_average(&entry->rate), mode, buf, sizeof(buf)); + mvwprintw(win, 0, COLS * 53 / 80 + 13, "%s", buf); }
/* @@ -567,6 +563,7 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, unsigned long screen_idx = 1;
struct timeval tv; + struct timeval tv_rate; time_t starttime = 0; time_t now = 0; time_t timeint = 0; @@ -620,7 +617,6 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
int instance_id; int revlook = options->revlook; - int statcleared = 0; int wasempty = 1;
const int statx = COLS * 47 / 80; @@ -735,6 +731,7 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter,
exitloop = 0; gettimeofday(&tv, NULL); + tv_rate = tv; starttime = timeint = closedint = tv.tv_sec;
PACKET_INIT(pkt); @@ -786,16 +783,18 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, * If highlight bar is on some entry, update the flow rate * indicator after five seconds. */ - if (table.barptr != NULL) { - if ((now - table.barptr->starttime) >= 5) { - update_flowrate(statwin, table.barptr, now, - &statcleared, options->actmode); - table.barptr->starttime = now; + unsigned long rate_msecs = timeval_diff_msec(&tv, &tv_rate); + if (rate_msecs > 1000) { + update_flowrate(&table, rate_msecs); + if (table.barptr != NULL) { + print_flowrate(table.barptr, statwin, + options->actmode); + } else { + wattrset(statwin, IPSTATATTR); + mvwprintw(statwin, 0, statx, + "No TCP entries "); } - } else { - wattrset(statwin, IPSTATATTR); - mvwprintw(statwin, 0, statx, - "No TCP entries "); + tv_rate = tv; }
/* @@ -838,12 +837,7 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, break;
tmptcp = table.barptr; - set_barptr((void *) &(table.barptr), - table.barptr->prev_entry, - &(table.barptr->prev_entry->starttime), - &(table.barptr->prev_entry->spanbr), - sizeof(unsigned long), - statwin, &statcleared, statx); + table.barptr = table.barptr->prev_entry;
printentry(&table, tmptcp, screen_idx, mode);
@@ -866,12 +860,7 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, break;
tmptcp = table.barptr; - set_barptr((void*) &(table.barptr), - table.barptr->next_entry, - &(table.barptr->next_entry->starttime), - &(table.barptr->next_entry->spanbr), - sizeof(unsigned long), - statwin, &statcleared, statx); + table.barptr = table.barptr->next_entry; printentry(&table, tmptcp, screen_idx,mode);
if (table.baridx == table.imaxy) @@ -913,14 +902,9 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, break;
pageupperwin(&table, SCROLLDOWN, &screen_idx); - set_barptr((void *) &(table.barptr), - table.lastvisible, - &(table.lastvisible->starttime), - &(table.lastvisible->spanbr), - sizeof(unsigned long), - statwin,&statcleared, statx); - table.baridx = - table.lastvisible->index - screen_idx + 1; + table.barptr = table.lastvisible; + table.baridx = table.lastvisible->index + - screen_idx + 1; refreshtcpwin(&table, screen_idx, mode); break; case KEY_NPAGE: @@ -935,12 +919,7 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, break;
pageupperwin(&table, SCROLLUP, &screen_idx); - set_barptr((void *) &(table.barptr), - table.firstvisible, - &(table.firstvisible->starttime), - &(table.firstvisible->spanbr), - sizeof(unsigned long), - statwin, &statcleared, statx); + table.barptr = table.firstvisible; table.baridx = 1; refreshtcpwin(&table, screen_idx, mode); break; @@ -1001,12 +980,7 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, logfile, options);
if (table.barptr != NULL) { - set_barptr((void *) &(table.barptr), - table.firstvisible, - &(table.firstvisible->starttime), - &(table.firstvisible->spanbr), - sizeof(unsigned long), - statwin, &statcleared, statx); + table.barptr = table.firstvisible; table.baridx = 1; } refreshtcpwin(&table, screen_idx, mode); @@ -1130,23 +1104,9 @@ void ipmon(struct OPTIONS *options, struct filterstate *ofilter, mode);
if (wasempty) { - set_barptr((void *) &(table.barptr), - table.firstvisible, - &(table.firstvisible->starttime), - &(table.firstvisible->spanbr), - sizeof(unsigned long), - statwin, &statcleared, statx); + table.barptr = table.firstvisible; table.baridx = 1; } - - if ((table.barptr == tcpentry) - || (table.barptr == tcpentry->oth_connection)) - set_barptr((void *) &(table.barptr), - table.barptr, - &(table.barptr->starttime), - &(table.barptr->spanbr), - sizeof(unsigned long), statwin, - &statcleared, statx); } } /* diff --git a/src/tcptable.c b/src/tcptable.c index 6012587..9d4462e 100644 --- a/src/tcptable.c +++ b/src/tcptable.c @@ -255,6 +255,9 @@ struct tcptableent *addentry(struct tcptable *table, unsigned long int saddr, new_entry->reused = new_entry->oth_connection->reused = 0; table->count++;
+ rate_alloc(&new_entry->rate, 5); + rate_alloc(&new_entry->oth_connection->rate, 5); + print_tcp_num_entries(table); } else { /* @@ -386,6 +389,9 @@ struct tcptableent *addentry(struct tcptable *table, unsigned long int saddr, new_entry->conn_starttime = new_entry->oth_connection->conn_starttime = time(NULL);
+ rate_init(&new_entry->rate); + rate_init(&new_entry->oth_connection->rate); + /* * Mark flow rate start time and byte counter for flow computation * if the highlight bar is on either flow of the new connection. @@ -1007,6 +1013,7 @@ void destroytcptable(struct tcptable *table) c_next_entry = table->head->next_entry;
while (ctemp != NULL) { + rate_destroy(&ctemp->rate); free(ctemp); ctemp = c_next_entry;
@@ -1057,6 +1064,7 @@ static void destroy_tcp_entry(struct tcptable *table, struct tcptableent *ptmp) else table->tail = ptmp->prev_entry;
+ rate_destroy(&ptmp->rate); free(ptmp);
if (table->head == NULL) { diff --git a/src/tcptable.h b/src/tcptable.h index 7b2a83d..8b3b931 100644 --- a/src/tcptable.h +++ b/src/tcptable.h @@ -7,6 +7,8 @@
***/
+#include "rate.h" + #define max(a, b) (( a > b) ? a : b)
#define FLAG_SYN 1 @@ -49,6 +51,7 @@ struct tcptableent { int inclosed; int half_bracket; unsigned long spanbr; + struct rate rate; time_t lastupdate; time_t starttime; time_t conn_starttime;
Signed-off-by: Vitezslav Samel vitezslav@samel.cz --- Makefile | 2 -- src/bar.c | 32 -------------------------------- src/bar.h | 7 ------- 3 files changed, 0 insertions(+), 41 deletions(-) delete mode 100644 src/bar.c delete mode 100644 src/bar.h
diff --git a/Makefile b/Makefile index f187e50..e6ba342 100644 --- a/Makefile +++ b/Makefile @@ -103,7 +103,6 @@ iptraf-h += src/pktsize.h iptraf-h += src/landesc.h iptraf-h += src/dirs.h iptraf-h += src/getpath.h -iptraf-h += src/bar.h iptraf-h += src/options.h iptraf-h += src/promisc.h iptraf-h += src/parseproto.h @@ -125,7 +124,6 @@ iptraf-o += src/tui/winops.o iptraf-o += src/error.o iptraf-o += src/log.o iptraf-o += src/getpath.o -iptraf-o += src/bar.o iptraf-o += src/parseproto.o iptraf-o += src/fltselect.o iptraf-o += src/ipfilter.o diff --git a/src/bar.c b/src/bar.c deleted file mode 100644 index bc88c4c..0000000 --- a/src/bar.c +++ /dev/null @@ -1,32 +0,0 @@ -/* For terms of usage/redistribution/modification see the LICENSE file */ -/* For authors and contributors see the AUTHORS file */ - -// TODO: full rewrite - -/* - * Set the highlight bar to point to the specified entry. - * This routine also sets the cleared flag (indicates whether the - * flow rate has been displayed). The flow rate computation timer - * and accumulator are also reset. - */ - -#include "iptraf-ng-compat.h" - -#include "tui/winops.h" - -#include "attrs.h" - -void set_barptr(void **barptr, void *entry, time_t * starttime, void *spanbr, - size_t size, WINDOW * win, int *cleared, int x) -{ - *barptr = entry; - *starttime = time(NULL); - memset(spanbr, 0, size); - - if (!(*cleared)) { - wattrset(win, IPSTATATTR); - mvwprintw(win, 0, x, "Computing"); - tx_wcoloreol(win); - *cleared = 1; - } -} diff --git a/src/bar.h b/src/bar.h deleted file mode 100644 index b1be8d8..0000000 --- a/src/bar.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef IPTRAF_NG_BAR_H -#define IPTRAF_NG_BAR_H - -void set_barptr(void **barptr, void *entry, time_t * starttime, void *spanbr, - size_t size, WINDOW * win, int *cleared, int x); - -#endif /* IPTRAF_NG_BAR_H */
iptraf-ng@lists.fedorahosted.org