add reporting of changes since last request

This commit is contained in:
Andreas Gocht-Zech
2023-08-20 22:07:43 +02:00
parent b581af6990
commit 668ae29c0a
5 changed files with 31 additions and 0 deletions

View File

@@ -76,6 +76,8 @@ PYBIND11_MODULE(nethogs, m) {
.def_readwrite("device_name", &NethogsMonitorRecord::device_name)
.def_readwrite("sent_bytes", &NethogsMonitorRecord::sent_bytes)
.def_readwrite("recv_bytes", &NethogsMonitorRecord::recv_bytes)
.def_readwrite("sent_bytes_last", &NethogsMonitorRecord::sent_bytes_last)
.def_readwrite("recv_bytes_last", &NethogsMonitorRecord::sent_bytes_last)
.def_readwrite("sent_kbs", &NethogsMonitorRecord::sent_kbs)
.def_readwrite("recv_kbs", &NethogsMonitorRecord::recv_kbs);

View File

@@ -207,10 +207,14 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) {
const u_int32_t uid = curproc->getVal()->getUid();
u_int64_t sent_bytes;
u_int64_t recv_bytes;
u_int64_t sent_bytes_last;
u_int64_t recv_bytes_last;
float sent_kbs;
float recv_kbs;
curproc->getVal()->getkbps(&recv_kbs, &sent_kbs);
curproc->getVal()->gettotal(&recv_bytes, &sent_bytes);
curproc->getVal()->getlast(&recv_bytes_last, &sent_bytes_last);
// notify update
bool const new_data =
@@ -239,9 +243,12 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) {
NHM_UPDATE_ONE_FIELD(data.uid, uid)
NHM_UPDATE_ONE_FIELD(data.sent_bytes, sent_bytes)
NHM_UPDATE_ONE_FIELD(data.recv_bytes, recv_bytes)
NHM_UPDATE_ONE_FIELD(data.sent_bytes_last, sent_bytes_last)
NHM_UPDATE_ONE_FIELD(data.recv_bytes_last, recv_bytes_last)
NHM_UPDATE_ONE_FIELD(data.sent_kbs, sent_kbs)
NHM_UPDATE_ONE_FIELD(data.recv_kbs, recv_kbs)
#undef NHM_UPDATE_ONE_FIELD
if (data_change) {

View File

@@ -26,6 +26,8 @@ typedef struct NethogsMonitorRecord {
const char *device_name;
uint64_t sent_bytes;
uint64_t recv_bytes;
uint64_t sent_bytes_last;
uint64_t recv_bytes_last;
float sent_kbs;
float recv_kbs;
} NethogsMonitorRecord;

View File

@@ -192,6 +192,20 @@ void Process::gettotalb(float *recvd, float *sent) {
*recvd = sum_recv;
}
/** get only bytes since last request */
void Process::getlast(u_int64_t *recvd, u_int64_t *sent) {
u_int64_t sum_sent = 0, sum_recv = 0;
gettotal(&sum_recv, &sum_sent);
*sent = sum_sent - this->sent_last_reported;
*recvd = sum_recv - this->rcvd_last_reported;
this->sent_last_reported = *sent;
this->rcvd_last_reported = *recvd;
}
Process *findProcess(struct prg_node *node) {
ProcList *current = processes;
while (current != NULL) {

View File

@@ -77,6 +77,8 @@ public:
uid = 0;
sent_by_closed_bytes = 0;
rcvd_by_closed_bytes = 0;
sent_last_reported = 0;
rcvd_last_reported = 0;
}
void check() { assert(pid >= 0); }
@@ -95,6 +97,7 @@ public:
void gettotalmb(float *recvd, float *sent);
void gettotalkb(float *recvd, float *sent);
void gettotalb(float *recvd, float *sent);
void getlast(u_int64_t *recvd, u_int64_t *sent);
char *name;
char *cmdline;
@@ -103,6 +106,9 @@ public:
u_int64_t sent_by_closed_bytes;
u_int64_t rcvd_by_closed_bytes;
u_int64_t sent_last_reported;
u_int64_t rcvd_last_reported;
ConnList connections;
uid_t getUid() { return uid; }