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