X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Ftop.c;h=d90e5123497a4c922f827cce4e127364b66277c2;hb=d5f0ff5df86d06825110527ddc252b1268e31479;hp=b050435da2813b76b3f598d8a5b49f727aec69c0;hpb=d917c8cb6b69475d568ccbe82389b9f2b3eb5e80;p=tinc diff --git a/src/top.c b/src/top.c index b050435d..d90e5123 100644 --- a/src/top.c +++ b/src/top.c @@ -1,6 +1,6 @@ /* top.c -- Show real-time statistics from a running tincd - Copyright (C) 2011 Guus Sliepen + Copyright (C) 2011-2012 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -57,7 +57,7 @@ static int sortmode = 0; static bool cumulative = false; static list_t node_list; -static struct timeval now, prev, diff; +static struct timeval cur, prev, diff; static int delay = 1000; static bool changed = true; static const char *unit = "bytes"; @@ -69,10 +69,10 @@ static float scale = 1; static void update(int fd) { sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC); - gettimeofday(&now, NULL); + gettimeofday(&cur, NULL); - timersub(&now, &prev, &diff); - prev = now; + timersub(&cur, &prev, &diff); + prev = cur; float interval = diff.tv_sec + diff.tv_usec * 1e-6; char line[4096]; @@ -136,6 +136,63 @@ static void update(int fd) { } } +static int cmpfloat(float a, float b) { + if(a < b) + return -1; + else if(a > b) + return 1; + else + return 0; +} + +static int cmpu64(uint64_t a, uint64_t b) { + if(a < b) + return -1; + else if(a > b) + return 1; + else + return 0; +} + +static int sortfunc(const void *a, const void *b) { + const nodestats_t *na = *(const nodestats_t **)a; + const nodestats_t *nb = *(const nodestats_t **)b; + switch(sortmode) { + case 1: + if(cumulative) + return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i; + else + return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i; + case 2: + if(cumulative) + return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i; + else + return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i; + case 3: + if(cumulative) + return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i; + else + return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i; + case 4: + if(cumulative) + return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i; + else + return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i; + case 5: + if(cumulative) + return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i; + else + return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i; + case 6: + if(cumulative) + return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i; + else + return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i; + default: + return strcmp(na->name, nb->name) ?: na->i - nb->i; + } +} + static void redraw(void) { erase(); @@ -157,63 +214,6 @@ static void redraw(void) { for(int i = 0; i < n; i++) sorted[i]->i = i; - int cmpfloat(float a, float b) { - if(a < b) - return -1; - else if(a > b) - return 1; - else - return 0; - } - - int cmpu64(uint64_t a, uint64_t b) { - if(a < b) - return -1; - else if(a > b) - return 1; - else - return 0; - } - - int sortfunc(const void *a, const void *b) { - const nodestats_t *na = *(const nodestats_t **)a; - const nodestats_t *nb = *(const nodestats_t **)b; - switch(sortmode) { - case 1: - if(cumulative) - return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i; - else - return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i; - case 2: - if(cumulative) - return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i; - else - return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i; - case 3: - if(cumulative) - return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i; - else - return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i; - case 4: - if(cumulative) - return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i; - else - return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i; - case 5: - if(cumulative) - return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i; - else - return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i; - case 6: - if(cumulative) - return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i; - else - return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i; - default: - return strcmp(na->name, nb->name) ?: na->i - nb->i; - } - } - qsort(sorted, n, sizeof *sorted, sortfunc); for(int i = 0, row = 3; i < n; i++, row++) {