diff --git a/process.cpp b/process.cpp index 8f55cdf..00ace70 100644 --- a/process.cpp +++ b/process.cpp @@ -43,6 +43,8 @@ extern local_addr * local_addrs; +extern timeval curtime; + /* * connection-inode table. takes information from /proc/net/tcp. * key contains source ip, source port, destination ip, destination @@ -293,3 +295,105 @@ void procclean () //delete conninode; prg_cache_clear(); } + + +/** Get the kb/s values for this process */ +void getkbps (Process * curproc, float * recvd, float * sent) +{ + u_int32_t sum_sent = 0, + sum_recv = 0; + + /* walk though all this process's connections, and sum + * them up */ + ConnList * curconn = curproc->connections; + ConnList * previous = NULL; + while (curconn != NULL) + { + if (curconn->getVal()->getLastPacket() <= curtime.tv_sec - CONNTIMEOUT) + { + /* stalled connection, remove. */ + ConnList * todelete = curconn; + Connection * conn_todelete = curconn->getVal(); + curconn = curconn->getNext(); + if (todelete == curproc->connections) + curproc->connections = curconn; + if (previous != NULL) + previous->setNext(curconn); + delete (todelete); + delete (conn_todelete); + } + else + { + u_int32_t sent = 0, recv = 0; + curconn->getVal()->sumanddel(curtime, &recv, &sent); + sum_sent += sent; + sum_recv += recv; + previous = curconn; + curconn = curconn->getNext(); + } + } + *recvd = tokbps(sum_recv); + *sent = tokbps(sum_sent); +} + +/** get total values for this process */ +void gettotal(Process * curproc, u_int32_t * recvd, u_int32_t * sent) +{ + u_int32_t sum_sent = 0, + sum_recv = 0; + ConnList * curconn = curproc->connections; + while (curconn != NULL) + { + Connection * conn = curconn->getVal(); + sum_sent += conn->sumSent; + sum_recv += conn->sumRecv; + curconn = curconn->getNext(); + } + //std::cout << "Sum sent: " << sum_sent << std::endl; + //std::cout << "Sum recv: " << sum_recv << std::endl; + *recvd = sum_recv; + *sent = sum_sent; +} + +void gettotalmb(Process * curproc, float * recvd, float * sent) +{ + u_int32_t sum_sent = 0, + sum_recv = 0; + gettotal(curproc, &sum_recv, &sum_sent); + *recvd = tomb(sum_recv); + *sent = tomb(sum_sent); +} + +/** get total values for this process */ +void gettotalkb(Process * curproc, float * recvd, float * sent) +{ + u_int32_t sum_sent = 0, + sum_recv = 0; + gettotal(curproc, &sum_recv, &sum_sent); + *recvd = tokb(sum_recv); + *sent = tokb(sum_sent); +} + +void gettotalb(Process * curproc, float * recvd, float * sent) +{ + u_int32_t sum_sent = 0, + sum_recv = 0; + gettotal(curproc, &sum_recv, &sum_sent); + //std::cout << "Total sent: " << sum_sent << std::endl; + *sent = sum_sent; + *recvd = sum_recv; +} + +float tomb (u_int32_t bytes) +{ + return ((double)bytes) / 1024 / 1024; +} +float tokb (u_int32_t bytes) +{ + return ((double)bytes) / 1024; +} + +float tokbps (u_int32_t bytes) +{ + return (((double)bytes) / PERIOD) / 1024; +} diff --git a/process.h b/process.h index 728649b..abd1440 100644 --- a/process.h +++ b/process.h @@ -143,4 +143,13 @@ void refreshconninode (); void procclean (); +void getkbps (Process * curproc, float * recvd, float * sent); +void gettotal(Process * curproc, u_int32_t * recvd, u_int32_t * sent); +void gettotalmb(Process * curproc, float * recvd, float * sent); +void gettotalkb(Process * curproc, float * recvd, float * sent); +void gettotalb(Process * curproc, float * recvd, float * sent); +float tomb (u_int32_t bytes); +float tokb (u_int32_t bytes); +float tokbps (u_int32_t bytes); + #endif diff --git a/refresh.cpp b/refresh.cpp index 05136b9..b718f8a 100644 --- a/refresh.cpp +++ b/refresh.cpp @@ -38,104 +38,3 @@ void alarm_cb (int /*i*/) alarm(refreshdelay); } - -float tomb (u_int32_t bytes) -{ - return ((double)bytes) / 1024 / 1024; -} -float tokb (u_int32_t bytes) -{ - return ((double)bytes) / 1024; -} - -float tokbps (u_int32_t bytes) -{ - return (((double)bytes) / PERIOD) / 1024; -} - -/** Get the kb/s values for this process */ -void getkbps (Process * curproc, float * recvd, float * sent) -{ - u_int32_t sum_sent = 0, - sum_recv = 0; - - /* walk though all this process's connections, and sum - * them up */ - ConnList * curconn = curproc->connections; - ConnList * previous = NULL; - while (curconn != NULL) - { - if (curconn->getVal()->getLastPacket() <= curtime.tv_sec - CONNTIMEOUT) - { - /* stalled connection, remove. */ - ConnList * todelete = curconn; - Connection * conn_todelete = curconn->getVal(); - curconn = curconn->getNext(); - if (todelete == curproc->connections) - curproc->connections = curconn; - if (previous != NULL) - previous->setNext(curconn); - delete (todelete); - delete (conn_todelete); - } - else - { - u_int32_t sent = 0, recv = 0; - curconn->getVal()->sumanddel(curtime, &recv, &sent); - sum_sent += sent; - sum_recv += recv; - previous = curconn; - curconn = curconn->getNext(); - } - } - *recvd = tokbps(sum_recv); - *sent = tokbps(sum_sent); -} - -/** get total values for this process */ -void gettotal(Process * curproc, u_int32_t * recvd, u_int32_t * sent) -{ - u_int32_t sum_sent = 0, - sum_recv = 0; - ConnList * curconn = curproc->connections; - while (curconn != NULL) - { - Connection * conn = curconn->getVal(); - sum_sent += conn->sumSent; - sum_recv += conn->sumRecv; - curconn = curconn->getNext(); - } - //std::cout << "Sum sent: " << sum_sent << std::endl; - //std::cout << "Sum recv: " << sum_recv << std::endl; - *recvd = sum_recv; - *sent = sum_sent; -} - -void gettotalmb(Process * curproc, float * recvd, float * sent) -{ - u_int32_t sum_sent = 0, - sum_recv = 0; - gettotal(curproc, &sum_recv, &sum_sent); - *recvd = tomb(sum_recv); - *sent = tomb(sum_sent); -} - -/** get total values for this process */ -void gettotalkb(Process * curproc, float * recvd, float * sent) -{ - u_int32_t sum_sent = 0, - sum_recv = 0; - gettotal(curproc, &sum_recv, &sum_sent); - *recvd = tokb(sum_recv); - *sent = tokb(sum_sent); -} - -void gettotalb(Process * curproc, float * recvd, float * sent) -{ - u_int32_t sum_sent = 0, - sum_recv = 0; - gettotal(curproc, &sum_recv, &sum_sent); - //std::cout << "Total sent: " << sum_sent << std::endl; - *sent = sum_sent; - *recvd = sum_recv; -} diff --git a/refresh.h b/refresh.h index a1cf6cc..d7c06ae 100644 --- a/refresh.h +++ b/refresh.h @@ -20,12 +20,3 @@ */ void alarm_cb (int i); - -float tomb (u_int32_t bytes); -float tokb (u_int32_t bytes); -float tokbps (u_int32_t bytes); -void getkbps (Process * curproc, float * recvd, float * sent); -void gettotal(Process * curproc, u_int32_t * recvd, u_int32_t * sent); -void gettotalmb(Process * curproc, float * recvd, float * sent); -void gettotalkb(Process * curproc, float * recvd, float * sent); -void gettotalb(Process * curproc, float * recvd, float * sent);