Prefer ncurses over curses.
[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 void update(int fd) {
70         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
71         gettimeofday(&cur, NULL);
72
73         timersub(&cur, &prev, &diff);
74         prev = cur;
75         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
76
77         char line[4096];
78         char name[4096];
79         int code;
80         int req;
81         uint64_t in_packets;
82         uint64_t in_bytes;
83         uint64_t out_packets;
84         uint64_t out_bytes;
85
86         for list_each(nodestats_t, ns, &node_list)
87                 ns->known = false;
88
89         while(recvline(fd, line, sizeof line)) {
90                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
91
92                 if(n == 2)
93                         break;
94
95                 if(n != 7) {
96                         endwin();
97                         fprintf(stderr, "Error receiving traffic information\n");
98                         exit(1);
99                 }
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
138 static int cmpfloat(float a, float b) {
139         if(a < b)
140                 return -1;
141         else if(a > b)
142                 return 1;
143         else
144                 return 0;
145 }
146
147 static int cmpu64(uint64_t a, uint64_t b) {
148         if(a < b)
149                 return -1;
150         else if(a > b)
151                 return 1;
152         else
153                 return 0;
154 }
155
156 static int sortfunc(const void *a, const void *b) {
157         const nodestats_t *na = *(const nodestats_t **)a;
158         const nodestats_t *nb = *(const nodestats_t **)b;
159         switch(sortmode) {
160                 case 1:
161                         if(cumulative)
162                                 return -cmpu64(na->in_packets, nb->in_packets) ?: na->i - nb->i;
163                         else
164                                 return -cmpfloat(na->in_packets_rate, nb->in_packets_rate) ?: na->i - nb->i;
165                 case 2:
166                         if(cumulative)
167                                 return -cmpu64(na->in_bytes, nb->in_bytes) ?: na->i - nb->i;
168                         else
169                                 return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate) ?: na->i - nb->i;
170                 case 3:
171                         if(cumulative)
172                                 return -cmpu64(na->out_packets, nb->out_packets) ?: na->i - nb->i;
173                         else
174                                 return -cmpfloat(na->out_packets_rate, nb->out_packets_rate) ?: na->i - nb->i;
175                 case 4:
176                         if(cumulative)
177                                 return -cmpu64(na->out_bytes, nb->out_bytes) ?: na->i - nb->i;
178                         else
179                                 return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate) ?: na->i - nb->i;
180                 case 5:
181                         if(cumulative)
182                                 return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets) ?: na->i - nb->i;
183                         else
184                                 return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate) ?: na->i - nb->i;
185                 case 6:
186                         if(cumulative)
187                                 return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes) ?: na->i - nb->i;
188                         else
189                                 return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate) ?: na->i - nb->i;
190                 default:
191                         return strcmp(na->name, nb->name) ?: na->i - nb->i;
192         }
193 }
194
195 static void redraw(void) {
196         erase();
197
198         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-10s  %s", netname ?: "", node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
199         attrset(A_REVERSE);
200         mvprintw(2, 0, "Node                IN %s   IN %s   OUT %s  OUT %s", punit, bunit, punit, bunit);
201         chgat(-1, A_REVERSE, 0, NULL);
202
203         static nodestats_t **sorted = 0;
204         static int n = 0;
205         if(changed) {
206                 n = 0;
207                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
208                 for list_each(nodestats_t, ns, &node_list)
209                         sorted[n++] = ns;
210                 changed = false;
211         }
212
213         for(int i = 0; i < n; i++)
214                 sorted[i]->i = i;
215
216         if(sorted)
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.0f %10.0f %10.0f %10.0f",
231                                         node->name, node->in_packets * pscale, node->in_bytes * bscale, node->out_packets * pscale, node->out_bytes * bscale);
232                 else
233                         mvprintw(row, 0, "%-16s %10.0f %10.0f %10.0f %10.0f",
234                                         node->name, node->in_packets_rate * pscale, node->in_bytes_rate * bscale, node->out_packets_rate * pscale, node->out_bytes_rate * bscale);
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                                 bunit = "bytes";
290                                 bscale = 1;
291                                 punit = "pkts";
292                                 pscale = 1;
293                                 break;
294                         case 'k':
295                                 bunit = "kbyte";
296                                 bscale = 1e-3;
297                                 punit = "pkts";
298                                 pscale = 1;
299                                 break;
300                         case 'M':
301                                 bunit = "Mbyte";
302                                 bscale = 1e-6;
303                                 punit = "kpkt";
304                                 pscale = 1e-3;
305                                 break;
306                         case 'G':
307                                 bunit = "Gbyte";
308                                 bscale = 1e-9;
309                                 punit = "Mpkt";
310                                 pscale = 1e-6;
311                                 break;
312                         case 'q':
313                         case KEY_BREAK:
314                                 running = false;
315                                 break;
316                         default:
317                                 break;
318                 }
319         }
320
321         endwin();
322 }
323
324 #endif