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