Fix version_get.h generation on BSD.
[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 #undef KEY_EVENT  /* There are conflicting declarations for KEY_EVENT in Windows wincon.h and curses.h. */
25 #include <curses.h>
26
27 #include "control_common.h"
28 #include "list.h"
29 #include "names.h"
30 #include "tincctl.h"
31 #include "top.h"
32 #include "xalloc.h"
33
34 typedef struct nodestats_t {
35         char *name;
36         int i;
37         uint64_t in_packets;
38         uint64_t in_bytes;
39         uint64_t out_packets;
40         uint64_t out_bytes;
41         float in_packets_rate;
42         float in_bytes_rate;
43         float out_packets_rate;
44         float out_bytes_rate;
45         bool known;
46 } nodestats_t;
47
48 static const char *const sortname[] = {
49         "name",
50         "in pkts",
51         "in bytes",
52         "out pkts",
53         "out bytes",
54         "tot pkts",
55         "tot bytes",
56 };
57
58 static int sortmode = 0;
59 static bool cumulative = false;
60
61 static list_t node_list;
62 static struct timeval cur, prev, diff;
63 static int delay = 1000;
64 static bool changed = true;
65 static const char *bunit = "bytes";
66 static float bscale = 1;
67 static const char *punit = "pkts";
68 static float pscale = 1;
69
70 static bool update(int fd) {
71         if(!sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC))
72                 return false;
73
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                         return true;
97
98                 if(n != 7)
99                         return false;
100
101                 nodestats_t *found = NULL;
102
103                 for list_each(nodestats_t, ns, &node_list) {
104                         int result = strcmp(name, ns->name);
105                         if(result > 0) {
106                                 continue;
107                         } if(result == 0) {
108                                 found = ns;
109                                 break;
110                         } else {
111                                 found = xzalloc(sizeof *found);
112                                 found->name = xstrdup(name);
113                                 list_insert_before(&node_list, node, found);
114                                 changed = true;
115                                 break;
116                         }
117                 }
118
119                 if(!found) {
120                         found = xzalloc(sizeof *found);
121                         found->name = xstrdup(name);
122                         list_insert_tail(&node_list, found);
123                         changed = true;
124                 }
125
126                 found->known = true;
127                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
128                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
129                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
130                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
131                 found->in_packets = in_packets;
132                 found->in_bytes = in_bytes;
133                 found->out_packets = out_packets;
134                 found->out_bytes = out_bytes;
135         }
136
137         return false;
138 }
139
140 static int cmpfloat(float a, float b) {
141         if(a < b)
142                 return -1;
143         else if(a > b)
144                 return 1;
145         else
146                 return 0;
147 }
148
149 static int cmpu64(uint64_t a, uint64_t b) {
150         if(a < b)
151                 return -1;
152         else if(a > b)
153                 return 1;
154         else
155                 return 0;
156 }
157
158 static int sortfunc(const void *a, const void *b) {
159         const nodestats_t *na = *(const nodestats_t **)a;
160         const nodestats_t *nb = *(const nodestats_t **)b;
161         switch(sortmode) {
162                 case 1:
163                         if(cumulative)
164                                 return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
165                         else
166                                 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
167                 case 2:
168                         if(cumulative)
169                                 return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
170                         else
171                                 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
172                 case 3:
173                         if(cumulative)
174                                 return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
175                         else
176                                 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
177                 case 4:
178                         if(cumulative)
179                                 return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
180                         else
181                                 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
182                 case 5:
183                         if(cumulative)
184                                 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
185                         else
186                                 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
187                 case 6:
188                         if(cumulative)
189                                 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
190                         else
191                                 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
192                 default:
193                         return strcmp(na->name, nb->name) ?: na->i - nb->i;
194         }
195 }
196
197 static void redraw(void) {
198         erase();
199
200         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
201         attrset(A_REVERSE);
202         mvprintw(2, 0, "Node                IN %s   IN %s   OUT %s  OUT %s", punit, bunit, punit, bunit);
203         chgat(-1, A_REVERSE, 0, NULL);
204
205         static nodestats_t **sorted = 0;
206         static int n = 0;
207         if(changed) {
208                 n = 0;
209                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
210                 for list_each(nodestats_t, ns, &node_list)
211                         sorted[n++] = ns;
212                 changed = false;
213         }
214
215         for(int i = 0; i < n; i++)
216                 sorted[i]->i = i;
217
218         if(sorted)
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.0f %10.0f %10.0f %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                 if(!update(fd))
252                         break;
253
254                 redraw();
255
256                 switch(getch()) {
257                         case 's': {
258                                 timeout(-1);
259                                 float input = delay * 1e-3;
260                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
261                                 scanw("%f", &input);
262                                 if(input < 0.1)
263                                         input = 0.1;
264                                 delay = input * 1e3;
265                                 timeout(delay);
266                                 break;
267                         }
268                         case 'c':
269                                 cumulative = !cumulative;
270                                 break;
271                         case 'n':
272                                 sortmode = 0;
273                                 break;
274                         case 'i':
275                                 sortmode = 2;
276                                 break;
277                         case 'I':
278                                 sortmode = 1;
279                                 break;
280                         case 'o':
281                                 sortmode = 4;
282                                 break;
283                         case 'O':
284                                 sortmode = 3;
285                                 break;
286                         case 't':
287                                 sortmode = 6;
288                                 break;
289                         case 'T':
290                                 sortmode = 5;
291                                 break;
292                         case 'b':
293                                 bunit = "bytes";
294                                 bscale = 1;
295                                 punit = "pkts";
296                                 pscale = 1;
297                                 break;
298                         case 'k':
299                                 bunit = "kbyte";
300                                 bscale = 1e-3;
301                                 punit = "pkts";
302                                 pscale = 1;
303                                 break;
304                         case 'M':
305                                 bunit = "Mbyte";
306                                 bscale = 1e-6;
307                                 punit = "kpkt";
308                                 pscale = 1e-3;
309                                 break;
310                         case 'G':
311                                 bunit = "Gbyte";
312                                 bscale = 1e-9;
313                                 punit = "Mpkt";
314                                 pscale = 1e-6;
315                                 break;
316                         case 'q':
317                         case KEY_BREAK:
318                                 running = false;
319                                 break;
320                         default:
321                                 break;
322                 }
323         }
324
325         endwin();
326 }
327
328 #endif