Move make_names() and related variables to its own source file.
[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 "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 #ifndef timersub
70 #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)
71 #endif
72
73 static void update(int fd) {
74         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
75         gettimeofday(&cur, NULL);
76
77         timersub(&cur, &prev, &diff);
78         prev = cur;
79         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
80
81         char line[4096];
82         char name[4096];
83         int code;
84         int req;
85         uint64_t in_packets;
86         uint64_t in_bytes;
87         uint64_t out_packets;
88         uint64_t out_bytes;
89
90         for list_each(nodestats_t, ns, &node_list)
91                 ns->known = false;
92
93         while(recvline(fd, line, sizeof line)) {
94                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
95
96                 if(n == 2)
97                         break;
98
99                 if(n != 7) {
100                         endwin();
101                         fprintf(stderr, "Error receiving traffic information\n");
102                         exit(1);
103                 }
104
105                 nodestats_t *found = NULL;
106
107                 for list_each(nodestats_t, ns, &node_list) {
108                         int result = strcmp(name, ns->name);
109                         if(result > 0) {
110                                 continue;
111                         } if(result == 0) {
112                                 found = ns;
113                                 break;
114                         } else {
115                                 found = xmalloc_and_zero(sizeof *found);
116                                 found->name = xstrdup(name);
117                                 list_insert_before(&node_list, node, found);
118                                 changed = true;
119                                 break;
120                         }
121                 }
122
123                 if(!found) {
124                         found = xmalloc_and_zero(sizeof *found);
125                         found->name = xstrdup(name);
126                         list_insert_tail(&node_list, found);
127                         changed = true;
128                 }
129
130                 found->known = true;
131                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
132                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
133                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
134                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
135                 found->in_packets = in_packets;
136                 found->in_bytes = in_bytes;
137                 found->out_packets = out_packets;
138                 found->out_bytes = out_bytes;
139         }
140 }
141
142 static int cmpfloat(float a, float b) {
143         if(a < b)
144                 return -1;
145         else if(a > b)
146                 return 1;
147         else
148                 return 0;
149 }
150
151 static int cmpu64(uint64_t a, uint64_t b) {
152         if(a < b)
153                 return -1;
154         else if(a > b)
155                 return 1;
156         else
157                 return 0;
158 }
159
160 static int sortfunc(const void *a, const void *b) {
161         const nodestats_t *na = *(const nodestats_t **)a;
162         const nodestats_t *nb = *(const nodestats_t **)b;
163         switch(sortmode) {
164                 case 1:
165                         if(cumulative)
166                                 return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
167                         else
168                                 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
169                 case 2:
170                         if(cumulative)
171                                 return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
172                         else
173                                 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
174                 case 3:
175                         if(cumulative)
176                                 return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
177                         else
178                                 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
179                 case 4:
180                         if(cumulative)
181                                 return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
182                         else
183                                 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
184                 case 5:
185                         if(cumulative)
186                                 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
187                         else
188                                 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
189                 case 6:
190                         if(cumulative)
191                                 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
192                         else
193                                 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
194                 default:
195                         return strcmp(na->name, nb->name) ?: na->i - nb->i;
196         }
197 }
198
199 static void redraw(void) {
200         erase();
201
202         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
203         attrset(A_REVERSE);
204         mvprintw(2, 0, "Node                IN %s   IN %s   OUT %s  OUT %s", punit, bunit, punit, bunit);
205         chgat(-1, A_REVERSE, 0, NULL);
206
207         static nodestats_t **sorted = 0;
208         static int n = 0;
209         if(changed) {
210                 n = 0;
211                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
212                 for list_each(nodestats_t, ns, &node_list)
213                         sorted[n++] = ns;
214                 changed = false;
215         }
216
217         for(int i = 0; i < n; i++)
218                 sorted[i]->i = i;
219
220         qsort(sorted, n, sizeof *sorted, sortfunc);
221
222         for(int i = 0, row = 3; i < n; i++, row++) {
223                 nodestats_t *node = sorted[i];
224                 if(node->known)
225                         if(node->in_packets_rate || node->out_packets_rate)
226                                 attrset(A_BOLD);
227                         else
228                                 attrset(A_NORMAL);
229                 else
230                         attrset(A_DIM);
231
232                 if(cumulative)
233                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
234                                         node->name, node->in_packets * pscale, node->in_bytes * bscale, node->out_packets * pscale, node->out_bytes * bscale);
235                 else
236                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
237                                         node->name, node->in_packets_rate * pscale, node->in_bytes_rate * bscale, node->out_packets_rate * pscale, node->out_bytes_rate * bscale);
238         }
239
240         attrset(A_NORMAL);
241         move(1, 0);
242
243         refresh();
244 }
245
246 void top(int fd) {
247         initscr();
248         timeout(delay);
249         bool running = true;
250
251         while(running) {
252                 update(fd);
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