Remove debugging message that was accidentily left in.
[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 static const char *unit = "bytes";
62 static float scale = 1;
63
64 static void update(int fd) {
65         sendline(fd, "%d %d", CONTROL, REQ_DUMP_TRAFFIC);
66         gettimeofday(&now, NULL);
67
68         timersub(&now, &prev, &diff);
69         prev = now;
70         float interval = diff.tv_sec + diff.tv_usec * 1e-6;
71
72         char line[4096];
73         char name[4096];
74         int code;
75         int req;
76         uint64_t in_packets;
77         uint64_t in_bytes;
78         uint64_t out_packets;
79         uint64_t out_bytes;
80
81         for(list_node_t *i = node_list.head; i; i = i->next) {
82                 nodestats_t *node = i->data;
83                 node->known = false;
84         }
85
86         while(recvline(fd, line, sizeof line)) {
87                 int n = sscanf(line, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, &code, &req, name, &in_packets, &in_bytes, &out_packets, &out_bytes);
88
89                 if(n == 2)
90                         break;
91
92                 if(n != 7) {
93                         endwin();
94                         fprintf(stderr, "Error receiving traffic information\n");
95                         exit(1);
96                 }
97
98                 nodestats_t *found = NULL;
99
100                 for(list_node_t *i = node_list.head; i; i = i->next) {
101                         nodestats_t *node = i->data;
102                         int result = strcmp(name, node->name);
103                         if(result > 0) {
104                                 continue;
105                         } if(result == 0) {
106                                 found = node;
107                                 break;
108                         } else {
109                                 found = xmalloc_and_zero(sizeof *found);
110                                 found->name = xstrdup(name);
111                                 list_insert_before(&node_list, i, found);
112                                 changed = true;
113                                 break;
114                         }
115                 }
116
117                 if(!found) {
118                         found = xmalloc_and_zero(sizeof *found);
119                         found->name = xstrdup(name);
120                         list_insert_tail(&node_list, found);
121                         changed = true;
122                 }
123
124                 found->known = true;
125                 found->in_packets_rate = (in_packets - found->in_packets) / interval;
126                 found->in_bytes_rate = (in_bytes - found->in_bytes) / interval;
127                 found->out_packets_rate = (out_packets - found->out_packets) / interval;
128                 found->out_bytes_rate = (out_bytes - found->out_bytes) / interval;
129                 found->in_packets = in_packets;
130                 found->in_bytes = in_bytes;
131                 found->out_packets = out_packets;
132                 found->out_bytes = out_bytes;
133         }
134 }
135
136 static void redraw(void) {
137         erase();
138
139         mvprintw(0, 0, "Tinc %-16s  Nodes: %4d  Sort: %-8s  %s", netname, node_list.count, sortname[sortmode], cumulative ? "Cumulative" : "Current");
140         attrset(A_REVERSE);
141         mvprintw(2, 0, "Node                IN pkts   IN %s   OUT pkts  OUT %s", unit, unit);
142         chgat(-1, A_REVERSE, 0, NULL);
143
144         static nodestats_t **sorted = 0;
145         static int n = 0;
146         if(changed) {
147                 n = 0;
148                 sorted = xrealloc(sorted, node_list.count * sizeof *sorted);
149                 for(list_node_t *i = node_list.head; i; i = i->next)
150                         sorted[n++] = i->data;
151                 changed = false;
152         }
153         
154         int cmpfloat(float a, float b) {
155                 if(a < b)
156                         return -1;
157                 else if(a > b)
158                         return 1;
159                 else
160                         return 0;
161         }
162
163         int cmpu64(uint64_t a, uint64_t b) {
164                 if(a < b)
165                         return -1;
166                 else if(a > b)
167                         return 1;
168                 else
169                         return 0;
170         }
171
172         int sortfunc(const void *a, const void *b) {
173                 const nodestats_t *na = *(const nodestats_t **)a;
174                 const nodestats_t *nb = *(const nodestats_t **)b;
175                 switch(sortmode) {
176                         case 1:
177                                 if(cumulative)
178                                         return -cmpu64(na->in_packets, nb->in_packets);
179                                 else
180                                         return -cmpfloat(na->in_packets_rate, nb->in_packets_rate);
181                         case 2:
182                                 if(cumulative)
183                                         return -cmpu64(na->in_bytes, nb->in_bytes);
184                                 else
185                                         return -cmpfloat(na->in_bytes_rate, nb->in_bytes_rate);
186                         case 3:
187                                 if(cumulative)
188                                         return -cmpu64(na->out_packets, nb->out_packets);
189                                 else
190                                         return -cmpfloat(na->out_packets_rate, nb->out_packets_rate);
191                         case 4:
192                                 if(cumulative)
193                                         return -cmpu64(na->out_bytes, nb->out_bytes);
194                                 else
195                                         return -cmpfloat(na->out_bytes_rate, nb->out_bytes_rate);
196                         case 5:
197                                 if(cumulative)
198                                         return -cmpu64(na->in_packets + na->out_packets, nb->in_packets + nb->out_packets);
199                                 else
200                                         return -cmpfloat(na->in_packets_rate + na->out_packets_rate, nb->in_packets_rate + nb->out_packets_rate);
201                         case 6:
202                                 if(cumulative)
203                                         return -cmpu64(na->in_bytes + na->out_bytes, nb->in_bytes + nb->out_bytes);
204                                 else
205                                         return -cmpfloat(na->in_bytes_rate + na->out_bytes_rate, nb->in_bytes_rate + nb->out_bytes_rate);
206                         default:
207                                 return strcmp(na->name, nb->name);
208                 }
209         }
210
211         qsort(sorted, n, sizeof *sorted, sortfunc);
212
213         for(int i = 0, row = 3; i < n; i++, row++) {
214                 nodestats_t *node = sorted[i];
215                 if(node->known)
216                         if(node->in_packets_rate || node->out_packets_rate)
217                                 attrset(A_BOLD);
218                         else
219                                 attrset(A_NORMAL);
220                 else
221                         attrset(A_DIM);
222
223                 if(cumulative)
224                         mvprintw(row, 0, "%-16s %'10"PRIu64" %'10.0f %'10"PRIu64" %'10.0f",
225                                         node->name, node->in_packets, node->in_bytes * scale, node->out_packets, node->out_bytes * scale);
226                 else
227                         mvprintw(row, 0, "%-16s %'10.0f %'10.0f %'10.0f %'10.0f",
228                                         node->name, node->in_packets_rate, node->in_bytes_rate * scale, node->out_packets_rate, node->out_bytes_rate * scale);
229         }
230
231         attrset(A_NORMAL);
232         move(1, 0);
233
234         refresh();
235 }
236
237 void top(int fd) {
238         initscr();
239         timeout(delay);
240
241         while(running) {
242                 update(fd);
243                 redraw();
244
245                 switch(getch()) {
246                         case 's': {
247                                 timeout(-1);
248                                 float input = delay * 1e-3;
249                                 mvprintw(1, 0, "Change delay from %.1fs to: ", input);
250                                 scanw("%f", &input);
251                                 if(input < 0.1)
252                                         input = 0.1;
253                                 delay = input * 1e3;
254                                 timeout(delay);
255                                 break;
256                         }
257                         case 'c':
258                                   cumulative = !cumulative;
259                                   break;
260                         case 'n':
261                                   sortmode = 0;
262                                   break;
263                         case 'i':
264                                   sortmode = 2;
265                                   break;
266                         case 'I':
267                                   sortmode = 1;
268                                   break;
269                         case 'o':
270                                   sortmode = 4;
271                                   break;
272                         case 'O':
273                                   sortmode = 3;
274                                   break;
275                         case 't':
276                                   sortmode = 6;
277                                   break;
278                         case 'T':
279                                   sortmode = 5;
280                                   break;
281                         case 'b':
282                                   unit = "bytes";
283                                   scale = 1;
284                                   break;
285                         case 'k':
286                                   unit = "kbyte";
287                                   scale = 1e-3;
288                                   break;
289                         case 'M':
290                                   unit = "Mbyte";
291                                   scale = 1e-6;
292                                   break;
293                         case 'G':
294                                   unit = "Gbyte";
295                                   scale = 1e-9;
296                                   break;
297                         case 'q':
298                         case 27:
299                         case KEY_BREAK:
300                                 running = false;
301                                 break;
302                         default:
303                                 break;
304                 }
305         }
306
307         endwin();
308 }