2 top.c -- Show real-time statistics from a running tincd
3 Copyright (C) 2011 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 #include "control_common.h"
30 typedef struct nodestats_t {
36 float in_packets_rate;
38 float out_packets_rate;
43 static const char *const sortname[] = {
53 static int sortmode = 0;
54 static bool cumulative = false;
56 static list_t node_list;
57 static struct timeval now, prev, diff;
58 static int delay = 1000;
59 static bool running = true;
60 static bool changed = true;
61 static const char *unit = "bytes";
62 static float scale = 1;
65 #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)
68 static void update(int fd) {
69 sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
70 gettimeofday(&now, NULL);
72 timersub(&now, &prev, &diff);
74 float interval = diff.tv_sec + diff.tv_usec * 1e-6;
85 for(list_node_t *i = node_list.head; i; i = i->next) {
86 nodestats_t *node = i->data;
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_node_t *i = node_list.head; i; i = i->next) {
105 nodestats_t *node = i->data;
106 int result = strcmp(name, node->name);
113 found = xmalloc_and_zero(sizeof *found);
114 found->name = xstrdup(name);
115 list_insert_before(&node_list, i, found);
122 found = xmalloc_and_zero(sizeof *found);
123 found->name = xstrdup(name);
124 list_insert_tail(&node_list, found);
129 found->in_packets_rate = (in_packets - found->in_packets) / interval;
130 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
131 found->out_packets_rate = (out_packets - found->out_packets) / interval;
132 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
133 found->in_packets = in_packets;
134 found->in_bytes = in_bytes;
135 found->out_packets = out_packets;
136 found->out_bytes = out_bytes;
140 static void redraw(void) {
143 mvprintw(0, 0, "Tinc %-16s Nodes: %4d Sort: %-8s %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
145 mvprintw(2, 0, "Node IN pkts IN %s OUT pkts OUT %s", unit, unit);
146 chgat(-1, A_REVERSE, 0, NULL);
148 static nodestats_t **sorted = 0;
152 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
153 for(list_node_t *i = node_list.head; i; i = i->next)
154 sorted[n++] = i->data;
158 int cmpfloat(float a, float b) {
167 int cmpu64(uint64_t a, uint64_t b) {
176 int sortfunc(const void *a, const void *b) {
177 const nodestats_t *na = *(const nodestats_t **)a;
178 const nodestats_t *nb = *(const nodestats_t **)b;
182 return -cmpu64(na->in_packets, nb->in_packets);
184 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
187 return -cmpu64(na->in_bytes, nb->in_bytes);
189 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
192 return -cmpu64(na->out_packets, nb->out_packets);
194 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
197 return -cmpu64(na->out_bytes, nb->out_bytes);
199 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
202 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
204 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
207 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
209 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
211 return strcmp(na->name, nb->name);
215 qsort(sorted, n, sizeof *sorted, sortfunc);
217 for(int i = 0, row = 3; i < n; i++, row++) {
218 nodestats_t *node = sorted[i];
220 if(node->in_packets_rate || node->out_packets_rate)
228 mvprintw(row, 0, "%-16s %10"PRIu64" %10.0f %10"PRIu64" %10.0f",
229 node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
231 mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
232 node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
252 float input = delay * 1e-3;
253 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
262 cumulative = !cumulative;