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