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