2 * Copyright (C) 2011-2012 Julien Desfossez
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;
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.
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.
25 #include <semaphore.h>
27 #include "cursesdisplay.h"
28 #include "lttngtoptypes.h"
29 #include "iostreamtop.h"
32 #define DEFAULT_DELAY 15
33 #define MAX_LINE_LENGTH 50
34 #define MAX_LOG_LINES 4
36 /* to prevent concurrent updates of the different windows */
37 sem_t update_display_sem
;
40 WINDOW
*footer
, *header
, *center
, *status
;
41 WINDOW
*pref_panel_window
= NULL
;
42 PANEL
*pref_panel
, *main_panel
;
44 int pref_panel_visible
= 0;
45 int pref_line_selected
= 0;
46 int pref_current_sort
= 0;
48 int last_display_index
, currently_displayed_index
;
50 struct processtop
*selected_process
= NULL
;
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) */
57 char log_lines
[MAX_LINE_LENGTH
* MAX_LOG_LINES
+ MAX_LOG_LINES
];
59 int max_elements
= 80;
61 int toggle_threads
= 1;
63 int toggle_pause
= -1;
66 GPtrArray
*selected_processes
;
68 pthread_t keyboard_thread
;
70 struct header_view cputopview
[6];
71 struct header_view iostreamtopview
[3];
72 struct header_view fileview
[3];
81 sem_post(&goodtodisplay
);
82 sem_post(&end_trace_sem
);
83 sem_post(&goodtoupdate
);
86 static void handle_sigterm(int signal
)
88 pthread_cancel(keyboard_thread
);
96 halfdelay(DEFAULT_DELAY
);
98 intrflush(stdscr
, false);
104 init_pair(1, COLOR_RED
, COLOR_BLACK
); /* - */
105 init_pair(2, COLOR_GREEN
, COLOR_BLACK
); /* + */
106 init_pair(3, COLOR_BLACK
, COLOR_WHITE
); /* keys */
107 init_pair(4, COLOR_WHITE
, COLOR_GREEN
); /* keys activated */
108 init_pair(5, COLOR_WHITE
, COLOR_BLUE
); /* select line */
109 init_pair(6, COLOR_WHITE
, COLOR_GREEN
); /* selected process */
111 termtype
= getenv("TERM");
112 if (!strcmp(termtype
, "xterm") || !strcmp(termtype
, "xterm-color") ||
113 !strcmp(termtype
, "vt220")) {
114 define_key("\033[H", KEY_HOME
);
115 define_key("\033[F", KEY_END
);
116 define_key("\033OP", KEY_F(1));
117 define_key("\033OQ", KEY_F(2));
118 define_key("\033OR", KEY_F(3));
119 define_key("\033OS", KEY_F(4));
120 define_key("\0330U", KEY_F(6));
121 define_key("\033[11~", KEY_F(1));
122 define_key("\033[12~", KEY_F(2));
123 define_key("\033[13~", KEY_F(3));
124 define_key("\033[14~", KEY_F(4));
125 define_key("\033[16~", KEY_F(6));
126 define_key("\033[17;2~", KEY_F(18));
128 signal(SIGTERM
, handle_sigterm
);
129 signal(SIGINT
, handle_sigterm
);
130 mousemask(BUTTON1_CLICKED
, NULL
);
134 WINDOW
*create_window(int height
, int width
, int startx
, int starty
)
137 win
= newwin(height
, width
, startx
, starty
);
143 WINDOW
*create_window_no_border(int height
, int width
, int startx
, int starty
)
146 win
= newwin(height
, width
, startx
, starty
);
151 void print_digit(WINDOW
*win
, int digit
)
154 wattron(win
, COLOR_PAIR(1));
155 wprintw(win
, "%d", digit
);
156 wattroff(win
, COLOR_PAIR(1));
157 } else if (digit
> 0) {
158 wattron(win
, COLOR_PAIR(2));
159 wprintw(win
, "+%d", digit
);
160 wattroff(win
, COLOR_PAIR(2));
166 void print_digits(WINDOW
*win
, int first
, int second
)
169 print_digit(win
, first
);
171 print_digit(win
, second
);
175 void print_headers(int line
, char *desc
, int value
, int first
, int second
)
177 wattron(header
, A_BOLD
);
178 mvwprintw(header
, line
, 4, "%s", desc
);
179 wattroff(header
, A_BOLD
);
180 mvwprintw(header
, line
, 16, "%d", value
);
181 wmove(header
, line
, 24);
182 print_digits(header
, first
, second
);
183 wmove(header
, line
, 40);
186 void set_window_title(WINDOW
*win
, char *title
)
188 wattron(win
, A_BOLD
);
189 mvwprintw(win
, 0, 1, title
);
190 wattroff(win
, A_BOLD
);
193 void print_log(char *str
)
196 int current_line
= 1;
197 int current_char
= 1;
199 /* rotate the line buffer */
200 if (nb_log_lines
>= MAX_LOG_LINES
) {
201 tmp
= strndup(log_lines
, MAX_LINE_LENGTH
* MAX_LOG_LINES
+ MAX_LOG_LINES
);
202 tmp2
= strchr(tmp
, '\n');
203 memset(log_lines
, '\0', strlen(log_lines
));
204 strncat(log_lines
, tmp2
+ 1, strlen(tmp2
) - 1);
205 log_lines
[strlen(log_lines
)] = '\n';
206 log_lines
[strlen(log_lines
)] = '\0';
211 strncat(log_lines
, str
, MAX_LINE_LENGTH
- 1);
213 if (nb_log_lines
< MAX_LOG_LINES
)
214 log_lines
[strlen(log_lines
)] = '\n';
215 log_lines
[strlen(log_lines
)] = '\0';
219 set_window_title(status
, "Status");
220 for (i
= 0; i
< strlen(log_lines
); i
++) {
221 if (log_lines
[i
] == '\n') {
222 wmove(status
, ++current_line
, 1);
225 mvwprintw(status
, current_line
, current_char
++, "%c",
232 int process_selected(struct processtop
*process
)
235 struct processtop
*stored_process
;
237 for (i
= 0; i
< selected_processes
->len
; i
++) {
238 stored_process
= g_ptr_array_index(selected_processes
, i
);
241 if (stored_process
->tid
== process
->tid
)
247 void update_selected_processes()
250 struct processtop
*stored_process
;
252 if (process_selected(selected_process
)) {
253 for (i
= 0; i
< selected_processes
->len
; i
++) {
254 stored_process
= g_ptr_array_index(selected_processes
, i
);
257 if (stored_process
->tid
== selected_process
->tid
)
258 g_ptr_array_remove(selected_processes
,
260 print_log("Process removed");
263 g_ptr_array_add(selected_processes
, selected_process
);
264 print_log("Process added");
268 void print_key(WINDOW
*win
, char *key
, char *desc
, int toggle
)
275 wattron(win
, COLOR_PAIR(pair
));
276 wprintw(footer
, "%s", key
);
277 wattroff(win
, COLOR_PAIR(pair
));
278 wprintw(footer
, ":%s", desc
);
283 sem_wait(&update_display_sem
);
286 print_key(footer
, "F2", "CPUtop ", current_view
== cpu
);
287 print_key(footer
, "F3", "PerfTop ", current_view
== perf
);
288 print_key(footer
, "F4", "IOTop ", current_view
== iostream
);
289 print_key(footer
, "Enter", "Details ", current_view
== process_details
);
290 print_key(footer
, "Space", "Highlight ", 0);
291 print_key(footer
, "q", "Quit ", 0);
292 print_key(footer
, "r", "Pref ", 0);
293 print_key(footer
, "t", "Threads ", toggle_threads
);
294 print_key(footer
, "v", "Virt ", toggle_virt
);
295 print_key(footer
, "p", "Pause ", toggle_pause
);
298 sem_post(&update_display_sem
);
305 set_window_title(header
, "Statistics for interval [gathering data...[");
306 wattron(header
, A_BOLD
);
307 mvwprintw(header
, 1, 4, "CPUs");
308 mvwprintw(header
, 2, 4, "Threads");
309 mvwprintw(header
, 3, 4, "FDs");
310 wattroff(header
, A_BOLD
);
314 static void scale_unit(uint64_t bytes
, char *ret
)
316 if (bytes
>= 1000000000)
317 sprintf(ret
, "%" PRIu64
"G", bytes
/1000000000);
318 if (bytes
>= 1000000)
319 sprintf(ret
, "%" PRIu64
"M", bytes
/1000000);
320 else if (bytes
>= 1000)
321 sprintf(ret
, "%" PRIu64
"K", bytes
/1000);
323 sprintf(ret
, "%" PRIu64
, bytes
);
329 struct processtop
*tmp
;
332 for (i
= 0; i
< data
->process_table
->len
; i
++) {
333 tmp
= g_ptr_array_index(data
->process_table
, i
);
334 total
+= tmp
->fileread
;
335 total
+= tmp
->filewrite
;
343 struct tm start
, end
;
344 uint64_t ts_nsec_start
, ts_nsec_end
;
347 ts_nsec_start
= data
->start
% NSEC_PER_SEC
;
348 start
= format_timestamp(data
->start
);
350 ts_nsec_end
= data
->end
% NSEC_PER_SEC
;
351 end
= format_timestamp(data
->end
);
355 set_window_title(header
, "Statistics for interval ");
356 wattron(header
, A_BOLD
);
358 wprintw(header
, "[%02d:%02d:%02d.%09" PRIu64
", %02d:%02d:%02d.%09" PRIu64
"[",
359 start
.tm_hour
, start
.tm_min
, start
.tm_sec
, ts_nsec_start
,
360 end
.tm_hour
, end
.tm_min
, end
.tm_sec
, ts_nsec_end
);
361 mvwprintw(header
, 1, 4, "CPUs");
362 wattroff(header
, A_BOLD
);
363 wprintw(header
, "\t%d\t(max/cpu : %0.2f%)", data
->cpu_table
->len
,
364 100.0/data
->cpu_table
->len
);
365 print_headers(2, "Threads", data
->nbthreads
, data
->nbnewthreads
,
366 -1*(data
->nbdeadthreads
));
367 print_headers(3, "FDs", data
->nbfiles
, data
->nbnewfiles
,
368 -1*(data
->nbclosedfiles
));
369 scale_unit(total_io(), io
);
370 mvwprintw(header
, 3, 43, "%sB/sec", io
);
374 gint
sort_by_cpu_desc(gconstpointer p1
, gconstpointer p2
)
376 struct processtop
*n1
= *(struct processtop
**)p1
;
377 struct processtop
*n2
= *(struct processtop
**)p2
;
378 unsigned long totaln1
= n1
->totalcpunsec
;
379 unsigned long totaln2
= n2
->totalcpunsec
;
381 if (totaln1
< totaln2
)
383 if (totaln1
== totaln2
)
388 gint
sort_by_tid_desc(gconstpointer p1
, gconstpointer p2
)
390 struct processtop
*n1
= *(struct processtop
**)p1
;
391 struct processtop
*n2
= *(struct processtop
**)p2
;
392 unsigned long totaln1
= n1
->tid
;
393 unsigned long totaln2
= n2
->tid
;
395 if (totaln1
< totaln2
)
397 if (totaln1
== totaln2
)
402 gint
sort_by_pid_desc(gconstpointer p1
, gconstpointer p2
)
404 struct processtop
*n1
= *(struct processtop
**)p1
;
405 struct processtop
*n2
= *(struct processtop
**)p2
;
406 unsigned long totaln1
= n1
->pid
;
407 unsigned long totaln2
= n2
->pid
;
409 if (totaln1
< totaln2
)
411 if (totaln1
== totaln2
)
416 gint
sort_by_process_read_desc(gconstpointer p1
, gconstpointer p2
)
418 struct processtop
*n1
= *(struct processtop
**)p1
;
419 struct processtop
*n2
= *(struct processtop
**)p2
;
420 unsigned long totaln1
= n1
->fileread
;
421 unsigned long totaln2
= n2
->fileread
;
423 if (totaln1
< totaln2
)
425 if (totaln1
== totaln2
)
430 gint
sort_by_process_write_desc(gconstpointer p1
, gconstpointer p2
)
432 struct processtop
*n1
= *(struct processtop
**)p1
;
433 struct processtop
*n2
= *(struct processtop
**)p2
;
434 unsigned long totaln1
= n1
->filewrite
;
435 unsigned long totaln2
= n2
->filewrite
;
437 if (totaln1
< totaln2
)
439 if (totaln1
== totaln2
)
444 gint
sort_by_process_total_desc(gconstpointer p1
, gconstpointer p2
)
446 struct processtop
*n1
= *(struct processtop
**)p1
;
447 struct processtop
*n2
= *(struct processtop
**)p2
;
448 unsigned long totaln1
= n1
->totalfilewrite
+ n1
->totalfileread
;
449 unsigned long totaln2
= n2
->totalfilewrite
+ n2
->totalfileread
;
451 if (totaln1
< totaln2
)
453 if (totaln1
== totaln2
)
458 gint
sort_by_file_read_desc(gconstpointer p1
, gconstpointer p2
)
460 struct files
*n1
= *(struct files
**)p1
;
461 struct files
*n2
= *(struct files
**)p2
;
462 unsigned long totaln1
;
463 unsigned long totaln2
;
468 if (totaln1
< totaln2
)
470 if (totaln1
== totaln2
)
475 gint
sort_by_file_write_desc(gconstpointer p1
, gconstpointer p2
)
477 struct files
*n1
= *(struct files
**)p1
;
478 struct files
*n2
= *(struct files
**)p2
;
479 unsigned long totaln1
;
480 unsigned long totaln2
;
485 if (totaln1
< totaln2
)
487 if (totaln1
== totaln2
)
492 gint
sort_by_file_fd_desc(gconstpointer p1
, gconstpointer p2
)
494 struct files
*n1
= *(struct files
**)p1
;
495 struct files
*n2
= *(struct files
**)p2
;
496 unsigned long totaln1
;
497 unsigned long totaln2
;
502 if (totaln1
< totaln2
)
504 if (totaln1
== totaln2
)
509 gint
sort_by_cpu_group_by_threads_desc(gconstpointer p1
, gconstpointer p2
)
511 struct processtop
*n1
= *(struct processtop
**)p1
;
512 struct processtop
*n2
= *(struct processtop
**)p2
;
513 unsigned long totaln1
= n1
->threadstotalcpunsec
;
514 unsigned long totaln2
= n2
->threadstotalcpunsec
;
516 if (totaln1
< totaln2
)
518 if (totaln1
== totaln2
)
523 void update_cputop_display()
526 int header_offset
= 2;
527 struct processtop
*tmp
;
528 unsigned long elapsed
;
530 int nblinedisplayed
= 0;
531 int current_line
= 0;
532 int current_row_offset
;
535 elapsed
= data
->end
- data
->start
;
536 maxcputime
= elapsed
* data
->cpu_table
->len
/ 100.0;
538 if (cputopview
[0].sort
== 1)
539 g_ptr_array_sort(data
->process_table
, sort_by_cpu_desc
);
540 else if (cputopview
[1].sort
== 1)
541 g_ptr_array_sort(data
->process_table
, sort_by_pid_desc
);
542 else if (cputopview
[2].sort
== 1)
543 g_ptr_array_sort(data
->process_table
, sort_by_tid_desc
);
544 else if (cputopview
[3].sort
== 1)
545 g_ptr_array_sort(data
->process_table
, sort_by_cpu_desc
);
547 g_ptr_array_sort(data
->process_table
, sort_by_cpu_desc
);
549 set_window_title(center
, "CPU Top");
550 wattron(center
, A_BOLD
);
552 for (i
= 0; i
< 6; i
++) {
553 if (toggle_virt
< 0 && (i
== 3 || i
== 4)) {
556 if (cputopview
[i
].sort
) {
557 wattron(center
, A_UNDERLINE
);
558 pref_current_sort
= i
;
560 mvwprintw(center
, 1, column
, cputopview
[i
].title
);
561 wattroff(center
, A_UNDERLINE
);
564 wattroff(center
, A_BOLD
);
566 max_center_lines
= LINES
- 5 - 7 - 1 - header_offset
;
568 /* iterate the process (thread) list */
569 for (i
= list_offset
; i
< data
->process_table
->len
&&
570 nblinedisplayed
< max_center_lines
; i
++) {
571 tmp
= g_ptr_array_index(data
->process_table
, i
);
572 current_row_offset
= 1;
573 if (tmp
->pid
!= tmp
->tid
)
574 if (toggle_threads
== -1)
577 if (process_selected(tmp
)) {
578 wattron(center
, COLOR_PAIR(6));
579 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
581 if (current_line
== selected_line
) {
582 selected_process
= tmp
;
583 wattron(center
, COLOR_PAIR(5));
584 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
587 mvwprintw(center
, current_line
+ header_offset
,
588 current_row_offset
, "%1.2f",
589 tmp
->totalcpunsec
/ maxcputime
);
590 current_row_offset
+= 10;
592 mvwprintw(center
, current_line
+ header_offset
,
593 current_row_offset
, "%d", tmp
->pid
);
594 current_row_offset
+= 10;
596 mvwprintw(center
, current_line
+ header_offset
,
597 current_row_offset
, "%d", tmp
->tid
);
598 current_row_offset
+= 10;
599 if (toggle_virt
> 0) {
601 mvwprintw(center
, current_line
+ header_offset
,
602 current_row_offset
, "%d", tmp
->vpid
);
603 current_row_offset
+= 10;
605 mvwprintw(center
, current_line
+ header_offset
,
606 current_row_offset
, "%d", tmp
->vtid
);
607 current_row_offset
+= 10;
610 mvwprintw(center
, current_line
+ header_offset
,
611 current_row_offset
, "%s", tmp
->comm
);
612 wattroff(center
, COLOR_PAIR(6));
613 wattroff(center
, COLOR_PAIR(5));
619 gint
sort_perf(gconstpointer p1
, gconstpointer p2
, gpointer key
)
621 struct processtop
*n1
= *(struct processtop
**) p1
;
622 struct processtop
*n2
= *(struct processtop
**) p2
;
624 struct perfcounter
*tmp1
, *tmp2
;
625 unsigned long totaln2
= 0;
626 unsigned long totaln1
= 0;
631 tmp1
= g_hash_table_lookup(n1
->perf
, key
);
635 totaln1
= tmp1
->count
;
637 tmp2
= g_hash_table_lookup(n2
->perf
, key
);
641 totaln2
= tmp2
->count
;
643 if (totaln1
< totaln2
)
645 if (totaln1
== totaln2
) {
648 if (totaln1
< totaln2
)
655 void print_key_title(char *key
, int line
)
657 wattron(center
, A_BOLD
);
658 mvwprintw(center
, line
, 1, "%s", key
);
659 mvwprintw(center
, line
, 30, " ");
660 wattroff(center
, A_BOLD
);
663 void update_process_details()
665 unsigned long elapsed
;
667 struct processtop
*tmp
;
668 struct files
*file_tmp
;
671 char filename_buf
[COLS
];
674 GPtrArray
*newfilearray
= g_ptr_array_new();
676 struct perfcounter
*perfn1
, *perfn2
;
679 set_window_title(center
, "Process details");
682 tmp
= find_process_tid(data
,
683 selected_process
->tid
,
684 selected_process
->comm
);
685 elapsed
= data
->end
- data
->start
;
686 maxcputime
= elapsed
* data
->cpu_table
->len
/ 100.0;
688 print_key_title("Name", line
++);
689 wprintw(center
, "%s", selected_process
->comm
);
690 print_key_title("TID", line
++);
691 wprintw(center
, "%d", selected_process
->tid
);
693 print_key_title("Does not exit at this time", 3);
697 print_key_title("PID", line
++);
698 wprintw(center
, "%d", tmp
->pid
);
699 print_key_title("PPID", line
++);
700 wprintw(center
, "%d", tmp
->ppid
);
701 print_key_title("VPID", line
++);
702 wprintw(center
, "%d", tmp
->vpid
);
703 print_key_title("VTID", line
++);
704 wprintw(center
, "%d", tmp
->vtid
);
705 print_key_title("VPPID", line
++);
706 wprintw(center
, "%d", tmp
->vppid
);
707 print_key_title("CPU", line
++);
708 wprintw(center
, "%1.2f %%", tmp
->totalcpunsec
/maxcputime
);
710 print_key_title("READ B/s", line
++);
711 scale_unit(tmp
->fileread
, unit
);
712 wprintw(center
, "%s", unit
);
714 print_key_title("WRITE B/s", line
++);
715 scale_unit(tmp
->filewrite
, unit
);
716 wprintw(center
, "%s", unit
);
718 g_hash_table_iter_init(&iter
, global_perf_liszt
);
719 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
720 print_key_title((char *) key
, line
++);
721 perfn2
= g_hash_table_lookup(tmp
->perf
, (char *) key
);
722 wprintw(center
, "%d", perfn2
? perfn2
->count
: 0);
726 wattron(center
, A_BOLD
);
728 for (i
= 0; i
< 3; i
++) {
729 if (fileview
[i
].sort
) {
730 pref_current_sort
= i
;
731 wattron(center
, A_UNDERLINE
);
733 mvwprintw(center
, line
, column
, fileview
[i
].title
);
734 wattroff(center
, A_UNDERLINE
);
737 mvwprintw(center
, line
++, column
, "FILENAME");
738 wattroff(center
, A_BOLD
);
741 * since the process_files_table array could contain NULL file structures,
742 * and that the positions inside the array is important (it is the FD), we
743 * need to create a temporary array that we can sort.
745 for (i
= 0; i
< tmp
->process_files_table
->len
; i
++) {
746 file_tmp
= g_ptr_array_index(tmp
->process_files_table
, i
);
748 g_ptr_array_add(newfilearray
, file_tmp
);
751 if (fileview
[0].sort
== 1)
752 g_ptr_array_sort(newfilearray
, sort_by_file_fd_desc
);
753 else if (fileview
[1].sort
== 1)
754 g_ptr_array_sort(newfilearray
, sort_by_file_read_desc
);
755 else if (fileview
[2].sort
== 1)
756 g_ptr_array_sort(newfilearray
, sort_by_file_write_desc
);
758 g_ptr_array_sort(newfilearray
, sort_by_file_read_desc
);
760 for (i
= selected_line
; i
< newfilearray
->len
&&
761 i
< (selected_line
+ max_center_lines
- line
+ 2); i
++) {
762 file_tmp
= g_ptr_array_index(newfilearray
, i
);
765 mvwprintw(center
, line
+ j
, 1, "%d", file_tmp
->fd
);
766 scale_unit(file_tmp
->read
, unit
);
767 mvwprintw(center
, line
+ j
, 11, "%s", unit
);
768 scale_unit(file_tmp
->write
, unit
);
769 mvwprintw(center
, line
+ j
, 21, "%s", unit
);
770 snprintf(filename_buf
, COLS
- 25, "%s", file_tmp
->name
);
771 mvwprintw(center
, line
+ j
, 31, "%s", filename_buf
);
774 g_ptr_array_free(newfilearray
, TRUE
);
780 int nblinedisplayed
= 0;
781 int current_line
= 0;
782 struct processtop
*tmp
;
783 int header_offset
= 2;
785 struct perfcounter
*perfn1
, *perfn2
;
786 char *perf_key
= NULL
;
791 set_window_title(center
, "Perf Top");
792 wattron(center
, A_BOLD
);
793 mvwprintw(center
, 1, 1, "PID");
794 mvwprintw(center
, 1, 11, "TID");
795 mvwprintw(center
, 1, 22, "NAME");
798 g_hash_table_iter_init(&iter
, global_perf_liszt
);
799 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
800 if (perfn1
->visible
) {
802 /* pref_current_sort = i; */
803 wattron(center
, A_UNDERLINE
);
805 /* + 5 to strip the "perf_" prefix */
806 mvwprintw(center
, 1, perf_row
, "%s",
808 wattroff(center
, A_UNDERLINE
);
812 perf_key
= (char *) key
;
815 wattroff(center
, A_BOLD
);
817 g_ptr_array_sort_with_data(data
->process_table
, sort_perf
, perf_key
);
819 for (i
= 0; i
< data
->process_table
->len
&&
820 nblinedisplayed
< max_center_lines
; i
++) {
821 tmp
= g_ptr_array_index(data
->process_table
, i
);
822 if (tmp
->pid
!= tmp
->tid
)
823 if (toggle_threads
== -1)
826 if (process_selected(tmp
)) {
827 wattron(center
, COLOR_PAIR(6));
828 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
830 if (current_line
== selected_line
) {
831 selected_process
= tmp
;
832 wattron(center
, COLOR_PAIR(5));
833 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
836 mvwprintw(center
, current_line
+ header_offset
, 1, "%d", tmp
->pid
);
837 mvwprintw(center
, current_line
+ header_offset
, 11, "%d", tmp
->tid
);
838 mvwprintw(center
, current_line
+ header_offset
, 22, "%s", tmp
->comm
);
840 g_hash_table_iter_init(&iter
, global_perf_liszt
);
843 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
844 if (perfn1
->visible
) {
845 perfn2
= g_hash_table_lookup(tmp
->perf
, (char *) key
);
847 value
= perfn2
->count
;
850 mvwprintw(center
, current_line
+ header_offset
,
851 perf_row
, "%d", value
);
856 wattroff(center
, COLOR_PAIR(6));
857 wattroff(center
, COLOR_PAIR(5));
863 void update_iostream()
866 int header_offset
= 2;
867 struct processtop
*tmp
;
868 int nblinedisplayed
= 0;
869 int current_line
= 0;
874 set_window_title(center
, "IO Top");
875 wattron(center
, A_BOLD
);
876 mvwprintw(center
, 1, 1, "PID");
877 mvwprintw(center
, 1, 11, "TID");
878 mvwprintw(center
, 1, 22, "NAME");
880 for (i
= 0; i
< 3; i
++) {
881 if (iostreamtopview
[i
].sort
) {
882 pref_current_sort
= i
;
883 wattron(center
, A_UNDERLINE
);
885 mvwprintw(center
, 1, column
, iostreamtopview
[i
].title
);
886 wattroff(center
, A_UNDERLINE
);
889 wattroff(center
, A_BOLD
);
890 wattroff(center
, A_UNDERLINE
);
892 if (iostreamtopview
[0].sort
== 1)
893 g_ptr_array_sort(data
->process_table
, sort_by_process_read_desc
);
894 else if (iostreamtopview
[1].sort
== 1)
895 g_ptr_array_sort(data
->process_table
, sort_by_process_write_desc
);
896 else if (iostreamtopview
[2].sort
== 1)
897 g_ptr_array_sort(data
->process_table
, sort_by_process_total_desc
);
899 g_ptr_array_sort(data
->process_table
, sort_by_process_total_desc
);
901 for (i
= list_offset
; i
< data
->process_table
->len
&&
902 nblinedisplayed
< max_center_lines
; i
++) {
903 tmp
= g_ptr_array_index(data
->process_table
, i
);
904 if (tmp
->pid
!= tmp
->tid
)
905 if (toggle_threads
== -1)
908 if (process_selected(tmp
)) {
909 wattron(center
, COLOR_PAIR(6));
910 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
912 if (current_line
== selected_line
) {
913 selected_process
= tmp
;
914 wattron(center
, COLOR_PAIR(5));
915 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
918 mvwprintw(center
, current_line
+ header_offset
, 1, "%d", tmp
->pid
);
920 mvwprintw(center
, current_line
+ header_offset
, 11, "%d", tmp
->tid
);
922 mvwprintw(center
, current_line
+ header_offset
, 22, "%s", tmp
->comm
);
924 /* READ (bytes/sec) */
925 scale_unit(tmp
->fileread
, unit
);
926 mvwprintw(center
, current_line
+ header_offset
, 40, "%s", unit
);
928 /* WRITE (bytes/sec) */
929 scale_unit(tmp
->filewrite
, unit
);
930 mvwprintw(center
, current_line
+ header_offset
, 52, "%s", unit
);
933 total
= tmp
->totalfileread
+ tmp
->totalfilewrite
;
935 scale_unit(total
, unit
);
936 mvwprintw(center
, current_line
+ header_offset
, 64, "%s", unit
);
938 wattroff(center
, COLOR_PAIR(6));
939 wattroff(center
, COLOR_PAIR(5));
945 void update_current_view()
947 sem_wait(&update_display_sem
);
954 switch (current_view
) {
956 update_cputop_display();
961 case process_details
:
962 update_process_details();
968 update_cputop_display();
975 sem_post(&update_display_sem
);
978 void update_process_detail_sort(int *line_selected
)
985 if (*line_selected
> (size
- 1))
986 *line_selected
= size
- 1;
987 else if (*line_selected
< 0)
990 if (fileview
[*line_selected
].sort
== 1)
991 fileview
[*line_selected
].reverse
= 1;
992 for (i
= 0; i
< size
; i
++)
993 fileview
[i
].sort
= 0;
994 fileview
[*line_selected
].sort
= 1;
997 void update_process_detail_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1004 if (pref_panel_window
) {
1005 del_panel(pref_panel
);
1006 delwin(pref_panel_window
);
1010 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1011 pref_panel
= new_panel(pref_panel_window
);
1013 werase(pref_panel_window
);
1014 box(pref_panel_window
, 0 , 0);
1015 set_window_title(pref_panel_window
, "Process Detail Preferences ");
1016 wattron(pref_panel_window
, A_BOLD
);
1017 mvwprintw(pref_panel_window
, size
+ 1, 1,
1018 " 's' : sort, space : toggle");
1019 wattroff(pref_panel_window
, A_BOLD
);
1021 if (*line_selected
> (size
- 1))
1022 *line_selected
= size
- 1;
1023 else if (*line_selected
< 0)
1025 if (toggle_sort
== 1) {
1026 update_process_detail_sort(line_selected
);
1027 update_current_view();
1030 for (i
= 0; i
< size
; i
++) {
1031 if (i
== *line_selected
) {
1032 wattron(pref_panel_window
, COLOR_PAIR(5));
1033 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1035 if (fileview
[i
].sort
== 1)
1036 wattron(pref_panel_window
, A_BOLD
);
1037 mvwprintw(pref_panel_window
, i
+ 1, 1, "[-] %s",
1039 wattroff(pref_panel_window
, A_BOLD
);
1040 wattroff(pref_panel_window
, COLOR_PAIR(5));
1047 void update_iostream_sort(int *line_selected
)
1053 if (*line_selected
> (size
- 1))
1054 *line_selected
= size
- 1;
1055 else if (*line_selected
< 0)
1057 if (iostreamtopview
[*line_selected
].sort
== 1)
1058 iostreamtopview
[*line_selected
].reverse
= 1;
1059 for (i
= 0; i
< size
; i
++)
1060 iostreamtopview
[i
].sort
= 0;
1061 iostreamtopview
[*line_selected
].sort
= 1;
1065 void update_iostream_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1072 if (pref_panel_window
) {
1073 del_panel(pref_panel
);
1074 delwin(pref_panel_window
);
1078 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1079 pref_panel
= new_panel(pref_panel_window
);
1081 werase(pref_panel_window
);
1082 box(pref_panel_window
, 0 , 0);
1083 set_window_title(pref_panel_window
, "IOTop Preferences ");
1084 wattron(pref_panel_window
, A_BOLD
);
1085 mvwprintw(pref_panel_window
, size
+ 1, 1,
1086 " 's' : sort, space : toggle");
1087 wattroff(pref_panel_window
, A_BOLD
);
1089 if (*line_selected
> (size
- 1))
1090 *line_selected
= size
- 1;
1091 else if (*line_selected
< 0)
1093 if (toggle_sort
== 1) {
1094 update_iostream_sort(line_selected
);
1095 update_current_view();
1098 for (i
= 0; i
< size
; i
++) {
1099 if (i
== *line_selected
) {
1100 wattron(pref_panel_window
, COLOR_PAIR(5));
1101 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1103 if (iostreamtopview
[i
].sort
== 1)
1104 wattron(pref_panel_window
, A_BOLD
);
1105 mvwprintw(pref_panel_window
, i
+ 1, 1, "[-] %s",
1106 iostreamtopview
[i
].title
);
1107 wattroff(pref_panel_window
, A_BOLD
);
1108 wattroff(pref_panel_window
, COLOR_PAIR(5));
1115 void update_cpu_sort(int *line_selected
)
1120 if (*line_selected
> (size
- 1))
1121 *line_selected
= size
- 1;
1122 else if (*line_selected
< 0)
1125 /* special case, we don't support sorting by procname for now */
1126 if (*line_selected
!= 3) {
1127 if (cputopview
[*line_selected
].sort
== 1)
1128 cputopview
[*line_selected
].reverse
= 1;
1129 for (i
= 0; i
< size
; i
++)
1130 cputopview
[i
].sort
= 0;
1131 cputopview
[*line_selected
].sort
= 1;
1135 void update_cpu_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1142 if (pref_panel_window
) {
1143 del_panel(pref_panel
);
1144 delwin(pref_panel_window
);
1148 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1149 pref_panel
= new_panel(pref_panel_window
);
1151 werase(pref_panel_window
);
1152 box(pref_panel_window
, 0 , 0);
1153 set_window_title(pref_panel_window
, "CPUTop Preferences ");
1154 wattron(pref_panel_window
, A_BOLD
);
1155 mvwprintw(pref_panel_window
, size
+ 1, 1,
1156 " 's' : sort, space : toggle");
1157 wattroff(pref_panel_window
, A_BOLD
);
1159 if (*line_selected
> (size
- 1))
1160 *line_selected
= size
- 1;
1161 else if (*line_selected
< 0)
1163 if (toggle_sort
== 1) {
1164 update_cpu_sort(line_selected
);
1165 update_current_view();
1168 for (i
= 0; i
< size
; i
++) {
1169 if (i
== *line_selected
) {
1170 wattron(pref_panel_window
, COLOR_PAIR(5));
1171 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1173 if (cputopview
[i
].sort
== 1)
1174 wattron(pref_panel_window
, A_BOLD
);
1175 mvwprintw(pref_panel_window
, i
+ 1, 1, "[-] %s",
1176 cputopview
[i
].title
);
1177 wattroff(pref_panel_window
, A_BOLD
);
1178 wattroff(pref_panel_window
, COLOR_PAIR(5));
1185 void update_perf_sort(int *line_selected
)
1188 struct perfcounter
*perf
;
1192 size
= g_hash_table_size(global_perf_liszt
);
1193 if (*line_selected
> (size
- 1))
1194 *line_selected
= size
- 1;
1195 else if (*line_selected
< 0)
1199 perflist
= g_list_first(g_hash_table_get_keys(global_perf_liszt
));
1201 perf
= g_hash_table_lookup(global_perf_liszt
, perflist
->data
);
1202 if (i
!= *line_selected
)
1207 perflist
= g_list_next(perflist
);
1211 void update_perf_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1214 struct perfcounter
*perf
;
1220 if (pref_panel_window
) {
1221 del_panel(pref_panel
);
1222 delwin(pref_panel_window
);
1224 size
= g_hash_table_size(global_perf_liszt
);
1226 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1227 pref_panel
= new_panel(pref_panel_window
);
1229 werase(pref_panel_window
);
1230 box(pref_panel_window
, 0 , 0);
1231 set_window_title(pref_panel_window
, "Perf Preferences ");
1232 wattron(pref_panel_window
, A_BOLD
);
1233 mvwprintw(pref_panel_window
, g_hash_table_size(global_perf_liszt
) + 1, 1,
1234 " 's' : sort, space : toggle");
1235 wattroff(pref_panel_window
, A_BOLD
);
1237 if (*line_selected
> (size
- 1))
1238 *line_selected
= size
- 1;
1239 else if (*line_selected
< 0)
1242 if (toggle_sort
== 1) {
1243 update_perf_sort(line_selected
);
1244 update_current_view();
1248 perflist
= g_list_first(g_hash_table_get_keys(global_perf_liszt
));
1250 perf
= g_hash_table_lookup(global_perf_liszt
, perflist
->data
);
1251 if (i
== *line_selected
&& toggle_view
== 1) {
1252 perf
->visible
= perf
->visible
== 1 ? 0:1;
1253 update_current_view();
1255 if (i
== *line_selected
) {
1256 wattron(pref_panel_window
, COLOR_PAIR(5));
1257 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1259 if (perf
->sort
== 1)
1260 wattron(pref_panel_window
, A_BOLD
);
1261 mvwprintw(pref_panel_window
, i
+ 1, 1, "[%c] %s",
1262 perf
->visible
== 1 ? 'x' : ' ',
1263 (char *) perflist
->data
+ 5);
1264 wattroff(pref_panel_window
, A_BOLD
);
1265 wattroff(pref_panel_window
, COLOR_PAIR(5));
1267 perflist
= g_list_next(perflist
);
1273 int update_preference_panel(int *line_selected
, int toggle_view
, int toggle_sort
)
1277 switch(current_view
) {
1279 update_perf_pref(line_selected
, toggle_view
, toggle_sort
);
1282 update_cpu_pref(line_selected
, toggle_view
, toggle_sort
);
1285 update_iostream_pref(line_selected
, toggle_view
, toggle_sort
);
1287 case process_details
:
1288 update_process_detail_pref(line_selected
, toggle_view
, toggle_sort
);
1298 int update_sort(int *line_selected
)
1302 switch(current_view
) {
1304 update_perf_sort(line_selected
);
1307 update_cpu_sort(line_selected
);
1310 update_iostream_sort(line_selected
);
1312 case process_details
:
1313 update_process_detail_sort(line_selected
);
1324 void toggle_pref_panel(void)
1328 if (pref_panel_visible
) {
1329 hide_panel(pref_panel
);
1330 pref_panel_visible
= 0;
1332 ret
= update_preference_panel(&pref_line_selected
, 0, 0);
1335 show_panel(pref_panel
);
1336 pref_panel_visible
= 1;
1342 void display(unsigned int index
)
1344 last_display_index
= index
;
1345 currently_displayed_index
= index
;
1346 data
= g_ptr_array_index(copies
, index
);
1349 max_elements
= data
->process_table
->len
;
1350 update_current_view();
1356 void pause_display()
1360 sem_wait(&pause_sem
);
1363 void resume_display()
1366 print_log("Resume");
1367 sem_post(&pause_sem
);
1370 void *handle_keyboard(void *p
)
1373 while((ch
= getch())) {
1375 /* Move the cursor and scroll */
1378 if (pref_panel_visible
) {
1379 pref_line_selected
++;
1380 update_preference_panel(&pref_line_selected
, 0, 0);
1382 if (selected_line
< (max_center_lines
- 1) &&
1383 selected_line
< max_elements
- 1) {
1386 } else if (selected_in_list
< (max_elements
- 1)
1387 && (list_offset
< (max_elements
- max_center_lines
))) {
1391 update_current_view();
1398 if (pref_panel_visible
) {
1399 if (pref_line_selected
> 0)
1400 pref_line_selected
--;
1401 update_preference_panel(&pref_line_selected
, 0, 0);
1403 if (selected_line
> 0) {
1406 } else if (selected_in_list
> 0 && list_offset
> 0) {
1410 update_current_view();
1416 /* Navigate the history with arrows */
1418 if (currently_displayed_index
> 0) {
1419 currently_displayed_index
--;
1420 print_log("Going back in time");
1422 print_log("Cannot rewind, last data is already displayed");
1424 data
= g_ptr_array_index(copies
, currently_displayed_index
);
1425 max_elements
= data
->process_table
->len
;
1427 /* we force to pause the display when moving in time */
1428 if (toggle_pause
< 0)
1431 update_current_view();
1435 if (currently_displayed_index
< last_display_index
) {
1436 currently_displayed_index
++;
1437 print_log("Going forward in time");
1438 data
= g_ptr_array_index(copies
, currently_displayed_index
);
1439 max_elements
= data
->process_table
->len
;
1440 update_current_view();
1443 print_log("Manually moving forward");
1445 if (toggle_pause
> 0) {
1446 sem_post(&pause_sem
);
1447 update_current_view();
1448 sem_wait(&pause_sem
);
1454 if (pref_panel_visible
) {
1455 update_preference_panel(&pref_line_selected
, 1, 0);
1457 update_selected_processes();
1458 update_current_view();
1462 if (pref_panel_visible
)
1463 update_preference_panel(&pref_line_selected
, 0, 1);
1466 /* perf uses a hashtable, it is ordered backward */
1467 if (current_view
== perf
) {
1468 pref_current_sort
--;
1469 } else if (!pref_panel_visible
) {
1470 pref_current_sort
++;
1472 update_sort(&pref_current_sort
);
1473 update_current_view();
1476 /* perf uses a hashtable, it is ordered backward */
1477 if (current_view
== perf
) {
1478 pref_current_sort
++;
1479 } else if (!pref_panel_visible
) {
1480 pref_current_sort
--;
1482 update_sort(&pref_current_sort
);
1483 update_current_view();
1486 case 13: /* FIXME : KEY_ENTER ?? */
1487 if (pref_panel_visible
)
1489 if (current_view
!= process_details
) {
1490 previous_view
= current_view
;
1491 current_view
= process_details
;
1493 current_view
= previous_view
;
1494 previous_view
= process_details
;
1497 update_current_view();
1501 if (pref_panel_visible
)
1502 toggle_pref_panel();
1505 update_current_view();
1508 if (pref_panel_visible
)
1509 toggle_pref_panel();
1512 update_current_view();
1515 if (pref_panel_visible
)
1516 toggle_pref_panel();
1517 current_view
= perf
;
1519 update_current_view();
1522 if (pref_panel_visible
)
1523 toggle_pref_panel();
1524 current_view
= iostream
;
1526 update_current_view();
1531 /* exit keyboard thread */
1535 toggle_threads
*= -1;
1536 update_current_view();
1539 if (toggle_pause
< 0) {
1546 toggle_pref_panel();
1550 update_current_view();
1552 /* ESCAPE, but slow to process, don't know why */
1554 if (pref_panel_visible
)
1555 toggle_pref_panel();
1556 else if (current_view
== process_details
) {
1557 current_view
= previous_view
;
1558 previous_view
= process_details
;
1560 update_current_view();
1564 update_current_view();
1572 void init_view_headers()
1574 cputopview
[0].title
= strdup("CPU(%)");
1575 cputopview
[0].sort
= 1;
1576 cputopview
[1].title
= strdup("PID");
1577 cputopview
[2].title
= strdup("TID");
1578 cputopview
[3].title
= strdup("VPID");
1579 cputopview
[4].title
= strdup("VTID");
1580 cputopview
[5].title
= strdup("NAME");
1582 iostreamtopview
[0].title
= strdup("R (B/sec)");
1583 iostreamtopview
[1].title
= strdup("W (B/sec)");
1584 iostreamtopview
[2].title
= strdup("Total (B)");
1585 iostreamtopview
[2].sort
= 1;
1587 fileview
[0].title
= strdup("FD");
1588 fileview
[1].title
= strdup("READ");
1589 fileview
[1].sort
= 1;
1590 fileview
[2].title
= strdup("WRITE");
1595 selected_processes
= g_ptr_array_new();
1596 sem_init(&update_display_sem
, 0, 1);
1597 init_view_headers();
1600 header
= create_window(5, COLS
- 1, 0, 0);
1601 center
= create_window(LINES
- 5 - 7, COLS
- 1, 5, 0);
1602 status
= create_window(MAX_LOG_LINES
+ 2, COLS
- 1, LINES
- 7, 0);
1603 footer
= create_window(1, COLS
- 1, LINES
- 1, 0);
1605 print_log("Starting display");
1607 main_panel
= new_panel(center
);
1614 pthread_create(&keyboard_thread
, NULL
, handle_keyboard
, (void *)NULL
);