Drop libevent and use our own event handling again.
[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 *unit = "bytes";
64 static float scale = 1;
65
66 #ifndef timersub
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)
68 #endif
69
70 static void update(int fd) {
71         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
72         gettimeofday(&cur, NULL);
73
74         timersub(&cur, &prev, &diff);
75         prev = cur;
76         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
77
78         char line[4096];
79         char name[4096];
80         int code;
81         int req;
82         uint64_t in_packets;
83         uint64_t in_bytes;
84         uint64_t out_packets;
85         uint64_t out_bytes;
86
87         for list_each(nodestats_t, ns, &node_list)
88                 ns->known = false;
89
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);
92
93                 if(n == 2)
94                         break;
95
96                 if(n != 7) {
97                         endwin();
98                         fprintf(stderr, "Error receiving traffic information\n");
99                         exit(1);
100                 }
101
102                 nodestats_t *found = NULL;
103
104                 for list_each(nodestats_t, ns, &node_list) {
105                         int result = strcmp(name, ns->name);
106                         if(result > 0) {
107                                 continue;
108                         } if(result == 0) {
109                                 found = ns;
110                                 break;
111                         } else {
112                                 found = xmalloc_and_zero(sizeof *found);
113                                 found->name = xstrdup(name);
114                                 list_insert_before(&node_list, node, found);
115                                 changed = true;
116                                 break;
117                         }
118                 }
119
120                 if(!found) {
121                         found = xmalloc_and_zero(sizeof *found);
122                         found->name = xstrdup(name);
123                         list_insert_tail(&node_list, found);
124                         changed = true;
125                 }
126
127                 found->known = true;
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;
136         }
137 }
138
139 static void redraw(void) {
140         erase();
141
142         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
143         attrset(A_REVERSE);
144         mvprintw(2, 0, "Node                IN pkts   IN %s   OUT pkts  OUT %s", unit, unit);
145         chgat(-1, A_REVERSE, 0, NULL);
146
147         static nodestats_t **sorted = 0;
148         static int n = 0;
149         if(changed) {
150                 n = 0;
151                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
152                 for list_each(nodestats_t, ns, &node_list)
153                         sorted[n++] = ns;
154                 changed = false;
155         }
156
157         for(int i = 0; i < n; i++)
158                 sorted[i]->i = i;
159
160         int cmpfloat(float a, float b) {
161                 if(a < b)
162                         return -1;
163                 else if(a > b)
164                         return 1;
165                 else
166                         return 0;
167         }
168
169         int cmpu64(uint64_t a, uint64_t b) {
170                 if(a < b)
171                         return -1;
172                 else if(a > b)
173                         return 1;
174                 else
175                         return 0;
176         }
177
178         int sortfunc(const void *a, const void *b) {
179                 const nodestats_t *na = *(const nodestats_t **)a;
180                 const nodestats_t *nb = *(const nodestats_t **)b;
181                 switch(sortmode) {
182                         case 1:
183                                 if(cumulative)
184                                         return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
185                                 else
186                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
187                         case 2:
188                                 if(cumulative)
189                                         return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
190                                 else
191                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
192                         case 3:
193                                 if(cumulative)
194                                         return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
195                                 else
196                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
197                         case 4:
198                                 if(cumulative)
199                                         return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
200                                 else
201                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
202                         case 5:
203                                 if(cumulative)
204                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
205                                 else
206                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
207                         case 6:
208                                 if(cumulative)
209                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
210                                 else
211                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
212                         default:
213                                 return strcmp(na->name, nb->name) ?: na->i - nb->i;
214                 }
215         }
216
217         qsort(sorted, n, sizeof *sorted, sortfunc);
218
219         for(int i = 0, row = 3; i < n; i++, row++) {
220                 nodestats_t *node = sorted[i];
221                 if(node->known)
222                         if(node->in_packets_rate || node->out_packets_rate)
223                                 attrset(A_BOLD);
224                         else
225                                 attrset(A_NORMAL);
226                 else
227                         attrset(A_DIM);
228
229                 if(cumulative)
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);
232                 else
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);
235         }
236
237         attrset(A_NORMAL);
238         move(1, 0);
239
240         refresh();
241 }
242
243 void top(int fd) {
244         initscr();
245         timeout(delay);
246         bool running = true;
247
248         while(running) {
249                 update(fd);
250                 redraw();
251
252                 switch(getch()) {
253                         case 's': {
254                                 timeout(-1);
255                                 float input = delay * 1e-3;
256                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
257                                 scanw("%f", &input);
258                                 if(input < 0.1)
259                                         input = 0.1;
260                                 delay = input * 1e3;
261                                 timeout(delay);
262                                 break;
263                         }
264                         case 'c':
265                                   cumulative = !cumulative;
266                                   break;
267                         case 'n':
268                                   sortmode = 0;
269                                   break;
270                         case 'i':
271                                   sortmode = 2;
272                                   break;
273                         case 'I':
274                                   sortmode = 1;
275                                   break;
276                         case 'o':
277                                   sortmode = 4;
278                                   break;
279                         case 'O':
280                                   sortmode = 3;
281                                   break;
282                         case 't':
283                                   sortmode = 6;
284                                   break;
285                         case 'T':
286                                   sortmode = 5;
287                                   break;
288                         case 'b':
289                                   unit = "bytes";
290                                   scale = 1;
291                                   break;
292                         case 'k':
293                                   unit = "kbyte";
294                                   scale = 1e-3;
295                                   break;
296                         case 'M':
297                                   unit = "Mbyte";
298                                   scale = 1e-6;
299                                   break;
300                         case 'G':
301                                   unit = "Gbyte";
302                                   scale = 1e-9;
303                                   break;
304                         case 'q':
305                         case KEY_BREAK:
306                                 running = false;
307                                 break;
308                         default:
309                                 break;
310                 }
311         }
312
313         endwin();
314 }
315
316 #endif