prepare the 0.3 release
[lttngtop.git] / src / cursesdisplay.c
1 /*
2 * Copyright (C) 2011-2012 Julien Desfossez
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <config.h>
19
20 #ifdef HAVE_LIBNCURSES
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <ncurses.h>
27 #include <panel.h>
28 #include <pthread.h>
29 #include <semaphore.h>
30
31 #include "cursesdisplay.h"
32 #include "lttngtoptypes.h"
33 #include "iostreamtop.h"
34 #include "common.h"
35
36 #define DEFAULT_DELAY 15
37 #define MAX_LINE_LENGTH 50
38 #define MAX_LOG_LINES 4
39
40 /* to prevent concurrent updates of the different windows */
41 sem_t update_display_sem;
42
43 char *termtype;
44 WINDOW *footer, *header, *center, *status;
45 WINDOW *pref_panel_window = NULL;
46 PANEL *pref_panel, *main_panel;
47
48 int pref_panel_visible = 0;
49 int pref_line_selected = 0;
50 int pref_current_sort = 0;
51
52 int last_display_index, currently_displayed_index;
53
54 struct processtop *selected_process = NULL;
55 int selected_ret;
56
57 int selected_line = 0; /* select bar position */
58 int selected_in_list = 0; /* selection relative to the whole list */
59 int list_offset = 0; /* first index in the list to display (scroll) */
60 int nb_log_lines = 0;
61 char log_lines[MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES];
62
63 int max_elements = 80;
64
65 int toggle_threads = 1;
66 int toggle_virt = -1;
67 int toggle_pause = -1;
68
69 int filter_host_panel = 0;
70
71 int max_center_lines;
72
73 pthread_t keyboard_thread;
74
75 struct header_view cputopview[6];
76 struct header_view iostreamtopview[3];
77 struct header_view fileview[3];
78 struct header_view kprobeview[2];
79
80 void reset_ncurses()
81 {
82 curs_set(1);
83 endwin();
84 quit = 1;
85 sem_post(&pause_sem);
86 sem_post(&timer);
87 sem_post(&goodtodisplay);
88 sem_post(&end_trace_sem);
89 sem_post(&goodtoupdate);
90 }
91
92 static void handle_sigterm(int signal)
93 {
94 pthread_cancel(keyboard_thread);
95 reset_ncurses();
96 }
97
98 void init_screen()
99 {
100 initscr();
101 noecho();
102 halfdelay(DEFAULT_DELAY);
103 nonl();
104 intrflush(stdscr, false);
105 keypad(stdscr, true);
106 curs_set(0);
107
108 if (has_colors()) {
109 start_color();
110 init_pair(1, COLOR_RED, COLOR_BLACK); /* - */
111 init_pair(2, COLOR_GREEN, COLOR_BLACK); /* + */
112 init_pair(3, COLOR_BLACK, COLOR_WHITE); /* keys */
113 init_pair(4, COLOR_WHITE, COLOR_GREEN); /* keys activated */
114 init_pair(5, COLOR_BLACK, COLOR_YELLOW); /* select line */
115 init_pair(6, COLOR_GREEN, COLOR_BLACK); /* selected process */
116 init_pair(7, COLOR_RED, COLOR_YELLOW); /* selected process + line*/
117 }
118 termtype = getenv("TERM");
119 if (!strcmp(termtype, "xterm") || !strcmp(termtype, "xterm-color") ||
120 !strcmp(termtype, "vt220")) {
121 define_key("\033[H", KEY_HOME);
122 define_key("\033[F", KEY_END);
123 define_key("\033OP", KEY_F(1));
124 define_key("\033OQ", KEY_F(2));
125 define_key("\033OR", KEY_F(3));
126 define_key("\033OS", KEY_F(4));
127 define_key("\0330U", KEY_F(6));
128 define_key("\033[11~", KEY_F(1));
129 define_key("\033[12~", KEY_F(2));
130 define_key("\033[13~", KEY_F(3));
131 define_key("\033[14~", KEY_F(4));
132 define_key("\033[16~", KEY_F(6));
133 define_key("\033[17;2~", KEY_F(18));
134 }
135 signal(SIGTERM, handle_sigterm);
136 signal(SIGINT, handle_sigterm);
137 mousemask(BUTTON1_CLICKED, NULL);
138 refresh();
139 }
140
141 WINDOW *create_window(int height, int width, int startx, int starty)
142 {
143 WINDOW *win;
144 win = newwin(height, width, startx, starty);
145 box(win, 0 , 0);
146 wrefresh(win);
147 return win;
148 }
149
150 WINDOW *create_window_no_border(int height, int width, int startx, int starty)
151 {
152 WINDOW *win;
153 win = newwin(height, width, startx, starty);
154 wrefresh(win);
155 return win;
156 }
157
158 void print_digit(WINDOW *win, int digit)
159 {
160 if (digit < 0) {
161 wattron(win, COLOR_PAIR(1));
162 wprintw(win, "%d", digit);
163 wattroff(win, COLOR_PAIR(1));
164 } else if (digit > 0) {
165 wattron(win, COLOR_PAIR(2));
166 wprintw(win, "+%d", digit);
167 wattroff(win, COLOR_PAIR(2));
168 } else {
169 wprintw(win, "0");
170 }
171 }
172
173 void print_digits(WINDOW *win, int first, int second)
174 {
175 wprintw(win, "(");
176 print_digit(win, first);
177 wprintw(win, ", ");
178 print_digit(win, second);
179 wprintw(win, ")");
180 }
181
182 void print_headers(int line, char *desc, int value, int first, int second)
183 {
184 wattron(header, A_BOLD);
185 mvwprintw(header, line, 4, "%s", desc);
186 wattroff(header, A_BOLD);
187 mvwprintw(header, line, 16, "%d", value);
188 wmove(header, line, 24);
189 print_digits(header, first, second);
190 wmove(header, line, 40);
191 }
192
193 void set_window_title(WINDOW *win, char *title)
194 {
195 wattron(win, A_BOLD);
196 mvwprintw(win, 0, 1, title);
197 wattroff(win, A_BOLD);
198 }
199
200 void print_log(char *str)
201 {
202 int i;
203 int current_line = 1;
204 int current_char = 1;
205 char *tmp, *tmp2;
206 /* rotate the line buffer */
207 if (nb_log_lines >= MAX_LOG_LINES) {
208 tmp = strndup(log_lines, MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES);
209 tmp2 = strchr(tmp, '\n');
210 memset(log_lines, '\0', strlen(log_lines));
211 strncat(log_lines, tmp2 + 1, strlen(tmp2) - 1);
212 log_lines[strlen(log_lines)] = '\n';
213 log_lines[strlen(log_lines)] = '\0';
214 free(tmp);
215 }
216 nb_log_lines++;
217
218 strncat(log_lines, str, MAX_LINE_LENGTH - 1);
219
220 if (nb_log_lines < MAX_LOG_LINES)
221 log_lines[strlen(log_lines)] = '\n';
222 log_lines[strlen(log_lines)] = '\0';
223
224 werase(status);
225 box(status, 0 , 0);
226 set_window_title(status, "Status");
227 for (i = 0; i < strlen(log_lines); i++) {
228 if (log_lines[i] == '\n') {
229 wmove(status, ++current_line, 1);
230 current_char = 1;
231 } else {
232 mvwprintw(status, current_line, current_char++, "%c",
233 log_lines[i]);
234 }
235 }
236 wrefresh(status);
237 }
238
239 int process_selected(struct processtop *process)
240 {
241 if (lookup_filter_tid_list(process->tid))
242 return 1;
243 return 0;
244 }
245
246 void update_selected_processes()
247 {
248 if (process_selected(selected_process)) {
249 remove_filter_tid_list(selected_process->tid);
250 } else {
251 add_filter_tid_list(selected_process);
252 }
253 }
254
255 void print_key(WINDOW *win, char *key, char *desc, int toggle)
256 {
257 int pair;
258 if (toggle > 0)
259 pair = 4;
260 else
261 pair = 3;
262 wattron(win, COLOR_PAIR(pair));
263 wprintw(footer, "%s", key);
264 wattroff(win, COLOR_PAIR(pair));
265 wprintw(footer, ":%s", desc);
266 }
267
268 void update_footer()
269 {
270 sem_wait(&update_display_sem);
271 werase(footer);
272 wmove(footer, 1, 1);
273 print_key(footer, "F2", "CPUtop ", current_view == cpu);
274 print_key(footer, "F3", "PerfTop ", current_view == perf);
275 print_key(footer, "F4", "IOTop ", current_view == iostream);
276 print_key(footer, "Enter", "Details ", current_view == process_details);
277 print_key(footer, "Space", "Highlight ", 0);
278 print_key(footer, "q", "Quit ", 0);
279 print_key(footer, "r", "Pref ", 0);
280 print_key(footer, "t", "Threads ", toggle_threads);
281 print_key(footer, "v", "Virt ", toggle_virt);
282 print_key(footer, "p", "Pause ", toggle_pause);
283
284 wrefresh(footer);
285 sem_post(&update_display_sem);
286 }
287
288 void basic_header()
289 {
290 werase(header);
291 box(header, 0 , 0);
292 set_window_title(header, "Statistics for interval [gathering data...[");
293 wattron(header, A_BOLD);
294 mvwprintw(header, 1, 4, "CPUs");
295 mvwprintw(header, 2, 4, "Threads");
296 mvwprintw(header, 3, 4, "FDs");
297 wattroff(header, A_BOLD);
298 wrefresh(header);
299 }
300
301 static void scale_unit(uint64_t bytes, char *ret)
302 {
303 if (bytes >= 1000000000)
304 sprintf(ret, "%" PRIu64 "G", bytes/1000000000);
305 if (bytes >= 1000000)
306 sprintf(ret, "%" PRIu64 "M", bytes/1000000);
307 else if (bytes >= 1000)
308 sprintf(ret, "%" PRIu64 "K", bytes/1000);
309 else
310 sprintf(ret, "%" PRIu64, bytes);
311 }
312
313 uint64_t total_io()
314 {
315 int i;
316 struct processtop *tmp;
317 uint64_t total = 0;
318
319 for (i = 0; i < data->process_table->len; i++) {
320 tmp = g_ptr_array_index(data->process_table, i);
321 total += tmp->fileread;
322 total += tmp->filewrite;
323 }
324
325 return total;
326 }
327
328 void update_header()
329 {
330 struct tm start, end;
331 uint64_t ts_nsec_start, ts_nsec_end;
332 char io[4];
333
334 ts_nsec_start = data->start % NSEC_PER_SEC;
335 start = format_timestamp(data->start);
336
337 ts_nsec_end = data->end % NSEC_PER_SEC;
338 end = format_timestamp(data->end);
339
340 werase(header);
341 box(header, 0 , 0);
342 set_window_title(header, "Statistics for interval ");
343 wattron(header, A_BOLD);
344
345 wprintw(header, "[%02d:%02d:%02d.%09" PRIu64 ", %02d:%02d:%02d.%09" PRIu64 "[",
346 start.tm_hour, start.tm_min, start.tm_sec, ts_nsec_start,
347 end.tm_hour, end.tm_min, end.tm_sec, ts_nsec_end);
348 mvwprintw(header, 1, 4, "CPUs");
349 wattroff(header, A_BOLD);
350 wprintw(header, "\t%d\t(max/cpu : %0.2f%)", data->cpu_table->len,
351 100.0/data->cpu_table->len);
352 print_headers(2, "Threads", data->nbthreads, data->nbnewthreads,
353 -1*(data->nbdeadthreads));
354 print_headers(3, "FDs", data->nbfiles, data->nbnewfiles,
355 -1*(data->nbclosedfiles));
356 scale_unit(total_io(), io);
357 mvwprintw(header, 3, 43, "%sB/sec", io);
358 wrefresh(header);
359 }
360
361 gint sort_by_cpu_desc(gconstpointer p1, gconstpointer p2)
362 {
363 struct processtop *n1 = *(struct processtop **)p1;
364 struct processtop *n2 = *(struct processtop **)p2;
365 unsigned long totaln1 = n1->totalcpunsec;
366 unsigned long totaln2 = n2->totalcpunsec;
367
368 if (totaln1 < totaln2)
369 return 1;
370 if (totaln1 == totaln2)
371 return 0;
372 return -1;
373 }
374
375 gint sort_by_tid_desc(gconstpointer p1, gconstpointer p2)
376 {
377 struct processtop *n1 = *(struct processtop **)p1;
378 struct processtop *n2 = *(struct processtop **)p2;
379 unsigned long totaln1 = n1->tid;
380 unsigned long totaln2 = n2->tid;
381
382 if (totaln1 < totaln2)
383 return 1;
384 if (totaln1 == totaln2)
385 return 0;
386 return -1;
387 }
388
389 gint sort_by_pid_desc(gconstpointer p1, gconstpointer p2)
390 {
391 struct processtop *n1 = *(struct processtop **)p1;
392 struct processtop *n2 = *(struct processtop **)p2;
393 unsigned long totaln1 = n1->pid;
394 unsigned long totaln2 = n2->pid;
395
396 if (totaln1 < totaln2)
397 return 1;
398 if (totaln1 == totaln2)
399 return 0;
400 return -1;
401 }
402
403 gint sort_by_process_read_desc(gconstpointer p1, gconstpointer p2)
404 {
405 struct processtop *n1 = *(struct processtop **)p1;
406 struct processtop *n2 = *(struct processtop **)p2;
407 unsigned long totaln1 = n1->fileread;
408 unsigned long totaln2 = n2->fileread;
409
410 if (totaln1 < totaln2)
411 return 1;
412 if (totaln1 == totaln2)
413 return 0;
414 return -1;
415 }
416
417 gint sort_by_process_write_desc(gconstpointer p1, gconstpointer p2)
418 {
419 struct processtop *n1 = *(struct processtop **)p1;
420 struct processtop *n2 = *(struct processtop **)p2;
421 unsigned long totaln1 = n1->filewrite;
422 unsigned long totaln2 = n2->filewrite;
423
424 if (totaln1 < totaln2)
425 return 1;
426 if (totaln1 == totaln2)
427 return 0;
428 return -1;
429 }
430
431 gint sort_by_process_total_desc(gconstpointer p1, gconstpointer p2)
432 {
433 struct processtop *n1 = *(struct processtop **)p1;
434 struct processtop *n2 = *(struct processtop **)p2;
435 unsigned long totaln1 = n1->totalfilewrite + n1->totalfileread;
436 unsigned long totaln2 = n2->totalfilewrite + n2->totalfileread;
437
438 if (totaln1 < totaln2)
439 return 1;
440 if (totaln1 == totaln2)
441 return 0;
442 return -1;
443 }
444
445 gint sort_by_file_read_desc(gconstpointer p1, gconstpointer p2)
446 {
447 struct files *n1 = *(struct files **)p1;
448 struct files *n2 = *(struct files **)p2;
449 unsigned long totaln1;
450 unsigned long totaln2;
451
452 totaln1 = n1->read;
453 totaln2 = n2->read;
454
455 if (totaln1 < totaln2)
456 return 1;
457 if (totaln1 == totaln2)
458 return 0;
459 return -1;
460 }
461
462 gint sort_by_file_write_desc(gconstpointer p1, gconstpointer p2)
463 {
464 struct files *n1 = *(struct files **)p1;
465 struct files *n2 = *(struct files **)p2;
466 unsigned long totaln1;
467 unsigned long totaln2;
468
469 totaln1 = n1->write;
470 totaln2 = n2->write;
471
472 if (totaln1 < totaln2)
473 return 1;
474 if (totaln1 == totaln2)
475 return 0;
476 return -1;
477 }
478
479 gint sort_by_file_fd_desc(gconstpointer p1, gconstpointer p2)
480 {
481 struct files *n1 = *(struct files **)p1;
482 struct files *n2 = *(struct files **)p2;
483 unsigned long totaln1;
484 unsigned long totaln2;
485
486 totaln1 = n1->fd;
487 totaln2 = n2->fd;
488
489 if (totaln1 < totaln2)
490 return 1;
491 if (totaln1 == totaln2)
492 return 0;
493 return -1;
494 }
495
496 gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2)
497 {
498 struct processtop *n1 = *(struct processtop **)p1;
499 struct processtop *n2 = *(struct processtop **)p2;
500 unsigned long totaln1 = n1->threadstotalcpunsec;
501 unsigned long totaln2 = n2->threadstotalcpunsec;
502
503 if (totaln1 < totaln2)
504 return 1;
505 if (totaln1 == totaln2)
506 return 0;
507 return -1;
508 }
509
510 void update_kprobes_display()
511 {
512 int i, column;
513 struct kprobes *probe;
514 int header_offset = 2;
515 int current_line = 0;
516
517 set_window_title(center, "Kprobes Top ");
518 wattron(center, A_BOLD);
519 column = 1;
520 for (i = 0; i < 2; i++) {
521 if (kprobeview[i].sort) {
522 wattron(center, A_UNDERLINE);
523 pref_current_sort = i;
524 }
525 mvwprintw(center, 1, column, "%s", kprobeview[i].title);
526 wattroff(center, A_UNDERLINE);
527 column += 30;
528 }
529 wattroff(center, A_BOLD);
530
531 for (i = 0; i < data->kprobes_table->len; i++) {
532 column = 1;
533 probe = g_ptr_array_index(data->kprobes_table, i);
534 mvwprintw(center, current_line + header_offset, column,
535 "%s", probe->probe_name + 6);
536 column += 30;
537 mvwprintw(center, current_line + header_offset, column,
538 "%d", probe->count);
539 current_line++;
540 }
541 }
542
543 void update_cputop_display()
544 {
545 int i;
546 int header_offset = 2;
547 struct processtop *tmp;
548 unsigned long elapsed;
549 double maxcputime;
550 int nblinedisplayed = 0;
551 int current_line = 0;
552 int current_row_offset;
553 int column;
554
555 elapsed = data->end - data->start;
556 maxcputime = elapsed * data->cpu_table->len / 100.0;
557
558 if (cputopview[0].sort == 1)
559 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
560 else if (cputopview[1].sort == 1)
561 g_ptr_array_sort(data->process_table, sort_by_pid_desc);
562 else if (cputopview[2].sort == 1)
563 g_ptr_array_sort(data->process_table, sort_by_tid_desc);
564 else if (cputopview[3].sort == 1)
565 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
566 else
567 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
568
569 set_window_title(center, "CPU Top");
570 wattron(center, A_BOLD);
571 column = 1;
572 for (i = 0; i < 6; i++) {
573 if (toggle_virt < 0 && (i == 3 || i == 4)) {
574 continue;
575 }
576 if (cputopview[i].sort) {
577 wattron(center, A_UNDERLINE);
578 pref_current_sort = i;
579 }
580 mvwprintw(center, 1, column, cputopview[i].title);
581 wattroff(center, A_UNDERLINE);
582 column += 10;
583 }
584 wattroff(center, A_BOLD);
585
586 max_center_lines = LINES - 5 - 7 - 1 - header_offset;
587
588 /* iterate the process (thread) list */
589 for (i = list_offset; i < data->process_table->len &&
590 nblinedisplayed < max_center_lines; i++) {
591 tmp = g_ptr_array_index(data->process_table, i);
592 current_row_offset = 1;
593 if (toggle_filter > 0 && !lookup_filter_tid_list(tmp->tid))
594 continue;
595
596 if (tmp->pid != tmp->tid)
597 if (toggle_threads == -1)
598 continue;
599
600 /* line */
601 if (current_line == selected_line) {
602 selected_process = tmp;
603 wattron(center, COLOR_PAIR(5));
604 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
605 }
606 /* filtered process */
607 if (process_selected(tmp)) {
608 if (current_line == selected_line)
609 wattron(center, COLOR_PAIR(7));
610 else
611 wattron(center, COLOR_PAIR(6));
612 }
613 /* CPU(%) */
614 mvwprintw(center, current_line + header_offset,
615 current_row_offset, "%1.2f",
616 tmp->totalcpunsec / maxcputime);
617 current_row_offset += 10;
618 /* PID */
619 mvwprintw(center, current_line + header_offset,
620 current_row_offset, "%d", tmp->pid);
621 current_row_offset += 10;
622 /* TID */
623 mvwprintw(center, current_line + header_offset,
624 current_row_offset, "%d", tmp->tid);
625 current_row_offset += 10;
626 if (toggle_virt > 0) {
627 /* VPID */
628 mvwprintw(center, current_line + header_offset,
629 current_row_offset, "%d", tmp->vpid);
630 current_row_offset += 10;
631 /* VTID */
632 mvwprintw(center, current_line + header_offset,
633 current_row_offset, "%d", tmp->vtid);
634 current_row_offset += 10;
635 }
636 /* NAME */
637 mvwprintw(center, current_line + header_offset,
638 current_row_offset, "%s", tmp->comm);
639 wattroff(center, COLOR_PAIR(7));
640 wattroff(center, COLOR_PAIR(6));
641 wattroff(center, COLOR_PAIR(5));
642 nblinedisplayed++;
643 current_line++;
644 }
645 }
646
647 gint sort_perf(gconstpointer p1, gconstpointer p2, gpointer key)
648 {
649 struct processtop *n1 = *(struct processtop **) p1;
650 struct processtop *n2 = *(struct processtop **) p2;
651
652 struct perfcounter *tmp1, *tmp2;
653 unsigned long totaln2 = 0;
654 unsigned long totaln1 = 0;
655
656 if (!key)
657 return 0;
658
659 tmp1 = g_hash_table_lookup(n1->perf, key);
660 if (!tmp1)
661 totaln1 = 0;
662 else
663 totaln1 = tmp1->count;
664
665 tmp2 = g_hash_table_lookup(n2->perf, key);
666 if (!tmp2)
667 totaln2 = 0;
668 else
669 totaln2 = tmp2->count;
670
671 if (totaln1 < totaln2)
672 return 1;
673 if (totaln1 == totaln2) {
674 totaln1 = n1->tid;
675 totaln2 = n2->tid;
676 if (totaln1 < totaln2)
677 return 1;
678 return -1;
679 }
680 return -1;
681 }
682
683 void print_key_title(char *key, int line)
684 {
685 wattron(center, A_BOLD);
686 mvwprintw(center, line, 1, "%s", key);
687 mvwprintw(center, line, 30, " ");
688 wattroff(center, A_BOLD);
689 }
690
691 void update_process_details()
692 {
693 unsigned long elapsed;
694 double maxcputime;
695 struct processtop *tmp;
696 struct files *file_tmp;
697 int i, j = 0;
698 char unit[4];
699 char filename_buf[COLS];
700 int line = 1;
701 int column;
702 GPtrArray *newfilearray = g_ptr_array_new();
703 GHashTableIter iter;
704 struct perfcounter *perfn1, *perfn2;
705 gpointer key;
706
707 set_window_title(center, "Process details");
708
709
710 tmp = find_process_tid(data,
711 selected_process->tid,
712 selected_process->comm);
713 elapsed = data->end - data->start;
714 maxcputime = elapsed * data->cpu_table->len / 100.0;
715
716 print_key_title("Name", line++);
717 wprintw(center, "%s", selected_process->comm);
718 print_key_title("TID", line++);
719 wprintw(center, "%d", selected_process->tid);
720 if (!tmp) {
721 print_key_title("Does not exit at this time", 3);
722 return;
723 }
724
725 print_key_title("PID", line++);
726 wprintw(center, "%d", tmp->pid);
727 print_key_title("PPID", line++);
728 wprintw(center, "%d", tmp->ppid);
729 print_key_title("VPID", line++);
730 wprintw(center, "%d", tmp->vpid);
731 print_key_title("VTID", line++);
732 wprintw(center, "%d", tmp->vtid);
733 print_key_title("VPPID", line++);
734 wprintw(center, "%d", tmp->vppid);
735 print_key_title("CPU", line++);
736 wprintw(center, "%1.2f %%", tmp->totalcpunsec/maxcputime);
737
738 print_key_title("READ B/s", line++);
739 scale_unit(tmp->fileread, unit);
740 wprintw(center, "%s", unit);
741
742 print_key_title("WRITE B/s", line++);
743 scale_unit(tmp->filewrite, unit);
744 wprintw(center, "%s", unit);
745
746 g_hash_table_iter_init(&iter, global_perf_liszt);
747 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
748 print_key_title((char *) key, line++);
749 perfn2 = g_hash_table_lookup(tmp->perf, (char *) key);
750 wprintw(center, "%d", perfn2 ? perfn2->count : 0);
751 }
752 line++;
753
754 wattron(center, A_BOLD);
755 column = 1;
756 for (i = 0; i < 3; i++) {
757 if (fileview[i].sort) {
758 pref_current_sort = i;
759 wattron(center, A_UNDERLINE);
760 }
761 mvwprintw(center, line, column, fileview[i].title);
762 wattroff(center, A_UNDERLINE);
763 column += 10;
764 }
765 mvwprintw(center, line++, column, "FILENAME");
766 wattroff(center, A_BOLD);
767
768 /*
769 * since the process_files_table array could contain NULL file structures,
770 * and that the positions inside the array is important (it is the FD), we
771 * need to create a temporary array that we can sort.
772 */
773 for (i = 0; i < tmp->process_files_table->len; i++) {
774 file_tmp = g_ptr_array_index(tmp->process_files_table, i);
775 if (file_tmp)
776 g_ptr_array_add(newfilearray, file_tmp);
777 }
778
779 if (fileview[0].sort == 1)
780 g_ptr_array_sort(newfilearray, sort_by_file_fd_desc);
781 else if (fileview[1].sort == 1)
782 g_ptr_array_sort(newfilearray, sort_by_file_read_desc);
783 else if (fileview[2].sort == 1)
784 g_ptr_array_sort(newfilearray, sort_by_file_write_desc);
785 else
786 g_ptr_array_sort(newfilearray, sort_by_file_read_desc);
787
788 for (i = selected_line; i < newfilearray->len &&
789 i < (selected_line + max_center_lines - line + 2); i++) {
790 file_tmp = g_ptr_array_index(newfilearray, i);
791 if (!file_tmp)
792 continue;
793 mvwprintw(center, line + j, 1, "%d", file_tmp->fd);
794 scale_unit(file_tmp->read, unit);
795 mvwprintw(center, line + j, 11, "%s", unit);
796 scale_unit(file_tmp->write, unit);
797 mvwprintw(center, line + j, 21, "%s", unit);
798 snprintf(filename_buf, COLS - 25, "%s", file_tmp->name);
799 mvwprintw(center, line + j, 31, "%s", filename_buf);
800 j++;
801 }
802 g_ptr_array_free(newfilearray, TRUE);
803 }
804
805 void update_perf()
806 {
807 int i;
808 int nblinedisplayed = 0;
809 int current_line = 0;
810 struct processtop *tmp;
811 int header_offset = 2;
812 int perf_row = 40;
813 struct perfcounter *perfn1, *perfn2;
814 char *perf_key = NULL;
815 int value;
816 GHashTableIter iter;
817 gpointer key;
818
819 set_window_title(center, "Perf Top");
820 wattron(center, A_BOLD);
821 mvwprintw(center, 1, 1, "PID");
822 mvwprintw(center, 1, 11, "TID");
823 mvwprintw(center, 1, 22, "NAME");
824
825 perf_row = 40;
826 g_hash_table_iter_init(&iter, global_perf_liszt);
827 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
828 if (perfn1->visible) {
829 if (perfn1->sort) {
830 /* pref_current_sort = i; */
831 wattron(center, A_UNDERLINE);
832 }
833 /* + 5 to strip the "perf_" prefix */
834 mvwprintw(center, 1, perf_row, "%s",
835 (char *) key + 5);
836 wattroff(center, A_UNDERLINE);
837 perf_row += 20;
838 }
839 if (perfn1->sort) {
840 perf_key = (char *) key;
841 }
842 }
843 wattroff(center, A_BOLD);
844
845 g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key);
846
847 for (i = 0; i < data->process_table->len &&
848 nblinedisplayed < max_center_lines; i++) {
849 tmp = g_ptr_array_index(data->process_table, i);
850
851 if (toggle_filter > 0 && !lookup_filter_tid_list(tmp->tid))
852 continue;
853
854 if (tmp->pid != tmp->tid)
855 if (toggle_threads == -1)
856 continue;
857
858 if (process_selected(tmp)) {
859 if (current_line == selected_line)
860 wattron(center, COLOR_PAIR(7));
861 else
862 wattron(center, COLOR_PAIR(6));
863 }
864 if (current_line == selected_line) {
865 selected_process = tmp;
866 wattron(center, COLOR_PAIR(5));
867 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
868 }
869
870 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
871 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
872 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
873
874 g_hash_table_iter_init(&iter, global_perf_liszt);
875
876 perf_row = 40;
877 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
878 if (perfn1->visible) {
879 perfn2 = g_hash_table_lookup(tmp->perf, (char *) key);
880 if (perfn2)
881 value = perfn2->count;
882 else
883 value = 0;
884 mvwprintw(center, current_line + header_offset,
885 perf_row, "%d", value);
886 perf_row += 20;
887 }
888 }
889
890 wattroff(center, COLOR_PAIR(6));
891 wattroff(center, COLOR_PAIR(5));
892 nblinedisplayed++;
893 current_line++;
894 }
895 }
896
897 void update_iostream()
898 {
899 int i;
900 int header_offset = 2;
901 struct processtop *tmp;
902 int nblinedisplayed = 0;
903 int current_line = 0;
904 int total = 0;
905 char unit[4];
906 int column;
907
908 set_window_title(center, "IO Top");
909 wattron(center, A_BOLD);
910 mvwprintw(center, 1, 1, "PID");
911 mvwprintw(center, 1, 11, "TID");
912 mvwprintw(center, 1, 22, "NAME");
913 column = 40;
914 for (i = 0; i < 3; i++) {
915 if (iostreamtopview[i].sort) {
916 pref_current_sort = i;
917 wattron(center, A_UNDERLINE);
918 }
919 mvwprintw(center, 1, column, iostreamtopview[i].title);
920 wattroff(center, A_UNDERLINE);
921 column += 12;
922 }
923 wattroff(center, A_BOLD);
924 wattroff(center, A_UNDERLINE);
925
926 if (iostreamtopview[0].sort == 1)
927 g_ptr_array_sort(data->process_table, sort_by_process_read_desc);
928 else if (iostreamtopview[1].sort == 1)
929 g_ptr_array_sort(data->process_table, sort_by_process_write_desc);
930 else if (iostreamtopview[2].sort == 1)
931 g_ptr_array_sort(data->process_table, sort_by_process_total_desc);
932 else
933 g_ptr_array_sort(data->process_table, sort_by_process_total_desc);
934
935 for (i = list_offset; i < data->process_table->len &&
936 nblinedisplayed < max_center_lines; i++) {
937 tmp = g_ptr_array_index(data->process_table, i);
938
939 if (toggle_filter > 0 && !lookup_filter_tid_list(tmp->tid))
940 continue;
941
942 if (tmp->pid != tmp->tid)
943 if (toggle_threads == -1)
944 continue;
945
946 if (process_selected(tmp)) {
947 if (current_line == selected_line)
948 wattron(center, COLOR_PAIR(7));
949 else
950 wattron(center, COLOR_PAIR(6));
951 }
952 if (current_line == selected_line) {
953 selected_process = tmp;
954 wattron(center, COLOR_PAIR(5));
955 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
956 }
957 /* TGID */
958 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
959 /* PID */
960 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
961 /* NAME */
962 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
963
964 /* READ (bytes/sec) */
965 scale_unit(tmp->fileread, unit);
966 mvwprintw(center, current_line + header_offset, 40, "%s", unit);
967
968 /* WRITE (bytes/sec) */
969 scale_unit(tmp->filewrite, unit);
970 mvwprintw(center, current_line + header_offset, 52, "%s", unit);
971
972 /* TOTAL STREAM */
973 total = tmp->totalfileread + tmp->totalfilewrite;
974
975 scale_unit(total, unit);
976 mvwprintw(center, current_line + header_offset, 64, "%s", unit);
977
978 wattroff(center, COLOR_PAIR(6));
979 wattroff(center, COLOR_PAIR(5));
980 nblinedisplayed++;
981 current_line++;
982 }
983 }
984
985 void update_current_view()
986 {
987 sem_wait(&update_display_sem);
988 if (!data)
989 return;
990 update_header();
991
992 werase(center);
993 box(center, 0, 0);
994 switch (current_view) {
995 case cpu:
996 update_cputop_display();
997 break;
998 case perf:
999 update_perf();
1000 break;
1001 case process_details:
1002 update_process_details();
1003 break;
1004 case iostream:
1005 update_iostream();
1006 break;
1007 case tree:
1008 update_cputop_display();
1009 break;
1010 case kprobes:
1011 update_kprobes_display();
1012 break;
1013 default:
1014 break;
1015 }
1016 update_panels();
1017 doupdate();
1018 sem_post(&update_display_sem);
1019 }
1020
1021 void update_process_detail_sort(int *line_selected)
1022 {
1023 int i;
1024 int size;
1025
1026 size = 3;
1027
1028 if (*line_selected > (size - 1))
1029 *line_selected = size - 1;
1030 else if (*line_selected < 0)
1031 *line_selected = 0;
1032
1033 if (fileview[*line_selected].sort == 1)
1034 fileview[*line_selected].reverse = 1;
1035 for (i = 0; i < size; i++)
1036 fileview[i].sort = 0;
1037 fileview[*line_selected].sort = 1;
1038 }
1039
1040 void update_process_detail_pref(int *line_selected, int toggle_view, int toggle_sort)
1041 {
1042 int i;
1043 int size;
1044
1045 if (!data)
1046 return;
1047 if (pref_panel_window) {
1048 del_panel(pref_panel);
1049 delwin(pref_panel_window);
1050 }
1051 size = 3;
1052
1053 pref_panel_window = create_window(size + 2, 30, 10, 10);
1054 pref_panel = new_panel(pref_panel_window);
1055
1056 werase(pref_panel_window);
1057 box(pref_panel_window, 0 , 0);
1058 set_window_title(pref_panel_window, "Process Detail Preferences ");
1059 wattron(pref_panel_window, A_BOLD);
1060 mvwprintw(pref_panel_window, size + 1, 1,
1061 " 's' : sort, space : toggle");
1062 wattroff(pref_panel_window, A_BOLD);
1063
1064 if (*line_selected > (size - 1))
1065 *line_selected = size - 1;
1066 else if (*line_selected < 0)
1067 *line_selected = 0;
1068 if (toggle_sort == 1) {
1069 update_process_detail_sort(line_selected);
1070 update_current_view();
1071 }
1072
1073 for (i = 0; i < size; i++) {
1074 if (i == *line_selected) {
1075 wattron(pref_panel_window, COLOR_PAIR(5));
1076 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1077 }
1078 if (fileview[i].sort == 1)
1079 wattron(pref_panel_window, A_BOLD);
1080 mvwprintw(pref_panel_window, i + 1, 1, "[-] %s",
1081 fileview[i].title);
1082 wattroff(pref_panel_window, A_BOLD);
1083 wattroff(pref_panel_window, COLOR_PAIR(5));
1084
1085 }
1086 update_panels();
1087 doupdate();
1088 }
1089
1090 void update_iostream_sort(int *line_selected)
1091 {
1092 int i;
1093 int size;
1094
1095 size = 3;
1096 if (*line_selected > (size - 1))
1097 *line_selected = size - 1;
1098 else if (*line_selected < 0)
1099 *line_selected = 0;
1100 if (iostreamtopview[*line_selected].sort == 1)
1101 iostreamtopview[*line_selected].reverse = 1;
1102 for (i = 0; i < size; i++)
1103 iostreamtopview[i].sort = 0;
1104 iostreamtopview[*line_selected].sort = 1;
1105
1106 }
1107
1108 void update_iostream_pref(int *line_selected, int toggle_view, int toggle_sort)
1109 {
1110 int i;
1111 int size;
1112
1113 if (!data)
1114 return;
1115 if (pref_panel_window) {
1116 del_panel(pref_panel);
1117 delwin(pref_panel_window);
1118 }
1119 size = 3;
1120
1121 pref_panel_window = create_window(size + 2, 30, 10, 10);
1122 pref_panel = new_panel(pref_panel_window);
1123
1124 werase(pref_panel_window);
1125 box(pref_panel_window, 0 , 0);
1126 set_window_title(pref_panel_window, "IOTop Preferences ");
1127 wattron(pref_panel_window, A_BOLD);
1128 mvwprintw(pref_panel_window, size + 1, 1,
1129 " 's' : sort, space : toggle");
1130 wattroff(pref_panel_window, A_BOLD);
1131
1132 if (*line_selected > (size - 1))
1133 *line_selected = size - 1;
1134 else if (*line_selected < 0)
1135 *line_selected = 0;
1136 if (toggle_sort == 1) {
1137 update_iostream_sort(line_selected);
1138 update_current_view();
1139 }
1140
1141 for (i = 0; i < size; i++) {
1142 if (i == *line_selected) {
1143 wattron(pref_panel_window, COLOR_PAIR(5));
1144 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1145 }
1146 if (iostreamtopview[i].sort == 1)
1147 wattron(pref_panel_window, A_BOLD);
1148 mvwprintw(pref_panel_window, i + 1, 1, "[-] %s",
1149 iostreamtopview[i].title);
1150 wattroff(pref_panel_window, A_BOLD);
1151 wattroff(pref_panel_window, COLOR_PAIR(5));
1152
1153 }
1154 update_panels();
1155 doupdate();
1156 }
1157
1158 void update_cpu_sort(int *line_selected)
1159 {
1160 int i;
1161 int size = 3;
1162
1163 if (*line_selected > (size - 1))
1164 *line_selected = size - 1;
1165 else if (*line_selected < 0)
1166 *line_selected = 0;
1167
1168 /* special case, we don't support sorting by procname for now */
1169 if (*line_selected != 3) {
1170 if (cputopview[*line_selected].sort == 1)
1171 cputopview[*line_selected].reverse = 1;
1172 for (i = 0; i < size; i++)
1173 cputopview[i].sort = 0;
1174 cputopview[*line_selected].sort = 1;
1175 }
1176 }
1177
1178 void update_cpu_pref(int *line_selected, int toggle_view, int toggle_sort)
1179 {
1180 int i;
1181 int size;
1182
1183 if (!data)
1184 return;
1185 if (pref_panel_window) {
1186 del_panel(pref_panel);
1187 delwin(pref_panel_window);
1188 }
1189 size = 4;
1190
1191 pref_panel_window = create_window(size + 2, 30, 10, 10);
1192 pref_panel = new_panel(pref_panel_window);
1193
1194 werase(pref_panel_window);
1195 box(pref_panel_window, 0 , 0);
1196 set_window_title(pref_panel_window, "CPUTop Preferences ");
1197 wattron(pref_panel_window, A_BOLD);
1198 mvwprintw(pref_panel_window, size + 1, 1,
1199 " 's' : sort, space : toggle");
1200 wattroff(pref_panel_window, A_BOLD);
1201
1202 if (*line_selected > (size - 1))
1203 *line_selected = size - 1;
1204 else if (*line_selected < 0)
1205 *line_selected = 0;
1206 if (toggle_sort == 1) {
1207 update_cpu_sort(line_selected);
1208 update_current_view();
1209 }
1210
1211 for (i = 0; i < size; i++) {
1212 if (i == *line_selected) {
1213 wattron(pref_panel_window, COLOR_PAIR(5));
1214 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1215 }
1216 if (cputopview[i].sort == 1)
1217 wattron(pref_panel_window, A_BOLD);
1218 mvwprintw(pref_panel_window, i + 1, 1, "[-] %s",
1219 cputopview[i].title);
1220 wattroff(pref_panel_window, A_BOLD);
1221 wattroff(pref_panel_window, COLOR_PAIR(5));
1222
1223 }
1224 update_panels();
1225 doupdate();
1226 }
1227
1228 void update_perf_sort(int *line_selected)
1229 {
1230 int i;
1231 struct perfcounter *perf;
1232 GList *perflist;
1233 int size;
1234
1235 size = g_hash_table_size(global_perf_liszt);
1236 if (*line_selected > (size - 1))
1237 *line_selected = size - 1;
1238 else if (*line_selected < 0)
1239 *line_selected = 0;
1240
1241 i = 0;
1242 perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
1243 while (perflist) {
1244 perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
1245 if (i != *line_selected)
1246 perf->sort = 0;
1247 else
1248 perf->sort = 1;
1249 i++;
1250 perflist = g_list_next(perflist);
1251 }
1252 }
1253
1254 void update_perf_pref(int *line_selected, int toggle_view, int toggle_sort)
1255 {
1256 int i;
1257 struct perfcounter *perf;
1258 GList *perflist;
1259 int size;
1260
1261 if (!data)
1262 return;
1263 if (pref_panel_window) {
1264 del_panel(pref_panel);
1265 delwin(pref_panel_window);
1266 }
1267 size = g_hash_table_size(global_perf_liszt);
1268
1269 pref_panel_window = create_window(size + 2, 30, 10, 10);
1270 pref_panel = new_panel(pref_panel_window);
1271
1272 werase(pref_panel_window);
1273 box(pref_panel_window, 0 , 0);
1274 set_window_title(pref_panel_window, "Perf Preferences ");
1275 wattron(pref_panel_window, A_BOLD);
1276 mvwprintw(pref_panel_window, g_hash_table_size(global_perf_liszt) + 1, 1,
1277 " 's' : sort, space : toggle");
1278 wattroff(pref_panel_window, A_BOLD);
1279
1280 if (*line_selected > (size - 1))
1281 *line_selected = size - 1;
1282 else if (*line_selected < 0)
1283 *line_selected = 0;
1284
1285 if (toggle_sort == 1) {
1286 update_perf_sort(line_selected);
1287 update_current_view();
1288 }
1289
1290 i = 0;
1291 perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
1292 while (perflist) {
1293 perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
1294 if (i == *line_selected && toggle_view == 1) {
1295 perf->visible = perf->visible == 1 ? 0:1;
1296 update_current_view();
1297 }
1298 if (i == *line_selected) {
1299 wattron(pref_panel_window, COLOR_PAIR(5));
1300 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1301 }
1302 if (perf->sort == 1)
1303 wattron(pref_panel_window, A_BOLD);
1304 mvwprintw(pref_panel_window, i + 1, 1, "[%c] %s",
1305 perf->visible == 1 ? 'x' : ' ',
1306 (char *) perflist->data + 5);
1307 wattroff(pref_panel_window, A_BOLD);
1308 wattroff(pref_panel_window, COLOR_PAIR(5));
1309 i++;
1310 perflist = g_list_next(perflist);
1311 }
1312 update_panels();
1313 doupdate();
1314 }
1315
1316 void update_hostname_pref(int *line_selected, int toggle_filter, int toggle_sort)
1317 {
1318 int i;
1319 struct host *host;
1320 GList *hostlist;
1321 int size;
1322
1323 if (!data)
1324 return;
1325 if (pref_panel_window) {
1326 del_panel(pref_panel);
1327 delwin(pref_panel_window);
1328 }
1329 size = g_hash_table_size(global_host_list);
1330
1331 pref_panel_window = create_window(size + 2, 30, 10, 10);
1332 pref_panel = new_panel(pref_panel_window);
1333
1334 werase(pref_panel_window);
1335 box(pref_panel_window, 0 , 0);
1336 set_window_title(pref_panel_window, "Hosts Preferences ");
1337 wattron(pref_panel_window, A_BOLD);
1338 mvwprintw(pref_panel_window, g_hash_table_size(global_host_list) + 1, 1,
1339 " space : toggle filter");
1340 wattroff(pref_panel_window, A_BOLD);
1341
1342 if (*line_selected > (size - 1))
1343 *line_selected = size - 1;
1344 else if (*line_selected < 0)
1345 *line_selected = 0;
1346
1347 i = 0;
1348 hostlist = g_list_first(g_hash_table_get_keys(global_host_list));
1349 while (hostlist) {
1350 host = g_hash_table_lookup(global_host_list, hostlist->data);
1351 if (i == *line_selected && toggle_filter == 1) {
1352 host->filter = host->filter == 1 ? 0:1;
1353 update_hostname_filter(host);
1354 update_current_view();
1355 }
1356 if (i == *line_selected) {
1357 wattron(pref_panel_window, COLOR_PAIR(5));
1358 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1359 }
1360 if (host->filter == 1)
1361 wattron(pref_panel_window, A_BOLD);
1362 mvwprintw(pref_panel_window, i + 1, 1, "[%c] %s",
1363 host->filter == 1 ? 'x' : ' ',
1364 (char *) hostlist->data);
1365 wattroff(pref_panel_window, A_BOLD);
1366 wattroff(pref_panel_window, COLOR_PAIR(5));
1367 i++;
1368 hostlist = g_list_next(hostlist);
1369 }
1370 update_panels();
1371 doupdate();
1372 }
1373
1374 int update_preference_panel(int *line_selected, int toggle_view, int toggle_sort)
1375 {
1376 int ret = 0;
1377
1378 switch(current_view) {
1379 case perf:
1380 if (filter_host_panel)
1381 update_hostname_pref(line_selected,
1382 toggle_view, toggle_sort);
1383 else
1384 update_perf_pref(line_selected,
1385 toggle_view, toggle_sort);
1386 break;
1387 case cpu:
1388 if (filter_host_panel)
1389 update_hostname_pref(line_selected,
1390 toggle_view, toggle_sort);
1391 else
1392 update_cpu_pref(line_selected,
1393 toggle_view, toggle_sort);
1394 break;
1395 case iostream:
1396 if (filter_host_panel)
1397 update_hostname_pref(line_selected,
1398 toggle_view, toggle_sort);
1399 else
1400 update_iostream_pref(line_selected,
1401 toggle_view, toggle_sort);
1402 break;
1403 case process_details:
1404 update_process_detail_pref(line_selected,
1405 toggle_view, toggle_sort);
1406 break;
1407 default:
1408 ret = -1;
1409 break;
1410 }
1411
1412 return ret;
1413 }
1414
1415 int update_sort(int *line_selected)
1416 {
1417 int ret = 0;
1418
1419 switch(current_view) {
1420 case perf:
1421 update_perf_sort(line_selected);
1422 break;
1423 case cpu:
1424 update_cpu_sort(line_selected);
1425 break;
1426 case iostream:
1427 update_iostream_sort(line_selected);
1428 break;
1429 case process_details:
1430 update_process_detail_sort(line_selected);
1431 break;
1432 default:
1433 ret = -1;
1434 break;
1435 }
1436
1437 return ret;
1438 }
1439
1440 void toggle_pref_panel(void)
1441 {
1442 int ret;
1443
1444 if (pref_panel_visible) {
1445 hide_panel(pref_panel);
1446 pref_panel_visible = 0;
1447 } else {
1448 ret = update_preference_panel(&pref_line_selected, 0, 0);
1449 if (ret < 0)
1450 return;
1451 show_panel(pref_panel);
1452 pref_panel_visible = 1;
1453 }
1454 update_panels();
1455 doupdate();
1456 }
1457
1458 void toggle_host_panel(void)
1459 {
1460 int ret;
1461
1462 filter_host_panel = filter_host_panel ? 0 : 1;
1463 if (pref_panel_visible) {
1464 hide_panel(pref_panel);
1465 pref_panel_visible = 0;
1466 } else {
1467 ret = update_preference_panel(&pref_line_selected, 0, 0);
1468 if (ret < 0)
1469 return;
1470 show_panel(pref_panel);
1471 pref_panel_visible = 1;
1472 }
1473 update_panels();
1474 doupdate();
1475 }
1476
1477 void display(unsigned int index)
1478 {
1479 last_display_index = index;
1480 currently_displayed_index = index;
1481 data = g_ptr_array_index(copies, index);
1482 if (!data)
1483 return;
1484 max_elements = data->process_table->len;
1485 update_current_view();
1486 update_footer();
1487 update_panels();
1488 doupdate();
1489 }
1490
1491 void pause_display()
1492 {
1493 toggle_pause = 1;
1494 print_log("Pause");
1495 sem_wait(&pause_sem);
1496 }
1497
1498 void resume_display()
1499 {
1500 toggle_pause = -1;
1501 print_log("Resume");
1502 sem_post(&pause_sem);
1503 }
1504
1505 void *handle_keyboard(void *p)
1506 {
1507 int ch;
1508 while((ch = getch())) {
1509 switch(ch) {
1510 /* Move the cursor and scroll */
1511 case 'j':
1512 case KEY_DOWN:
1513 if (pref_panel_visible) {
1514 pref_line_selected++;
1515 update_preference_panel(&pref_line_selected, 0, 0);
1516 } else {
1517 if (selected_line < (max_center_lines - 1) &&
1518 selected_line < max_elements - 1) {
1519 selected_line++;
1520 selected_in_list++;
1521 } else if (selected_in_list < (max_elements - 1)
1522 && (list_offset < (max_elements - max_center_lines))) {
1523 selected_in_list++;
1524 list_offset++;
1525 }
1526 update_current_view();
1527 }
1528 break;
1529 case KEY_NPAGE:
1530 break;
1531 case 'k':
1532 case KEY_UP:
1533 if (pref_panel_visible) {
1534 if (pref_line_selected > 0)
1535 pref_line_selected--;
1536 update_preference_panel(&pref_line_selected, 0, 0);
1537 } else {
1538 if (selected_line > 0) {
1539 selected_line--;
1540 selected_in_list--;
1541 } else if (selected_in_list > 0 && list_offset > 0) {
1542 selected_in_list--;
1543 list_offset--;
1544 }
1545 update_current_view();
1546 }
1547 break;
1548 case KEY_PPAGE:
1549 break;
1550
1551 /* Navigate the history with arrows */
1552 case KEY_LEFT:
1553 if (currently_displayed_index > 0) {
1554 currently_displayed_index--;
1555 print_log("Going back in time");
1556 } else {
1557 print_log("Cannot rewind, last data is already displayed");
1558 }
1559 data = g_ptr_array_index(copies, currently_displayed_index);
1560 max_elements = data->process_table->len;
1561
1562 /* we force to pause the display when moving in time */
1563 if (toggle_pause < 0)
1564 pause_display();
1565
1566 update_current_view();
1567 update_footer();
1568 break;
1569 case KEY_RIGHT:
1570 if (currently_displayed_index < last_display_index) {
1571 currently_displayed_index++;
1572 print_log("Going forward in time");
1573 data = g_ptr_array_index(copies, currently_displayed_index);
1574 max_elements = data->process_table->len;
1575 update_current_view();
1576 update_footer();
1577 } else {
1578 print_log("Manually moving forward");
1579 sem_post(&timer);
1580 if (toggle_pause > 0) {
1581 sem_post(&pause_sem);
1582 update_current_view();
1583 sem_wait(&pause_sem);
1584 }
1585 }
1586
1587 break;
1588 case ' ':
1589 if (pref_panel_visible) {
1590 update_preference_panel(&pref_line_selected, 1, 0);
1591 } else {
1592 update_selected_processes();
1593 if (toggle_filter > 0) {
1594 max_elements = g_hash_table_size(global_filter_list);
1595 if (selected_line >= max_elements)
1596 selected_line = max_elements - 1;
1597 }
1598 update_current_view();
1599 }
1600 break;
1601 case 's':
1602 if (pref_panel_visible)
1603 update_preference_panel(&pref_line_selected, 0, 1);
1604 break;
1605 case '>':
1606 /* perf uses a hashtable, it is ordered backward */
1607 if (current_view == perf) {
1608 pref_current_sort--;
1609 } else if (!pref_panel_visible) {
1610 pref_current_sort++;
1611 }
1612 update_sort(&pref_current_sort);
1613 update_current_view();
1614 break;
1615 case '<':
1616 /* perf uses a hashtable, it is ordered backward */
1617 if (current_view == perf) {
1618 pref_current_sort++;
1619 } else if (!pref_panel_visible) {
1620 pref_current_sort--;
1621 }
1622 update_sort(&pref_current_sort);
1623 update_current_view();
1624 break;
1625
1626 case 13: /* FIXME : KEY_ENTER ?? */
1627 if (pref_panel_visible)
1628 break;
1629 if (current_view != process_details) {
1630 previous_view = current_view;
1631 current_view = process_details;
1632 } else {
1633 current_view = previous_view;
1634 previous_view = process_details;
1635 }
1636 selected_line = 0;
1637 update_current_view();
1638 break;
1639
1640 case KEY_F(1):
1641 if (pref_panel_visible)
1642 toggle_pref_panel();
1643 current_view = cpu;
1644 selected_line = 0;
1645 update_current_view();
1646 break;
1647 case KEY_F(2):
1648 if (pref_panel_visible)
1649 toggle_pref_panel();
1650 current_view = cpu;
1651 selected_line = 0;
1652 update_current_view();
1653 break;
1654 case KEY_F(3):
1655 if (pref_panel_visible)
1656 toggle_pref_panel();
1657 current_view = perf;
1658 selected_line = 0;
1659 update_current_view();
1660 break;
1661 case KEY_F(4):
1662 if (pref_panel_visible)
1663 toggle_pref_panel();
1664 current_view = iostream;
1665 selected_line = 0;
1666 update_current_view();
1667 break;
1668 case KEY_F(5):
1669 if (pref_panel_visible)
1670 toggle_pref_panel();
1671 current_view = kprobes;
1672 selected_line = 0;
1673 update_current_view();
1674 break;
1675 case KEY_F(10):
1676 case 'q':
1677 reset_ncurses();
1678 /* exit keyboard thread */
1679 pthread_exit(0);
1680 break;
1681 case 'f':
1682 toggle_filter *= -1;
1683 selected_line = 0;
1684 if (toggle_filter > 0)
1685 max_elements = g_hash_table_size(global_filter_list);
1686 else
1687 max_elements = data->process_table->len;
1688 update_current_view();
1689 break;
1690 case 'h':
1691 toggle_host_panel();
1692 break;
1693 case 't':
1694 toggle_threads *= -1;
1695 update_current_view();
1696 break;
1697 case 'p':
1698 if (toggle_pause < 0) {
1699 pause_display();
1700 } else {
1701 resume_display();
1702 }
1703 break;
1704 case 'r':
1705 toggle_pref_panel();
1706 break;
1707 case 'v':
1708 toggle_virt *= -1;
1709 update_current_view();
1710 break;
1711 /* ESCAPE, but slow to process, don't know why */
1712 case 27:
1713 if (pref_panel_visible)
1714 toggle_pref_panel();
1715 else if (current_view == process_details) {
1716 current_view = previous_view;
1717 previous_view = process_details;
1718 }
1719 update_current_view();
1720 break;
1721 default:
1722 if (data)
1723 update_current_view();
1724 break;
1725 }
1726 update_footer();
1727 }
1728 return NULL;
1729 }
1730
1731 void init_view_headers()
1732 {
1733 cputopview[0].title = strdup("CPU(%)");
1734 cputopview[0].sort = 1;
1735 cputopview[1].title = strdup("PID");
1736 cputopview[2].title = strdup("TID");
1737 cputopview[3].title = strdup("VPID");
1738 cputopview[4].title = strdup("VTID");
1739 cputopview[5].title = strdup("NAME");
1740
1741 iostreamtopview[0].title = strdup("R (B/sec)");
1742 iostreamtopview[1].title = strdup("W (B/sec)");
1743 iostreamtopview[2].title = strdup("Total (B)");
1744 iostreamtopview[2].sort = 1;
1745
1746 fileview[0].title = strdup("FD");
1747 fileview[1].title = strdup("READ");
1748 fileview[1].sort = 1;
1749 fileview[2].title = strdup("WRITE");
1750
1751 kprobeview[0].title = strdup("NAME");
1752 kprobeview[1].title = strdup("HIT");
1753 kprobeview[1].sort = 1;
1754 }
1755
1756 void init_ncurses()
1757 {
1758 sem_init(&update_display_sem, 0, 1);
1759 init_view_headers();
1760 init_screen();
1761
1762 header = create_window(5, COLS - 1, 0, 0);
1763 center = create_window(LINES - 5 - 7, COLS - 1, 5, 0);
1764 status = create_window(MAX_LOG_LINES + 2, COLS - 1, LINES - 7, 0);
1765 footer = create_window(1, COLS - 1, LINES - 1, 0);
1766
1767 print_log("Starting display");
1768
1769 main_panel = new_panel(center);
1770
1771 current_view = cpu;
1772
1773 basic_header();
1774 update_footer();
1775
1776 pthread_create(&keyboard_thread, NULL, handle_keyboard, (void *)NULL);
1777 }
1778
1779 #endif /* HAVE_LIBNCURSES */
This page took 0.111744 seconds and 4 git commands to generate.