2 top.c -- Show real-time statistics from a running tincd
3 Copyright (C) 2011-2012 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.
26 #include "control_common.h"
32 typedef struct nodestats_t {
39 float in_packets_rate;
41 float out_packets_rate;
46 static const char *const sortname[] = {
56 static int sortmode = 0;
57 static bool cumulative = false;
59 static list_t node_list;
60 static struct timeval cur, prev, diff;
61 static int delay = 1000;
62 static bool changed = true;
63 static const char *unit = "bytes";
64 static float scale = 1;
67 #define timersub(a, b, c) do {(c)->tv_sec = (a)->tv_sec - (b)->tv_sec; (c)->tv_usec = (a)->tv_usec = (b)->tv_usec;} while(0)
70 static void update(int fd) {
71 sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
72 gettimeofday(&cur, NULL);
74 timersub(&cur, &prev, &diff);
76 float interval = diff.tv_sec + diff.tv_usec * 1e-6;
87 for list_each(nodestats_t, ns, &node_list)
90 while(recvline(fd, line, sizeof line)) {
91 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
98 fprintf(stderr, "Error receiving traffic information\n");
102 nodestats_t *found = NULL;
104 for list_each(nodestats_t, ns, &node_list) {
105 int result = strcmp(name, ns->name);
112 found = xmalloc_and_zero(sizeof *found);
113 found->name = xstrdup(name);
114 list_insert_before(&node_list, node, found);
121 found = xmalloc_and_zero(sizeof *found);
122 found->name = xstrdup(name);
123 list_insert_tail(&node_list, found);
128 found->in_packets_rate = (in_packets - found->in_packets) / interval;
129 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
130 found->out_packets_rate = (out_packets - found->out_packets) / interval;
131 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
132 found->in_packets = in_packets;
133 found->in_bytes = in_bytes;
134 found->out_packets = out_packets;
135 found->out_bytes = out_bytes;
139 static int cmpfloat(float a, float b) {
148 static int cmpu64(uint64_t a, uint64_t b) {
157 static int sortfunc(const void *a, const void *b) {
158 const nodestats_t *na = *(const nodestats_t **)a;
159 const nodestats_t *nb = *(const nodestats_t **)b;
163 return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
165 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
168 return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
170 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
173 return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
175 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
178 return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
180 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
183 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
185 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
188 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
190 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
192 return strcmp(na->name, nb->name) ?: na->i - nb->i;
196 static void redraw(void) {
199 mvprintw(0, 0, "Tinc %-16s Nodes: %4d Sort: %-10s %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
201 mvprintw(2, 0, "Node IN pkts IN %s OUT pkts OUT %s", unit, unit);
202 chgat(-1, A_REVERSE, 0, NULL);
204 static nodestats_t **sorted = 0;
208 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
209 for list_each(nodestats_t, ns, &node_list)
214 for(int i = 0; i < n; i++)
217 qsort(sorted, n, sizeof *sorted, sortfunc);
219 for(int i = 0, row = 3; i < n; i++, row++) {
220 nodestats_t *node = sorted[i];
222 if(node->in_packets_rate || node->out_packets_rate)
230 mvprintw(row, 0, "%-16s %10"PRIu64" %10.0f %10"PRIu64" %10.0f",
231 node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
233 mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
234 node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
255 float input = delay * 1e-3;
256 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
265 cumulative = !cumulative;