Initial import of code.
[lttngtop.git] / lttngtop / cursesdisplay.c
CommitLineData
1fc22eb4
JD
1/*
2 * Copyright (C) 2011 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
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <signal.h>
22#include <string.h>
23#include <ncurses.h>
24#include <panel.h>
25#include <pthread.h>
26#include <semaphore.h>
27
28#include "cursesdisplay.h"
29#include "lttngtoptypes.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 */
37sem_t update_display_sem;
38
39char *termtype;
40WINDOW *footer, *header, *center, *status;
41WINDOW *perf_panel_window = NULL;
42PANEL *perf_panel, *main_panel;
43
44int perf_panel_visible = 0;
45int perf_line_selected = 0;
46
47int last_display_index, currently_displayed_index;
48
49struct processtop *selected_process = NULL;
50int selected_tid;
51char *selected_comm;
52int selected_ret;
53
54int selected_line = 0; /* select bar position */
55int selected_in_list = 0; /* selection relative to the whole list */
56int list_offset = 0; /* first index in the list to display (scroll) */
57int nb_log_lines = 0;
58char log_lines[MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES];
59
60int max_elements = 80;
61
62int toggle_threads = -1;
63int toggle_pause = -1;
64int toggle_tree = -1;
65
66int max_center_lines;
67
68pthread_t keyboard_thread;
69
70void reset_ncurses()
71{
72 curs_set(1);
73 endwin();
74 exit(0);
75}
76
77static void handle_sigterm(int signal)
78{
79 reset_ncurses();
80}
81
82void init_screen()
83{
84 initscr();
85 noecho();
86 halfdelay(DEFAULT_DELAY);
87 nonl();
88 intrflush(stdscr, false);
89 keypad(stdscr, true);
90 curs_set(0);
91
92 if (has_colors()) {
93 start_color();
94 init_pair(1, COLOR_RED, COLOR_BLACK); /* - */
95 init_pair(2, COLOR_GREEN, COLOR_BLACK); /* + */
96 init_pair(3, COLOR_BLACK, COLOR_WHITE); /* keys */
97 init_pair(4, COLOR_WHITE, COLOR_GREEN); /* keys activated */
98 init_pair(5, COLOR_WHITE, COLOR_BLUE); /* select line */
99 }
100 termtype = getenv("TERM");
101 if (!strcmp(termtype, "xterm") || !strcmp(termtype, "xterm-color") ||
102 !strcmp(termtype, "vt220")) {
103 define_key("\033[H", KEY_HOME);
104 define_key("\033[F", KEY_END);
105 define_key("\033OP", KEY_F(1));
106 define_key("\033OQ", KEY_F(2));
107 define_key("\033OR", KEY_F(3));
108 define_key("\033OS", KEY_F(4));
109 define_key("\0330U", KEY_F(6));
110 define_key("\033[11~", KEY_F(1));
111 define_key("\033[12~", KEY_F(2));
112 define_key("\033[13~", KEY_F(3));
113 define_key("\033[14~", KEY_F(4));
114 define_key("\033[16~", KEY_F(6));
115 define_key("\033[17;2~", KEY_F(18));
116 }
117 signal(SIGTERM, handle_sigterm);
118 mousemask(BUTTON1_CLICKED, NULL);
119 refresh();
120}
121
122WINDOW *create_window(int height, int width, int startx, int starty)
123{
124 WINDOW *win;
125 win = newwin(height, width, startx, starty);
126 box(win, 0 , 0);
127 wrefresh(win);
128 return win;
129}
130
131WINDOW *create_window_no_border(int height, int width, int startx, int starty)
132{
133 WINDOW *win;
134 win = newwin(height, width, startx, starty);
135 wrefresh(win);
136 return win;
137}
138
139void print_digit(WINDOW *win, int digit)
140{
141 if (digit < 0) {
142 wattron(win, COLOR_PAIR(1));
143 wprintw(win, "%d", digit);
144 wattroff(win, COLOR_PAIR(1));
145 } else if (digit > 0) {
146 wattron(win, COLOR_PAIR(2));
147 wprintw(win, "+%d", digit);
148 wattroff(win, COLOR_PAIR(2));
149 } else {
150 wprintw(win, "0");
151 }
152}
153
154void print_digits(WINDOW *win, int first, int second)
155{
156 wprintw(win, "(");
157 print_digit(win, first);
158 wprintw(win, ", ");
159 print_digit(win, second);
160 wprintw(win, ")");
161}
162
163void print_headers(int line, char *desc, int value, int first, int second)
164{
165 wattron(header, A_BOLD);
166 mvwprintw(header, line, 4, "%s", desc);
167 wattroff(header, A_BOLD);
168 mvwprintw(header, line, 16, "N/A", value);
169 wmove(header, line, 24);
170 print_digits(header, first, second);
171 wmove(header, line, 40);
172}
173
174void set_window_title(WINDOW *win, char *title)
175{
176 wattron(win, A_BOLD);
177 mvwprintw(win, 0, 1, title);
178 wattroff(win, A_BOLD);
179}
180
181void print_log(char *str)
182{
183 int i;
184 int current_line = 1;
185 int current_char = 1;
186 char *tmp, *tmp2;
187 /* rotate the line buffer */
188 if (nb_log_lines >= MAX_LOG_LINES) {
189 tmp = strndup(log_lines, MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES);
190 tmp2 = strchr(tmp, '\n');
191 memset(log_lines, '\0', strlen(log_lines));
192 strncat(log_lines, tmp2 + 1, strlen(tmp2) - 1);
193 log_lines[strlen(log_lines)] = '\n';
194 log_lines[strlen(log_lines)] = '\0';
195 free(tmp);
196 }
197 nb_log_lines++;
198
199 strncat(log_lines, str, MAX_LINE_LENGTH - 1);
200
201 if (nb_log_lines < MAX_LOG_LINES)
202 log_lines[strlen(log_lines)] = '\n';
203 log_lines[strlen(log_lines)] = '\0';
204
205 werase(status);
206 box(status, 0 , 0);
207 set_window_title(status, "Status");
208 for (i = 0; i < strlen(log_lines); i++) {
209 if (log_lines[i] == '\n') {
210 wmove(status, ++current_line, 1);
211 current_char = 1;
212 } else {
213 mvwprintw(status, current_line, current_char++, "%c", log_lines[i]);
214 }
215 }
216 wrefresh(status);
217}
218
219void print_key(WINDOW *win, char *key, char *desc, int toggle)
220{
221 int pair;
222 if (toggle > 0)
223 pair = 4;
224 else
225 pair = 3;
226 wattron(win, COLOR_PAIR(pair));
227 wprintw(footer, "%s", key);
228 wattroff(win, COLOR_PAIR(pair));
229 wprintw(footer, ":%s", desc);
230}
231
232void update_footer()
233{
234 sem_wait(&update_display_sem);
235 werase(footer);
236 wmove(footer, 1, 1);
237 print_key(footer, "F2", "CPUtop ", current_view == cpu);
238 print_key(footer, "F3", "PerfTop ", current_view == perf);
239 print_key(footer, "F6", "IOTop ", current_view == iostream);
240 print_key(footer, "Enter", "Details ", current_view == process_details);
241 print_key(footer, "q", "Quit | ", 0);
242 print_key(footer, "P", "Perf Pref ", 0);
243 print_key(footer, "p", "Pause ", toggle_pause);
244
245 wrefresh(footer);
246 sem_post(&update_display_sem);
247}
248
249void basic_header()
250{
251 werase(header);
252 box(header, 0 , 0);
253 set_window_title(header, "Statistics for interval [gathering data...[");
254 wattron(header, A_BOLD);
255 mvwprintw(header, 1, 4, "CPUs");
256 mvwprintw(header, 2, 4, "Processes");
257 mvwprintw(header, 3, 4, "Threads");
258 mvwprintw(header, 4, 4, "Files");
259 mvwprintw(header, 5, 4, "Network");
260 mvwprintw(header, 6, 4, "IO");
261 wattroff(header, A_BOLD);
262 wrefresh(header);
263}
264
265void update_header()
266{
267 werase(header);
268 box(header, 0 , 0);
269 set_window_title(header, "Statistics for interval ");
270 wattron(header, A_BOLD);
271 /*
272 wprintw(header, "[%lu.%lu, %lu.%lu[",
273 data->start.tv_sec, data->start.tv_nsec,
274 data->end.tv_sec, data->end.tv_nsec);
275 */
276 wprintw(header, "[%lu, %lu[",
277 data->start,
278 data->end);
279 mvwprintw(header, 1, 4, "CPUs");
280 wattroff(header, A_BOLD);
281 wprintw(header, "\t%d\t(max/cpu : %0.2f%)", data->cpu_table->len,
282 100.0/data->cpu_table->len);
283 print_headers(2, "Processes", data->nbproc, data->nbnewproc,
284 -1*(data->nbdeadproc));
285 print_headers(3, "Threads", data->nbthreads, data->nbnewthreads,
286 -1*(data->nbdeadthreads));
287 print_headers(4, "Files", data->nbfiles, data->nbnewfiles,
288 -1*(data->nbclosedfiles));
289 mvwprintw(header, 4, 43, "N/A kbytes/sec");
290 print_headers(5, "Network", 114, 0, 0);
291 mvwprintw(header, 5, 43, "N/A Mbytes/sec");
292 wrefresh(header);
293}
294
295gint sort_by_cpu_desc(gconstpointer p1, gconstpointer p2)
296{
297 struct processtop *n1 = *(struct processtop **)p1;
298 struct processtop *n2 = *(struct processtop **)p2;
299 unsigned long totaln1 = n1->totalcpunsec;
300 unsigned long totaln2 = n2->totalcpunsec;
301
302 if (totaln1 < totaln2)
303 return 1;
304 if (totaln1 == totaln2)
305 return 0;
306 return -1;
307}
308
309gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2)
310{
311 struct processtop *n1 = *(struct processtop **)p1;
312 struct processtop *n2 = *(struct processtop **)p2;
313 unsigned long totaln1 = n1->threadstotalcpunsec;
314 unsigned long totaln2 = n2->threadstotalcpunsec;
315
316 if (totaln1 < totaln2)
317 return 1;
318 if (totaln1 == totaln2)
319 return 0;
320 return -1;
321}
322
323void update_cputop_display()
324{
325 int i;
326 int header_offset = 2;
327 struct processtop *tmp;
328 unsigned long elapsed;
329 double maxcputime;
330 int nblinedisplayed = 0;
331 int current_line = 0;
332
333 elapsed = data->end - data->start;
334 maxcputime = elapsed * data->cpu_table->len / 100.0;
335
336 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
337
338 set_window_title(center, "CPU Top");
339 wattron(center, A_BOLD);
340 mvwprintw(center, 1, 1, "CPU(%)");
341 mvwprintw(center, 1, 12, "TGID");
342 mvwprintw(center, 1, 22, "PID");
343 mvwprintw(center, 1, 32, "NAME");
344 wattroff(center, A_BOLD);
345
346 max_center_lines = LINES - 7 - 7 - 1 - header_offset;
347
348 /* iterate the process (thread) list */
349 for (i = list_offset; i < data->process_table->len &&
350 nblinedisplayed < max_center_lines; i++) {
351 tmp = g_ptr_array_index(data->process_table, i);
352
353 if (current_line == selected_line) {
354 selected_process = tmp;
355 selected_tid = tmp->tid;
356 selected_comm = tmp->comm;
357 wattron(center, COLOR_PAIR(5));
358 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
359 }
360 /* CPU(%) */
361 mvwprintw(center, current_line + header_offset, 1, "%1.2f",
362 tmp->totalcpunsec / maxcputime);
363 /* TGID */
364 mvwprintw(center, current_line + header_offset, 12, "%d", tmp->pid);
365 /* PID */
366 mvwprintw(center, current_line + header_offset, 22, "%d", tmp->tid);
367 /* NAME */
368 mvwprintw(center, current_line + header_offset, 32, "%s", tmp->comm);
369 wattroff(center, COLOR_PAIR(5));
370 nblinedisplayed++;
371 current_line++;
372 }
373}
374
375gint sort_perf(gconstpointer p1, gconstpointer p2, gpointer key)
376{
377 struct processtop *n1 = *(struct processtop **) p1;
378 struct processtop *n2 = *(struct processtop **) p2;
379
380 struct perfcounter *tmp1, *tmp2;
381 unsigned long totaln2 = 0;
382 unsigned long totaln1 = 0;
383
384 if (!key)
385 return 0;
386
387 tmp1 = g_hash_table_lookup(n1->perf, key);
388 if (!tmp1)
389 totaln1 = 0;
390 else
391 totaln1 = tmp1->count;
392
393 tmp2 = g_hash_table_lookup(n2->perf, key);
394 if (!tmp2)
395 totaln2 = 0;
396 else
397 totaln2 = tmp2->count;
398
399 if (totaln1 < totaln2)
400 return 1;
401 if (totaln1 == totaln2) {
402 totaln1 = n1->tid;
403 totaln2 = n2->tid;
404 if (totaln1 < totaln2)
405 return 1;
406 return -1;
407 }
408 return -1;
409}
410
411void print_key_title(char *key, int line)
412{
413 wattron(center, A_BOLD);
414 mvwprintw(center, line, 1, "%s\t", key);
415 wattroff(center, A_BOLD);
416}
417
418void update_process_details()
419{
420 unsigned long elapsed;
421 double maxcputime;
422 struct processtop *tmp = find_process_tid(data, selected_tid, selected_comm);
423
424 set_window_title(center, "Process details");
425
426
427 elapsed = data->end - data->start;
428 maxcputime = elapsed * data->cpu_table->len / 100.0;
429
430 print_key_title("Name", 1);
431 wprintw(center, "%s", selected_comm);
432 print_key_title("TID", 2);
433 wprintw(center, "%d", selected_tid);
434 if (!tmp) {
435 print_key_title("Does not exit at this time", 3);
436 return;
437 }
438
439 print_key_title("PID", 3);
440 wprintw(center, "%d", tmp->pid);
441 print_key_title("PPID", 4);
442 wprintw(center, "%d", tmp->ppid);
443 print_key_title("CPU", 5);
444 wprintw(center, "%1.2f %%", tmp->totalcpunsec/maxcputime);
445}
446
447void update_perf()
448{
449 int i, j;
450 int nblinedisplayed = 0;
451 int current_line = 0;
452 struct processtop *tmp;
453 int header_offset = 2;
454 int perf_row = 40;
455 struct perfcounter *perfn1, *perfn2;
456 GList *perflist;
457 char *perf_key = NULL;
458 int value;
459
460 set_window_title(center, "Perf Top");
461 wattron(center, A_BOLD);
462 mvwprintw(center, 1, 1, "PID");
463 mvwprintw(center, 1, 11, "TID");
464 mvwprintw(center, 1, 22, "NAME");
465
466 perf_row = 40;
467 perflist = g_list_first(g_hash_table_get_keys(data->perf_list));
468 while (perflist) {
469 perfn1 = g_hash_table_lookup(data->perf_list, perflist->data);
470 /* + 6 to strip the "_perf_" prefix */
471 if (perfn1->visible) {
472 mvwprintw(center, 1, perf_row, "%s",
473 (char *) perflist->data + 6);
474 perf_row += 20;
475 }
476 if (perfn1->sort) {
477 perf_key = (char *) perflist->data;
478 }
479 perflist = g_list_next(perflist);
480 }
481 wattroff(center, A_BOLD);
482
483 g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key);
484 for (i = 0; i < data->process_table->len &&
485 nblinedisplayed < max_center_lines; i++) {
486 GList *perf_keys;
487 tmp = g_ptr_array_index(data->process_table, i);
488
489 if (current_line == selected_line) {
490 selected_process = tmp;
491 wattron(center, COLOR_PAIR(5));
492 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
493 }
494
495 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
496 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
497 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
498
499 /* FIXME : sometimes there is a segfault here, I have no idea why :-( */
500 perf_keys = g_hash_table_get_keys(data->perf_list);
501 if (perf_keys)
502 perflist = g_list_first(perf_keys);
503 else
504 perflist = NULL;
505
506 perf_row = 40;
507 j = 0;
508 while (perflist) {
509 j++;
510 perfn1 = g_hash_table_lookup(data->perf_list, perflist->data);
511 if (!perfn1) {
512 perflist = g_list_next(perflist);
513 continue;
514 }
515 if (perfn1->visible) {
516 perfn2 = g_hash_table_lookup(tmp->perf, perflist->data);
517 if (perfn2)
518 value = perfn2->count;
519 else
520 value = 0;
521 mvwprintw(center, current_line + header_offset, perf_row, "%d", value);
522 perf_row += 20;
523 }
524 perflist = g_list_next(perflist);
525 }
526
527 wattroff(center, COLOR_PAIR(5));
528 nblinedisplayed++;
529 current_line++;
530 }
531}
532
533void update_fileio()
534{
535 int i;
536 int offset;
537
538 set_window_title(center, "IO Top");
539 wattron(center, A_BOLD);
540 mvwprintw(center, 1, 10, "READ");
541 mvwprintw(center, 2, 1, "bytes");
542 mvwprintw(center, 2, 15, "bytes/sec");
543
544 mvwprintw(center, 1, 39, "WRITE");
545 mvwprintw(center, 2, 33, "bytes");
546 mvwprintw(center, 2, 45, "bytes/sec");
547
548 if (toggle_threads > 0) {
549 mvwprintw(center, 1, 60, "TGID");
550 mvwprintw(center, 1, 70, "PID");
551 offset = 8;
552 } else {
553 mvwprintw(center, 1, 60, "PID(TGID)");
554 offset = 0;
555 }
556 mvwprintw(center, 1, 72 + offset, "NAME");
557 wattroff(center, A_BOLD);
558
559 for (i = 3; i < LINES - 3 - 8 - 1; i++) {
560 mvwprintw(center, i, 1, "%d", i*1000);
561 mvwprintw(center, i, 15, "%dk", i);
562 mvwprintw(center, i, 28, "| %d", i*2000);
563 mvwprintw(center, i, 45, "%dk", i*2);
564 if (toggle_threads > 0) {
565 mvwprintw(center, i, 57, "| %d", i);
566 mvwprintw(center, i, 70, "%d", i);
567 } else {
568 mvwprintw(center, i, 57, "| %d", i);
569 }
570 mvwprintw(center, i, 72 + offset, "process_%d", i);
571 }
572}
573
574gint sort_by_ret_desc(gconstpointer p1, gconstpointer p2)
575{
576 struct processtop *n1 = *(struct processtop **)p1;
577 struct processtop *n2 = *(struct processtop **)p2;
578 unsigned long totaln1 = n1->iostream->ret_total;
579 unsigned long totaln2 = n2->iostream->ret_total;
580
581 if (totaln1 < totaln2)
582 return 1;
583 if (totaln1 == totaln2)
584 return 0;
585 return -1;
586}
587
588void update_iostream()
589{
590 int i;
591 int header_offset = 2;
592 struct processtop *tmp;
593 int nblinedisplayed = 0;
594 int current_line = 0;
595
596 set_window_title(center, "IO Top");
597 wattron(center, A_BOLD);
598 mvwprintw(center, 1, 1, "READ (B/s)");
599 mvwprintw(center, 1, 20, "WRITE (B/s)");
600
601 mvwprintw(center, 1, 40, "TOTAL STREAM");
602
603 mvwprintw(center, 1, 60, "TGID");
604 mvwprintw(center, 1, 80, "PID");
605
606 mvwprintw(center, 1, 92, "NAME");
607 wattroff(center, A_BOLD);
608
609 g_ptr_array_sort(data->process_table, sort_by_ret_desc);
610
611 for (i = list_offset; i < data->process_table->len &&
612 nblinedisplayed < max_center_lines; i++) {
613 tmp = g_ptr_array_index(data->process_table, i);
614
615 if (current_line == selected_line) {
616 selected_process = tmp;
617 selected_tid = tmp->tid;
618 selected_comm = tmp->comm;
619 wattron(center, COLOR_PAIR(5));
620 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
621 }
622 /* READ (bytes/sec) */
623 mvwprintw(center, current_line + header_offset, 1, "%lu",
624 tmp->iostream->ret_read);
625
626 /* WRITE (bytes/sec) */
627 mvwprintw(center, current_line + header_offset, 20, "%lu",
628 tmp->iostream->ret_write);
629
630 /* TOTAL STREAM */
631 if(tmp->iostream->ret_total >= 1000000)
632 mvwprintw(center, current_line + header_offset, 40, "%lu MB",
633 tmp->iostream->ret_total/1000000);
634 else if(tmp->iostream->ret_total >=1000)
635 mvwprintw(center, current_line + header_offset, 40, "%lu KB",
636 tmp->iostream->ret_total/1000);
637 else
638 mvwprintw(center, current_line + header_offset, 40, "%lu B",
639 tmp->iostream->ret_total);
640 /* TGID */
641 mvwprintw(center, current_line + header_offset, 60, "%d", tmp->pid);
642 /* PID */
643 mvwprintw(center, current_line + header_offset, 80, "%d", tmp->tid);
644 /* NAME */
645 mvwprintw(center, current_line + header_offset, 92, "%s", tmp->comm);
646 wattroff(center, COLOR_PAIR(5));
647 nblinedisplayed++;
648 current_line++;
649 }
650}
651
652void update_current_view()
653{
654 sem_wait(&update_display_sem);
655 if (!data)
656 return;
657 update_header();
658
659 werase(center);
660 box(center, 0, 0);
661 switch (current_view) {
662 case cpu:
663 update_cputop_display();
664 break;
665 case perf:
666 update_perf();
667 break;
668 case process_details:
669 update_process_details();
670 break;
671 case fileio:
672 update_fileio();
673 break;
674 case iostream:
675 update_iostream();
676 break;
677 case tree:
678 update_cputop_display();
679 break;
680 default:
681 break;
682 }
683 update_panels();
684 doupdate();
685 sem_post(&update_display_sem);
686}
687
688void setup_perf_panel()
689{
690 int size;
691 if (!data)
692 return;
693 if (perf_panel_window) {
694 del_panel(perf_panel);
695 delwin(perf_panel_window);
696 }
697 size = g_hash_table_size(data->perf_list);
698 perf_panel_window = create_window(size + 2, 30, 10, 10);
699 perf_panel = new_panel(perf_panel_window);
700 perf_panel_visible = 0;
701 hide_panel(perf_panel);
702}
703
704void update_perf_panel(int line_selected, int toggle_view, int toggle_sort)
705{
706 int i;
707 struct perfcounter *perf;
708 GList *perflist;
709
710 if (!data)
711 return;
712
713 werase(perf_panel_window);
714 box(perf_panel_window, 0 , 0);
715 set_window_title(perf_panel_window, "Perf Preferences ");
716 wattron(perf_panel_window, A_BOLD);
717 mvwprintw(perf_panel_window, g_hash_table_size(data->perf_list) + 1, 1, " 's' to sort");
718 wattroff(perf_panel_window, A_BOLD);
719
720 if (toggle_sort == 1) {
721 i = 0;
722 perflist = g_list_first(g_hash_table_get_keys(data->perf_list));
723 while (perflist) {
724 perf = g_hash_table_lookup(data->perf_list, perflist->data);
725 if (i != line_selected)
726 perf->sort = 0;
727 else
728 perf->sort = 1;
729 i++;
730 perflist = g_list_next(perflist);
731 }
732 update_current_view();
733 }
734
735 i = 0;
736 perflist = g_list_first(g_hash_table_get_keys(data->perf_list));
737 while (perflist) {
738 perf = g_hash_table_lookup(data->perf_list, perflist->data);
739 if (i == line_selected && toggle_view == 1) {
740 perf->visible = perf->visible == 1 ? 0:1;
741 update_current_view();
742 }
743 if (i == line_selected) {
744 wattron(perf_panel_window, COLOR_PAIR(5));
745 mvwhline(perf_panel_window, i + 1, 1, ' ', 30 - 2);
746 }
747 if (perf->sort == 1)
748 wattron(perf_panel_window, A_BOLD);
749 mvwprintw(perf_panel_window, i + 1, 1, "[%c] %s",
750 perf->visible == 1 ? 'x' : ' ',
751 (char *) perflist->data + 6);
752 wattroff(perf_panel_window, A_BOLD);
753 wattroff(perf_panel_window, COLOR_PAIR(5));
754 i++;
755 perflist = g_list_next(perflist);
756 }
757 update_panels();
758 doupdate();
759}
760
761
762void toggle_perf_panel(void)
763{
764 if (perf_panel_visible) {
765 hide_panel(perf_panel);
766 perf_panel_visible = 0;
767 } else {
768 setup_perf_panel();
769 update_perf_panel(perf_line_selected, 0, 0);
770 show_panel(perf_panel);
771 perf_panel_visible = 1;
772 }
773 update_panels();
774 doupdate();
775}
776
777void display(unsigned int index)
778{
779 last_display_index = index;
780 currently_displayed_index = index;
781 data = g_ptr_array_index(copies, index);
782 if (!data)
783 return;
784 max_elements = data->process_table->len;
785 update_current_view();
786 update_footer();
787 update_panels();
788 doupdate();
789}
790
791void pause_display()
792{
793 toggle_pause = 1;
794 print_log("Pause");
795 sem_wait(&pause_sem);
796}
797
798void resume_display()
799{
800 toggle_pause = -1;
801 print_log("Resume");
802 sem_post(&pause_sem);
803}
804
805void *handle_keyboard(void *p)
806{
807 int ch;
808 while((ch = getch())) {
809 switch(ch) {
810 /* Move the cursor and scroll */
811 case KEY_DOWN:
812 if (perf_panel_visible) {
813 if (perf_line_selected < g_hash_table_size(data->perf_list) - 1)
814 perf_line_selected++;
815 update_perf_panel(perf_line_selected, 0, 0);
816 } else {
817 if (selected_line < (max_center_lines - 1) &&
818 selected_line < max_elements - 1) {
819 selected_line++;
820 selected_in_list++;
821 } else if (selected_in_list < (max_elements - 1)
822 && (list_offset < (max_elements - max_center_lines))) {
823 selected_in_list++;
824 list_offset++;
825 }
826 update_current_view();
827 }
828 break;
829 case KEY_NPAGE:
830 if ((selected_line + 10 < max_center_lines - 1) &&
831 ((selected_line + 10) < max_elements - 1)) {
832 selected_line += 10;
833 selected_in_list += 10;
834 } else if (max_elements > max_center_lines) {
835 selected_line = max_center_lines - 1;
836 if (selected_in_list + 10 < max_elements - 1) {
837 selected_in_list += 10;
838 list_offset += (selected_in_list - max_center_lines + 1);
839 }
840 } else if (selected_line + 10 > max_elements) {
841 selected_line = max_elements - 1;
842 }
843 update_current_view();
844 break;
845 case KEY_UP:
846 if (perf_panel_visible) {
847 if (perf_line_selected > 0)
848 perf_line_selected--;
849 update_perf_panel(perf_line_selected, 0, 0);
850 } else {
851 if (selected_line > 0) {
852 selected_line--;
853 selected_in_list--;
854 } else if (selected_in_list > 0 && list_offset > 0) {
855 selected_in_list--;
856 list_offset--;
857 }
858 update_current_view();
859 }
860 break;
861 case KEY_PPAGE:
862 if (selected_line - 10 > 0)
863 selected_line -= 10;
864 else
865 selected_line = 0;
866 update_current_view();
867 break;
868
869 /* Navigate the history with arrows */
870 case KEY_LEFT:
871 if (currently_displayed_index > 0) {
872 currently_displayed_index--;
873 print_log("Going back in time");
874 } else {
875 print_log("Cannot rewind, last data is already displayed");
876 }
877 data = g_ptr_array_index(copies, currently_displayed_index);
878 max_elements = data->process_table->len;
879
880 /* we force to pause the display when moving in time */
881 if (toggle_pause < 0)
882 pause_display();
883
884 update_current_view();
885 update_footer();
886 break;
887 case KEY_RIGHT:
888 if (currently_displayed_index < last_display_index) {
889 currently_displayed_index++;
890 print_log("Going forward in time");
891 data = g_ptr_array_index(copies, currently_displayed_index);
892 max_elements = data->process_table->len;
893 update_current_view();
894 update_footer();
895 } else {
896 print_log("Manually moving forward");
897 sem_post(&timer);
898 /* we force to resume the refresh when moving forward */
899 if (toggle_pause > 0)
900 resume_display();
901 }
902
903 break;
904 case ' ':
905 if (perf_panel_visible)
906 update_perf_panel(perf_line_selected, 1, 0);
907 break;
908 case 's':
909 if (perf_panel_visible)
910 update_perf_panel(perf_line_selected, 0, 1);
911 break;
912
913 case 13: /* FIXME : KEY_ENTER ?? */
914 if (current_view == cpu) {
915 current_view = process_details;
916 }
917 update_current_view();
918 break;
919
920 case KEY_F(1):
921 toggle_tree *= -1;
922 current_view = cpu;
923 update_current_view();
924 break;
925 case KEY_F(2):
926 current_view = cpu;
927 update_current_view();
928 break;
929 case KEY_F(3):
930 current_view = perf;
931 toggle_tree = -1;
932 update_current_view();
933 break;
934 case KEY_F(4):
935 current_view = fileio;
936 toggle_tree = -1;
937 update_current_view();
938 break;
939 case KEY_F(5):
940 current_view = netio;
941 toggle_tree = -1;
942 update_current_view();
943 break;
944 case KEY_F(6):
945 current_view = iostream;
946 toggle_tree = -1;
947 update_current_view();
948 break;
949 case KEY_F(10):
950 case 'q':
951 reset_ncurses();
952 break;
953 case 't':
954 toggle_threads *= -1;
955 update_current_view();
956 break;
957 case 'p':
958 if (toggle_pause < 0) {
959 pause_display();
960 } else {
961 resume_display();
962 }
963 break;
964 case 'P':
965 toggle_perf_panel();
966 break;
967 default:
968 /*
969 * commented because it makes the list refresh in different order
970 * if we sort and there are equal values
971 if (data)
972 update_current_view();
973 */
974 break;
975 }
976 update_footer();
977 }
978 return NULL;
979}
980
981void init_ncurses()
982{
983 sem_init(&update_display_sem, 0, 1);
984 init_screen();
985
986 header = create_window(7, COLS - 1, 0, 0);
987 center = create_window(LINES - 7 - 7, COLS - 1, 7, 0);
988 status = create_window(MAX_LOG_LINES + 2, COLS - 1, LINES - 7, 0);
989 footer = create_window(1, COLS - 1, LINES - 1, 0);
990
991 print_log("Starting display");
992
993 main_panel = new_panel(center);
994 setup_perf_panel();
995
996 current_view = cpu;
997
998 basic_header();
999 update_footer();
1000
1001 pthread_create(&keyboard_thread, NULL, handle_keyboard, (void *)NULL);
1002}
1003
This page took 0.095087 seconds and 4 git commands to generate.