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