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