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