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