Make traffic statistics more readable with configurable scaling.
[tinc] / src / top.c
1 /*
2     top.c -- Show real-time statistics from a running tincd
3     Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include <curses.h>
23
24 #include "control_common.h"
25 #include "list.h"
26 #include "tincctl.h"
27 #include "top.h"
28 #include "xalloc.h"
29
30 typedef struct nodestats_t {
31         char *name;
32         uint64_t in_packets;
33         uint64_t in_bytes;
34         uint64_t out_packets;
35         uint64_t out_bytes;
36         float in_packets_rate;
37         float in_bytes_rate;
38         float out_packets_rate;
39         float out_bytes_rate;
40         bool known;
41 } nodestats_t;
42
43 static const char *const sortname[] = {
44         "name",
45         "in pkts",
46         "in bytes",
47         "out pkts",
48         "out bytes",
49         "tot pkts",
50         "tot bytes",
51 };
52
53 static int sortmode = 0;
54 static bool cumulative = false;
55
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;
63
64 static void update(int fd) {
65         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
66         gettimeofday(&now, NULL);
67
68         timersub(&now, &prev, &diff);
69         prev = now;
70         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
71
72         char line[4096];
73         char name[4096];
74         int code;
75         int req;
76         uint64_t in_packets;
77         uint64_t in_bytes;
78         uint64_t out_packets;
79         uint64_t out_bytes;
80
81         for(list_node_t *i = node_list.head; i; i = i->next) {
82                 nodestats_t *node = i->data;
83                 node->known = false;
84         }
85
86         while(recvline(fd, line, sizeof line)) {
87                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
88
89                 if(n == 2)
90                         break;
91
92                 if(n != 7) {
93                         endwin();
94                         fprintf(stderr, "Error receiving traffic information\n");
95                         exit(1);
96                 }
97
98                 nodestats_t *found = NULL;
99
100                 for(list_node_t *i = node_list.head; i; i = i->next) {
101                         nodestats_t *node = i->data;
102                         int result = strcmp(name, node->name);
103                         if(result > 0) {
104                                 continue;
105                         } if(result == 0) {
106                                 found = node;
107                                 break;
108                         } else {
109                                 found = xmalloc_and_zero(sizeof *found);
110                                 found->name = xstrdup(name);
111                                 list_insert_after(&node_list, i, found);
112                                 break;
113                         }
114                 }
115
116                 if(!found) {
117                         found = xmalloc_and_zero(sizeof *found);
118                         found->name = xstrdup(name);
119                         list_insert_tail(&node_list, found);
120                 }
121
122                 found->known = true;
123                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
124                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
125                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
126                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
127                 found->in_packets = in_packets;
128                 found->in_bytes = in_bytes;
129                 found->out_packets = out_packets;
130                 found->out_bytes = out_bytes;
131         }
132 }
133
134 static void redraw(void) {
135         erase();
136
137         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-8s  %s", netname, node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
138         attrset(A_REVERSE);
139         mvprintw(2, 0, "Node                IN pkts   IN %s   OUT pkts  OUT %s", unit, unit);
140         chgat(-1, A_REVERSE, 0, NULL);
141
142         static nodestats_t **sorted = 0;
143         static int n = 0;
144         if(changed) {
145                 n = 0;
146                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
147                 for(list_node_t *i = node_list.head; i; i = i->next)
148                         sorted[n++] = i->data;
149                 changed = false;
150         }
151         
152         int cmpfloat(float a, float b) {
153                 if(a < b)
154                         return -1;
155                 else if(a > b)
156                         return 1;
157                 else
158                         return 0;
159         }
160
161         int cmpu64(uint64_t a, uint64_t b) {
162                 if(a < b)
163                         return -1;
164                 else if(a > b)
165                         return 1;
166                 else
167                         return 0;
168         }
169
170         int sortfunc(const void *a, const void *b) {
171                 const nodestats_t *na = *(const nodestats_t **)a;
172                 const nodestats_t *nb = *(const nodestats_t **)b;
173                 switch(sortmode) {
174                         case 1:
175                                 if(cumulative)
176                                         return -cmpu64(na->in_packets, nb->in_packets);
177                                 else
178                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
179                         case 2:
180                                 if(cumulative)
181                                         return -cmpu64(na->in_bytes, nb->in_bytes);
182                                 else
183                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
184                         case 3:
185                                 if(cumulative)
186                                         return -cmpu64(na->out_packets, nb->out_packets);
187                                 else
188                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
189                         case 4:
190                                 if(cumulative)
191                                         return -cmpu64(na->out_bytes, nb->out_bytes);
192                                 else
193                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
194                         case 5:
195                                 if(cumulative)
196                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
197                                 else
198                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
199                         case 6:
200                                 if(cumulative)
201                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
202                                 else
203                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
204                         default:
205                                 return strcmp(na->name, nb->name);
206                 }
207         }
208
209         qsort(sorted, n, sizeof *sorted, sortfunc);
210
211         for(int i = 0, row = 3; i < n; i++, row++) {
212                 nodestats_t *node = sorted[i];
213                 if(node->known)
214                         if(node->in_packets_rate || node->out_packets_rate)
215                                 attrset(A_BOLD);
216                         else
217                                 attrset(A_NORMAL);
218                 else
219                         attrset(A_DIM);
220
221                 if(cumulative)
222                         mvprintw(row, 0, "%-16s %'10"PRIu64" %'10.0f %'10"PRIu64" %'10.0f",
223                                         node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
224                 else
225                         mvprintw(row, 0, "%-16s %'10.0f %'10.0f %'10.0f %'10.0f",
226                                         node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
227         }
228
229         attrset(A_NORMAL);
230         move(1, 0);
231
232         refresh();
233 }
234
235 void top(int fd) {
236         initscr();
237         timeout(delay);
238
239         while(running) {
240                 update(fd);
241                 redraw();
242
243                 switch(getch()) {
244                         case 's': {
245                                 timeout(-1);
246                                 float input = delay * 1e-3;
247                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
248                                 scanw("%f", &input);
249                                 if(input < 0.1)
250                                         input = 0.1;
251                                 delay = input * 1e3;
252                                 timeout(delay);
253                                 break;
254                         }
255                         case 'c':
256                                   cumulative = !cumulative;
257                                   break;
258                         case 'n':
259                                   sortmode = 0;
260                                   break;
261                         case 'i':
262                                   sortmode = 2;
263                                   break;
264                         case 'I':
265                                   sortmode = 1;
266                                   break;
267                         case 'o':
268                                   sortmode = 4;
269                                   break;
270                         case 'O':
271                                   sortmode = 3;
272                                   break;
273                         case 't':
274                                   sortmode = 6;
275                                   break;
276                         case 'T':
277                                   sortmode = 5;
278                                   break;
279                         case 'b':
280                                   unit = "bytes";
281                                   scale = 1;
282                                   break;
283                         case 'k':
284                                   unit = "kbyte";
285                                   scale = 1e-3;
286                                   break;
287                         case 'M':
288                                   unit = "Mbyte";
289                                   scale = 1e-6;
290                                   break;
291                         case 'G':
292                                   unit = "Gbyte";
293                                   scale = 1e-9;
294                                   break;
295                         case 'q':
296                         case 27:
297                         case KEY_BREAK:
298                                 running = false;
299                                 break;
300                         default:
301                                 break;
302                 }
303         }
304
305         endwin();
306 }