d6befea5be042fe34f7d9647ed7b61c9eca94085
[lttngtop.git] / src / cursesdisplay.c
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 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 *perf_panel_window = NULL;
42 PANEL *perf_panel, *main_panel;
43
44 int perf_panel_visible = 0;
45 int perf_line_selected = 0;
46
47 int last_display_index, currently_displayed_index;
48
49 struct processtop *selected_process = NULL;
50 int selected_tid;
51 char *selected_comm;
52 int selected_ret;
53
54 int selected_line = 0; /* select bar position */
55 int selected_in_list = 0; /* selection relative to the whole list */
56 int list_offset = 0; /* first index in the list to display (scroll) */
57 int nb_log_lines = 0;
58 char log_lines[MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES];
59
60 int max_elements = 80;
61
62 int toggle_threads = -1;
63 int toggle_pause = -1;
64 int toggle_tree = -1;
65
66 int max_center_lines;
67
68 pthread_t keyboard_thread;
69
70 void reset_ncurses()
71 {
72 curs_set(1);
73 endwin();
74 exit(0);
75 }
76
77 static void handle_sigterm(int signal)
78 {
79 reset_ncurses();
80 }
81
82 void 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
122 WINDOW *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
131 WINDOW *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
139 void 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
154 void 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
163 void 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
174 void 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
181 void 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",
214 log_lines[i]);
215 }
216 }
217 wrefresh(status);
218 }
219
220 void print_key(WINDOW *win, char *key, char *desc, int toggle)
221 {
222 int pair;
223 if (toggle > 0)
224 pair = 4;
225 else
226 pair = 3;
227 wattron(win, COLOR_PAIR(pair));
228 wprintw(footer, "%s", key);
229 wattroff(win, COLOR_PAIR(pair));
230 wprintw(footer, ":%s", desc);
231 }
232
233 void update_footer()
234 {
235 sem_wait(&update_display_sem);
236 werase(footer);
237 wmove(footer, 1, 1);
238 print_key(footer, "F2", "CPUtop ", current_view == cpu);
239 print_key(footer, "F3", "PerfTop ", current_view == perf);
240 print_key(footer, "F6", "IOTop ", current_view == iostream);
241 print_key(footer, "Enter", "Details ", current_view == process_details);
242 print_key(footer, "q", "Quit | ", 0);
243 print_key(footer, "P", "Perf Pref ", 0);
244 print_key(footer, "p", "Pause ", toggle_pause);
245
246 wrefresh(footer);
247 sem_post(&update_display_sem);
248 }
249
250 void basic_header()
251 {
252 werase(header);
253 box(header, 0 , 0);
254 set_window_title(header, "Statistics for interval [gathering data...[");
255 wattron(header, A_BOLD);
256 mvwprintw(header, 1, 4, "CPUs");
257 mvwprintw(header, 2, 4, "Processes");
258 mvwprintw(header, 3, 4, "Threads");
259 mvwprintw(header, 4, 4, "Files");
260 mvwprintw(header, 5, 4, "Network");
261 mvwprintw(header, 6, 4, "IO");
262 wattroff(header, A_BOLD);
263 wrefresh(header);
264 }
265
266 void update_header()
267 {
268 werase(header);
269 box(header, 0 , 0);
270 set_window_title(header, "Statistics for interval ");
271 wattron(header, A_BOLD);
272 /*
273 wprintw(header, "[%lu.%lu, %lu.%lu[",
274 data->start.tv_sec, data->start.tv_nsec,
275 data->end.tv_sec, data->end.tv_nsec);
276 */
277 wprintw(header, "[%lu, %lu[",
278 data->start,
279 data->end);
280 mvwprintw(header, 1, 4, "CPUs");
281 wattroff(header, A_BOLD);
282 wprintw(header, "\t%d\t(max/cpu : %0.2f%)", data->cpu_table->len,
283 100.0/data->cpu_table->len);
284 print_headers(2, "Processes", data->nbproc, data->nbnewproc,
285 -1*(data->nbdeadproc));
286 print_headers(3, "Threads", data->nbthreads, data->nbnewthreads,
287 -1*(data->nbdeadthreads));
288 print_headers(4, "Files", data->nbfiles, data->nbnewfiles,
289 -1*(data->nbclosedfiles));
290 mvwprintw(header, 4, 43, "N/A kbytes/sec");
291 print_headers(5, "Network", 114, 0, 0);
292 mvwprintw(header, 5, 43, "N/A Mbytes/sec");
293 wrefresh(header);
294 }
295
296 gint sort_by_cpu_desc(gconstpointer p1, gconstpointer p2)
297 {
298 struct processtop *n1 = *(struct processtop **)p1;
299 struct processtop *n2 = *(struct processtop **)p2;
300 unsigned long totaln1 = n1->totalcpunsec;
301 unsigned long totaln2 = n2->totalcpunsec;
302
303 if (totaln1 < totaln2)
304 return 1;
305 if (totaln1 == totaln2)
306 return 0;
307 return -1;
308 }
309
310 gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2)
311 {
312 struct processtop *n1 = *(struct processtop **)p1;
313 struct processtop *n2 = *(struct processtop **)p2;
314 unsigned long totaln1 = n1->threadstotalcpunsec;
315 unsigned long totaln2 = n2->threadstotalcpunsec;
316
317 if (totaln1 < totaln2)
318 return 1;
319 if (totaln1 == totaln2)
320 return 0;
321 return -1;
322 }
323
324 void update_cputop_display()
325 {
326 int i;
327 int header_offset = 2;
328 struct processtop *tmp;
329 unsigned long elapsed;
330 double maxcputime;
331 int nblinedisplayed = 0;
332 int current_line = 0;
333
334 elapsed = data->end - data->start;
335 maxcputime = elapsed * data->cpu_table->len / 100.0;
336
337 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
338
339 set_window_title(center, "CPU Top");
340 wattron(center, A_BOLD);
341 mvwprintw(center, 1, 1, "CPU(%)");
342 mvwprintw(center, 1, 12, "TGID");
343 mvwprintw(center, 1, 22, "PID");
344 mvwprintw(center, 1, 32, "NAME");
345 wattroff(center, A_BOLD);
346
347 max_center_lines = LINES - 7 - 7 - 1 - header_offset;
348
349 /* iterate the process (thread) list */
350 for (i = list_offset; i < data->process_table->len &&
351 nblinedisplayed < max_center_lines; i++) {
352 tmp = g_ptr_array_index(data->process_table, i);
353
354 if (current_line == selected_line) {
355 selected_process = tmp;
356 selected_tid = tmp->tid;
357 selected_comm = tmp->comm;
358 wattron(center, COLOR_PAIR(5));
359 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
360 }
361 /* CPU(%) */
362 mvwprintw(center, current_line + header_offset, 1, "%1.2f",
363 tmp->totalcpunsec / maxcputime);
364 /* TGID */
365 mvwprintw(center, current_line + header_offset, 12, "%d", tmp->pid);
366 /* PID */
367 mvwprintw(center, current_line + header_offset, 22, "%d", tmp->tid);
368 /* NAME */
369 mvwprintw(center, current_line + header_offset, 32, "%s", tmp->comm);
370 wattroff(center, COLOR_PAIR(5));
371 nblinedisplayed++;
372 current_line++;
373 }
374 }
375
376 gint sort_perf(gconstpointer p1, gconstpointer p2, gpointer key)
377 {
378 struct processtop *n1 = *(struct processtop **) p1;
379 struct processtop *n2 = *(struct processtop **) p2;
380
381 struct perfcounter *tmp1, *tmp2;
382 unsigned long totaln2 = 0;
383 unsigned long totaln1 = 0;
384
385 if (!key)
386 return 0;
387
388 tmp1 = g_hash_table_lookup(n1->perf, key);
389 if (!tmp1)
390 totaln1 = 0;
391 else
392 totaln1 = tmp1->count;
393
394 tmp2 = g_hash_table_lookup(n2->perf, key);
395 if (!tmp2)
396 totaln2 = 0;
397 else
398 totaln2 = tmp2->count;
399
400 if (totaln1 < totaln2)
401 return 1;
402 if (totaln1 == totaln2) {
403 totaln1 = n1->tid;
404 totaln2 = n2->tid;
405 if (totaln1 < totaln2)
406 return 1;
407 return -1;
408 }
409 return -1;
410 }
411
412 void print_key_title(char *key, int line)
413 {
414 wattron(center, A_BOLD);
415 mvwprintw(center, line, 1, "%s\t", key);
416 wattroff(center, A_BOLD);
417 }
418
419 void update_process_details()
420 {
421 unsigned long elapsed;
422 double maxcputime;
423 struct processtop *tmp = find_process_tid(data, selected_tid, selected_comm);
424 struct files *file_tmp;
425 struct file_history *history = tmp->files_history;
426 int i, j = 0;
427
428 set_window_title(center, "Process details");
429
430
431 elapsed = data->end - data->start;
432 maxcputime = elapsed * data->cpu_table->len / 100.0;
433
434 print_key_title("Name", 1);
435 wprintw(center, "%s", selected_comm);
436 print_key_title("TID", 2);
437 wprintw(center, "%d", selected_tid);
438 if (!tmp) {
439 print_key_title("Does not exit at this time", 3);
440 return;
441 }
442
443 print_key_title("PID", 3);
444 wprintw(center, "%d", tmp->pid);
445 print_key_title("PPID", 4);
446 wprintw(center, "%d", tmp->ppid);
447 print_key_title("CPU", 5);
448 wprintw(center, "%1.2f %%", tmp->totalcpunsec/maxcputime);
449
450 print_key_title("READ B/s", 6);
451 wprintw(center, "%d", tmp->fileread);
452
453 print_key_title("WRITE B/s", 7);
454 wprintw(center, "%d", tmp->filewrite);
455
456 for (i = 0; i < tmp->process_files_table->len; i++) {
457 file_tmp = get_file(tmp, i);
458 if (file_tmp != NULL) {
459 print_key_title("file", 8+j);
460 wprintw(center, "%s fd = %d", file_tmp->name, i);
461 wprintw(center, " read = %d", file_tmp->read);
462 wprintw(center, " write = %d", file_tmp->write);
463 j++;
464 }
465 }
466 /*
467 print_key_title("----------- Files History -----------",8+j);
468 j = 0;
469 while (history != NULL) {
470 file_tmp = history->file;
471 wprintw(center, "fd = %d\n", file_tmp->fd);//, file_tmp->fd);
472 history = history->next;
473 }
474 */
475 }
476
477 void update_perf()
478 {
479 int i;
480 int nblinedisplayed = 0;
481 int current_line = 0;
482 struct processtop *tmp;
483 int header_offset = 2;
484 int perf_row = 40;
485 struct perfcounter *perfn1, *perfn2;
486 char *perf_key = NULL;
487 int value;
488 GHashTableIter iter;
489 gpointer key;
490
491 set_window_title(center, "Perf Top");
492 wattron(center, A_BOLD);
493 mvwprintw(center, 1, 1, "PID");
494 mvwprintw(center, 1, 11, "TID");
495 mvwprintw(center, 1, 22, "NAME");
496
497 perf_row = 40;
498 g_hash_table_iter_init(&iter, data->perf_list);
499 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
500 if (perfn1->visible) {
501 /* + 5 to strip the "perf_" prefix */
502 mvwprintw(center, 1, perf_row, "%s",
503 (char *) key + 5);
504 perf_row += 20;
505 }
506 if (perfn1->sort) {
507 perf_key = (char *) key;
508 }
509 }
510
511 wattroff(center, A_BOLD);
512
513 g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key);
514
515 for (i = 0; i < data->process_table->len &&
516 nblinedisplayed < max_center_lines; i++) {
517 tmp = g_ptr_array_index(data->process_table, i);
518
519 if (current_line == selected_line) {
520 selected_process = tmp;
521 wattron(center, COLOR_PAIR(5));
522 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
523 }
524
525 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
526 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
527 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
528
529 g_hash_table_iter_init(&iter, data->perf_list);
530
531 perf_row = 40;
532 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
533 if (perfn1->visible) {
534 perfn2 = g_hash_table_lookup(tmp->perf, (char *) key);
535 if (perfn2)
536 value = perfn2->count;
537 else
538 value = 0;
539 mvwprintw(center, current_line + header_offset,
540 perf_row, "%d", value);
541 perf_row += 20;
542 }
543 }
544
545 wattroff(center, COLOR_PAIR(5));
546 nblinedisplayed++;
547 current_line++;
548 }
549 }
550
551 void update_fileio()
552 {
553 int i;
554 int offset;
555
556 set_window_title(center, "IO Top");
557 wattron(center, A_BOLD);
558 mvwprintw(center, 1, 10, "READ");
559 mvwprintw(center, 2, 1, "bytes");
560 mvwprintw(center, 2, 15, "bytes/sec");
561
562 mvwprintw(center, 1, 39, "WRITE");
563 mvwprintw(center, 2, 33, "bytes");
564 mvwprintw(center, 2, 45, "bytes/sec");
565
566 if (toggle_threads > 0) {
567 mvwprintw(center, 1, 60, "TGID");
568 mvwprintw(center, 1, 70, "PID");
569 offset = 8;
570 } else {
571 mvwprintw(center, 1, 60, "PID(TGID)");
572 offset = 0;
573 }
574 mvwprintw(center, 1, 72 + offset, "NAME");
575 wattroff(center, A_BOLD);
576
577 for (i = 3; i < LINES - 3 - 8 - 1; i++) {
578 mvwprintw(center, i, 1, "%d", i*1000);
579 mvwprintw(center, i, 15, "%dk", i);
580 mvwprintw(center, i, 28, "| %d", i*2000);
581 mvwprintw(center, i, 45, "%dk", i*2);
582 if (toggle_threads > 0) {
583 mvwprintw(center, i, 57, "| %d", i);
584 mvwprintw(center, i, 70, "%d", i);
585 } else {
586 mvwprintw(center, i, 57, "| %d", i);
587 }
588 mvwprintw(center, i, 72 + offset, "process_%d", i);
589 }
590 }
591
592 gint sort_by_ret_desc(gconstpointer p1, gconstpointer p2)
593 {
594 struct processtop *n1 = *(struct processtop **)p1;
595 struct processtop *n2 = *(struct processtop **)p2;
596
597 unsigned long totaln1 = n1->totalfileread + n1->totalfilewrite;
598 unsigned long totaln2 = n2->totalfileread + n2->totalfilewrite;
599
600 if (totaln1 < totaln2)
601 return 1;
602 if (totaln1 == totaln2)
603 return 0;
604 return -1;
605 }
606
607 void update_iostream()
608 {
609 int i;
610 int header_offset = 2;
611 struct processtop *tmp;
612 int nblinedisplayed = 0;
613 int current_line = 0;
614 int total = 0;
615
616 set_window_title(center, "IO Top");
617 wattron(center, A_BOLD);
618 mvwprintw(center, 1, 1, "READ (B/s)");
619 mvwprintw(center, 1, 20, "WRITE (B/s)");
620
621 mvwprintw(center, 1, 40, "TOTAL STREAM");
622
623 mvwprintw(center, 1, 60, "TGID");
624 mvwprintw(center, 1, 80, "PID");
625
626 mvwprintw(center, 1, 92, "NAME");
627 wattroff(center, A_BOLD);
628
629 g_ptr_array_sort(data->process_table, sort_by_ret_desc);
630
631 for (i = list_offset; i < data->process_table->len &&
632 nblinedisplayed < max_center_lines; i++) {
633 tmp = g_ptr_array_index(data->process_table, i);
634
635 if (current_line == selected_line) {
636 selected_process = tmp;
637 selected_tid = tmp->tid;
638 selected_comm = tmp->comm;
639 wattron(center, COLOR_PAIR(5));
640 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
641 }
642
643 /* READ (bytes/sec) */
644 mvwprintw(center, current_line + header_offset, 1, "%lu",
645 tmp->fileread);
646
647 /* WRITE (bytes/sec) */
648 mvwprintw(center, current_line + header_offset, 20, "%lu",
649 tmp->filewrite);
650
651 /* TOTAL STREAM */
652 total = tmp->totalfileread + tmp->totalfilewrite;
653
654 if (total >= 1000000)
655 mvwprintw(center, current_line + header_offset, 40, "%lu MB",
656 total/1000000);
657 else if (total >= 1000)
658 mvwprintw(center, current_line + header_offset, 40, "%lu KB",
659 total/1000);
660 else
661 mvwprintw(center, current_line + header_offset, 40, "%lu B",
662 total);
663
664 /* TGID */
665 mvwprintw(center, current_line + header_offset, 60, "%d", tmp->pid);
666 /* PID */
667 mvwprintw(center, current_line + header_offset, 80, "%d", tmp->tid);
668 /* NAME */
669 mvwprintw(center, current_line + header_offset, 92, "%s", tmp->comm);
670 wattroff(center, COLOR_PAIR(5));
671 nblinedisplayed++;
672 current_line++;
673 }
674 }
675
676 void update_current_view()
677 {
678 sem_wait(&update_display_sem);
679 if (!data)
680 return;
681 update_header();
682
683 werase(center);
684 box(center, 0, 0);
685 switch (current_view) {
686 case cpu:
687 update_cputop_display();
688 break;
689 case perf:
690 update_perf();
691 break;
692 case process_details:
693 update_process_details();
694 break;
695 case fileio:
696 update_fileio();
697 break;
698 case iostream:
699 update_iostream();
700 break;
701 case tree:
702 update_cputop_display();
703 break;
704 default:
705 break;
706 }
707 update_panels();
708 doupdate();
709 sem_post(&update_display_sem);
710 }
711
712 void setup_perf_panel()
713 {
714 int size;
715 if (!data)
716 return;
717 if (perf_panel_window) {
718 del_panel(perf_panel);
719 delwin(perf_panel_window);
720 }
721 size = g_hash_table_size(data->perf_list);
722 perf_panel_window = create_window(size + 2, 30, 10, 10);
723 perf_panel = new_panel(perf_panel_window);
724 perf_panel_visible = 0;
725 hide_panel(perf_panel);
726 }
727
728 void update_perf_panel(int line_selected, int toggle_view, int toggle_sort)
729 {
730 int i;
731 struct perfcounter *perf;
732 GList *perflist;
733
734 if (!data)
735 return;
736
737 werase(perf_panel_window);
738 box(perf_panel_window, 0 , 0);
739 set_window_title(perf_panel_window, "Perf Preferences ");
740 wattron(perf_panel_window, A_BOLD);
741 mvwprintw(perf_panel_window, g_hash_table_size(data->perf_list) + 1, 1,
742 " 's' to sort");
743 wattroff(perf_panel_window, A_BOLD);
744
745 if (toggle_sort == 1) {
746 i = 0;
747 perflist = g_list_first(g_hash_table_get_keys(data->perf_list));
748 while (perflist) {
749 perf = g_hash_table_lookup(data->perf_list, perflist->data);
750 if (i != line_selected)
751 perf->sort = 0;
752 else
753 perf->sort = 1;
754 i++;
755 perflist = g_list_next(perflist);
756 }
757 update_current_view();
758 }
759
760 i = 0;
761 perflist = g_list_first(g_hash_table_get_keys(data->perf_list));
762 while (perflist) {
763 perf = g_hash_table_lookup(data->perf_list, perflist->data);
764 if (i == line_selected && toggle_view == 1) {
765 perf->visible = perf->visible == 1 ? 0:1;
766 update_current_view();
767 }
768 if (i == line_selected) {
769 wattron(perf_panel_window, COLOR_PAIR(5));
770 mvwhline(perf_panel_window, i + 1, 1, ' ', 30 - 2);
771 }
772 if (perf->sort == 1)
773 wattron(perf_panel_window, A_BOLD);
774 mvwprintw(perf_panel_window, i + 1, 1, "[%c] %s",
775 perf->visible == 1 ? 'x' : ' ',
776 (char *) perflist->data + 6);
777 wattroff(perf_panel_window, A_BOLD);
778 wattroff(perf_panel_window, COLOR_PAIR(5));
779 i++;
780 perflist = g_list_next(perflist);
781 }
782 update_panels();
783 doupdate();
784 }
785
786
787 void toggle_perf_panel(void)
788 {
789 if (perf_panel_visible) {
790 hide_panel(perf_panel);
791 perf_panel_visible = 0;
792 } else {
793 setup_perf_panel();
794 update_perf_panel(perf_line_selected, 0, 0);
795 show_panel(perf_panel);
796 perf_panel_visible = 1;
797 }
798 update_panels();
799 doupdate();
800 }
801
802 void display(unsigned int index)
803 {
804 last_display_index = index;
805 currently_displayed_index = index;
806 data = g_ptr_array_index(copies, index);
807 if (!data)
808 return;
809 max_elements = data->process_table->len;
810 update_current_view();
811 update_footer();
812 update_panels();
813 doupdate();
814 }
815
816 void pause_display()
817 {
818 toggle_pause = 1;
819 print_log("Pause");
820 sem_wait(&pause_sem);
821 }
822
823 void resume_display()
824 {
825 toggle_pause = -1;
826 print_log("Resume");
827 sem_post(&pause_sem);
828 }
829
830 void *handle_keyboard(void *p)
831 {
832 int ch;
833 while((ch = getch())) {
834 switch(ch) {
835 /* Move the cursor and scroll */
836 case KEY_DOWN:
837 if (perf_panel_visible) {
838 if (perf_line_selected < g_hash_table_size(data->perf_list) - 1)
839 perf_line_selected++;
840 update_perf_panel(perf_line_selected, 0, 0);
841 } else {
842 if (selected_line < (max_center_lines - 1) &&
843 selected_line < max_elements - 1) {
844 selected_line++;
845 selected_in_list++;
846 } else if (selected_in_list < (max_elements - 1)
847 && (list_offset < (max_elements - max_center_lines))) {
848 selected_in_list++;
849 list_offset++;
850 }
851 update_current_view();
852 }
853 break;
854 case KEY_NPAGE:
855 if ((selected_line + 10 < max_center_lines - 1) &&
856 ((selected_line + 10) < max_elements - 1)) {
857 selected_line += 10;
858 selected_in_list += 10;
859 } else if (max_elements > max_center_lines) {
860 selected_line = max_center_lines - 1;
861 if (selected_in_list + 10 < max_elements - 1) {
862 selected_in_list += 10;
863 list_offset += (selected_in_list - max_center_lines + 1);
864 }
865 } else if (selected_line + 10 > max_elements) {
866 selected_line = max_elements - 1;
867 }
868 update_current_view();
869 break;
870 case KEY_UP:
871 if (perf_panel_visible) {
872 if (perf_line_selected > 0)
873 perf_line_selected--;
874 update_perf_panel(perf_line_selected, 0, 0);
875 } else {
876 if (selected_line > 0) {
877 selected_line--;
878 selected_in_list--;
879 } else if (selected_in_list > 0 && list_offset > 0) {
880 selected_in_list--;
881 list_offset--;
882 }
883 update_current_view();
884 }
885 break;
886 case KEY_PPAGE:
887 if (selected_line - 10 > 0)
888 selected_line -= 10;
889 else
890 selected_line = 0;
891 update_current_view();
892 break;
893
894 /* Navigate the history with arrows */
895 case KEY_LEFT:
896 if (currently_displayed_index > 0) {
897 currently_displayed_index--;
898 print_log("Going back in time");
899 } else {
900 print_log("Cannot rewind, last data is already displayed");
901 }
902 data = g_ptr_array_index(copies, currently_displayed_index);
903 max_elements = data->process_table->len;
904
905 /* we force to pause the display when moving in time */
906 if (toggle_pause < 0)
907 pause_display();
908
909 update_current_view();
910 update_footer();
911 break;
912 case KEY_RIGHT:
913 if (currently_displayed_index < last_display_index) {
914 currently_displayed_index++;
915 print_log("Going forward in time");
916 data = g_ptr_array_index(copies, currently_displayed_index);
917 max_elements = data->process_table->len;
918 update_current_view();
919 update_footer();
920 } else {
921 print_log("Manually moving forward");
922 sem_post(&timer);
923 /* we force to resume the refresh when moving forward */
924 if (toggle_pause > 0)
925 resume_display();
926 }
927
928 break;
929 case ' ':
930 if (perf_panel_visible)
931 update_perf_panel(perf_line_selected, 1, 0);
932 break;
933 case 's':
934 if (perf_panel_visible)
935 update_perf_panel(perf_line_selected, 0, 1);
936 break;
937
938 case 13: /* FIXME : KEY_ENTER ?? */
939 if (current_view == cpu) {
940 current_view = process_details;
941 }
942 update_current_view();
943 break;
944
945 case KEY_F(1):
946 toggle_tree *= -1;
947 current_view = cpu;
948 update_current_view();
949 break;
950 case KEY_F(2):
951 current_view = cpu;
952 update_current_view();
953 break;
954 case KEY_F(3):
955 current_view = perf;
956 toggle_tree = -1;
957 update_current_view();
958 break;
959 case KEY_F(4):
960 current_view = fileio;
961 toggle_tree = -1;
962 update_current_view();
963 break;
964 case KEY_F(5):
965 current_view = netio;
966 toggle_tree = -1;
967 update_current_view();
968 break;
969 case KEY_F(6):
970 current_view = iostream;
971 toggle_tree = -1;
972 update_current_view();
973 break;
974 case KEY_F(10):
975 case 'q':
976 reset_ncurses();
977 break;
978 case 't':
979 toggle_threads *= -1;
980 update_current_view();
981 break;
982 case 'p':
983 if (toggle_pause < 0) {
984 pause_display();
985 } else {
986 resume_display();
987 }
988 break;
989 case 'P':
990 toggle_perf_panel();
991 break;
992 default:
993 if (data)
994 update_current_view();
995 break;
996 }
997 update_footer();
998 }
999 return NULL;
1000 }
1001
1002 void init_ncurses()
1003 {
1004 sem_init(&update_display_sem, 0, 1);
1005 init_screen();
1006
1007 header = create_window(7, COLS - 1, 0, 0);
1008 center = create_window(LINES - 7 - 7, COLS - 1, 7, 0);
1009 status = create_window(MAX_LOG_LINES + 2, COLS - 1, LINES - 7, 0);
1010 footer = create_window(1, COLS - 1, LINES - 1, 0);
1011
1012 print_log("Starting display");
1013
1014 main_panel = new_panel(center);
1015 setup_perf_panel();
1016
1017 current_view = cpu;
1018
1019 basic_header();
1020 update_footer();
1021
1022 pthread_create(&keyboard_thread, NULL, handle_keyboard, (void *)NULL);
1023 }
1024
This page took 0.077965 seconds and 3 git commands to generate.