2 top.c -- Show real-time statistics from a running tincd
3 Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #undef KEY_EVENT /* There are conflicting declarations for KEY_EVENT in Windows wincon.h and curses.h. */
27 #include "control_common.h"
34 typedef struct nodestats_t {
41 float in_packets_rate;
43 float out_packets_rate;
48 static const char *const sortname[] = {
58 static int sortmode = 0;
59 static bool cumulative = false;
61 static list_t node_list;
62 static struct timeval cur, prev, diff;
63 static int delay = 1000;
64 static bool changed = true;
65 static const char *bunit = "bytes";
66 static float bscale = 1;
67 static const char *punit = "pkts";
68 static float pscale = 1;
70 static bool update(int fd) {
71 if(!sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC))
74 gettimeofday(&cur, NULL);
76 timersub(&cur, &prev, &diff);
78 float interval = diff.tv_sec + diff.tv_usec * 1e-6;
89 for list_each(nodestats_t, ns, &node_list)
92 while(recvline(fd, line, sizeof line)) {
93 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
101 nodestats_t *found = NULL;
103 for list_each(nodestats_t, ns, &node_list) {
104 int result = strcmp(name, ns->name);
111 found = xzalloc(sizeof *found);
112 found->name = xstrdup(name);
113 list_insert_before(&node_list, node, found);
120 found = xzalloc(sizeof *found);
121 found->name = xstrdup(name);
122 list_insert_tail(&node_list, found);
127 found->in_packets_rate = (in_packets - found->in_packets) / interval;
128 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
129 found->out_packets_rate = (out_packets - found->out_packets) / interval;
130 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
131 found->in_packets = in_packets;
132 found->in_bytes = in_bytes;
133 found->out_packets = out_packets;
134 found->out_bytes = out_bytes;
140 static int cmpfloat(float a, float b) {
149 static int cmpu64(uint64_t a, uint64_t b) {
158 static int sortfunc(const void *a, const void *b) {
159 const nodestats_t *na = *(const nodestats_t **)a;
160 const nodestats_t *nb = *(const nodestats_t **)b;
164 return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
166 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
169 return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
171 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
174 return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
176 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
179 return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
181 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
184 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
186 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
189 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
191 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
193 return strcmp(na->name, nb->name) ?: na->i - nb->i;
197 static void redraw(void) {
200 mvprintw(0, 0, "Tinc %-16s Nodes: %4d Sort: %-10s %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
202 mvprintw(2, 0, "Node IN %s IN %s OUT %s OUT %s", punit, bunit, punit, bunit);
203 chgat(-1, A_REVERSE, 0, NULL);
205 static nodestats_t **sorted = 0;
209 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
210 for list_each(nodestats_t, ns, &node_list)
215 for(int i = 0; i < n; i++)
219 qsort(sorted, n, sizeof *sorted, sortfunc);
221 for(int i = 0, row = 3; i < n; i++, row++) {
222 nodestats_t *node = sorted[i];
224 if(node->in_packets_rate || node->out_packets_rate)
232 mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
233 node->name, node->in_packets * pscale, node->in_bytes * bscale, node->out_packets * pscale, node->out_bytes * bscale);
235 mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
236 node->name, node->in_packets_rate * pscale, node->in_bytes_rate * bscale, node->out_packets_rate * pscale, node->out_bytes_rate * bscale);
259 float input = delay * 1e-3;
260 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
269 cumulative = !cumulative;