fix: cleanup the lttngtop-live-simple traces
[lttngtop.git] / src / cursesdisplay.c
CommitLineData
1fc22eb4 1/*
aa15ac1c 2 * Copyright (C) 2011-2012 Julien Desfossez
1fc22eb4
JD
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 *
71bd7ce1
AM
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.
1fc22eb4
JD
16 */
17
ec016cb4
JD
18#include <config.h>
19
20#ifdef HAVE_LIBNCURSES
21
1fc22eb4
JD
22#include <stdio.h>
23#include <stdlib.h>
24#include <signal.h>
25#include <string.h>
26#include <ncurses.h>
27#include <panel.h>
28#include <pthread.h>
29#include <semaphore.h>
30
31#include "cursesdisplay.h"
32#include "lttngtoptypes.h"
b093de8a 33#include "iostreamtop.h"
1fc22eb4
JD
34#include "common.h"
35
36#define DEFAULT_DELAY 15
37#define MAX_LINE_LENGTH 50
38#define MAX_LOG_LINES 4
39
40/* to prevent concurrent updates of the different windows */
41sem_t update_display_sem;
42
43char *termtype;
44WINDOW *footer, *header, *center, *status;
0d91c12a
JD
45WINDOW *pref_panel_window = NULL;
46PANEL *pref_panel, *main_panel;
1fc22eb4 47
0d91c12a 48int pref_panel_visible = 0;
3b15348c 49int pref_line_selected = 0;
41ff0645 50int pref_current_sort = 0;
1fc22eb4
JD
51
52int last_display_index, currently_displayed_index;
53
54struct processtop *selected_process = NULL;
1fc22eb4
JD
55int selected_ret;
56
57int selected_line = 0; /* select bar position */
58int selected_in_list = 0; /* selection relative to the whole list */
59int list_offset = 0; /* first index in the list to display (scroll) */
60int nb_log_lines = 0;
61char log_lines[MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES];
62
63int max_elements = 80;
64
e15ed00a 65int toggle_threads = 1;
1402044a 66int toggle_virt = -1;
1fc22eb4 67int toggle_pause = -1;
1fc22eb4 68
467097ac
JD
69int filter_host_panel = 0;
70
1fc22eb4
JD
71int max_center_lines;
72
73pthread_t keyboard_thread;
74
1402044a 75struct header_view cputopview[6];
89f1e0d1 76struct header_view iostreamtopview[3];
32647247 77struct header_view fileview[3];
246d5992 78struct header_view kprobeview[2];
3b15348c 79
1fc22eb4
JD
80void reset_ncurses()
81{
82 curs_set(1);
83 endwin();
ae9e85c7 84 quit = 1;
33572a17
JD
85 sem_post(&pause_sem);
86 sem_post(&timer);
87 sem_post(&goodtodisplay);
1402044a
JD
88 sem_post(&end_trace_sem);
89 sem_post(&goodtoupdate);
1fc22eb4
JD
90}
91
92static void handle_sigterm(int signal)
93{
ae9e85c7 94 pthread_cancel(keyboard_thread);
1fc22eb4
JD
95 reset_ncurses();
96}
97
98void init_screen()
99{
100 initscr();
101 noecho();
102 halfdelay(DEFAULT_DELAY);
103 nonl();
104 intrflush(stdscr, false);
105 keypad(stdscr, true);
106 curs_set(0);
107
108 if (has_colors()) {
109 start_color();
110 init_pair(1, COLOR_RED, COLOR_BLACK); /* - */
111 init_pair(2, COLOR_GREEN, COLOR_BLACK); /* + */
112 init_pair(3, COLOR_BLACK, COLOR_WHITE); /* keys */
113 init_pair(4, COLOR_WHITE, COLOR_GREEN); /* keys activated */
da4353bb
JD
114 init_pair(5, COLOR_BLACK, COLOR_YELLOW); /* select line */
115 init_pair(6, COLOR_GREEN, COLOR_BLACK); /* selected process */
116 init_pair(7, COLOR_RED, COLOR_YELLOW); /* selected process + line*/
1fc22eb4
JD
117 }
118 termtype = getenv("TERM");
119 if (!strcmp(termtype, "xterm") || !strcmp(termtype, "xterm-color") ||
120 !strcmp(termtype, "vt220")) {
121 define_key("\033[H", KEY_HOME);
122 define_key("\033[F", KEY_END);
123 define_key("\033OP", KEY_F(1));
124 define_key("\033OQ", KEY_F(2));
125 define_key("\033OR", KEY_F(3));
126 define_key("\033OS", KEY_F(4));
127 define_key("\0330U", KEY_F(6));
128 define_key("\033[11~", KEY_F(1));
129 define_key("\033[12~", KEY_F(2));
130 define_key("\033[13~", KEY_F(3));
131 define_key("\033[14~", KEY_F(4));
132 define_key("\033[16~", KEY_F(6));
133 define_key("\033[17;2~", KEY_F(18));
134 }
135 signal(SIGTERM, handle_sigterm);
ae9e85c7 136 signal(SIGINT, handle_sigterm);
1fc22eb4
JD
137 mousemask(BUTTON1_CLICKED, NULL);
138 refresh();
139}
140
141WINDOW *create_window(int height, int width, int startx, int starty)
142{
143 WINDOW *win;
144 win = newwin(height, width, startx, starty);
145 box(win, 0 , 0);
146 wrefresh(win);
147 return win;
148}
149
150WINDOW *create_window_no_border(int height, int width, int startx, int starty)
151{
152 WINDOW *win;
153 win = newwin(height, width, startx, starty);
154 wrefresh(win);
155 return win;
156}
157
158void print_digit(WINDOW *win, int digit)
159{
160 if (digit < 0) {
161 wattron(win, COLOR_PAIR(1));
162 wprintw(win, "%d", digit);
163 wattroff(win, COLOR_PAIR(1));
164 } else if (digit > 0) {
165 wattron(win, COLOR_PAIR(2));
166 wprintw(win, "+%d", digit);
167 wattroff(win, COLOR_PAIR(2));
168 } else {
169 wprintw(win, "0");
170 }
171}
172
173void print_digits(WINDOW *win, int first, int second)
174{
175 wprintw(win, "(");
176 print_digit(win, first);
177 wprintw(win, ", ");
178 print_digit(win, second);
179 wprintw(win, ")");
180}
181
182void print_headers(int line, char *desc, int value, int first, int second)
183{
184 wattron(header, A_BOLD);
185 mvwprintw(header, line, 4, "%s", desc);
186 wattroff(header, A_BOLD);
e05a35a6 187 mvwprintw(header, line, 16, "%d", value);
1fc22eb4
JD
188 wmove(header, line, 24);
189 print_digits(header, first, second);
190 wmove(header, line, 40);
191}
192
193void set_window_title(WINDOW *win, char *title)
194{
195 wattron(win, A_BOLD);
196 mvwprintw(win, 0, 1, title);
197 wattroff(win, A_BOLD);
198}
199
200void print_log(char *str)
201{
202 int i;
203 int current_line = 1;
204 int current_char = 1;
205 char *tmp, *tmp2;
206 /* rotate the line buffer */
207 if (nb_log_lines >= MAX_LOG_LINES) {
208 tmp = strndup(log_lines, MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES);
209 tmp2 = strchr(tmp, '\n');
210 memset(log_lines, '\0', strlen(log_lines));
211 strncat(log_lines, tmp2 + 1, strlen(tmp2) - 1);
212 log_lines[strlen(log_lines)] = '\n';
213 log_lines[strlen(log_lines)] = '\0';
214 free(tmp);
215 }
216 nb_log_lines++;
217
218 strncat(log_lines, str, MAX_LINE_LENGTH - 1);
219
220 if (nb_log_lines < MAX_LOG_LINES)
221 log_lines[strlen(log_lines)] = '\n';
222 log_lines[strlen(log_lines)] = '\0';
223
224 werase(status);
225 box(status, 0 , 0);
226 set_window_title(status, "Status");
227 for (i = 0; i < strlen(log_lines); i++) {
228 if (log_lines[i] == '\n') {
229 wmove(status, ++current_line, 1);
230 current_char = 1;
231 } else {
b093de8a
MB
232 mvwprintw(status, current_line, current_char++, "%c",
233 log_lines[i]);
1fc22eb4
JD
234 }
235 }
236 wrefresh(status);
237}
238
635dc837
JD
239int process_selected(struct processtop *process)
240{
da4353bb
JD
241 if (lookup_filter_tid_list(process->tid))
242 return 1;
635dc837
JD
243 return 0;
244}
245
246void update_selected_processes()
247{
248 if (process_selected(selected_process)) {
da4353bb 249 remove_filter_tid_list(selected_process->tid);
635dc837 250 } else {
b99227ca 251 add_filter_tid_list(selected_process);
635dc837
JD
252 }
253}
254
1fc22eb4
JD
255void print_key(WINDOW *win, char *key, char *desc, int toggle)
256{
257 int pair;
258 if (toggle > 0)
259 pair = 4;
260 else
261 pair = 3;
262 wattron(win, COLOR_PAIR(pair));
263 wprintw(footer, "%s", key);
264 wattroff(win, COLOR_PAIR(pair));
265 wprintw(footer, ":%s", desc);
266}
267
268void update_footer()
269{
270 sem_wait(&update_display_sem);
271 werase(footer);
272 wmove(footer, 1, 1);
273 print_key(footer, "F2", "CPUtop ", current_view == cpu);
274 print_key(footer, "F3", "PerfTop ", current_view == perf);
e7be7d41 275 print_key(footer, "F4", "IOTop ", current_view == iostream);
1fc22eb4 276 print_key(footer, "Enter", "Details ", current_view == process_details);
635dc837 277 print_key(footer, "Space", "Highlight ", 0);
d33aae69 278 print_key(footer, "q", "Quit ", 0);
f9ff6939 279 print_key(footer, "r", "Pref ", 0);
e15ed00a 280 print_key(footer, "t", "Threads ", toggle_threads);
1402044a 281 print_key(footer, "v", "Virt ", toggle_virt);
1fc22eb4
JD
282 print_key(footer, "p", "Pause ", toggle_pause);
283
284 wrefresh(footer);
285 sem_post(&update_display_sem);
286}
287
288void basic_header()
289{
290 werase(header);
291 box(header, 0 , 0);
292 set_window_title(header, "Statistics for interval [gathering data...[");
293 wattron(header, A_BOLD);
294 mvwprintw(header, 1, 4, "CPUs");
e05a35a6 295 mvwprintw(header, 2, 4, "Threads");
dc8f04dd 296 mvwprintw(header, 3, 4, "FDs");
1fc22eb4
JD
297 wattroff(header, A_BOLD);
298 wrefresh(header);
299}
300
69362330
JD
301static void scale_unit(uint64_t bytes, char *ret)
302{
303 if (bytes >= 1000000000)
304 sprintf(ret, "%" PRIu64 "G", bytes/1000000000);
305 if (bytes >= 1000000)
306 sprintf(ret, "%" PRIu64 "M", bytes/1000000);
307 else if (bytes >= 1000)
308 sprintf(ret, "%" PRIu64 "K", bytes/1000);
309 else
310 sprintf(ret, "%" PRIu64, bytes);
311}
41ff0645 312
69362330
JD
313uint64_t total_io()
314{
315 int i;
316 struct processtop *tmp;
317 uint64_t total = 0;
318
319 for (i = 0; i < data->process_table->len; i++) {
320 tmp = g_ptr_array_index(data->process_table, i);
321 total += tmp->fileread;
322 total += tmp->filewrite;
323 }
324
325 return total;
326}
327
1fc22eb4
JD
328void update_header()
329{
91be6bb5
JD
330 struct tm start, end;
331 uint64_t ts_nsec_start, ts_nsec_end;
69362330 332 char io[4];
91be6bb5
JD
333
334 ts_nsec_start = data->start % NSEC_PER_SEC;
335 start = format_timestamp(data->start);
336
337 ts_nsec_end = data->end % NSEC_PER_SEC;
338 end = format_timestamp(data->end);
339
1fc22eb4
JD
340 werase(header);
341 box(header, 0 , 0);
342 set_window_title(header, "Statistics for interval ");
343 wattron(header, A_BOLD);
91be6bb5
JD
344
345 wprintw(header, "[%02d:%02d:%02d.%09" PRIu64 ", %02d:%02d:%02d.%09" PRIu64 "[",
346 start.tm_hour, start.tm_min, start.tm_sec, ts_nsec_start,
347 end.tm_hour, end.tm_min, end.tm_sec, ts_nsec_end);
1fc22eb4
JD
348 mvwprintw(header, 1, 4, "CPUs");
349 wattroff(header, A_BOLD);
350 wprintw(header, "\t%d\t(max/cpu : %0.2f%)", data->cpu_table->len,
351 100.0/data->cpu_table->len);
e05a35a6 352 print_headers(2, "Threads", data->nbthreads, data->nbnewthreads,
1fc22eb4 353 -1*(data->nbdeadthreads));
dc8f04dd 354 print_headers(3, "FDs", data->nbfiles, data->nbnewfiles,
1fc22eb4 355 -1*(data->nbclosedfiles));
69362330
JD
356 scale_unit(total_io(), io);
357 mvwprintw(header, 3, 43, "%sB/sec", io);
1fc22eb4
JD
358 wrefresh(header);
359}
360
361gint sort_by_cpu_desc(gconstpointer p1, gconstpointer p2)
362{
363 struct processtop *n1 = *(struct processtop **)p1;
364 struct processtop *n2 = *(struct processtop **)p2;
365 unsigned long totaln1 = n1->totalcpunsec;
366 unsigned long totaln2 = n2->totalcpunsec;
367
368 if (totaln1 < totaln2)
369 return 1;
370 if (totaln1 == totaln2)
371 return 0;
372 return -1;
373}
374
3b15348c
JD
375gint sort_by_tid_desc(gconstpointer p1, gconstpointer p2)
376{
377 struct processtop *n1 = *(struct processtop **)p1;
378 struct processtop *n2 = *(struct processtop **)p2;
379 unsigned long totaln1 = n1->tid;
380 unsigned long totaln2 = n2->tid;
381
382 if (totaln1 < totaln2)
383 return 1;
384 if (totaln1 == totaln2)
385 return 0;
386 return -1;
387}
388
389gint sort_by_pid_desc(gconstpointer p1, gconstpointer p2)
390{
391 struct processtop *n1 = *(struct processtop **)p1;
392 struct processtop *n2 = *(struct processtop **)p2;
393 unsigned long totaln1 = n1->pid;
394 unsigned long totaln2 = n2->pid;
395
396 if (totaln1 < totaln2)
397 return 1;
398 if (totaln1 == totaln2)
399 return 0;
400 return -1;
401}
402
89f1e0d1
JD
403gint sort_by_process_read_desc(gconstpointer p1, gconstpointer p2)
404{
405 struct processtop *n1 = *(struct processtop **)p1;
406 struct processtop *n2 = *(struct processtop **)p2;
407 unsigned long totaln1 = n1->fileread;
408 unsigned long totaln2 = n2->fileread;
409
410 if (totaln1 < totaln2)
411 return 1;
412 if (totaln1 == totaln2)
413 return 0;
414 return -1;
415}
416
417gint sort_by_process_write_desc(gconstpointer p1, gconstpointer p2)
418{
419 struct processtop *n1 = *(struct processtop **)p1;
420 struct processtop *n2 = *(struct processtop **)p2;
421 unsigned long totaln1 = n1->filewrite;
422 unsigned long totaln2 = n2->filewrite;
423
424 if (totaln1 < totaln2)
425 return 1;
426 if (totaln1 == totaln2)
427 return 0;
428 return -1;
429}
430
431gint sort_by_process_total_desc(gconstpointer p1, gconstpointer p2)
432{
433 struct processtop *n1 = *(struct processtop **)p1;
434 struct processtop *n2 = *(struct processtop **)p2;
41f5890b
JD
435 unsigned long totaln1 = n1->totalfilewrite + n1->totalfileread;
436 unsigned long totaln2 = n2->totalfilewrite + n2->totalfileread;
89f1e0d1
JD
437
438 if (totaln1 < totaln2)
439 return 1;
440 if (totaln1 == totaln2)
441 return 0;
442 return -1;
443}
444
32647247
JD
445gint sort_by_file_read_desc(gconstpointer p1, gconstpointer p2)
446{
447 struct files *n1 = *(struct files **)p1;
448 struct files *n2 = *(struct files **)p2;
449 unsigned long totaln1;
450 unsigned long totaln2;
451
452 totaln1 = n1->read;
453 totaln2 = n2->read;
454
455 if (totaln1 < totaln2)
456 return 1;
457 if (totaln1 == totaln2)
458 return 0;
459 return -1;
460}
461
462gint sort_by_file_write_desc(gconstpointer p1, gconstpointer p2)
463{
464 struct files *n1 = *(struct files **)p1;
465 struct files *n2 = *(struct files **)p2;
466 unsigned long totaln1;
467 unsigned long totaln2;
468
469 totaln1 = n1->write;
470 totaln2 = n2->write;
471
472 if (totaln1 < totaln2)
473 return 1;
474 if (totaln1 == totaln2)
475 return 0;
476 return -1;
477}
478
479gint sort_by_file_fd_desc(gconstpointer p1, gconstpointer p2)
480{
481 struct files *n1 = *(struct files **)p1;
482 struct files *n2 = *(struct files **)p2;
483 unsigned long totaln1;
484 unsigned long totaln2;
485
486 totaln1 = n1->fd;
487 totaln2 = n2->fd;
488
489 if (totaln1 < totaln2)
490 return 1;
491 if (totaln1 == totaln2)
492 return 0;
493 return -1;
494}
495
1fc22eb4
JD
496gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2)
497{
498 struct processtop *n1 = *(struct processtop **)p1;
499 struct processtop *n2 = *(struct processtop **)p2;
500 unsigned long totaln1 = n1->threadstotalcpunsec;
501 unsigned long totaln2 = n2->threadstotalcpunsec;
502
503 if (totaln1 < totaln2)
504 return 1;
505 if (totaln1 == totaln2)
506 return 0;
507 return -1;
508}
509
fbbda4da
JD
510void update_kprobes_display()
511{
512 int i, column;
246d5992
JD
513 struct kprobes *probe;
514 int header_offset = 2;
515 int current_line = 0;
fbbda4da 516
246d5992 517 set_window_title(center, "Kprobes Top ");
fbbda4da
JD
518 wattron(center, A_BOLD);
519 column = 1;
246d5992
JD
520 for (i = 0; i < 2; i++) {
521 if (kprobeview[i].sort) {
fbbda4da
JD
522 wattron(center, A_UNDERLINE);
523 pref_current_sort = i;
524 }
246d5992 525 mvwprintw(center, 1, column, "%s", kprobeview[i].title);
fbbda4da 526 wattroff(center, A_UNDERLINE);
246d5992 527 column += 30;
fbbda4da
JD
528 }
529 wattroff(center, A_BOLD);
246d5992
JD
530
531 for (i = 0; i < data->kprobes_table->len; i++) {
532 column = 1;
533 probe = g_ptr_array_index(data->kprobes_table, i);
534 mvwprintw(center, current_line + header_offset, column,
535 "%s", probe->probe_name + 6);
536 column += 30;
537 mvwprintw(center, current_line + header_offset, column,
538 "%d", probe->count);
539 current_line++;
540 }
fbbda4da
JD
541}
542
1fc22eb4
JD
543void update_cputop_display()
544{
545 int i;
546 int header_offset = 2;
547 struct processtop *tmp;
548 unsigned long elapsed;
549 double maxcputime;
550 int nblinedisplayed = 0;
551 int current_line = 0;
1402044a 552 int current_row_offset;
844d1c9c 553 int column;
1fc22eb4
JD
554
555 elapsed = data->end - data->start;
556 maxcputime = elapsed * data->cpu_table->len / 100.0;
557
3b15348c
JD
558 if (cputopview[0].sort == 1)
559 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
560 else if (cputopview[1].sort == 1)
561 g_ptr_array_sort(data->process_table, sort_by_pid_desc);
562 else if (cputopview[2].sort == 1)
563 g_ptr_array_sort(data->process_table, sort_by_tid_desc);
564 else if (cputopview[3].sort == 1)
565 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
566 else
567 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
1fc22eb4
JD
568
569 set_window_title(center, "CPU Top");
570 wattron(center, A_BOLD);
844d1c9c 571 column = 1;
1402044a
JD
572 for (i = 0; i < 6; i++) {
573 if (toggle_virt < 0 && (i == 3 || i == 4)) {
574 continue;
575 }
41ff0645 576 if (cputopview[i].sort) {
844d1c9c 577 wattron(center, A_UNDERLINE);
41ff0645
JD
578 pref_current_sort = i;
579 }
844d1c9c
JD
580 mvwprintw(center, 1, column, cputopview[i].title);
581 wattroff(center, A_UNDERLINE);
582 column += 10;
583 }
1fc22eb4
JD
584 wattroff(center, A_BOLD);
585
dc8f04dd 586 max_center_lines = LINES - 5 - 7 - 1 - header_offset;
1fc22eb4
JD
587
588 /* iterate the process (thread) list */
589 for (i = list_offset; i < data->process_table->len &&
590 nblinedisplayed < max_center_lines; i++) {
591 tmp = g_ptr_array_index(data->process_table, i);
1402044a 592 current_row_offset = 1;
da4353bb 593 if (toggle_filter > 0 && !lookup_filter_tid_list(tmp->tid))
c8d75a13
JD
594 continue;
595
e15ed00a
JD
596 if (tmp->pid != tmp->tid)
597 if (toggle_threads == -1)
598 continue;
1fc22eb4 599
da4353bb 600 /* line */
1fc22eb4
JD
601 if (current_line == selected_line) {
602 selected_process = tmp;
1fc22eb4
JD
603 wattron(center, COLOR_PAIR(5));
604 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
605 }
da4353bb
JD
606 /* filtered process */
607 if (process_selected(tmp)) {
608 if (current_line == selected_line)
609 wattron(center, COLOR_PAIR(7));
610 else
611 wattron(center, COLOR_PAIR(6));
612 }
1fc22eb4 613 /* CPU(%) */
1402044a
JD
614 mvwprintw(center, current_line + header_offset,
615 current_row_offset, "%1.2f",
1fc22eb4 616 tmp->totalcpunsec / maxcputime);
1402044a 617 current_row_offset += 10;
1fc22eb4 618 /* PID */
1402044a
JD
619 mvwprintw(center, current_line + header_offset,
620 current_row_offset, "%d", tmp->pid);
621 current_row_offset += 10;
6153ec12 622 /* TID */
1402044a
JD
623 mvwprintw(center, current_line + header_offset,
624 current_row_offset, "%d", tmp->tid);
625 current_row_offset += 10;
626 if (toggle_virt > 0) {
627 /* VPID */
628 mvwprintw(center, current_line + header_offset,
629 current_row_offset, "%d", tmp->vpid);
630 current_row_offset += 10;
631 /* VTID */
632 mvwprintw(center, current_line + header_offset,
633 current_row_offset, "%d", tmp->vtid);
634 current_row_offset += 10;
635 }
1fc22eb4 636 /* NAME */
1402044a
JD
637 mvwprintw(center, current_line + header_offset,
638 current_row_offset, "%s", tmp->comm);
da4353bb 639 wattroff(center, COLOR_PAIR(7));
635dc837 640 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
641 wattroff(center, COLOR_PAIR(5));
642 nblinedisplayed++;
643 current_line++;
644 }
645}
646
647gint sort_perf(gconstpointer p1, gconstpointer p2, gpointer key)
648{
649 struct processtop *n1 = *(struct processtop **) p1;
650 struct processtop *n2 = *(struct processtop **) p2;
651
652 struct perfcounter *tmp1, *tmp2;
653 unsigned long totaln2 = 0;
654 unsigned long totaln1 = 0;
655
656 if (!key)
657 return 0;
658
659 tmp1 = g_hash_table_lookup(n1->perf, key);
660 if (!tmp1)
661 totaln1 = 0;
662 else
663 totaln1 = tmp1->count;
664
665 tmp2 = g_hash_table_lookup(n2->perf, key);
666 if (!tmp2)
667 totaln2 = 0;
668 else
669 totaln2 = tmp2->count;
670
671 if (totaln1 < totaln2)
672 return 1;
673 if (totaln1 == totaln2) {
674 totaln1 = n1->tid;
675 totaln2 = n2->tid;
676 if (totaln1 < totaln2)
677 return 1;
678 return -1;
679 }
680 return -1;
681}
682
683void print_key_title(char *key, int line)
684{
685 wattron(center, A_BOLD);
951303cc
JD
686 mvwprintw(center, line, 1, "%s", key);
687 mvwprintw(center, line, 30, " ");
1fc22eb4
JD
688 wattroff(center, A_BOLD);
689}
690
691void update_process_details()
692{
693 unsigned long elapsed;
694 double maxcputime;
d26643ed 695 struct processtop *tmp;
b093de8a
MB
696 struct files *file_tmp;
697 int i, j = 0;
69362330 698 char unit[4];
97514683 699 char filename_buf[COLS];
951303cc 700 int line = 1;
cc30e58d 701 int column;
32647247 702 GPtrArray *newfilearray = g_ptr_array_new();
951303cc
JD
703 GHashTableIter iter;
704 struct perfcounter *perfn1, *perfn2;
705 gpointer key;
1fc22eb4
JD
706
707 set_window_title(center, "Process details");
708
709
d26643ed
JD
710 tmp = find_process_tid(data,
711 selected_process->tid,
712 selected_process->comm);
1fc22eb4
JD
713 elapsed = data->end - data->start;
714 maxcputime = elapsed * data->cpu_table->len / 100.0;
715
951303cc 716 print_key_title("Name", line++);
d26643ed 717 wprintw(center, "%s", selected_process->comm);
951303cc 718 print_key_title("TID", line++);
d26643ed 719 wprintw(center, "%d", selected_process->tid);
1fc22eb4
JD
720 if (!tmp) {
721 print_key_title("Does not exit at this time", 3);
722 return;
723 }
724
951303cc 725 print_key_title("PID", line++);
1fc22eb4 726 wprintw(center, "%d", tmp->pid);
951303cc 727 print_key_title("PPID", line++);
1fc22eb4 728 wprintw(center, "%d", tmp->ppid);
1402044a
JD
729 print_key_title("VPID", line++);
730 wprintw(center, "%d", tmp->vpid);
731 print_key_title("VTID", line++);
732 wprintw(center, "%d", tmp->vtid);
733 print_key_title("VPPID", line++);
734 wprintw(center, "%d", tmp->vppid);
951303cc 735 print_key_title("CPU", line++);
1fc22eb4 736 wprintw(center, "%1.2f %%", tmp->totalcpunsec/maxcputime);
b093de8a 737
951303cc 738 print_key_title("READ B/s", line++);
69362330
JD
739 scale_unit(tmp->fileread, unit);
740 wprintw(center, "%s", unit);
b093de8a 741
951303cc 742 print_key_title("WRITE B/s", line++);
69362330
JD
743 scale_unit(tmp->filewrite, unit);
744 wprintw(center, "%s", unit);
b093de8a 745
951303cc
JD
746 g_hash_table_iter_init(&iter, global_perf_liszt);
747 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
748 print_key_title((char *) key, line++);
749 perfn2 = g_hash_table_lookup(tmp->perf, (char *) key);
750 wprintw(center, "%d", perfn2 ? perfn2->count : 0);
751 }
752 line++;
753
93d80d35 754 wattron(center, A_BOLD);
cc30e58d
JD
755 column = 1;
756 for (i = 0; i < 3; i++) {
41ff0645
JD
757 if (fileview[i].sort) {
758 pref_current_sort = i;
cc30e58d 759 wattron(center, A_UNDERLINE);
41ff0645 760 }
cc30e58d
JD
761 mvwprintw(center, line, column, fileview[i].title);
762 wattroff(center, A_UNDERLINE);
763 column += 10;
764 }
765 mvwprintw(center, line++, column, "FILENAME");
93d80d35
JD
766 wattroff(center, A_BOLD);
767
32647247
JD
768 /*
769 * since the process_files_table array could contain NULL file structures,
770 * and that the positions inside the array is important (it is the FD), we
771 * need to create a temporary array that we can sort.
772 */
773 for (i = 0; i < tmp->process_files_table->len; i++) {
774 file_tmp = g_ptr_array_index(tmp->process_files_table, i);
775 if (file_tmp)
776 g_ptr_array_add(newfilearray, file_tmp);
777 }
778
779 if (fileview[0].sort == 1)
780 g_ptr_array_sort(newfilearray, sort_by_file_fd_desc);
781 else if (fileview[1].sort == 1)
782 g_ptr_array_sort(newfilearray, sort_by_file_read_desc);
783 else if (fileview[2].sort == 1)
784 g_ptr_array_sort(newfilearray, sort_by_file_write_desc);
785 else
786 g_ptr_array_sort(newfilearray, sort_by_file_read_desc);
787
788 for (i = selected_line; i < newfilearray->len &&
951303cc 789 i < (selected_line + max_center_lines - line + 2); i++) {
32647247
JD
790 file_tmp = g_ptr_array_index(newfilearray, i);
791 if (!file_tmp)
792 continue;
951303cc 793 mvwprintw(center, line + j, 1, "%d", file_tmp->fd);
32647247 794 scale_unit(file_tmp->read, unit);
cc30e58d 795 mvwprintw(center, line + j, 11, "%s", unit);
32647247 796 scale_unit(file_tmp->write, unit);
cc30e58d 797 mvwprintw(center, line + j, 21, "%s", unit);
32647247 798 snprintf(filename_buf, COLS - 25, "%s", file_tmp->name);
cc30e58d 799 mvwprintw(center, line + j, 31, "%s", filename_buf);
32647247 800 j++;
b093de8a 801 }
32647247 802 g_ptr_array_free(newfilearray, TRUE);
1fc22eb4
JD
803}
804
805void update_perf()
806{
bb053abb 807 int i;
1fc22eb4
JD
808 int nblinedisplayed = 0;
809 int current_line = 0;
810 struct processtop *tmp;
811 int header_offset = 2;
812 int perf_row = 40;
813 struct perfcounter *perfn1, *perfn2;
1fc22eb4
JD
814 char *perf_key = NULL;
815 int value;
85db4618
JD
816 GHashTableIter iter;
817 gpointer key;
1fc22eb4
JD
818
819 set_window_title(center, "Perf Top");
820 wattron(center, A_BOLD);
821 mvwprintw(center, 1, 1, "PID");
822 mvwprintw(center, 1, 11, "TID");
823 mvwprintw(center, 1, 22, "NAME");
824
825 perf_row = 40;
0d91c12a 826 g_hash_table_iter_init(&iter, global_perf_liszt);
85db4618 827 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
1fc22eb4 828 if (perfn1->visible) {
41ff0645 829 if (perfn1->sort) {
bc958efa 830 /* pref_current_sort = i; */
41ff0645 831 wattron(center, A_UNDERLINE);
41ff0645 832 }
6419078d 833 /* + 5 to strip the "perf_" prefix */
1fc22eb4 834 mvwprintw(center, 1, perf_row, "%s",
6419078d 835 (char *) key + 5);
41ff0645 836 wattroff(center, A_UNDERLINE);
1fc22eb4
JD
837 perf_row += 20;
838 }
839 if (perfn1->sort) {
85db4618 840 perf_key = (char *) key;
1fc22eb4 841 }
1fc22eb4
JD
842 }
843 wattroff(center, A_BOLD);
844
845 g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key);
85db4618 846
91be6bb5 847 for (i = 0; i < data->process_table->len &&
1fc22eb4 848 nblinedisplayed < max_center_lines; i++) {
1fc22eb4 849 tmp = g_ptr_array_index(data->process_table, i);
c8d75a13 850
da4353bb 851 if (toggle_filter > 0 && !lookup_filter_tid_list(tmp->tid))
c8d75a13
JD
852 continue;
853
e15ed00a
JD
854 if (tmp->pid != tmp->tid)
855 if (toggle_threads == -1)
856 continue;
1fc22eb4 857
635dc837 858 if (process_selected(tmp)) {
da4353bb
JD
859 if (current_line == selected_line)
860 wattron(center, COLOR_PAIR(7));
861 else
862 wattron(center, COLOR_PAIR(6));
635dc837 863 }
1fc22eb4
JD
864 if (current_line == selected_line) {
865 selected_process = tmp;
866 wattron(center, COLOR_PAIR(5));
867 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
868 }
869
870 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
871 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
872 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
873
0d91c12a 874 g_hash_table_iter_init(&iter, global_perf_liszt);
1fc22eb4
JD
875
876 perf_row = 40;
85db4618 877 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
1fc22eb4 878 if (perfn1->visible) {
85db4618 879 perfn2 = g_hash_table_lookup(tmp->perf, (char *) key);
1fc22eb4
JD
880 if (perfn2)
881 value = perfn2->count;
882 else
883 value = 0;
b093de8a
MB
884 mvwprintw(center, current_line + header_offset,
885 perf_row, "%d", value);
1fc22eb4
JD
886 perf_row += 20;
887 }
1fc22eb4
JD
888 }
889
635dc837 890 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
891 wattroff(center, COLOR_PAIR(5));
892 nblinedisplayed++;
893 current_line++;
894 }
895}
896
1fc22eb4
JD
897void update_iostream()
898{
899 int i;
900 int header_offset = 2;
901 struct processtop *tmp;
902 int nblinedisplayed = 0;
903 int current_line = 0;
b093de8a 904 int total = 0;
69362330 905 char unit[4];
41f5890b 906 int column;
1fc22eb4
JD
907
908 set_window_title(center, "IO Top");
909 wattron(center, A_BOLD);
69362330
JD
910 mvwprintw(center, 1, 1, "PID");
911 mvwprintw(center, 1, 11, "TID");
912 mvwprintw(center, 1, 22, "NAME");
41f5890b
JD
913 column = 40;
914 for (i = 0; i < 3; i++) {
41ff0645
JD
915 if (iostreamtopview[i].sort) {
916 pref_current_sort = i;
41f5890b 917 wattron(center, A_UNDERLINE);
41ff0645 918 }
41f5890b
JD
919 mvwprintw(center, 1, column, iostreamtopview[i].title);
920 wattroff(center, A_UNDERLINE);
921 column += 12;
922 }
1fc22eb4 923 wattroff(center, A_BOLD);
41f5890b 924 wattroff(center, A_UNDERLINE);
1fc22eb4 925
89f1e0d1
JD
926 if (iostreamtopview[0].sort == 1)
927 g_ptr_array_sort(data->process_table, sort_by_process_read_desc);
928 else if (iostreamtopview[1].sort == 1)
929 g_ptr_array_sort(data->process_table, sort_by_process_write_desc);
930 else if (iostreamtopview[2].sort == 1)
931 g_ptr_array_sort(data->process_table, sort_by_process_total_desc);
932 else
933 g_ptr_array_sort(data->process_table, sort_by_process_total_desc);
1fc22eb4
JD
934
935 for (i = list_offset; i < data->process_table->len &&
936 nblinedisplayed < max_center_lines; i++) {
937 tmp = g_ptr_array_index(data->process_table, i);
c8d75a13 938
da4353bb 939 if (toggle_filter > 0 && !lookup_filter_tid_list(tmp->tid))
c8d75a13
JD
940 continue;
941
e15ed00a
JD
942 if (tmp->pid != tmp->tid)
943 if (toggle_threads == -1)
944 continue;
1fc22eb4 945
635dc837 946 if (process_selected(tmp)) {
da4353bb
JD
947 if (current_line == selected_line)
948 wattron(center, COLOR_PAIR(7));
949 else
950 wattron(center, COLOR_PAIR(6));
635dc837 951 }
1fc22eb4
JD
952 if (current_line == selected_line) {
953 selected_process = tmp;
1fc22eb4
JD
954 wattron(center, COLOR_PAIR(5));
955 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
956 }
69362330
JD
957 /* TGID */
958 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
959 /* PID */
960 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
961 /* NAME */
962 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
b093de8a 963
1fc22eb4 964 /* READ (bytes/sec) */
69362330
JD
965 scale_unit(tmp->fileread, unit);
966 mvwprintw(center, current_line + header_offset, 40, "%s", unit);
1fc22eb4
JD
967
968 /* WRITE (bytes/sec) */
69362330
JD
969 scale_unit(tmp->filewrite, unit);
970 mvwprintw(center, current_line + header_offset, 52, "%s", unit);
1fc22eb4
JD
971
972 /* TOTAL STREAM */
b093de8a
MB
973 total = tmp->totalfileread + tmp->totalfilewrite;
974
69362330
JD
975 scale_unit(total, unit);
976 mvwprintw(center, current_line + header_offset, 64, "%s", unit);
b093de8a 977
635dc837 978 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
979 wattroff(center, COLOR_PAIR(5));
980 nblinedisplayed++;
981 current_line++;
982 }
983}
984
985void update_current_view()
986{
987 sem_wait(&update_display_sem);
988 if (!data)
989 return;
990 update_header();
991
992 werase(center);
993 box(center, 0, 0);
994 switch (current_view) {
995 case cpu:
996 update_cputop_display();
997 break;
998 case perf:
999 update_perf();
1000 break;
1001 case process_details:
1002 update_process_details();
1003 break;
1fc22eb4
JD
1004 case iostream:
1005 update_iostream();
1006 break;
1007 case tree:
1008 update_cputop_display();
1009 break;
fbbda4da
JD
1010 case kprobes:
1011 update_kprobes_display();
1012 break;
1fc22eb4
JD
1013 default:
1014 break;
1015 }
1016 update_panels();
1017 doupdate();
1018 sem_post(&update_display_sem);
1019}
1020
41ff0645
JD
1021void update_process_detail_sort(int *line_selected)
1022{
1023 int i;
1024 int size;
1025
1026 size = 3;
1027
1028 if (*line_selected > (size - 1))
1029 *line_selected = size - 1;
1030 else if (*line_selected < 0)
1031 *line_selected = 0;
1032
1033 if (fileview[*line_selected].sort == 1)
1034 fileview[*line_selected].reverse = 1;
1035 for (i = 0; i < size; i++)
1036 fileview[i].sort = 0;
1037 fileview[*line_selected].sort = 1;
1038}
1039
32647247
JD
1040void update_process_detail_pref(int *line_selected, int toggle_view, int toggle_sort)
1041{
1042 int i;
1043 int size;
1044
1045 if (!data)
1046 return;
1047 if (pref_panel_window) {
1048 del_panel(pref_panel);
1049 delwin(pref_panel_window);
1050 }
1051 size = 3;
1052
1053 pref_panel_window = create_window(size + 2, 30, 10, 10);
1054 pref_panel = new_panel(pref_panel_window);
1055
1056 werase(pref_panel_window);
1057 box(pref_panel_window, 0 , 0);
1058 set_window_title(pref_panel_window, "Process Detail Preferences ");
1059 wattron(pref_panel_window, A_BOLD);
1060 mvwprintw(pref_panel_window, size + 1, 1,
d2fd8c70 1061 " 's' : sort, space : toggle");
32647247
JD
1062 wattroff(pref_panel_window, A_BOLD);
1063
1064 if (*line_selected > (size - 1))
1065 *line_selected = size - 1;
41ff0645
JD
1066 else if (*line_selected < 0)
1067 *line_selected = 0;
32647247 1068 if (toggle_sort == 1) {
41ff0645 1069 update_process_detail_sort(line_selected);
32647247
JD
1070 update_current_view();
1071 }
1072
1073 for (i = 0; i < size; i++) {
1074 if (i == *line_selected) {
1075 wattron(pref_panel_window, COLOR_PAIR(5));
1076 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1077 }
1078 if (fileview[i].sort == 1)
1079 wattron(pref_panel_window, A_BOLD);
d33aae69 1080 mvwprintw(pref_panel_window, i + 1, 1, "[-] %s",
32647247
JD
1081 fileview[i].title);
1082 wattroff(pref_panel_window, A_BOLD);
1083 wattroff(pref_panel_window, COLOR_PAIR(5));
1084
1085 }
1086 update_panels();
1087 doupdate();
1088}
1089
41ff0645
JD
1090void update_iostream_sort(int *line_selected)
1091{
1092 int i;
1093 int size;
1094
1095 size = 3;
1096 if (*line_selected > (size - 1))
1097 *line_selected = size - 1;
1098 else if (*line_selected < 0)
1099 *line_selected = 0;
1100 if (iostreamtopview[*line_selected].sort == 1)
1101 iostreamtopview[*line_selected].reverse = 1;
1102 for (i = 0; i < size; i++)
1103 iostreamtopview[i].sort = 0;
1104 iostreamtopview[*line_selected].sort = 1;
1105
1106}
1107
89f1e0d1
JD
1108void update_iostream_pref(int *line_selected, int toggle_view, int toggle_sort)
1109{
1110 int i;
1111 int size;
1112
1113 if (!data)
1114 return;
1115 if (pref_panel_window) {
1116 del_panel(pref_panel);
1117 delwin(pref_panel_window);
1118 }
1119 size = 3;
1120
1121 pref_panel_window = create_window(size + 2, 30, 10, 10);
1122 pref_panel = new_panel(pref_panel_window);
1123
1124 werase(pref_panel_window);
1125 box(pref_panel_window, 0 , 0);
1126 set_window_title(pref_panel_window, "IOTop Preferences ");
1127 wattron(pref_panel_window, A_BOLD);
1128 mvwprintw(pref_panel_window, size + 1, 1,
d2fd8c70 1129 " 's' : sort, space : toggle");
89f1e0d1
JD
1130 wattroff(pref_panel_window, A_BOLD);
1131
1132 if (*line_selected > (size - 1))
1133 *line_selected = size - 1;
41ff0645
JD
1134 else if (*line_selected < 0)
1135 *line_selected = 0;
89f1e0d1 1136 if (toggle_sort == 1) {
41ff0645 1137 update_iostream_sort(line_selected);
89f1e0d1
JD
1138 update_current_view();
1139 }
1140
1141 for (i = 0; i < size; i++) {
1142 if (i == *line_selected) {
1143 wattron(pref_panel_window, COLOR_PAIR(5));
1144 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1145 }
1146 if (iostreamtopview[i].sort == 1)
1147 wattron(pref_panel_window, A_BOLD);
d33aae69 1148 mvwprintw(pref_panel_window, i + 1, 1, "[-] %s",
89f1e0d1
JD
1149 iostreamtopview[i].title);
1150 wattroff(pref_panel_window, A_BOLD);
1151 wattroff(pref_panel_window, COLOR_PAIR(5));
1152
1153 }
1154 update_panels();
1155 doupdate();
1156}
1157
41ff0645
JD
1158void update_cpu_sort(int *line_selected)
1159{
1160 int i;
1161 int size = 3;
1162
1163 if (*line_selected > (size - 1))
1164 *line_selected = size - 1;
1165 else if (*line_selected < 0)
1166 *line_selected = 0;
1167
1168 /* special case, we don't support sorting by procname for now */
1169 if (*line_selected != 3) {
1170 if (cputopview[*line_selected].sort == 1)
1171 cputopview[*line_selected].reverse = 1;
1172 for (i = 0; i < size; i++)
1173 cputopview[i].sort = 0;
1174 cputopview[*line_selected].sort = 1;
1175 }
1176}
1177
3b15348c
JD
1178void update_cpu_pref(int *line_selected, int toggle_view, int toggle_sort)
1179{
1180 int i;
1181 int size;
1182
1183 if (!data)
1184 return;
1185 if (pref_panel_window) {
1186 del_panel(pref_panel);
1187 delwin(pref_panel_window);
1188 }
1189 size = 4;
1190
1191 pref_panel_window = create_window(size + 2, 30, 10, 10);
1192 pref_panel = new_panel(pref_panel_window);
1193
1194 werase(pref_panel_window);
1195 box(pref_panel_window, 0 , 0);
1196 set_window_title(pref_panel_window, "CPUTop Preferences ");
1197 wattron(pref_panel_window, A_BOLD);
1198 mvwprintw(pref_panel_window, size + 1, 1,
d2fd8c70 1199 " 's' : sort, space : toggle");
3b15348c
JD
1200 wattroff(pref_panel_window, A_BOLD);
1201
89f1e0d1
JD
1202 if (*line_selected > (size - 1))
1203 *line_selected = size - 1;
41ff0645
JD
1204 else if (*line_selected < 0)
1205 *line_selected = 0;
3b15348c 1206 if (toggle_sort == 1) {
41ff0645
JD
1207 update_cpu_sort(line_selected);
1208 update_current_view();
3b15348c
JD
1209 }
1210
89f1e0d1 1211 for (i = 0; i < size; i++) {
3b15348c
JD
1212 if (i == *line_selected) {
1213 wattron(pref_panel_window, COLOR_PAIR(5));
1214 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1215 }
1216 if (cputopview[i].sort == 1)
1217 wattron(pref_panel_window, A_BOLD);
d33aae69 1218 mvwprintw(pref_panel_window, i + 1, 1, "[-] %s",
3b15348c
JD
1219 cputopview[i].title);
1220 wattroff(pref_panel_window, A_BOLD);
1221 wattroff(pref_panel_window, COLOR_PAIR(5));
1222
1223 }
1224 update_panels();
1225 doupdate();
1226}
1227
41ff0645
JD
1228void update_perf_sort(int *line_selected)
1229{
1230 int i;
1231 struct perfcounter *perf;
1232 GList *perflist;
1233 int size;
1234
1235 size = g_hash_table_size(global_perf_liszt);
1236 if (*line_selected > (size - 1))
1237 *line_selected = size - 1;
1238 else if (*line_selected < 0)
1239 *line_selected = 0;
1240
1241 i = 0;
1242 perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
1243 while (perflist) {
1244 perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
1245 if (i != *line_selected)
1246 perf->sort = 0;
1247 else
1248 perf->sort = 1;
1249 i++;
1250 perflist = g_list_next(perflist);
1251 }
1252}
1253
3b15348c 1254void update_perf_pref(int *line_selected, int toggle_view, int toggle_sort)
1fc22eb4 1255{
1e05f0ae
JD
1256 int i;
1257 struct perfcounter *perf;
1258 GList *perflist;
1fc22eb4 1259 int size;
1e05f0ae 1260
1fc22eb4
JD
1261 if (!data)
1262 return;
0d91c12a
JD
1263 if (pref_panel_window) {
1264 del_panel(pref_panel);
1265 delwin(pref_panel_window);
1fc22eb4 1266 }
0d91c12a 1267 size = g_hash_table_size(global_perf_liszt);
1e05f0ae 1268
0d91c12a
JD
1269 pref_panel_window = create_window(size + 2, 30, 10, 10);
1270 pref_panel = new_panel(pref_panel_window);
1fc22eb4 1271
0d91c12a
JD
1272 werase(pref_panel_window);
1273 box(pref_panel_window, 0 , 0);
1274 set_window_title(pref_panel_window, "Perf Preferences ");
1275 wattron(pref_panel_window, A_BOLD);
1276 mvwprintw(pref_panel_window, g_hash_table_size(global_perf_liszt) + 1, 1,
d2fd8c70 1277 " 's' : sort, space : toggle");
0d91c12a 1278 wattroff(pref_panel_window, A_BOLD);
1fc22eb4 1279
41ff0645
JD
1280 if (*line_selected > (size - 1))
1281 *line_selected = size - 1;
1282 else if (*line_selected < 0)
1283 *line_selected = 0;
1284
1fc22eb4 1285 if (toggle_sort == 1) {
41ff0645 1286 update_perf_sort(line_selected);
1fc22eb4
JD
1287 update_current_view();
1288 }
1289
1290 i = 0;
0d91c12a 1291 perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
1fc22eb4 1292 while (perflist) {
0d91c12a 1293 perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
3b15348c 1294 if (i == *line_selected && toggle_view == 1) {
1fc22eb4
JD
1295 perf->visible = perf->visible == 1 ? 0:1;
1296 update_current_view();
1297 }
3b15348c 1298 if (i == *line_selected) {
0d91c12a
JD
1299 wattron(pref_panel_window, COLOR_PAIR(5));
1300 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1fc22eb4
JD
1301 }
1302 if (perf->sort == 1)
0d91c12a
JD
1303 wattron(pref_panel_window, A_BOLD);
1304 mvwprintw(pref_panel_window, i + 1, 1, "[%c] %s",
1fc22eb4 1305 perf->visible == 1 ? 'x' : ' ',
b8a1df45 1306 (char *) perflist->data + 5);
0d91c12a
JD
1307 wattroff(pref_panel_window, A_BOLD);
1308 wattroff(pref_panel_window, COLOR_PAIR(5));
1fc22eb4
JD
1309 i++;
1310 perflist = g_list_next(perflist);
1311 }
1312 update_panels();
1313 doupdate();
1314}
1315
467097ac
JD
1316void update_hostname_pref(int *line_selected, int toggle_filter, int toggle_sort)
1317{
1318 int i;
1319 struct host *host;
1320 GList *hostlist;
1321 int size;
1322
1323 if (!data)
1324 return;
1325 if (pref_panel_window) {
1326 del_panel(pref_panel);
1327 delwin(pref_panel_window);
1328 }
1329 size = g_hash_table_size(global_host_list);
1330
1331 pref_panel_window = create_window(size + 2, 30, 10, 10);
1332 pref_panel = new_panel(pref_panel_window);
1333
1334 werase(pref_panel_window);
1335 box(pref_panel_window, 0 , 0);
1336 set_window_title(pref_panel_window, "Hosts Preferences ");
1337 wattron(pref_panel_window, A_BOLD);
1338 mvwprintw(pref_panel_window, g_hash_table_size(global_host_list) + 1, 1,
1339 " space : toggle filter");
1340 wattroff(pref_panel_window, A_BOLD);
1341
1342 if (*line_selected > (size - 1))
1343 *line_selected = size - 1;
1344 else if (*line_selected < 0)
1345 *line_selected = 0;
1346
1347 i = 0;
1348 hostlist = g_list_first(g_hash_table_get_keys(global_host_list));
1349 while (hostlist) {
1350 host = g_hash_table_lookup(global_host_list, hostlist->data);
1351 if (i == *line_selected && toggle_filter == 1) {
1352 host->filter = host->filter == 1 ? 0:1;
b99227ca 1353 update_hostname_filter(host);
467097ac
JD
1354 update_current_view();
1355 }
1356 if (i == *line_selected) {
1357 wattron(pref_panel_window, COLOR_PAIR(5));
1358 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1359 }
1360 if (host->filter == 1)
1361 wattron(pref_panel_window, A_BOLD);
1362 mvwprintw(pref_panel_window, i + 1, 1, "[%c] %s",
1363 host->filter == 1 ? 'x' : ' ',
1364 (char *) hostlist->data);
1365 wattroff(pref_panel_window, A_BOLD);
1366 wattroff(pref_panel_window, COLOR_PAIR(5));
1367 i++;
1368 hostlist = g_list_next(hostlist);
1369 }
1370 update_panels();
1371 doupdate();
1372}
1373
3b15348c 1374int update_preference_panel(int *line_selected, int toggle_view, int toggle_sort)
1fc22eb4 1375{
1e05f0ae
JD
1376 int ret = 0;
1377
0d91c12a
JD
1378 switch(current_view) {
1379 case perf:
467097ac
JD
1380 if (filter_host_panel)
1381 update_hostname_pref(line_selected,
1382 toggle_view, toggle_sort);
1383 else
1384 update_perf_pref(line_selected,
1385 toggle_view, toggle_sort);
3b15348c
JD
1386 break;
1387 case cpu:
467097ac
JD
1388 if (filter_host_panel)
1389 update_hostname_pref(line_selected,
1390 toggle_view, toggle_sort);
1391 else
1392 update_cpu_pref(line_selected,
1393 toggle_view, toggle_sort);
0d91c12a 1394 break;
89f1e0d1 1395 case iostream:
467097ac
JD
1396 if (filter_host_panel)
1397 update_hostname_pref(line_selected,
1398 toggle_view, toggle_sort);
1399 else
1400 update_iostream_pref(line_selected,
1401 toggle_view, toggle_sort);
89f1e0d1 1402 break;
32647247 1403 case process_details:
467097ac
JD
1404 update_process_detail_pref(line_selected,
1405 toggle_view, toggle_sort);
32647247 1406 break;
0d91c12a 1407 default:
1e05f0ae 1408 ret = -1;
0d91c12a
JD
1409 break;
1410 }
1e05f0ae
JD
1411
1412 return ret;
0d91c12a
JD
1413}
1414
41ff0645
JD
1415int update_sort(int *line_selected)
1416{
1417 int ret = 0;
1418
1419 switch(current_view) {
1420 case perf:
1421 update_perf_sort(line_selected);
1422 break;
1423 case cpu:
1424 update_cpu_sort(line_selected);
1425 break;
1426 case iostream:
1427 update_iostream_sort(line_selected);
1428 break;
1429 case process_details:
1430 update_process_detail_sort(line_selected);
1431 break;
1432 default:
1433 ret = -1;
1434 break;
1435 }
1436
1437 return ret;
1438}
1439
0d91c12a
JD
1440void toggle_pref_panel(void)
1441{
1e05f0ae
JD
1442 int ret;
1443
0d91c12a
JD
1444 if (pref_panel_visible) {
1445 hide_panel(pref_panel);
1446 pref_panel_visible = 0;
1fc22eb4 1447 } else {
3b15348c 1448 ret = update_preference_panel(&pref_line_selected, 0, 0);
1e05f0ae
JD
1449 if (ret < 0)
1450 return;
0d91c12a
JD
1451 show_panel(pref_panel);
1452 pref_panel_visible = 1;
1fc22eb4
JD
1453 }
1454 update_panels();
1455 doupdate();
1456}
1457
467097ac
JD
1458void toggle_host_panel(void)
1459{
1460 int ret;
1461
1462 filter_host_panel = filter_host_panel ? 0 : 1;
1463 if (pref_panel_visible) {
1464 hide_panel(pref_panel);
1465 pref_panel_visible = 0;
1466 } else {
1467 ret = update_preference_panel(&pref_line_selected, 0, 0);
1468 if (ret < 0)
1469 return;
1470 show_panel(pref_panel);
1471 pref_panel_visible = 1;
1472 }
1473 update_panels();
1474 doupdate();
1475}
1476
1fc22eb4
JD
1477void display(unsigned int index)
1478{
1479 last_display_index = index;
1480 currently_displayed_index = index;
1481 data = g_ptr_array_index(copies, index);
1482 if (!data)
1483 return;
1484 max_elements = data->process_table->len;
1485 update_current_view();
1486 update_footer();
1487 update_panels();
1488 doupdate();
1489}
1490
1491void pause_display()
1492{
1493 toggle_pause = 1;
1494 print_log("Pause");
1495 sem_wait(&pause_sem);
1496}
1497
1498void resume_display()
1499{
1500 toggle_pause = -1;
1501 print_log("Resume");
1502 sem_post(&pause_sem);
1503}
1504
1505void *handle_keyboard(void *p)
1506{
1507 int ch;
1508 while((ch = getch())) {
1509 switch(ch) {
1510 /* Move the cursor and scroll */
8c05e516 1511 case 'j':
1fc22eb4 1512 case KEY_DOWN:
0d91c12a 1513 if (pref_panel_visible) {
3b15348c
JD
1514 pref_line_selected++;
1515 update_preference_panel(&pref_line_selected, 0, 0);
1fc22eb4
JD
1516 } else {
1517 if (selected_line < (max_center_lines - 1) &&
1518 selected_line < max_elements - 1) {
1519 selected_line++;
1520 selected_in_list++;
1521 } else if (selected_in_list < (max_elements - 1)
1522 && (list_offset < (max_elements - max_center_lines))) {
1523 selected_in_list++;
1524 list_offset++;
1525 }
1526 update_current_view();
1527 }
1528 break;
1529 case KEY_NPAGE:
1fc22eb4 1530 break;
8c05e516 1531 case 'k':
1fc22eb4 1532 case KEY_UP:
0d91c12a 1533 if (pref_panel_visible) {
3b15348c
JD
1534 if (pref_line_selected > 0)
1535 pref_line_selected--;
1536 update_preference_panel(&pref_line_selected, 0, 0);
1fc22eb4
JD
1537 } else {
1538 if (selected_line > 0) {
1539 selected_line--;
1540 selected_in_list--;
1541 } else if (selected_in_list > 0 && list_offset > 0) {
1542 selected_in_list--;
1543 list_offset--;
1544 }
1545 update_current_view();
1546 }
1547 break;
1548 case KEY_PPAGE:
1fc22eb4
JD
1549 break;
1550
1551 /* Navigate the history with arrows */
1552 case KEY_LEFT:
1553 if (currently_displayed_index > 0) {
1554 currently_displayed_index--;
1555 print_log("Going back in time");
1556 } else {
1557 print_log("Cannot rewind, last data is already displayed");
1558 }
1559 data = g_ptr_array_index(copies, currently_displayed_index);
1560 max_elements = data->process_table->len;
1561
1562 /* we force to pause the display when moving in time */
1563 if (toggle_pause < 0)
1564 pause_display();
1565
1566 update_current_view();
1567 update_footer();
1568 break;
1569 case KEY_RIGHT:
1570 if (currently_displayed_index < last_display_index) {
1571 currently_displayed_index++;
1572 print_log("Going forward in time");
1573 data = g_ptr_array_index(copies, currently_displayed_index);
1574 max_elements = data->process_table->len;
1575 update_current_view();
1576 update_footer();
1577 } else {
1578 print_log("Manually moving forward");
1579 sem_post(&timer);
3e59e768
JD
1580 if (toggle_pause > 0) {
1581 sem_post(&pause_sem);
1582 update_current_view();
1583 sem_wait(&pause_sem);
1584 }
1fc22eb4
JD
1585 }
1586
1587 break;
1588 case ' ':
0d91c12a 1589 if (pref_panel_visible) {
3b15348c 1590 update_preference_panel(&pref_line_selected, 1, 0);
635dc837
JD
1591 } else {
1592 update_selected_processes();
da4353bb
JD
1593 if (toggle_filter > 0) {
1594 max_elements = g_hash_table_size(global_filter_list);
da4353bb
JD
1595 if (selected_line >= max_elements)
1596 selected_line = max_elements - 1;
1597 }
635dc837
JD
1598 update_current_view();
1599 }
1fc22eb4
JD
1600 break;
1601 case 's':
0d91c12a 1602 if (pref_panel_visible)
3b15348c 1603 update_preference_panel(&pref_line_selected, 0, 1);
1fc22eb4 1604 break;
41ff0645 1605 case '>':
bc958efa
JD
1606 /* perf uses a hashtable, it is ordered backward */
1607 if (current_view == perf) {
1608 pref_current_sort--;
1609 } else if (!pref_panel_visible) {
41ff0645 1610 pref_current_sort++;
41ff0645 1611 }
bc958efa
JD
1612 update_sort(&pref_current_sort);
1613 update_current_view();
41ff0645
JD
1614 break;
1615 case '<':
bc958efa
JD
1616 /* perf uses a hashtable, it is ordered backward */
1617 if (current_view == perf) {
1618 pref_current_sort++;
1619 } else if (!pref_panel_visible) {
1620 pref_current_sort--;
41ff0645 1621 }
bc958efa
JD
1622 update_sort(&pref_current_sort);
1623 update_current_view();
41ff0645 1624 break;
1fc22eb4
JD
1625
1626 case 13: /* FIXME : KEY_ENTER ?? */
d2fd8c70
JD
1627 if (pref_panel_visible)
1628 break;
041aa219
JD
1629 if (current_view != process_details) {
1630 previous_view = current_view;
1fc22eb4 1631 current_view = process_details;
041aa219
JD
1632 } else {
1633 current_view = previous_view;
1634 previous_view = process_details;
1fc22eb4 1635 }
7d9ea41a 1636 selected_line = 0;
1fc22eb4
JD
1637 update_current_view();
1638 break;
1639
1640 case KEY_F(1):
3b15348c
JD
1641 if (pref_panel_visible)
1642 toggle_pref_panel();
1fc22eb4 1643 current_view = cpu;
af54ebcf 1644 selected_line = 0;
1fc22eb4
JD
1645 update_current_view();
1646 break;
1647 case KEY_F(2):
3b15348c
JD
1648 if (pref_panel_visible)
1649 toggle_pref_panel();
1fc22eb4 1650 current_view = cpu;
af54ebcf 1651 selected_line = 0;
1fc22eb4
JD
1652 update_current_view();
1653 break;
1654 case KEY_F(3):
3b15348c
JD
1655 if (pref_panel_visible)
1656 toggle_pref_panel();
1fc22eb4 1657 current_view = perf;
af54ebcf 1658 selected_line = 0;
1fc22eb4
JD
1659 update_current_view();
1660 break;
1661 case KEY_F(4):
3b15348c
JD
1662 if (pref_panel_visible)
1663 toggle_pref_panel();
1fc22eb4 1664 current_view = iostream;
af54ebcf 1665 selected_line = 0;
1fc22eb4
JD
1666 update_current_view();
1667 break;
fbbda4da
JD
1668 case KEY_F(5):
1669 if (pref_panel_visible)
1670 toggle_pref_panel();
1671 current_view = kprobes;
1672 selected_line = 0;
1673 update_current_view();
1674 break;
1fc22eb4
JD
1675 case KEY_F(10):
1676 case 'q':
1677 reset_ncurses();
ae9e85c7
JD
1678 /* exit keyboard thread */
1679 pthread_exit(0);
1fc22eb4 1680 break;
da4353bb
JD
1681 case 'f':
1682 toggle_filter *= -1;
1683 selected_line = 0;
1684 if (toggle_filter > 0)
1685 max_elements = g_hash_table_size(global_filter_list);
1686 else
1687 max_elements = data->process_table->len;
1688 update_current_view();
1689 break;
467097ac
JD
1690 case 'h':
1691 toggle_host_panel();
1692 break;
1fc22eb4
JD
1693 case 't':
1694 toggle_threads *= -1;
1695 update_current_view();
1696 break;
1697 case 'p':
1698 if (toggle_pause < 0) {
1699 pause_display();
1700 } else {
1701 resume_display();
1702 }
33572a17 1703 break;
f9ff6939 1704 case 'r':
0d91c12a 1705 toggle_pref_panel();
1fc22eb4 1706 break;
1402044a
JD
1707 case 'v':
1708 toggle_virt *= -1;
1709 update_current_view();
1710 break;
d2fd8c70
JD
1711 /* ESCAPE, but slow to process, don't know why */
1712 case 27:
1713 if (pref_panel_visible)
1714 toggle_pref_panel();
1715 else if (current_view == process_details) {
1716 current_view = previous_view;
1717 previous_view = process_details;
1718 }
1719 update_current_view();
1720 break;
1fc22eb4 1721 default:
b093de8a
MB
1722 if (data)
1723 update_current_view();
1fc22eb4
JD
1724 break;
1725 }
1726 update_footer();
1727 }
1728 return NULL;
1729}
1730
89f1e0d1
JD
1731void init_view_headers()
1732{
1733 cputopview[0].title = strdup("CPU(%)");
1734 cputopview[0].sort = 1;
6153ec12
JD
1735 cputopview[1].title = strdup("PID");
1736 cputopview[2].title = strdup("TID");
1402044a
JD
1737 cputopview[3].title = strdup("VPID");
1738 cputopview[4].title = strdup("VTID");
1739 cputopview[5].title = strdup("NAME");
89f1e0d1
JD
1740
1741 iostreamtopview[0].title = strdup("R (B/sec)");
1742 iostreamtopview[1].title = strdup("W (B/sec)");
1743 iostreamtopview[2].title = strdup("Total (B)");
1744 iostreamtopview[2].sort = 1;
32647247
JD
1745
1746 fileview[0].title = strdup("FD");
1747 fileview[1].title = strdup("READ");
1748 fileview[1].sort = 1;
1749 fileview[2].title = strdup("WRITE");
246d5992
JD
1750
1751 kprobeview[0].title = strdup("NAME");
1752 kprobeview[1].title = strdup("HIT");
1753 kprobeview[1].sort = 1;
89f1e0d1
JD
1754}
1755
1fc22eb4
JD
1756void init_ncurses()
1757{
1758 sem_init(&update_display_sem, 0, 1);
89f1e0d1 1759 init_view_headers();
1fc22eb4
JD
1760 init_screen();
1761
dc8f04dd
JD
1762 header = create_window(5, COLS - 1, 0, 0);
1763 center = create_window(LINES - 5 - 7, COLS - 1, 5, 0);
1fc22eb4
JD
1764 status = create_window(MAX_LOG_LINES + 2, COLS - 1, LINES - 7, 0);
1765 footer = create_window(1, COLS - 1, LINES - 1, 0);
1766
1767 print_log("Starting display");
1768
1769 main_panel = new_panel(center);
1fc22eb4
JD
1770
1771 current_view = cpu;
1772
1773 basic_header();
1774 update_footer();
1775
1776 pthread_create(&keyboard_thread, NULL, handle_keyboard, (void *)NULL);
1777}
ec016cb4
JD
1778
1779#endif /* HAVE_LIBNCURSES */
This page took 0.193644 seconds and 4 git commands to generate.