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()
240 if (process_selected(selected_process
)) {
241 g_ptr_array_remove(selected_processes
, selected_process
);
242 print_log("Process removed");
244 g_ptr_array_add(selected_processes
, selected_process
);
245 print_log("Process added");
249 void print_key(WINDOW
*win
, char *key
, char *desc
, int toggle
)
256 wattron(win
, COLOR_PAIR(pair
));
257 wprintw(footer
, "%s", key
);
258 wattroff(win
, COLOR_PAIR(pair
));
259 wprintw(footer
, ":%s", desc
);
264 sem_wait(&update_display_sem
);
267 print_key(footer
, "F2", "CPUtop ", current_view
== cpu
);
268 print_key(footer
, "F3", "PerfTop ", current_view
== perf
);
269 print_key(footer
, "F4", "IOTop ", current_view
== iostream
);
270 print_key(footer
, "Enter", "Details ", current_view
== process_details
);
271 print_key(footer
, "Space", "Highlight ", 0);
272 print_key(footer
, "q", "Quit | ", 0);
273 print_key(footer
, "P", "Perf Pref ", 0);
274 print_key(footer
, "p", "Pause ", toggle_pause
);
277 sem_post(&update_display_sem
);
284 set_window_title(header
, "Statistics for interval [gathering data...[");
285 wattron(header
, A_BOLD
);
286 mvwprintw(header
, 1, 4, "CPUs");
287 mvwprintw(header
, 2, 4, "Threads");
288 mvwprintw(header
, 3, 4, "FDs");
289 wattroff(header
, A_BOLD
);
293 struct tm
format_timestamp(uint64_t timestamp
)
296 uint64_t ts_sec
= 0, ts_nsec
;
300 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
301 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
303 time_s
= (time_t) ts_sec
;
305 localtime_r(&time_s
, &tm
);
310 static void scale_unit(uint64_t bytes
, char *ret
)
312 if (bytes
>= 1000000000)
313 sprintf(ret
, "%" PRIu64
"G", bytes
/1000000000);
314 if (bytes
>= 1000000)
315 sprintf(ret
, "%" PRIu64
"M", bytes
/1000000);
316 else if (bytes
>= 1000)
317 sprintf(ret
, "%" PRIu64
"K", bytes
/1000);
319 sprintf(ret
, "%" PRIu64
, bytes
);
324 struct processtop
*tmp
;
327 for (i
= 0; i
< data
->process_table
->len
; i
++) {
328 tmp
= g_ptr_array_index(data
->process_table
, i
);
329 total
+= tmp
->fileread
;
330 total
+= tmp
->filewrite
;
338 struct tm start
, end
;
339 uint64_t ts_nsec_start
, ts_nsec_end
;
342 ts_nsec_start
= data
->start
% NSEC_PER_SEC
;
343 start
= format_timestamp(data
->start
);
345 ts_nsec_end
= data
->end
% NSEC_PER_SEC
;
346 end
= format_timestamp(data
->end
);
350 set_window_title(header
, "Statistics for interval ");
351 wattron(header
, A_BOLD
);
353 wprintw(header
, "[%02d:%02d:%02d.%09" PRIu64
", %02d:%02d:%02d.%09" PRIu64
"[",
354 start
.tm_hour
, start
.tm_min
, start
.tm_sec
, ts_nsec_start
,
355 end
.tm_hour
, end
.tm_min
, end
.tm_sec
, ts_nsec_end
);
356 mvwprintw(header
, 1, 4, "CPUs");
357 wattroff(header
, A_BOLD
);
358 wprintw(header
, "\t%d\t(max/cpu : %0.2f%)", data
->cpu_table
->len
,
359 100.0/data
->cpu_table
->len
);
360 print_headers(2, "Threads", data
->nbthreads
, data
->nbnewthreads
,
361 -1*(data
->nbdeadthreads
));
362 print_headers(3, "FDs", data
->nbfiles
, data
->nbnewfiles
,
363 -1*(data
->nbclosedfiles
));
364 scale_unit(total_io(), io
);
365 mvwprintw(header
, 3, 43, "%sB/sec", io
);
369 gint
sort_by_cpu_desc(gconstpointer p1
, gconstpointer p2
)
371 struct processtop
*n1
= *(struct processtop
**)p1
;
372 struct processtop
*n2
= *(struct processtop
**)p2
;
373 unsigned long totaln1
= n1
->totalcpunsec
;
374 unsigned long totaln2
= n2
->totalcpunsec
;
376 if (totaln1
< totaln2
)
378 if (totaln1
== totaln2
)
383 gint
sort_by_tid_desc(gconstpointer p1
, gconstpointer p2
)
385 struct processtop
*n1
= *(struct processtop
**)p1
;
386 struct processtop
*n2
= *(struct processtop
**)p2
;
387 unsigned long totaln1
= n1
->tid
;
388 unsigned long totaln2
= n2
->tid
;
390 if (totaln1
< totaln2
)
392 if (totaln1
== totaln2
)
397 gint
sort_by_pid_desc(gconstpointer p1
, gconstpointer p2
)
399 struct processtop
*n1
= *(struct processtop
**)p1
;
400 struct processtop
*n2
= *(struct processtop
**)p2
;
401 unsigned long totaln1
= n1
->pid
;
402 unsigned long totaln2
= n2
->pid
;
404 if (totaln1
< totaln2
)
406 if (totaln1
== totaln2
)
411 gint
sort_by_process_read_desc(gconstpointer p1
, gconstpointer p2
)
413 struct processtop
*n1
= *(struct processtop
**)p1
;
414 struct processtop
*n2
= *(struct processtop
**)p2
;
415 unsigned long totaln1
= n1
->fileread
;
416 unsigned long totaln2
= n2
->fileread
;
418 if (totaln1
< totaln2
)
420 if (totaln1
== totaln2
)
425 gint
sort_by_process_write_desc(gconstpointer p1
, gconstpointer p2
)
427 struct processtop
*n1
= *(struct processtop
**)p1
;
428 struct processtop
*n2
= *(struct processtop
**)p2
;
429 unsigned long totaln1
= n1
->filewrite
;
430 unsigned long totaln2
= n2
->filewrite
;
432 if (totaln1
< totaln2
)
434 if (totaln1
== totaln2
)
439 gint
sort_by_process_total_desc(gconstpointer p1
, gconstpointer p2
)
441 struct processtop
*n1
= *(struct processtop
**)p1
;
442 struct processtop
*n2
= *(struct processtop
**)p2
;
443 unsigned long totaln1
= n1
->filewrite
+ n1
->fileread
;
444 unsigned long totaln2
= n2
->filewrite
+ n2
->fileread
;
446 if (totaln1
< totaln2
)
448 if (totaln1
== totaln2
)
453 gint
sort_by_file_read_desc(gconstpointer p1
, gconstpointer p2
)
455 struct files
*n1
= *(struct files
**)p1
;
456 struct files
*n2
= *(struct files
**)p2
;
457 unsigned long totaln1
;
458 unsigned long totaln2
;
463 if (totaln1
< totaln2
)
465 if (totaln1
== totaln2
)
470 gint
sort_by_file_write_desc(gconstpointer p1
, gconstpointer p2
)
472 struct files
*n1
= *(struct files
**)p1
;
473 struct files
*n2
= *(struct files
**)p2
;
474 unsigned long totaln1
;
475 unsigned long totaln2
;
480 if (totaln1
< totaln2
)
482 if (totaln1
== totaln2
)
487 gint
sort_by_file_fd_desc(gconstpointer p1
, gconstpointer p2
)
489 struct files
*n1
= *(struct files
**)p1
;
490 struct files
*n2
= *(struct files
**)p2
;
491 unsigned long totaln1
;
492 unsigned long totaln2
;
497 if (totaln1
< totaln2
)
499 if (totaln1
== totaln2
)
504 gint
sort_by_cpu_group_by_threads_desc(gconstpointer p1
, gconstpointer p2
)
506 struct processtop
*n1
= *(struct processtop
**)p1
;
507 struct processtop
*n2
= *(struct processtop
**)p2
;
508 unsigned long totaln1
= n1
->threadstotalcpunsec
;
509 unsigned long totaln2
= n2
->threadstotalcpunsec
;
511 if (totaln1
< totaln2
)
513 if (totaln1
== totaln2
)
518 void update_cputop_display()
521 int header_offset
= 2;
522 struct processtop
*tmp
;
523 unsigned long elapsed
;
525 int nblinedisplayed
= 0;
526 int current_line
= 0;
528 elapsed
= data
->end
- data
->start
;
529 maxcputime
= elapsed
* data
->cpu_table
->len
/ 100.0;
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
);
540 g_ptr_array_sort(data
->process_table
, sort_by_cpu_desc
);
542 set_window_title(center
, "CPU Top");
543 wattron(center
, A_BOLD
);
544 mvwprintw(center
, 1, 1, cputopview
[0].title
);
545 mvwprintw(center
, 1, 12, cputopview
[1].title
);
546 mvwprintw(center
, 1, 22, cputopview
[2].title
);
547 mvwprintw(center
, 1, 32, cputopview
[3].title
);
548 wattroff(center
, A_BOLD
);
550 max_center_lines
= LINES
- 5 - 7 - 1 - header_offset
;
552 /* iterate the process (thread) list */
553 for (i
= list_offset
; i
< data
->process_table
->len
&&
554 nblinedisplayed
< max_center_lines
; i
++) {
555 tmp
= g_ptr_array_index(data
->process_table
, i
);
557 if (process_selected(tmp
)) {
558 wattron(center
, COLOR_PAIR(6));
559 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
561 if (current_line
== selected_line
) {
562 selected_process
= tmp
;
563 wattron(center
, COLOR_PAIR(5));
564 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
567 mvwprintw(center
, current_line
+ header_offset
, 1, "%1.2f",
568 tmp
->totalcpunsec
/ maxcputime
);
570 mvwprintw(center
, current_line
+ header_offset
, 12, "%d", tmp
->pid
);
572 mvwprintw(center
, current_line
+ header_offset
, 22, "%d", tmp
->tid
);
574 mvwprintw(center
, current_line
+ header_offset
, 32, "%s", tmp
->comm
);
575 wattroff(center
, COLOR_PAIR(6));
576 wattroff(center
, COLOR_PAIR(5));
582 gint
sort_perf(gconstpointer p1
, gconstpointer p2
, gpointer key
)
584 struct processtop
*n1
= *(struct processtop
**) p1
;
585 struct processtop
*n2
= *(struct processtop
**) p2
;
587 struct perfcounter
*tmp1
, *tmp2
;
588 unsigned long totaln2
= 0;
589 unsigned long totaln1
= 0;
594 tmp1
= g_hash_table_lookup(n1
->perf
, key
);
598 totaln1
= tmp1
->count
;
600 tmp2
= g_hash_table_lookup(n2
->perf
, key
);
604 totaln2
= tmp2
->count
;
606 if (totaln1
< totaln2
)
608 if (totaln1
== totaln2
) {
611 if (totaln1
< totaln2
)
618 void print_key_title(char *key
, int line
)
620 wattron(center
, A_BOLD
);
621 mvwprintw(center
, line
, 1, "%s\t", key
);
622 wattroff(center
, A_BOLD
);
625 void update_process_details()
627 unsigned long elapsed
;
629 struct processtop
*tmp
;
630 struct files
*file_tmp
;
633 char filename_buf
[COLS
];
634 GPtrArray
*newfilearray
= g_ptr_array_new();
636 set_window_title(center
, "Process details");
639 tmp
= find_process_tid(data
,
640 selected_process
->tid
,
641 selected_process
->comm
);
642 elapsed
= data
->end
- data
->start
;
643 maxcputime
= elapsed
* data
->cpu_table
->len
/ 100.0;
645 print_key_title("Name", 1);
646 wprintw(center
, "%s", selected_process
->comm
);
647 print_key_title("TID", 2);
648 wprintw(center
, "%d", selected_process
->tid
);
650 print_key_title("Does not exit at this time", 3);
654 print_key_title("PID", 3);
655 wprintw(center
, "%d", tmp
->pid
);
656 print_key_title("PPID", 4);
657 wprintw(center
, "%d", tmp
->ppid
);
658 print_key_title("CPU", 5);
659 wprintw(center
, "%1.2f %%", tmp
->totalcpunsec
/maxcputime
);
661 print_key_title("READ B/s", 6);
662 scale_unit(tmp
->fileread
, unit
);
663 wprintw(center
, "%s", unit
);
665 print_key_title("WRITE B/s", 7);
666 scale_unit(tmp
->filewrite
, unit
);
667 wprintw(center
, "%s", unit
);
669 wattron(center
, A_BOLD
);
670 mvwprintw(center
, 8, 1, "FD");
671 mvwprintw(center
, 8, 10, "READ");
672 mvwprintw(center
, 8, 17, "WRITE");
673 mvwprintw(center
, 8, 24, "FILENAME");
674 wattroff(center
, A_BOLD
);
677 * since the process_files_table array could contain NULL file structures,
678 * and that the positions inside the array is important (it is the FD), we
679 * need to create a temporary array that we can sort.
681 for (i
= 0; i
< tmp
->process_files_table
->len
; i
++) {
682 file_tmp
= g_ptr_array_index(tmp
->process_files_table
, i
);
684 g_ptr_array_add(newfilearray
, file_tmp
);
687 if (fileview
[0].sort
== 1)
688 g_ptr_array_sort(newfilearray
, sort_by_file_fd_desc
);
689 else if (fileview
[1].sort
== 1)
690 g_ptr_array_sort(newfilearray
, sort_by_file_read_desc
);
691 else if (fileview
[2].sort
== 1)
692 g_ptr_array_sort(newfilearray
, sort_by_file_write_desc
);
694 g_ptr_array_sort(newfilearray
, sort_by_file_read_desc
);
696 for (i
= selected_line
; i
< newfilearray
->len
&&
697 i
< (selected_line
+ max_center_lines
- 7); i
++) {
698 file_tmp
= g_ptr_array_index(newfilearray
, i
);
701 mvwprintw(center
, 9 + j
, 1, "%d", file_tmp
->fd
);
702 scale_unit(file_tmp
->read
, unit
);
703 mvwprintw(center
, 9 + j
, 10, "%s", unit
);
704 scale_unit(file_tmp
->write
, unit
);
705 mvwprintw(center
, 9 + j
, 17, "%s", unit
);
706 snprintf(filename_buf
, COLS
- 25, "%s", file_tmp
->name
);
707 mvwprintw(center
, 9 + j
, 24, "%s", filename_buf
);
710 g_ptr_array_free(newfilearray
, TRUE
);
716 int nblinedisplayed
= 0;
717 int current_line
= 0;
718 struct processtop
*tmp
;
719 int header_offset
= 2;
721 struct perfcounter
*perfn1
, *perfn2
;
722 char *perf_key
= NULL
;
727 set_window_title(center
, "Perf Top");
728 wattron(center
, A_BOLD
);
729 mvwprintw(center
, 1, 1, "PID");
730 mvwprintw(center
, 1, 11, "TID");
731 mvwprintw(center
, 1, 22, "NAME");
734 g_hash_table_iter_init(&iter
, global_perf_liszt
);
735 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
736 if (perfn1
->visible
) {
737 /* + 5 to strip the "perf_" prefix */
738 mvwprintw(center
, 1, perf_row
, "%s",
743 perf_key
= (char *) key
;
747 wattroff(center
, A_BOLD
);
749 g_ptr_array_sort_with_data(data
->process_table
, sort_perf
, perf_key
);
751 for (i
= 0; i
< data
->process_table
->len
&&
752 nblinedisplayed
< max_center_lines
; i
++) {
753 tmp
= g_ptr_array_index(data
->process_table
, i
);
755 if (process_selected(tmp
)) {
756 wattron(center
, COLOR_PAIR(6));
757 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
759 if (current_line
== selected_line
) {
760 selected_process
= tmp
;
761 wattron(center
, COLOR_PAIR(5));
762 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
765 mvwprintw(center
, current_line
+ header_offset
, 1, "%d", tmp
->pid
);
766 mvwprintw(center
, current_line
+ header_offset
, 11, "%d", tmp
->tid
);
767 mvwprintw(center
, current_line
+ header_offset
, 22, "%s", tmp
->comm
);
769 g_hash_table_iter_init(&iter
, global_perf_liszt
);
772 while (g_hash_table_iter_next (&iter
, &key
, (gpointer
) &perfn1
)) {
773 if (perfn1
->visible
) {
774 perfn2
= g_hash_table_lookup(tmp
->perf
, (char *) key
);
776 value
= perfn2
->count
;
779 mvwprintw(center
, current_line
+ header_offset
,
780 perf_row
, "%d", value
);
785 wattroff(center
, COLOR_PAIR(6));
786 wattroff(center
, COLOR_PAIR(5));
792 void update_iostream()
795 int header_offset
= 2;
796 struct processtop
*tmp
;
797 int nblinedisplayed
= 0;
798 int current_line
= 0;
802 set_window_title(center
, "IO Top");
803 wattron(center
, A_BOLD
);
804 mvwprintw(center
, 1, 1, "PID");
805 mvwprintw(center
, 1, 11, "TID");
806 mvwprintw(center
, 1, 22, "NAME");
807 mvwprintw(center
, 1, 40, "R (B/sec)");
808 mvwprintw(center
, 1, 52, "W (B/sec)");
809 mvwprintw(center
, 1, 64, "Total");
810 wattroff(center
, A_BOLD
);
812 if (iostreamtopview
[0].sort
== 1)
813 g_ptr_array_sort(data
->process_table
, sort_by_process_read_desc
);
814 else if (iostreamtopview
[1].sort
== 1)
815 g_ptr_array_sort(data
->process_table
, sort_by_process_write_desc
);
816 else if (iostreamtopview
[2].sort
== 1)
817 g_ptr_array_sort(data
->process_table
, sort_by_process_total_desc
);
819 g_ptr_array_sort(data
->process_table
, sort_by_process_total_desc
);
821 for (i
= list_offset
; i
< data
->process_table
->len
&&
822 nblinedisplayed
< max_center_lines
; i
++) {
823 tmp
= g_ptr_array_index(data
->process_table
, i
);
825 if (process_selected(tmp
)) {
826 wattron(center
, COLOR_PAIR(6));
827 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
829 if (current_line
== selected_line
) {
830 selected_process
= tmp
;
831 wattron(center
, COLOR_PAIR(5));
832 mvwhline(center
, current_line
+ header_offset
, 1, ' ', COLS
-3);
835 mvwprintw(center
, current_line
+ header_offset
, 1, "%d", tmp
->pid
);
837 mvwprintw(center
, current_line
+ header_offset
, 11, "%d", tmp
->tid
);
839 mvwprintw(center
, current_line
+ header_offset
, 22, "%s", tmp
->comm
);
841 /* READ (bytes/sec) */
842 scale_unit(tmp
->fileread
, unit
);
843 mvwprintw(center
, current_line
+ header_offset
, 40, "%s", unit
);
845 /* WRITE (bytes/sec) */
846 scale_unit(tmp
->filewrite
, unit
);
847 mvwprintw(center
, current_line
+ header_offset
, 52, "%s", unit
);
850 total
= tmp
->totalfileread
+ tmp
->totalfilewrite
;
852 scale_unit(total
, unit
);
853 mvwprintw(center
, current_line
+ header_offset
, 64, "%s", unit
);
855 wattroff(center
, COLOR_PAIR(6));
856 wattroff(center
, COLOR_PAIR(5));
862 void update_current_view()
864 sem_wait(&update_display_sem
);
871 switch (current_view
) {
873 update_cputop_display();
878 case process_details
:
879 update_process_details();
885 update_cputop_display();
892 sem_post(&update_display_sem
);
895 void update_process_detail_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
902 if (pref_panel_window
) {
903 del_panel(pref_panel
);
904 delwin(pref_panel_window
);
908 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
909 pref_panel
= new_panel(pref_panel_window
);
911 werase(pref_panel_window
);
912 box(pref_panel_window
, 0 , 0);
913 set_window_title(pref_panel_window
, "Process Detail Preferences ");
914 wattron(pref_panel_window
, A_BOLD
);
915 mvwprintw(pref_panel_window
, size
+ 1, 1,
917 wattroff(pref_panel_window
, A_BOLD
);
919 if (*line_selected
> (size
- 1))
920 *line_selected
= size
- 1;
921 if (toggle_sort
== 1) {
922 if (fileview
[*line_selected
].sort
== 1)
923 fileview
[*line_selected
].reverse
= 1;
924 for (i
= 0; i
< size
; i
++)
925 fileview
[i
].sort
= 0;
926 fileview
[*line_selected
].sort
= 1;
927 update_current_view();
930 for (i
= 0; i
< size
; i
++) {
931 if (i
== *line_selected
) {
932 wattron(pref_panel_window
, COLOR_PAIR(5));
933 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
935 if (fileview
[i
].sort
== 1)
936 wattron(pref_panel_window
, A_BOLD
);
937 mvwprintw(pref_panel_window
, i
+ 1, 1, "[x] %s",
939 wattroff(pref_panel_window
, A_BOLD
);
940 wattroff(pref_panel_window
, COLOR_PAIR(5));
947 void update_iostream_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
954 if (pref_panel_window
) {
955 del_panel(pref_panel
);
956 delwin(pref_panel_window
);
960 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
961 pref_panel
= new_panel(pref_panel_window
);
963 werase(pref_panel_window
);
964 box(pref_panel_window
, 0 , 0);
965 set_window_title(pref_panel_window
, "IOTop Preferences ");
966 wattron(pref_panel_window
, A_BOLD
);
967 mvwprintw(pref_panel_window
, size
+ 1, 1,
969 wattroff(pref_panel_window
, A_BOLD
);
971 if (*line_selected
> (size
- 1))
972 *line_selected
= size
- 1;
973 if (toggle_sort
== 1) {
974 if (iostreamtopview
[*line_selected
].sort
== 1)
975 iostreamtopview
[*line_selected
].reverse
= 1;
976 for (i
= 0; i
< size
; i
++)
977 iostreamtopview
[i
].sort
= 0;
978 iostreamtopview
[*line_selected
].sort
= 1;
979 update_current_view();
982 for (i
= 0; i
< size
; i
++) {
983 if (i
== *line_selected
) {
984 wattron(pref_panel_window
, COLOR_PAIR(5));
985 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
987 if (iostreamtopview
[i
].sort
== 1)
988 wattron(pref_panel_window
, A_BOLD
);
989 mvwprintw(pref_panel_window
, i
+ 1, 1, "[x] %s",
990 iostreamtopview
[i
].title
);
991 wattroff(pref_panel_window
, A_BOLD
);
992 wattroff(pref_panel_window
, COLOR_PAIR(5));
999 void update_cpu_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1006 if (pref_panel_window
) {
1007 del_panel(pref_panel
);
1008 delwin(pref_panel_window
);
1012 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1013 pref_panel
= new_panel(pref_panel_window
);
1015 werase(pref_panel_window
);
1016 box(pref_panel_window
, 0 , 0);
1017 set_window_title(pref_panel_window
, "CPUTop Preferences ");
1018 wattron(pref_panel_window
, A_BOLD
);
1019 mvwprintw(pref_panel_window
, size
+ 1, 1,
1021 wattroff(pref_panel_window
, A_BOLD
);
1023 if (*line_selected
> (size
- 1))
1024 *line_selected
= size
- 1;
1025 if (toggle_sort
== 1) {
1026 /* special case, we don't support sorting by procname for now */
1027 if (*line_selected
!= 3) {
1028 if (cputopview
[*line_selected
].sort
== 1)
1029 cputopview
[*line_selected
].reverse
= 1;
1030 for (i
= 0; i
< size
; i
++)
1031 cputopview
[i
].sort
= 0;
1032 cputopview
[*line_selected
].sort
= 1;
1033 update_current_view();
1037 for (i
= 0; i
< size
; i
++) {
1038 if (i
== *line_selected
) {
1039 wattron(pref_panel_window
, COLOR_PAIR(5));
1040 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1042 if (cputopview
[i
].sort
== 1)
1043 wattron(pref_panel_window
, A_BOLD
);
1044 mvwprintw(pref_panel_window
, i
+ 1, 1, "[x] %s",
1045 cputopview
[i
].title
);
1046 wattroff(pref_panel_window
, A_BOLD
);
1047 wattroff(pref_panel_window
, COLOR_PAIR(5));
1054 void update_perf_pref(int *line_selected
, int toggle_view
, int toggle_sort
)
1057 struct perfcounter
*perf
;
1063 if (pref_panel_window
) {
1064 del_panel(pref_panel
);
1065 delwin(pref_panel_window
);
1067 size
= g_hash_table_size(global_perf_liszt
);
1069 pref_panel_window
= create_window(size
+ 2, 30, 10, 10);
1070 pref_panel
= new_panel(pref_panel_window
);
1072 werase(pref_panel_window
);
1073 box(pref_panel_window
, 0 , 0);
1074 set_window_title(pref_panel_window
, "Perf Preferences ");
1075 wattron(pref_panel_window
, A_BOLD
);
1076 mvwprintw(pref_panel_window
, g_hash_table_size(global_perf_liszt
) + 1, 1,
1078 wattroff(pref_panel_window
, A_BOLD
);
1080 if (toggle_sort
== 1) {
1082 perflist
= g_list_first(g_hash_table_get_keys(global_perf_liszt
));
1084 perf
= g_hash_table_lookup(global_perf_liszt
, perflist
->data
);
1085 if (i
!= *line_selected
)
1090 perflist
= g_list_next(perflist
);
1092 update_current_view();
1096 perflist
= g_list_first(g_hash_table_get_keys(global_perf_liszt
));
1098 perf
= g_hash_table_lookup(global_perf_liszt
, perflist
->data
);
1099 if (i
== *line_selected
&& toggle_view
== 1) {
1100 perf
->visible
= perf
->visible
== 1 ? 0:1;
1101 update_current_view();
1103 if (i
== *line_selected
) {
1104 wattron(pref_panel_window
, COLOR_PAIR(5));
1105 mvwhline(pref_panel_window
, i
+ 1, 1, ' ', 30 - 2);
1107 if (perf
->sort
== 1)
1108 wattron(pref_panel_window
, A_BOLD
);
1109 mvwprintw(pref_panel_window
, i
+ 1, 1, "[%c] %s",
1110 perf
->visible
== 1 ? 'x' : ' ',
1111 (char *) perflist
->data
+ 5);
1112 wattroff(pref_panel_window
, A_BOLD
);
1113 wattroff(pref_panel_window
, COLOR_PAIR(5));
1115 perflist
= g_list_next(perflist
);
1121 int update_preference_panel(int *line_selected
, int toggle_view
, int toggle_sort
)
1125 switch(current_view
) {
1127 update_perf_pref(line_selected
, toggle_view
, toggle_sort
);
1130 update_cpu_pref(line_selected
, toggle_view
, toggle_sort
);
1133 update_iostream_pref(line_selected
, toggle_view
, toggle_sort
);
1135 case process_details
:
1136 update_process_detail_pref(line_selected
, toggle_view
, toggle_sort
);
1146 void toggle_pref_panel(void)
1150 if (pref_panel_visible
) {
1151 hide_panel(pref_panel
);
1152 pref_panel_visible
= 0;
1154 ret
= update_preference_panel(&pref_line_selected
, 0, 0);
1157 show_panel(pref_panel
);
1158 pref_panel_visible
= 1;
1164 void display(unsigned int index
)
1166 last_display_index
= index
;
1167 currently_displayed_index
= index
;
1168 data
= g_ptr_array_index(copies
, index
);
1171 max_elements
= data
->process_table
->len
;
1172 update_current_view();
1178 void pause_display()
1182 sem_wait(&pause_sem
);
1185 void resume_display()
1188 print_log("Resume");
1189 sem_post(&pause_sem
);
1192 void *handle_keyboard(void *p
)
1195 while((ch
= getch())) {
1197 /* Move the cursor and scroll */
1200 if (pref_panel_visible
) {
1201 pref_line_selected
++;
1202 update_preference_panel(&pref_line_selected
, 0, 0);
1204 if (selected_line
< (max_center_lines
- 1) &&
1205 selected_line
< max_elements
- 1) {
1208 } else if (selected_in_list
< (max_elements
- 1)
1209 && (list_offset
< (max_elements
- max_center_lines
))) {
1213 update_current_view();
1220 if (pref_panel_visible
) {
1221 if (pref_line_selected
> 0)
1222 pref_line_selected
--;
1223 update_preference_panel(&pref_line_selected
, 0, 0);
1225 if (selected_line
> 0) {
1228 } else if (selected_in_list
> 0 && list_offset
> 0) {
1232 update_current_view();
1238 /* Navigate the history with arrows */
1240 if (currently_displayed_index
> 0) {
1241 currently_displayed_index
--;
1242 print_log("Going back in time");
1244 print_log("Cannot rewind, last data is already displayed");
1246 data
= g_ptr_array_index(copies
, currently_displayed_index
);
1247 max_elements
= data
->process_table
->len
;
1249 /* we force to pause the display when moving in time */
1250 if (toggle_pause
< 0)
1253 update_current_view();
1257 if (currently_displayed_index
< last_display_index
) {
1258 currently_displayed_index
++;
1259 print_log("Going forward in time");
1260 data
= g_ptr_array_index(copies
, currently_displayed_index
);
1261 max_elements
= data
->process_table
->len
;
1262 update_current_view();
1265 print_log("Manually moving forward");
1267 /* we force to resume the refresh when moving forward */
1268 if (toggle_pause
> 0)
1274 if (pref_panel_visible
) {
1275 update_preference_panel(&pref_line_selected
, 1, 0);
1277 update_selected_processes();
1278 update_current_view();
1282 if (pref_panel_visible
)
1283 update_preference_panel(&pref_line_selected
, 0, 1);
1286 case 13: /* FIXME : KEY_ENTER ?? */
1287 if (current_view
!= process_details
) {
1288 previous_view
= current_view
;
1289 current_view
= process_details
;
1291 current_view
= previous_view
;
1292 previous_view
= process_details
;
1294 update_current_view();
1298 if (pref_panel_visible
)
1299 toggle_pref_panel();
1302 update_current_view();
1305 if (pref_panel_visible
)
1306 toggle_pref_panel();
1309 update_current_view();
1312 if (pref_panel_visible
)
1313 toggle_pref_panel();
1314 current_view
= perf
;
1316 update_current_view();
1319 if (pref_panel_visible
)
1320 toggle_pref_panel();
1321 current_view
= iostream
;
1323 update_current_view();
1330 toggle_threads
*= -1;
1331 update_current_view();
1334 if (toggle_pause
< 0) {
1341 toggle_pref_panel();
1345 update_current_view();
1353 void init_view_headers()
1355 cputopview
[0].title
= strdup("CPU(%)");
1356 cputopview
[0].sort
= 1;
1357 cputopview
[1].title
= strdup("TGID");
1358 cputopview
[2].title
= strdup("PID");
1359 cputopview
[3].title
= strdup("NAME");
1361 iostreamtopview
[0].title
= strdup("R (B/sec)");
1362 iostreamtopview
[1].title
= strdup("W (B/sec)");
1363 iostreamtopview
[2].title
= strdup("Total (B)");
1364 iostreamtopview
[2].sort
= 1;
1366 fileview
[0].title
= strdup("FD");
1367 fileview
[1].title
= strdup("READ");
1368 fileview
[1].sort
= 1;
1369 fileview
[2].title
= strdup("WRITE");
1374 selected_processes
= g_ptr_array_new();
1375 sem_init(&update_display_sem
, 0, 1);
1376 init_view_headers();
1379 header
= create_window(5, COLS
- 1, 0, 0);
1380 center
= create_window(LINES
- 5 - 7, COLS
- 1, 5, 0);
1381 status
= create_window(MAX_LOG_LINES
+ 2, COLS
- 1, LINES
- 7, 0);
1382 footer
= create_window(1, COLS
- 1, LINES
- 1, 0);
1384 print_log("Starting display");
1386 main_panel
= new_panel(center
);
1393 pthread_create(&keyboard_thread
, NULL
, handle_keyboard
, (void *)NULL
);