2 * Copyright (C) 2011 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;
47 int last_display_index
, currently_displayed_index
;
49 struct processtop
*selected_process
= NULL
;
52 int selected_line
= 0; /* select bar position */
53 int selected_in_list
= 0; /* selection relative to the whole list */
54 int list_offset
= 0; /* first index in the list to display (scroll) */
56 char log_lines
[MAX_LINE_LENGTH
* MAX_LOG_LINES
+ MAX_LOG_LINES
];
58 int max_elements
= 80;
60 int toggle_threads
= -1;
61 int toggle_pause
= -1;
64 GPtrArray
*selected_processes
;
66 pthread_t keyboard_thread
;
68 struct header_view cputopview
[4];
69 struct header_view iostreamtopview
[3];
70 struct header_view fileview
[3];
79 static void handle_sigterm(int signal
)
88 halfdelay(DEFAULT_DELAY
);
90 intrflush(stdscr
, false);
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 */
101 init_pair(6, COLOR_WHITE
, COLOR_GREEN
); /* selected process */
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));
120 signal(SIGTERM
, handle_sigterm
);
121 mousemask(BUTTON1_CLICKED
, NULL
);
125 WINDOW
*create_window(int height
, int width
, int startx
, int starty
)
128 win
= newwin(height
, width
, startx
, starty
);
134 WINDOW
*create_window_no_border(int height
, int width
, int startx
, int starty
)
137 win
= newwin(height
, width
, startx
, starty
);
142 void print_digit(WINDOW
*win
, int digit
)
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));
157 void print_digits(WINDOW
*win
, int first
, int second
)
160 print_digit(win
, first
);
162 print_digit(win
, second
);
166 void print_headers(int line
, char *desc
, int value
, int first
, int second
)
168 wattron(header
, A_BOLD
);
169 mvwprintw(header
, line
, 4, "%s", desc
);
170 wattroff(header
, A_BOLD
);
171 mvwprintw(header
, line
, 16, "%d", value
);
172 wmove(header
, line
, 24);
173 print_digits(header
, first
, second
);
174 wmove(header
, line
, 40);
177 void set_window_title(WINDOW
*win
, char *title
)
179 wattron(win
, A_BOLD
);
180 mvwprintw(win
, 0, 1, title
);
181 wattroff(win
, A_BOLD
);
184 void print_log(char *str
)
187 int current_line
= 1;
188 int current_char
= 1;
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';
202 strncat(log_lines
, str
, MAX_LINE_LENGTH
- 1);
204 if (nb_log_lines
< MAX_LOG_LINES
)
205 log_lines
[strlen(log_lines
)] = '\n';
206 log_lines
[strlen(log_lines
)] = '\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);
216 mvwprintw(status
, current_line
, current_char
++, "%c",
223 int process_selected(struct processtop
*process
)
226 struct processtop
*stored_process
;
228 for (i
= 0; i
< selected_processes
->len
; i
++) {
229 stored_process
= g_ptr_array_index(selected_processes
, i
);
232 if (stored_process
->tid
== process
->tid
)
238 void update_selected_processes()
241 struct processtop
*stored_process
;
243 if (process_selected(selected_process
)) {
244 for (i
= 0; i
< selected_processes
->len
; i
++) {
245 stored_process
= g_ptr_array_index(selected_processes
, i
);
248 if (stored_process
->tid
== selected_process
->tid
)
249 g_ptr_array_remove(selected_processes
,
251 print_log("Process removed");
254 g_ptr_array_add(selected_processes
, selected_process
);
255 print_log("Process added");
259 void print_key(WINDOW
*win
, char *key
, char *desc
, int toggle
)
266 wattron(win
, COLOR_PAIR(pair
));
267 wprintw(footer
, "%s", key
);
268 wattroff(win
, COLOR_PAIR(pair
));
269 wprintw(footer
, ":%s", desc
);
274 sem_wait(&update_display_sem
);
277 print_key(footer
, "F2", "CPUtop ", current_view
== cpu
);
278 print_key(footer
, "F3", "PerfTop ", current_view
== perf
);
279 print_key(footer
, "F4", "IOTop ", current_view
== iostream
);
280 print_key(footer
, "Enter", "Details ", current_view
== process_details
);
281 print_key(footer
, "Space", "Highlight ", 0);
282 print_key(footer
, "q", "Quit ", 0);
283 print_key(footer
, "r", "Pref ", 0);
284 print_key(footer
, "p", "Pause ", toggle_pause
);
287 sem_post(&update_display_sem
);
294 set_window_title(header
, "Statistics for interval [gathering data...[");
295 wattron(header
, A_BOLD
);
296 mvwprintw(header
, 1, 4, "CPUs");
297 mvwprintw(header
, 2, 4, "Threads");
298 mvwprintw(header
, 3, 4, "FDs");
299 wattroff(header
, A_BOLD
);
303 struct tm
format_timestamp(uint64_t timestamp
)
306 uint64_t ts_sec
= 0, ts_nsec
;
310 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
311 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
313 time_s
= (time_t) ts_sec
;
315 localtime_r(&time_s
, &tm
);
320 static void scale_unit(uint64_t bytes
, char *ret
)
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);
329 sprintf(ret
, "%" PRIu64
, bytes
);
334 struct processtop
*tmp
;
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
;
348 struct tm start
, end
;
349 uint64_t ts_nsec_start
, ts_nsec_end
;
352 ts_nsec_start
= data
->start
% NSEC_PER_SEC
;
353 start
= format_timestamp(data
->start
);
355 ts_nsec_end
= data
->end
% NSEC_PER_SEC
;
356 end
= format_timestamp(data
->end
);
360 set_window_title(header
, "Statistics for interval ");
361 wattron(header
, A_BOLD
);
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
);
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
);
370 print_headers(2, "Threads", data
->nbthreads
, data
->nbnewthreads
,
371 -1*(data
->nbdeadthreads
));
372 print_headers(3, "FDs", data
->nbfiles
, data
->nbnewfiles
,
373 -1*(data
->nbclosedfiles
));
374 scale_unit(total_io(), io
);
375 mvwprintw(header
, 3, 43, "%sB/sec", io
);
379 gint
sort_by_cpu_desc(gconstpointer p1
, gconstpointer p2
)
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
;
386 if (totaln1
< totaln2
)
388 if (totaln1
== totaln2
)
393 gint
sort_by_tid_desc(gconstpointer p1
, gconstpointer p2
)
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
;
400 if (totaln1
< totaln2
)
402 if (totaln1
== totaln2
)
407 gint
sort_by_pid_desc(gconstpointer p1
, gconstpointer p2
)
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
;
414 if (totaln1
< totaln2
)
416 if (totaln1
== totaln2
)
421 gint
sort_by_process_read_desc(gconstpointer p1
, gconstpointer p2
)
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
;
428 if (totaln1
< totaln2
)
430 if (totaln1
== totaln2
)
435 gint
sort_by_process_write_desc(gconstpointer p1
, gconstpointer p2
)
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
;
442 if (totaln1
< totaln2
)
444 if (totaln1
== totaln2
)
449 gint
sort_by_process_total_desc(gconstpointer p1
, gconstpointer p2
)
451 struct processtop
*n1
= *(struct processtop
**)p1
;
452 struct processtop
*n2
= *(struct processtop
**)p2
;
453 unsigned long totaln1
= n1
->totalfilewrite
+ n1
->totalfileread
;
454 unsigned long totaln2
= n2
->totalfilewrite
+ n2
->totalfileread
;
456 if (totaln1
< totaln2
)
458 if (totaln1
== totaln2
)
463 gint
sort_by_file_read_desc(gconstpointer p1
, gconstpointer p2
)
465 struct files
*n1
= *(struct files
**)p1
;
466 struct files
*n2
= *(struct files
**)p2
;
467 unsigned long totaln1
;
468 unsigned long totaln2
;
473 if (totaln1
< totaln2
)
475 if (totaln1
== totaln2
)
480 gint
sort_by_file_write_desc(gconstpointer p1
, gconstpointer p2
)
482 struct files
*n1
= *(struct files
**)p1
;
483 struct files
*n2
= *(struct files
**)p2
;
484 unsigned long totaln1
;
485 unsigned long totaln2
;
490 if (totaln1
< totaln2
)
492 if (totaln1
== totaln2
)
497 gint
sort_by_file_fd_desc(gconstpointer p1
, gconstpointer p2
)
499 struct files
*n1
= *(struct files
**)p1
;
500 struct files
*n2
= *(struct files
**)p2
;
501 unsigned long totaln1
;
502 unsigned long totaln2
;
507 if (totaln1
< totaln2
)
509 if (totaln1
== totaln2
)
514 gint
sort_by_cpu_group_by_threads_desc(gconstpointer p1
, gconstpointer p2
)
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
;
521 if (totaln1
< totaln2
)
523 if (totaln1
== totaln2
)
528 void update_cputop_display()
531 int header_offset
= 2;
532 struct processtop
*tmp
;
533 unsigned long elapsed
;
535 int nblinedisplayed
= 0;
536 int current_line
= 0;
539 elapsed
= data
->end
- data
->start
;
540 maxcputime
= elapsed
* data
->cpu_table
->len
/ 100.0;
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
);
551 g_ptr_array_sort(data
->process_table
, sort_by_cpu_desc
);
553 set_window_title(center
, "CPU Top");
554 wattron(center
, A_BOLD
);
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
);
563 wattroff(center
, A_BOLD
);
565 max_center_lines
= LINES
- 5 - 7 - 1 - header_offset
;
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
);
572 if (process_selected(tmp
)) {
573 wattron(center
, COLOR_PAIR(6));
574 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
576 if (current_line
== selected_line
) {
577 selected_process
= tmp
;
578 wattron(center
, COLOR_PAIR(5));
579 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
582 mvwprintw(center
, current_line
+ header_offset
, 1, "%1.2f",
583 tmp
->totalcpunsec
/ maxcputime
);
585 mvwprintw(center
, current_line
+ header_offset
, 11, "%d", tmp
->pid
);
587 mvwprintw(center
, current_line
+ header_offset
, 21, "%d", tmp
->tid
);
589 mvwprintw(center
, current_line
+ header_offset
, 31, "%s", tmp
->comm
);
590 wattroff(center
, COLOR_PAIR(6));
591 wattroff(center
, COLOR_PAIR(5));
597 gint
sort_perf(gconstpointer p1
, gconstpointer p2
, gpointer key
)
599 struct processtop
*n1
= *(struct processtop
**) p1
;
600 struct processtop
*n2
= *(struct processtop
**) p2
;
602 struct perfcounter
*tmp1
, *tmp2
;
603 unsigned long totaln2
= 0;
604 unsigned long totaln1
= 0;
609 tmp1
= g_hash_table_lookup(n1
->perf
, key
);
613 totaln1
= tmp1
->count
;
615 tmp2
= g_hash_table_lookup(n2
->perf
, key
);
619 totaln2
= tmp2
->count
;
621 if (totaln1
< totaln2
)
623 if (totaln1
== totaln2
) {
626 if (totaln1
< totaln2
)
633 void print_key_title(char *key
, int line
)
635 wattron(center
, A_BOLD
);
636 mvwprintw(center
, line
, 1, "%s", key
);
637 mvwprintw(center
, line
, 30, " ");
638 wattroff(center
, A_BOLD
);
641 void update_process_details()
643 unsigned long elapsed
;
645 struct processtop
*tmp
;
646 struct files
*file_tmp
;
649 char filename_buf
[COLS
];
652 GPtrArray
*newfilearray
= g_ptr_array_new();
654 struct perfcounter
*perfn1
, *perfn2
;
657 set_window_title(center
, "Process details");
660 tmp
= find_process_tid(data
,
661 selected_process
->tid
,
662 selected_process
->comm
);
663 elapsed
= data
->end
- data
->start
;
664 maxcputime
= elapsed
* data
->cpu_table
->len
/ 100.0;
666 print_key_title("Name", line
++);
667 wprintw(center
, "%s", selected_process
->comm
);
668 print_key_title("TID", line
++);
669 wprintw(center
, "%d", selected_process
->tid
);
671 print_key_title("Does not exit at this time", 3);
675 print_key_title("PID", line
++);
676 wprintw(center
, "%d", tmp
->pid
);
677 print_key_title("PPID", line
++);
678 wprintw(center
, "%d", tmp
->ppid
);
679 print_key_title("CPU", line
++);
680 wprintw(center
, "%1.2f %%", tmp
->totalcpunsec
/maxcputime
);
682 print_key_title("READ B/s", line
++);
683 scale_unit(tmp
->fileread
, unit
);
684 wprintw(center
, "%s", unit
);
686 print_key_title("WRITE B/s", line
++);
687 scale_unit(tmp
->filewrite
, unit
);
688 wprintw(center
, "%s", unit
);
690 g_hash_table_iter_init(&iter
, global_perf_liszt
);
691 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
692 print_key_title((char *) key
, line
++);
693 perfn2
= g_hash_table_lookup(tmp
->perf
, (char *) key
);
694 wprintw(center
, "%d", perfn2
? perfn2
->count
: 0);
698 wattron(center
, A_BOLD
);
700 for (i
= 0; i
< 3; i
++) {
701 if (fileview
[i
].sort
)
702 wattron(center
, A_UNDERLINE
);
703 mvwprintw(center
, line
, column
, fileview
[i
].title
);
704 wattroff(center
, A_UNDERLINE
);
707 mvwprintw(center
, line
++, column
, "FILENAME");
708 wattroff(center
, A_BOLD
);
711 * since the process_files_table array could contain NULL file structures,
712 * and that the positions inside the array is important (it is the FD), we
713 * need to create a temporary array that we can sort.
715 for (i
= 0; i
< tmp
->process_files_table
->len
; i
++) {
716 file_tmp
= g_ptr_array_index(tmp
->process_files_table
, i
);
718 g_ptr_array_add(newfilearray
, file_tmp
);
721 if (fileview
[0].sort
== 1)
722 g_ptr_array_sort(newfilearray
, sort_by_file_fd_desc
);
723 else if (fileview
[1].sort
== 1)
724 g_ptr_array_sort(newfilearray
, sort_by_file_read_desc
);
725 else if (fileview
[2].sort
== 1)
726 g_ptr_array_sort(newfilearray
, sort_by_file_write_desc
);
728 g_ptr_array_sort(newfilearray
, sort_by_file_read_desc
);
730 for (i
= selected_line
; i
< newfilearray
->len
&&
731 i
< (selected_line
+ max_center_lines
- line
+ 2); i
++) {
732 file_tmp
= g_ptr_array_index(newfilearray
, i
);
735 mvwprintw(center
, line
+ j
, 1, "%d", file_tmp
->fd
);
736 scale_unit(file_tmp
->read
, unit
);
737 mvwprintw(center
, line
+ j
, 11, "%s", unit
);
738 scale_unit(file_tmp
->write
, unit
);
739 mvwprintw(center
, line
+ j
, 21, "%s", unit
);
740 snprintf(filename_buf
, COLS
- 25, "%s", file_tmp
->name
);
741 mvwprintw(center
, line
+ j
, 31, "%s", filename_buf
);
744 g_ptr_array_free(newfilearray
, TRUE
);
750 int nblinedisplayed
= 0;
751 int current_line
= 0;
752 struct processtop
*tmp
;
753 int header_offset
= 2;
755 struct perfcounter
*perfn1
, *perfn2
;
756 char *perf_key
= NULL
;
761 set_window_title(center
, "Perf Top");
762 wattron(center
, A_BOLD
);
763 mvwprintw(center
, 1, 1, "PID");
764 mvwprintw(center
, 1, 11, "TID");
765 mvwprintw(center
, 1, 22, "NAME");
768 g_hash_table_iter_init(&iter
, global_perf_liszt
);
769 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
770 if (perfn1
->visible
) {
771 /* + 5 to strip the "perf_" prefix */
772 mvwprintw(center
, 1, perf_row
, "%s",
777 perf_key
= (char *) key
;
781 wattroff(center
, A_BOLD
);
783 g_ptr_array_sort_with_data(data
->process_table
, sort_perf
, perf_key
);
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
);
789 if (process_selected(tmp
)) {
790 wattron(center
, COLOR_PAIR(6));
791 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
793 if (current_line
== selected_line
) {
794 selected_process
= tmp
;
795 wattron(center
, COLOR_PAIR(5));
796 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
799 mvwprintw(center
, current_line
+ header_offset
, 1, "%d", tmp
->pid
);
800 mvwprintw(center
, current_line
+ header_offset
, 11, "%d", tmp
->tid
);
801 mvwprintw(center
, current_line
+ header_offset
, 22, "%s", tmp
->comm
);
803 g_hash_table_iter_init(&iter
, global_perf_liszt
);
806 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
807 if (perfn1
->visible
) {
808 perfn2
= g_hash_table_lookup(tmp
->perf
, (char *) key
);
810 value
= perfn2
->count
;
813 mvwprintw(center
, current_line
+ header_offset
,
814 perf_row
, "%d", value
);
819 wattroff(center
, COLOR_PAIR(6));
820 wattroff(center
, COLOR_PAIR(5));
826 void update_iostream()
829 int header_offset
= 2;
830 struct processtop
*tmp
;
831 int nblinedisplayed
= 0;
832 int current_line
= 0;
837 set_window_title(center
, "IO Top");
838 wattron(center
, A_BOLD
);
839 mvwprintw(center
, 1, 1, "PID");
840 mvwprintw(center
, 1, 11, "TID");
841 mvwprintw(center
, 1, 22, "NAME");
843 for (i
= 0; i
< 3; i
++) {
844 if (iostreamtopview
[i
].sort
)
845 wattron(center
, A_UNDERLINE
);
846 mvwprintw(center
, 1, column
, iostreamtopview
[i
].title
);
847 wattroff(center
, A_UNDERLINE
);
850 wattroff(center
, A_BOLD
);
851 wattroff(center
, A_UNDERLINE
);
853 if (iostreamtopview
[0].sort
== 1)
854 g_ptr_array_sort(data
->process_table
, sort_by_process_read_desc
);
855 else if (iostreamtopview
[1].sort
== 1)
856 g_ptr_array_sort(data
->process_table
, sort_by_process_write_desc
);
857 else if (iostreamtopview
[2].sort
== 1)
858 g_ptr_array_sort(data
->process_table
, sort_by_process_total_desc
);
860 g_ptr_array_sort(data
->process_table
, sort_by_process_total_desc
);
862 for (i
= list_offset
; i
< data
->process_table
->len
&&
863 nblinedisplayed
< max_center_lines
; i
++) {
864 tmp
= g_ptr_array_index(data
->process_table
, i
);
866 if (process_selected(tmp
)) {
867 wattron(center
, COLOR_PAIR(6));
868 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
870 if (current_line
== selected_line
) {
871 selected_process
= tmp
;
872 wattron(center
, COLOR_PAIR(5));
873 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
876 mvwprintw(center
, current_line
+ header_offset
, 1, "%d", tmp
->pid
);
878 mvwprintw(center
, current_line
+ header_offset
, 11, "%d", tmp
->tid
);
880 mvwprintw(center
, current_line
+ header_offset
, 22, "%s", tmp
->comm
);
882 /* READ (bytes/sec) */
883 scale_unit(tmp
->fileread
, unit
);
884 mvwprintw(center
, current_line
+ header_offset
, 40, "%s", unit
);
886 /* WRITE (bytes/sec) */
887 scale_unit(tmp
->filewrite
, unit
);
888 mvwprintw(center
, current_line
+ header_offset
, 52, "%s", unit
);
891 total
= tmp
->totalfileread
+ tmp
->totalfilewrite
;
893 scale_unit(total
, unit
);
894 mvwprintw(center
, current_line
+ header_offset
, 64, "%s", unit
);
896 wattroff(center
, COLOR_PAIR(6));
897 wattroff(center
, COLOR_PAIR(5));
903 void update_current_view()
905 sem_wait(&update_display_sem
);
912 switch (current_view
) {
914 update_cputop_display();
919 case process_details
:
920 update_process_details();
926 update_cputop_display();
933 sem_post(&update_display_sem
);
936 void update_process_detail_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
943 if (pref_panel_window
) {
944 del_panel(pref_panel
);
945 delwin(pref_panel_window
);
949 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
950 pref_panel
= new_panel(pref_panel_window
);
952 werase(pref_panel_window
);
953 box(pref_panel_window
, 0 , 0);
954 set_window_title(pref_panel_window
, "Process Detail Preferences ");
955 wattron(pref_panel_window
, A_BOLD
);
956 mvwprintw(pref_panel_window
, size
+ 1, 1,
957 " 's' : sort, space : toggle");
958 wattroff(pref_panel_window
, A_BOLD
);
960 if (*line_selected
> (size
- 1))
961 *line_selected
= size
- 1;
962 if (toggle_sort
== 1) {
963 if (fileview
[*line_selected
].sort
== 1)
964 fileview
[*line_selected
].reverse
= 1;
965 for (i
= 0; i
< size
; i
++)
966 fileview
[i
].sort
= 0;
967 fileview
[*line_selected
].sort
= 1;
968 update_current_view();
971 for (i
= 0; i
< size
; i
++) {
972 if (i
== *line_selected
) {
973 wattron(pref_panel_window
, COLOR_PAIR(5));
974 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
976 if (fileview
[i
].sort
== 1)
977 wattron(pref_panel_window
, A_BOLD
);
978 mvwprintw(pref_panel_window
, i
+ 1, 1, "[-] %s",
980 wattroff(pref_panel_window
, A_BOLD
);
981 wattroff(pref_panel_window
, COLOR_PAIR(5));
988 void update_iostream_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
995 if (pref_panel_window
) {
996 del_panel(pref_panel
);
997 delwin(pref_panel_window
);
1001 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1002 pref_panel
= new_panel(pref_panel_window
);
1004 werase(pref_panel_window
);
1005 box(pref_panel_window
, 0 , 0);
1006 set_window_title(pref_panel_window
, "IOTop Preferences ");
1007 wattron(pref_panel_window
, A_BOLD
);
1008 mvwprintw(pref_panel_window
, size
+ 1, 1,
1009 " 's' : sort, space : toggle");
1010 wattroff(pref_panel_window
, A_BOLD
);
1012 if (*line_selected
> (size
- 1))
1013 *line_selected
= size
- 1;
1014 if (toggle_sort
== 1) {
1015 if (iostreamtopview
[*line_selected
].sort
== 1)
1016 iostreamtopview
[*line_selected
].reverse
= 1;
1017 for (i
= 0; i
< size
; i
++)
1018 iostreamtopview
[i
].sort
= 0;
1019 iostreamtopview
[*line_selected
].sort
= 1;
1020 update_current_view();
1023 for (i
= 0; i
< size
; i
++) {
1024 if (i
== *line_selected
) {
1025 wattron(pref_panel_window
, COLOR_PAIR(5));
1026 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1028 if (iostreamtopview
[i
].sort
== 1)
1029 wattron(pref_panel_window
, A_BOLD
);
1030 mvwprintw(pref_panel_window
, i
+ 1, 1, "[-] %s",
1031 iostreamtopview
[i
].title
);
1032 wattroff(pref_panel_window
, A_BOLD
);
1033 wattroff(pref_panel_window
, COLOR_PAIR(5));
1040 void update_cpu_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1047 if (pref_panel_window
) {
1048 del_panel(pref_panel
);
1049 delwin(pref_panel_window
);
1053 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1054 pref_panel
= new_panel(pref_panel_window
);
1056 werase(pref_panel_window
);
1057 box(pref_panel_window
, 0 , 0);
1058 set_window_title(pref_panel_window
, "CPUTop Preferences ");
1059 wattron(pref_panel_window
, A_BOLD
);
1060 mvwprintw(pref_panel_window
, size
+ 1, 1,
1061 " 's' : sort, space : toggle");
1062 wattroff(pref_panel_window
, A_BOLD
);
1064 if (*line_selected
> (size
- 1))
1065 *line_selected
= size
- 1;
1066 if (toggle_sort
== 1) {
1067 /* special case, we don't support sorting by procname for now */
1068 if (*line_selected
!= 3) {
1069 if (cputopview
[*line_selected
].sort
== 1)
1070 cputopview
[*line_selected
].reverse
= 1;
1071 for (i
= 0; i
< size
; i
++)
1072 cputopview
[i
].sort
= 0;
1073 cputopview
[*line_selected
].sort
= 1;
1074 update_current_view();
1078 for (i
= 0; i
< size
; i
++) {
1079 if (i
== *line_selected
) {
1080 wattron(pref_panel_window
, COLOR_PAIR(5));
1081 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1083 if (cputopview
[i
].sort
== 1)
1084 wattron(pref_panel_window
, A_BOLD
);
1085 mvwprintw(pref_panel_window
, i
+ 1, 1, "[-] %s",
1086 cputopview
[i
].title
);
1087 wattroff(pref_panel_window
, A_BOLD
);
1088 wattroff(pref_panel_window
, COLOR_PAIR(5));
1095 void update_perf_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1098 struct perfcounter
*perf
;
1104 if (pref_panel_window
) {
1105 del_panel(pref_panel
);
1106 delwin(pref_panel_window
);
1108 size
= g_hash_table_size(global_perf_liszt
);
1110 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1111 pref_panel
= new_panel(pref_panel_window
);
1113 werase(pref_panel_window
);
1114 box(pref_panel_window
, 0 , 0);
1115 set_window_title(pref_panel_window
, "Perf Preferences ");
1116 wattron(pref_panel_window
, A_BOLD
);
1117 mvwprintw(pref_panel_window
, g_hash_table_size(global_perf_liszt
) + 1, 1,
1118 " 's' : sort, space : toggle");
1119 wattroff(pref_panel_window
, A_BOLD
);
1121 if (toggle_sort
== 1) {
1123 perflist
= g_list_first(g_hash_table_get_keys(global_perf_liszt
));
1125 perf
= g_hash_table_lookup(global_perf_liszt
, perflist
->data
);
1126 if (i
!= *line_selected
)
1131 perflist
= g_list_next(perflist
);
1133 update_current_view();
1137 perflist
= g_list_first(g_hash_table_get_keys(global_perf_liszt
));
1139 perf
= g_hash_table_lookup(global_perf_liszt
, perflist
->data
);
1140 if (i
== *line_selected
&& toggle_view
== 1) {
1141 perf
->visible
= perf
->visible
== 1 ? 0:1;
1142 update_current_view();
1144 if (i
== *line_selected
) {
1145 wattron(pref_panel_window
, COLOR_PAIR(5));
1146 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1148 if (perf
->sort
== 1)
1149 wattron(pref_panel_window
, A_BOLD
);
1150 mvwprintw(pref_panel_window
, i
+ 1, 1, "[%c] %s",
1151 perf
->visible
== 1 ? 'x' : ' ',
1152 (char *) perflist
->data
+ 5);
1153 wattroff(pref_panel_window
, A_BOLD
);
1154 wattroff(pref_panel_window
, COLOR_PAIR(5));
1156 perflist
= g_list_next(perflist
);
1162 int update_preference_panel(int *line_selected
, int toggle_view
, int toggle_sort
)
1166 switch(current_view
) {
1168 update_perf_pref(line_selected
, toggle_view
, toggle_sort
);
1171 update_cpu_pref(line_selected
, toggle_view
, toggle_sort
);
1174 update_iostream_pref(line_selected
, toggle_view
, toggle_sort
);
1176 case process_details
:
1177 update_process_detail_pref(line_selected
, toggle_view
, toggle_sort
);
1187 void toggle_pref_panel(void)
1191 if (pref_panel_visible
) {
1192 hide_panel(pref_panel
);
1193 pref_panel_visible
= 0;
1195 ret
= update_preference_panel(&pref_line_selected
, 0, 0);
1198 show_panel(pref_panel
);
1199 pref_panel_visible
= 1;
1205 void display(unsigned int index
)
1207 last_display_index
= index
;
1208 currently_displayed_index
= index
;
1209 data
= g_ptr_array_index(copies
, index
);
1212 max_elements
= data
->process_table
->len
;
1213 update_current_view();
1219 void pause_display()
1223 sem_wait(&pause_sem
);
1226 void resume_display()
1229 print_log("Resume");
1230 sem_post(&pause_sem
);
1233 void *handle_keyboard(void *p
)
1236 while((ch
= getch())) {
1238 /* Move the cursor and scroll */
1241 if (pref_panel_visible
) {
1242 pref_line_selected
++;
1243 update_preference_panel(&pref_line_selected
, 0, 0);
1245 if (selected_line
< (max_center_lines
- 1) &&
1246 selected_line
< max_elements
- 1) {
1249 } else if (selected_in_list
< (max_elements
- 1)
1250 && (list_offset
< (max_elements
- max_center_lines
))) {
1254 update_current_view();
1261 if (pref_panel_visible
) {
1262 if (pref_line_selected
> 0)
1263 pref_line_selected
--;
1264 update_preference_panel(&pref_line_selected
, 0, 0);
1266 if (selected_line
> 0) {
1269 } else if (selected_in_list
> 0 && list_offset
> 0) {
1273 update_current_view();
1279 /* Navigate the history with arrows */
1281 if (currently_displayed_index
> 0) {
1282 currently_displayed_index
--;
1283 print_log("Going back in time");
1285 print_log("Cannot rewind, last data is already displayed");
1287 data
= g_ptr_array_index(copies
, currently_displayed_index
);
1288 max_elements
= data
->process_table
->len
;
1290 /* we force to pause the display when moving in time */
1291 if (toggle_pause
< 0)
1294 update_current_view();
1298 if (currently_displayed_index
< last_display_index
) {
1299 currently_displayed_index
++;
1300 print_log("Going forward in time");
1301 data
= g_ptr_array_index(copies
, currently_displayed_index
);
1302 max_elements
= data
->process_table
->len
;
1303 update_current_view();
1306 print_log("Manually moving forward");
1308 if (toggle_pause
> 0) {
1309 sem_post(&pause_sem
);
1310 update_current_view();
1311 sem_wait(&pause_sem
);
1317 if (pref_panel_visible
) {
1318 update_preference_panel(&pref_line_selected
, 1, 0);
1320 update_selected_processes();
1321 update_current_view();
1325 if (pref_panel_visible
)
1326 update_preference_panel(&pref_line_selected
, 0, 1);
1329 case 13: /* FIXME : KEY_ENTER ?? */
1330 if (pref_panel_visible
)
1332 if (current_view
!= process_details
) {
1333 previous_view
= current_view
;
1334 current_view
= process_details
;
1336 current_view
= previous_view
;
1337 previous_view
= process_details
;
1339 update_current_view();
1343 if (pref_panel_visible
)
1344 toggle_pref_panel();
1347 update_current_view();
1350 if (pref_panel_visible
)
1351 toggle_pref_panel();
1354 update_current_view();
1357 if (pref_panel_visible
)
1358 toggle_pref_panel();
1359 current_view
= perf
;
1361 update_current_view();
1364 if (pref_panel_visible
)
1365 toggle_pref_panel();
1366 current_view
= iostream
;
1368 update_current_view();
1375 toggle_threads
*= -1;
1376 update_current_view();
1379 if (toggle_pause
< 0) {
1386 toggle_pref_panel();
1388 /* ESCAPE, but slow to process, don't know why */
1390 if (pref_panel_visible
)
1391 toggle_pref_panel();
1392 else if (current_view
== process_details
) {
1393 current_view
= previous_view
;
1394 previous_view
= process_details
;
1396 update_current_view();
1400 update_current_view();
1408 void init_view_headers()
1410 cputopview
[0].title
= strdup("CPU(%)");
1411 cputopview
[0].sort
= 1;
1412 cputopview
[1].title
= strdup("TGID");
1413 cputopview
[2].title
= strdup("PID");
1414 cputopview
[3].title
= strdup("NAME");
1416 iostreamtopview
[0].title
= strdup("R (B/sec)");
1417 iostreamtopview
[1].title
= strdup("W (B/sec)");
1418 iostreamtopview
[2].title
= strdup("Total (B)");
1419 iostreamtopview
[2].sort
= 1;
1421 fileview
[0].title
= strdup("FD");
1422 fileview
[1].title
= strdup("READ");
1423 fileview
[1].sort
= 1;
1424 fileview
[2].title
= strdup("WRITE");
1429 selected_processes
= g_ptr_array_new();
1430 sem_init(&update_display_sem
, 0, 1);
1431 init_view_headers();
1434 header
= create_window(5, COLS
- 1, 0, 0);
1435 center
= create_window(LINES
- 5 - 7, COLS
- 1, 5, 0);
1436 status
= create_window(MAX_LOG_LINES
+ 2, COLS
- 1, LINES
- 7, 0);
1437 footer
= create_window(1, COLS
- 1, LINES
- 1, 0);
1439 print_log("Starting display");
1441 main_panel
= new_panel(center
);
1448 pthread_create(&keyboard_thread
, NULL
, handle_keyboard
, (void *)NULL
);