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