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