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(a)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;
--
1.7.8.4