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