From 3facf4f34b76346f7d9cc4d788b1f8242052af5a Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Thu, 14 Apr 2016 20:00:30 +0200 Subject: [PATCH] Don't time out processes in a cumulative view (fixes #39) --- src/cui.cpp | 98 ++++++++++++++++--------------------------------- src/process.cpp | 26 +++++++++++++ src/process.h | 2 + 3 files changed, 59 insertions(+), 67 deletions(-) diff --git a/src/cui.cpp b/src/cui.cpp index 7c9c693..ecda531 100644 --- a/src/cui.cpp +++ b/src/cui.cpp @@ -348,85 +348,49 @@ void do_refresh() { refreshconninode(); refreshcount++; - ProcList *curproc = processes; - ProcList *previousproc = NULL; - int nproc = processes->size(); - /* initialise to null pointers */ - Line *lines[nproc]; - int n = 0; + if (viewMode == VIEWMODE_KBPS) { + remove_timed_out_processes(); + } -#ifndef NDEBUG - // initialise to null pointers + ProcList *curproc = processes; + int nproc = processes->size(); + + /* initialize to null pointers */ + Line *lines[nproc]; for (int i = 0; i < nproc; i++) lines[i] = NULL; -#endif + + int n = 0; while (curproc != NULL) { // walk though its connections, summing up their data, and // throwing away connections that haven't received a package - // in the last PROCESSTIMEOUT seconds. - assert(curproc != NULL); + // in the last CONNTIMEOUT seconds. assert(curproc->getVal() != NULL); assert(nproc == processes->size()); - /* remove timed-out processes (unless it's one of the the unknown process) - */ - if ((curproc->getVal()->getLastPacket() + PROCESSTIMEOUT <= - curtime.tv_sec) && - (curproc->getVal() != unknowntcp) && - (curproc->getVal() != unknownudp) && (curproc->getVal() != unknownip)) { - if (DEBUG) - std::cout << "PROC: Deleting process\n"; - ProcList *todelete = curproc; - Process *p_todelete = curproc->getVal(); - if (previousproc) { - previousproc->next = curproc->next; - curproc = curproc->next; - } else { - processes = curproc->getNext(); - curproc = processes; - } - delete todelete; - delete p_todelete; - nproc--; - // continue; + float value_sent = 0, value_recv = 0; + + if (viewMode == VIEWMODE_KBPS) { + curproc->getVal()->getkbps(&value_recv, &value_sent); + } else if (viewMode == VIEWMODE_TOTAL_KB) { + curproc->getVal()->gettotalkb(&value_recv, &value_sent); + } else if (viewMode == VIEWMODE_TOTAL_MB) { + curproc->getVal()->gettotalmb(&value_recv, &value_sent); + } else if (viewMode == VIEWMODE_TOTAL_B) { + curproc->getVal()->gettotalb(&value_recv, &value_sent); } else { - // add a non-timed-out process to the list of stuff to show - float value_sent = 0, value_recv = 0; - - if (viewMode == VIEWMODE_KBPS) { - // std::cout << "kbps viemode" << std::endl; - curproc->getVal()->getkbps(&value_recv, &value_sent); - } else if (viewMode == VIEWMODE_TOTAL_KB) { - // std::cout << "total viemode" << std::endl; - curproc->getVal()->gettotalkb(&value_recv, &value_sent); - } else if (viewMode == VIEWMODE_TOTAL_MB) { - // std::cout << "total viemode" << std::endl; - curproc->getVal()->gettotalmb(&value_recv, &value_sent); - } else if (viewMode == VIEWMODE_TOTAL_B) { - // std::cout << "total viemode" << std::endl; - curproc->getVal()->gettotalb(&value_recv, &value_sent); - } else { - forceExit(false, "Invalid viewMode: %d", viewMode); - } - uid_t uid = curproc->getVal()->getUid(); - assert(curproc->getVal()->pid >= 0); - assert(n < nproc); - - lines[n] = - new Line(curproc->getVal()->name, value_recv, value_sent, - curproc->getVal()->pid, uid, curproc->getVal()->devicename); - previousproc = curproc; - curproc = curproc->next; - n++; -#ifndef NDEBUG - assert(nproc == processes->size()); - if (curproc == NULL) - assert(n - 1 < nproc); - else - assert(n < nproc); -#endif + forceExit(false, "Invalid viewMode: %d", viewMode); } + uid_t uid = curproc->getVal()->getUid(); + assert(curproc->getVal()->pid >= 0); + assert(n < nproc); + + lines[n] = + new Line(curproc->getVal()->name, value_recv, value_sent, + curproc->getVal()->pid, uid, curproc->getVal()->devicename); + curproc = curproc->next; + n++; } /* sort the accumulated lines */ diff --git a/src/process.cpp b/src/process.cpp index a23a396..a31e220 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -346,3 +346,29 @@ void procclean() { // delete conninode; prg_cache_clear(); } + +void remove_timed_out_processes() { + ProcList *previousproc = NULL; + + for (ProcList *curproc = processes; curproc != NULL; curproc = curproc->next) { + if ((curproc->getVal()->getLastPacket() + PROCESSTIMEOUT <= + curtime.tv_sec) && + (curproc->getVal() != unknowntcp) && + (curproc->getVal() != unknownudp) && (curproc->getVal() != unknownip)) { + if (DEBUG) + std::cout << "PROC: Deleting process\n"; + ProcList *todelete = curproc; + Process *p_todelete = curproc->getVal(); + if (previousproc) { + previousproc->next = curproc->next; + curproc = curproc->next; + } else { + processes = curproc->getNext(); + curproc = processes; + } + delete todelete; + delete p_todelete; + } + previousproc = curproc; + } +} diff --git a/src/process.h b/src/process.h index f2e7ecf..ecc30bf 100644 --- a/src/process.h +++ b/src/process.h @@ -129,4 +129,6 @@ void refreshconninode(); void procclean(); +void remove_timed_out_processes(); + #endif