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