Allow the user to highlight processes
[lttngtop.git] / src / cursesdisplay.c
CommitLineData
1fc22eb4
JD
1/*
2 * Copyright (C) 2011 Julien Desfossez
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
71bd7ce1
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1fc22eb4
JD
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <signal.h>
21#include <string.h>
22#include <ncurses.h>
23#include <panel.h>
24#include <pthread.h>
25#include <semaphore.h>
26
27#include "cursesdisplay.h"
28#include "lttngtoptypes.h"
b093de8a 29#include "iostreamtop.h"
1fc22eb4
JD
30#include "common.h"
31
32#define DEFAULT_DELAY 15
33#define MAX_LINE_LENGTH 50
34#define MAX_LOG_LINES 4
35
36/* to prevent concurrent updates of the different windows */
37sem_t update_display_sem;
38
39char *termtype;
40WINDOW *footer, *header, *center, *status;
41WINDOW *perf_panel_window = NULL;
42PANEL *perf_panel, *main_panel;
43
44int perf_panel_visible = 0;
45int perf_line_selected = 0;
46
47int last_display_index, currently_displayed_index;
48
49struct processtop *selected_process = NULL;
1fc22eb4
JD
50int selected_ret;
51
52int selected_line = 0; /* select bar position */
53int selected_in_list = 0; /* selection relative to the whole list */
54int list_offset = 0; /* first index in the list to display (scroll) */
55int nb_log_lines = 0;
56char log_lines[MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES];
57
58int max_elements = 80;
59
60int toggle_threads = -1;
61int toggle_pause = -1;
1fc22eb4
JD
62
63int max_center_lines;
635dc837 64GPtrArray *selected_processes;
1fc22eb4
JD
65
66pthread_t keyboard_thread;
67
68void reset_ncurses()
69{
70 curs_set(1);
71 endwin();
72 exit(0);
73}
74
75static void handle_sigterm(int signal)
76{
77 reset_ncurses();
78}
79
80void init_screen()
81{
82 initscr();
83 noecho();
84 halfdelay(DEFAULT_DELAY);
85 nonl();
86 intrflush(stdscr, false);
87 keypad(stdscr, true);
88 curs_set(0);
89
90 if (has_colors()) {
91 start_color();
92 init_pair(1, COLOR_RED, COLOR_BLACK); /* - */
93 init_pair(2, COLOR_GREEN, COLOR_BLACK); /* + */
94 init_pair(3, COLOR_BLACK, COLOR_WHITE); /* keys */
95 init_pair(4, COLOR_WHITE, COLOR_GREEN); /* keys activated */
96 init_pair(5, COLOR_WHITE, COLOR_BLUE); /* select line */
635dc837 97 init_pair(6, COLOR_WHITE, COLOR_GREEN); /* selected process */
1fc22eb4
JD
98 }
99 termtype = getenv("TERM");
100 if (!strcmp(termtype, "xterm") || !strcmp(termtype, "xterm-color") ||
101 !strcmp(termtype, "vt220")) {
102 define_key("\033[H", KEY_HOME);
103 define_key("\033[F", KEY_END);
104 define_key("\033OP", KEY_F(1));
105 define_key("\033OQ", KEY_F(2));
106 define_key("\033OR", KEY_F(3));
107 define_key("\033OS", KEY_F(4));
108 define_key("\0330U", KEY_F(6));
109 define_key("\033[11~", KEY_F(1));
110 define_key("\033[12~", KEY_F(2));
111 define_key("\033[13~", KEY_F(3));
112 define_key("\033[14~", KEY_F(4));
113 define_key("\033[16~", KEY_F(6));
114 define_key("\033[17;2~", KEY_F(18));
115 }
116 signal(SIGTERM, handle_sigterm);
117 mousemask(BUTTON1_CLICKED, NULL);
118 refresh();
119}
120
121WINDOW *create_window(int height, int width, int startx, int starty)
122{
123 WINDOW *win;
124 win = newwin(height, width, startx, starty);
125 box(win, 0 , 0);
126 wrefresh(win);
127 return win;
128}
129
130WINDOW *create_window_no_border(int height, int width, int startx, int starty)
131{
132 WINDOW *win;
133 win = newwin(height, width, startx, starty);
134 wrefresh(win);
135 return win;
136}
137
138void print_digit(WINDOW *win, int digit)
139{
140 if (digit < 0) {
141 wattron(win, COLOR_PAIR(1));
142 wprintw(win, "%d", digit);
143 wattroff(win, COLOR_PAIR(1));
144 } else if (digit > 0) {
145 wattron(win, COLOR_PAIR(2));
146 wprintw(win, "+%d", digit);
147 wattroff(win, COLOR_PAIR(2));
148 } else {
149 wprintw(win, "0");
150 }
151}
152
153void print_digits(WINDOW *win, int first, int second)
154{
155 wprintw(win, "(");
156 print_digit(win, first);
157 wprintw(win, ", ");
158 print_digit(win, second);
159 wprintw(win, ")");
160}
161
162void print_headers(int line, char *desc, int value, int first, int second)
163{
164 wattron(header, A_BOLD);
165 mvwprintw(header, line, 4, "%s", desc);
166 wattroff(header, A_BOLD);
e05a35a6 167 mvwprintw(header, line, 16, "%d", value);
1fc22eb4
JD
168 wmove(header, line, 24);
169 print_digits(header, first, second);
170 wmove(header, line, 40);
171}
172
173void set_window_title(WINDOW *win, char *title)
174{
175 wattron(win, A_BOLD);
176 mvwprintw(win, 0, 1, title);
177 wattroff(win, A_BOLD);
178}
179
180void print_log(char *str)
181{
182 int i;
183 int current_line = 1;
184 int current_char = 1;
185 char *tmp, *tmp2;
186 /* rotate the line buffer */
187 if (nb_log_lines >= MAX_LOG_LINES) {
188 tmp = strndup(log_lines, MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES);
189 tmp2 = strchr(tmp, '\n');
190 memset(log_lines, '\0', strlen(log_lines));
191 strncat(log_lines, tmp2 + 1, strlen(tmp2) - 1);
192 log_lines[strlen(log_lines)] = '\n';
193 log_lines[strlen(log_lines)] = '\0';
194 free(tmp);
195 }
196 nb_log_lines++;
197
198 strncat(log_lines, str, MAX_LINE_LENGTH - 1);
199
200 if (nb_log_lines < MAX_LOG_LINES)
201 log_lines[strlen(log_lines)] = '\n';
202 log_lines[strlen(log_lines)] = '\0';
203
204 werase(status);
205 box(status, 0 , 0);
206 set_window_title(status, "Status");
207 for (i = 0; i < strlen(log_lines); i++) {
208 if (log_lines[i] == '\n') {
209 wmove(status, ++current_line, 1);
210 current_char = 1;
211 } else {
b093de8a
MB
212 mvwprintw(status, current_line, current_char++, "%c",
213 log_lines[i]);
1fc22eb4
JD
214 }
215 }
216 wrefresh(status);
217}
218
635dc837
JD
219int process_selected(struct processtop *process)
220{
221 int i;
222 struct processtop *stored_process;
223
224 for (i = 0; i < selected_processes->len; i++) {
225 stored_process = g_ptr_array_index(selected_processes, i);
226 if (stored_process->tid == process->tid)
227 return 1;
228 }
229 return 0;
230}
231
232void update_selected_processes()
233{
234 if (process_selected(selected_process)) {
235 g_ptr_array_remove(selected_processes, selected_process);
236 print_log("Process removed");
237 } else {
238 g_ptr_array_add(selected_processes, selected_process);
239 print_log("Process added");
240 }
241}
242
1fc22eb4
JD
243void print_key(WINDOW *win, char *key, char *desc, int toggle)
244{
245 int pair;
246 if (toggle > 0)
247 pair = 4;
248 else
249 pair = 3;
250 wattron(win, COLOR_PAIR(pair));
251 wprintw(footer, "%s", key);
252 wattroff(win, COLOR_PAIR(pair));
253 wprintw(footer, ":%s", desc);
254}
255
256void update_footer()
257{
258 sem_wait(&update_display_sem);
259 werase(footer);
260 wmove(footer, 1, 1);
261 print_key(footer, "F2", "CPUtop ", current_view == cpu);
262 print_key(footer, "F3", "PerfTop ", current_view == perf);
e7be7d41 263 print_key(footer, "F4", "IOTop ", current_view == iostream);
1fc22eb4 264 print_key(footer, "Enter", "Details ", current_view == process_details);
635dc837 265 print_key(footer, "Space", "Highlight ", 0);
1fc22eb4
JD
266 print_key(footer, "q", "Quit | ", 0);
267 print_key(footer, "P", "Perf Pref ", 0);
268 print_key(footer, "p", "Pause ", toggle_pause);
269
270 wrefresh(footer);
271 sem_post(&update_display_sem);
272}
273
274void basic_header()
275{
276 werase(header);
277 box(header, 0 , 0);
278 set_window_title(header, "Statistics for interval [gathering data...[");
279 wattron(header, A_BOLD);
280 mvwprintw(header, 1, 4, "CPUs");
e05a35a6 281 mvwprintw(header, 2, 4, "Threads");
dc8f04dd 282 mvwprintw(header, 3, 4, "FDs");
1fc22eb4
JD
283 wattroff(header, A_BOLD);
284 wrefresh(header);
285}
286
91be6bb5
JD
287struct tm format_timestamp(uint64_t timestamp)
288{
289 struct tm tm;
290 uint64_t ts_sec = 0, ts_nsec;
291 time_t time_s;
292
293 ts_nsec = timestamp;
294 ts_sec += ts_nsec / NSEC_PER_SEC;
295 ts_nsec = ts_nsec % NSEC_PER_SEC;
296
297 time_s = (time_t) ts_sec;
298
299 localtime_r(&time_s, &tm);
300
301 return tm;
302}
303
1fc22eb4
JD
304void update_header()
305{
91be6bb5
JD
306 struct tm start, end;
307 uint64_t ts_nsec_start, ts_nsec_end;
308
309 ts_nsec_start = data->start % NSEC_PER_SEC;
310 start = format_timestamp(data->start);
311
312 ts_nsec_end = data->end % NSEC_PER_SEC;
313 end = format_timestamp(data->end);
314
1fc22eb4
JD
315 werase(header);
316 box(header, 0 , 0);
317 set_window_title(header, "Statistics for interval ");
318 wattron(header, A_BOLD);
91be6bb5
JD
319
320 wprintw(header, "[%02d:%02d:%02d.%09" PRIu64 ", %02d:%02d:%02d.%09" PRIu64 "[",
321 start.tm_hour, start.tm_min, start.tm_sec, ts_nsec_start,
322 end.tm_hour, end.tm_min, end.tm_sec, ts_nsec_end);
1fc22eb4
JD
323 mvwprintw(header, 1, 4, "CPUs");
324 wattroff(header, A_BOLD);
325 wprintw(header, "\t%d\t(max/cpu : %0.2f%)", data->cpu_table->len,
326 100.0/data->cpu_table->len);
e05a35a6 327 print_headers(2, "Threads", data->nbthreads, data->nbnewthreads,
1fc22eb4 328 -1*(data->nbdeadthreads));
dc8f04dd 329 print_headers(3, "FDs", data->nbfiles, data->nbnewfiles,
1fc22eb4 330 -1*(data->nbclosedfiles));
e05a35a6 331 mvwprintw(header, 3, 43, "N/A kbytes/sec");
1fc22eb4
JD
332 wrefresh(header);
333}
334
335gint sort_by_cpu_desc(gconstpointer p1, gconstpointer p2)
336{
337 struct processtop *n1 = *(struct processtop **)p1;
338 struct processtop *n2 = *(struct processtop **)p2;
339 unsigned long totaln1 = n1->totalcpunsec;
340 unsigned long totaln2 = n2->totalcpunsec;
341
342 if (totaln1 < totaln2)
343 return 1;
344 if (totaln1 == totaln2)
345 return 0;
346 return -1;
347}
348
349gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2)
350{
351 struct processtop *n1 = *(struct processtop **)p1;
352 struct processtop *n2 = *(struct processtop **)p2;
353 unsigned long totaln1 = n1->threadstotalcpunsec;
354 unsigned long totaln2 = n2->threadstotalcpunsec;
355
356 if (totaln1 < totaln2)
357 return 1;
358 if (totaln1 == totaln2)
359 return 0;
360 return -1;
361}
362
363void update_cputop_display()
364{
365 int i;
366 int header_offset = 2;
367 struct processtop *tmp;
368 unsigned long elapsed;
369 double maxcputime;
370 int nblinedisplayed = 0;
371 int current_line = 0;
372
373 elapsed = data->end - data->start;
374 maxcputime = elapsed * data->cpu_table->len / 100.0;
375
376 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
377
378 set_window_title(center, "CPU Top");
379 wattron(center, A_BOLD);
380 mvwprintw(center, 1, 1, "CPU(%)");
381 mvwprintw(center, 1, 12, "TGID");
382 mvwprintw(center, 1, 22, "PID");
383 mvwprintw(center, 1, 32, "NAME");
384 wattroff(center, A_BOLD);
385
dc8f04dd 386 max_center_lines = LINES - 5 - 7 - 1 - header_offset;
1fc22eb4
JD
387
388 /* iterate the process (thread) list */
389 for (i = list_offset; i < data->process_table->len &&
390 nblinedisplayed < max_center_lines; i++) {
391 tmp = g_ptr_array_index(data->process_table, i);
392
635dc837
JD
393 if (process_selected(tmp)) {
394 wattron(center, COLOR_PAIR(6));
395 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
396 }
1fc22eb4
JD
397 if (current_line == selected_line) {
398 selected_process = tmp;
1fc22eb4
JD
399 wattron(center, COLOR_PAIR(5));
400 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
401 }
402 /* CPU(%) */
403 mvwprintw(center, current_line + header_offset, 1, "%1.2f",
404 tmp->totalcpunsec / maxcputime);
405 /* TGID */
406 mvwprintw(center, current_line + header_offset, 12, "%d", tmp->pid);
407 /* PID */
408 mvwprintw(center, current_line + header_offset, 22, "%d", tmp->tid);
409 /* NAME */
410 mvwprintw(center, current_line + header_offset, 32, "%s", tmp->comm);
635dc837 411 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
412 wattroff(center, COLOR_PAIR(5));
413 nblinedisplayed++;
414 current_line++;
415 }
416}
417
418gint sort_perf(gconstpointer p1, gconstpointer p2, gpointer key)
419{
420 struct processtop *n1 = *(struct processtop **) p1;
421 struct processtop *n2 = *(struct processtop **) p2;
422
423 struct perfcounter *tmp1, *tmp2;
424 unsigned long totaln2 = 0;
425 unsigned long totaln1 = 0;
426
427 if (!key)
428 return 0;
429
430 tmp1 = g_hash_table_lookup(n1->perf, key);
431 if (!tmp1)
432 totaln1 = 0;
433 else
434 totaln1 = tmp1->count;
435
436 tmp2 = g_hash_table_lookup(n2->perf, key);
437 if (!tmp2)
438 totaln2 = 0;
439 else
440 totaln2 = tmp2->count;
441
442 if (totaln1 < totaln2)
443 return 1;
444 if (totaln1 == totaln2) {
445 totaln1 = n1->tid;
446 totaln2 = n2->tid;
447 if (totaln1 < totaln2)
448 return 1;
449 return -1;
450 }
451 return -1;
452}
453
454void print_key_title(char *key, int line)
455{
456 wattron(center, A_BOLD);
457 mvwprintw(center, line, 1, "%s\t", key);
458 wattroff(center, A_BOLD);
459}
460
461void update_process_details()
462{
463 unsigned long elapsed;
464 double maxcputime;
635dc837
JD
465 struct processtop *tmp = find_process_tid(data,
466 selected_process->tid,
467 selected_process->comm);
b093de8a
MB
468 struct files *file_tmp;
469 int i, j = 0;
1fc22eb4
JD
470
471 set_window_title(center, "Process details");
472
473
474 elapsed = data->end - data->start;
475 maxcputime = elapsed * data->cpu_table->len / 100.0;
476
477 print_key_title("Name", 1);
635dc837 478 wprintw(center, "%s", tmp->comm);
1fc22eb4 479 print_key_title("TID", 2);
635dc837 480 wprintw(center, "%d", tmp->tid);
1fc22eb4
JD
481 if (!tmp) {
482 print_key_title("Does not exit at this time", 3);
483 return;
484 }
485
486 print_key_title("PID", 3);
487 wprintw(center, "%d", tmp->pid);
488 print_key_title("PPID", 4);
489 wprintw(center, "%d", tmp->ppid);
490 print_key_title("CPU", 5);
491 wprintw(center, "%1.2f %%", tmp->totalcpunsec/maxcputime);
b093de8a
MB
492
493 print_key_title("READ B/s", 6);
494 wprintw(center, "%d", tmp->fileread);
495
496 print_key_title("WRITE B/s", 7);
497 wprintw(center, "%d", tmp->filewrite);
498
93d80d35
JD
499 wattron(center, A_BOLD);
500 mvwprintw(center, 8, 1, "FD");
501 mvwprintw(center, 8, 12, "READ");
502 mvwprintw(center, 8, 22, "WRITE");
503 mvwprintw(center, 8, 32, "FILENAME");
504 wattroff(center, A_BOLD);
505
b093de8a
MB
506 for (i = 0; i < tmp->process_files_table->len; i++) {
507 file_tmp = get_file(tmp, i);
508 if (file_tmp != NULL) {
93d80d35
JD
509 mvwprintw(center, 9 + j, 1, "%d", i);
510 mvwprintw(center, 9 + j, 12, "%d", file_tmp->read);
511 mvwprintw(center, 9 + j, 22, "%d", file_tmp->write);
512 mvwprintw(center, 9 + j, 32, "%s", file_tmp->name);
b093de8a
MB
513 j++;
514 }
515 }
1fc22eb4
JD
516}
517
518void update_perf()
519{
bb053abb 520 int i;
1fc22eb4
JD
521 int nblinedisplayed = 0;
522 int current_line = 0;
523 struct processtop *tmp;
524 int header_offset = 2;
525 int perf_row = 40;
526 struct perfcounter *perfn1, *perfn2;
1fc22eb4
JD
527 char *perf_key = NULL;
528 int value;
85db4618
JD
529 GHashTableIter iter;
530 gpointer key;
1fc22eb4
JD
531
532 set_window_title(center, "Perf Top");
533 wattron(center, A_BOLD);
534 mvwprintw(center, 1, 1, "PID");
535 mvwprintw(center, 1, 11, "TID");
536 mvwprintw(center, 1, 22, "NAME");
537
538 perf_row = 40;
85db4618
JD
539 g_hash_table_iter_init(&iter, data->perf_list);
540 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
1fc22eb4 541 if (perfn1->visible) {
6419078d 542 /* + 5 to strip the "perf_" prefix */
1fc22eb4 543 mvwprintw(center, 1, perf_row, "%s",
6419078d 544 (char *) key + 5);
1fc22eb4
JD
545 perf_row += 20;
546 }
547 if (perfn1->sort) {
85db4618 548 perf_key = (char *) key;
1fc22eb4 549 }
1fc22eb4 550 }
85db4618 551
1fc22eb4
JD
552 wattroff(center, A_BOLD);
553
554 g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key);
85db4618 555
91be6bb5 556 for (i = 0; i < data->process_table->len &&
1fc22eb4 557 nblinedisplayed < max_center_lines; i++) {
1fc22eb4
JD
558 tmp = g_ptr_array_index(data->process_table, i);
559
635dc837
JD
560 if (process_selected(tmp)) {
561 wattron(center, COLOR_PAIR(6));
562 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
563 }
1fc22eb4
JD
564 if (current_line == selected_line) {
565 selected_process = tmp;
566 wattron(center, COLOR_PAIR(5));
567 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
568 }
569
570 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
571 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
572 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
573
85db4618 574 g_hash_table_iter_init(&iter, data->perf_list);
1fc22eb4
JD
575
576 perf_row = 40;
85db4618 577 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
1fc22eb4 578 if (perfn1->visible) {
85db4618 579 perfn2 = g_hash_table_lookup(tmp->perf, (char *) key);
1fc22eb4
JD
580 if (perfn2)
581 value = perfn2->count;
582 else
583 value = 0;
b093de8a
MB
584 mvwprintw(center, current_line + header_offset,
585 perf_row, "%d", value);
1fc22eb4
JD
586 perf_row += 20;
587 }
1fc22eb4
JD
588 }
589
635dc837 590 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
591 wattroff(center, COLOR_PAIR(5));
592 nblinedisplayed++;
593 current_line++;
594 }
595}
596
1fc22eb4
JD
597gint sort_by_ret_desc(gconstpointer p1, gconstpointer p2)
598{
599 struct processtop *n1 = *(struct processtop **)p1;
600 struct processtop *n2 = *(struct processtop **)p2;
b093de8a
MB
601
602 unsigned long totaln1 = n1->totalfileread + n1->totalfilewrite;
603 unsigned long totaln2 = n2->totalfileread + n2->totalfilewrite;
1fc22eb4
JD
604
605 if (totaln1 < totaln2)
606 return 1;
607 if (totaln1 == totaln2)
608 return 0;
609 return -1;
610}
611
612void update_iostream()
613{
614 int i;
615 int header_offset = 2;
616 struct processtop *tmp;
617 int nblinedisplayed = 0;
618 int current_line = 0;
b093de8a 619 int total = 0;
1fc22eb4
JD
620
621 set_window_title(center, "IO Top");
622 wattron(center, A_BOLD);
623 mvwprintw(center, 1, 1, "READ (B/s)");
624 mvwprintw(center, 1, 20, "WRITE (B/s)");
625
626 mvwprintw(center, 1, 40, "TOTAL STREAM");
627
628 mvwprintw(center, 1, 60, "TGID");
629 mvwprintw(center, 1, 80, "PID");
630
631 mvwprintw(center, 1, 92, "NAME");
632 wattroff(center, A_BOLD);
633
634 g_ptr_array_sort(data->process_table, sort_by_ret_desc);
635
636 for (i = list_offset; i < data->process_table->len &&
637 nblinedisplayed < max_center_lines; i++) {
638 tmp = g_ptr_array_index(data->process_table, i);
639
635dc837
JD
640 if (process_selected(tmp)) {
641 wattron(center, COLOR_PAIR(6));
642 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
643 }
1fc22eb4
JD
644 if (current_line == selected_line) {
645 selected_process = tmp;
1fc22eb4
JD
646 wattron(center, COLOR_PAIR(5));
647 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
648 }
b093de8a 649
1fc22eb4
JD
650 /* READ (bytes/sec) */
651 mvwprintw(center, current_line + header_offset, 1, "%lu",
b093de8a 652 tmp->fileread);
1fc22eb4
JD
653
654 /* WRITE (bytes/sec) */
655 mvwprintw(center, current_line + header_offset, 20, "%lu",
b093de8a 656 tmp->filewrite);
1fc22eb4
JD
657
658 /* TOTAL STREAM */
b093de8a
MB
659 total = tmp->totalfileread + tmp->totalfilewrite;
660
661 if (total >= 1000000)
1fc22eb4 662 mvwprintw(center, current_line + header_offset, 40, "%lu MB",
b093de8a
MB
663 total/1000000);
664 else if (total >= 1000)
1fc22eb4 665 mvwprintw(center, current_line + header_offset, 40, "%lu KB",
b093de8a 666 total/1000);
1fc22eb4
JD
667 else
668 mvwprintw(center, current_line + header_offset, 40, "%lu B",
b093de8a
MB
669 total);
670
1fc22eb4
JD
671 /* TGID */
672 mvwprintw(center, current_line + header_offset, 60, "%d", tmp->pid);
673 /* PID */
674 mvwprintw(center, current_line + header_offset, 80, "%d", tmp->tid);
675 /* NAME */
676 mvwprintw(center, current_line + header_offset, 92, "%s", tmp->comm);
635dc837 677 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
678 wattroff(center, COLOR_PAIR(5));
679 nblinedisplayed++;
680 current_line++;
681 }
682}
683
684void update_current_view()
685{
686 sem_wait(&update_display_sem);
687 if (!data)
688 return;
689 update_header();
690
691 werase(center);
692 box(center, 0, 0);
693 switch (current_view) {
694 case cpu:
695 update_cputop_display();
696 break;
697 case perf:
698 update_perf();
699 break;
700 case process_details:
701 update_process_details();
702 break;
1fc22eb4
JD
703 case iostream:
704 update_iostream();
705 break;
706 case tree:
707 update_cputop_display();
708 break;
709 default:
710 break;
711 }
712 update_panels();
713 doupdate();
714 sem_post(&update_display_sem);
715}
716
717void setup_perf_panel()
718{
719 int size;
720 if (!data)
721 return;
722 if (perf_panel_window) {
723 del_panel(perf_panel);
724 delwin(perf_panel_window);
725 }
726 size = g_hash_table_size(data->perf_list);
727 perf_panel_window = create_window(size + 2, 30, 10, 10);
728 perf_panel = new_panel(perf_panel_window);
729 perf_panel_visible = 0;
730 hide_panel(perf_panel);
731}
732
733void update_perf_panel(int line_selected, int toggle_view, int toggle_sort)
734{
735 int i;
736 struct perfcounter *perf;
737 GList *perflist;
738
739 if (!data)
740 return;
741
742 werase(perf_panel_window);
743 box(perf_panel_window, 0 , 0);
744 set_window_title(perf_panel_window, "Perf Preferences ");
745 wattron(perf_panel_window, A_BOLD);
b093de8a
MB
746 mvwprintw(perf_panel_window, g_hash_table_size(data->perf_list) + 1, 1,
747 " 's' to sort");
1fc22eb4
JD
748 wattroff(perf_panel_window, A_BOLD);
749
750 if (toggle_sort == 1) {
751 i = 0;
752 perflist = g_list_first(g_hash_table_get_keys(data->perf_list));
753 while (perflist) {
754 perf = g_hash_table_lookup(data->perf_list, perflist->data);
755 if (i != line_selected)
756 perf->sort = 0;
757 else
758 perf->sort = 1;
759 i++;
760 perflist = g_list_next(perflist);
761 }
762 update_current_view();
763 }
764
765 i = 0;
766 perflist = g_list_first(g_hash_table_get_keys(data->perf_list));
767 while (perflist) {
768 perf = g_hash_table_lookup(data->perf_list, perflist->data);
769 if (i == line_selected && toggle_view == 1) {
770 perf->visible = perf->visible == 1 ? 0:1;
771 update_current_view();
772 }
773 if (i == line_selected) {
774 wattron(perf_panel_window, COLOR_PAIR(5));
775 mvwhline(perf_panel_window, i + 1, 1, ' ', 30 - 2);
776 }
777 if (perf->sort == 1)
778 wattron(perf_panel_window, A_BOLD);
779 mvwprintw(perf_panel_window, i + 1, 1, "[%c] %s",
780 perf->visible == 1 ? 'x' : ' ',
b8a1df45 781 (char *) perflist->data + 5);
1fc22eb4
JD
782 wattroff(perf_panel_window, A_BOLD);
783 wattroff(perf_panel_window, COLOR_PAIR(5));
784 i++;
785 perflist = g_list_next(perflist);
786 }
787 update_panels();
788 doupdate();
789}
790
1fc22eb4
JD
791void toggle_perf_panel(void)
792{
793 if (perf_panel_visible) {
794 hide_panel(perf_panel);
795 perf_panel_visible = 0;
796 } else {
797 setup_perf_panel();
798 update_perf_panel(perf_line_selected, 0, 0);
799 show_panel(perf_panel);
800 perf_panel_visible = 1;
801 }
802 update_panels();
803 doupdate();
804}
805
806void display(unsigned int index)
807{
808 last_display_index = index;
809 currently_displayed_index = index;
810 data = g_ptr_array_index(copies, index);
811 if (!data)
812 return;
813 max_elements = data->process_table->len;
814 update_current_view();
815 update_footer();
816 update_panels();
817 doupdate();
818}
819
820void pause_display()
821{
822 toggle_pause = 1;
823 print_log("Pause");
824 sem_wait(&pause_sem);
825}
826
827void resume_display()
828{
829 toggle_pause = -1;
830 print_log("Resume");
831 sem_post(&pause_sem);
832}
833
834void *handle_keyboard(void *p)
835{
836 int ch;
837 while((ch = getch())) {
838 switch(ch) {
839 /* Move the cursor and scroll */
840 case KEY_DOWN:
841 if (perf_panel_visible) {
842 if (perf_line_selected < g_hash_table_size(data->perf_list) - 1)
843 perf_line_selected++;
844 update_perf_panel(perf_line_selected, 0, 0);
845 } else {
846 if (selected_line < (max_center_lines - 1) &&
847 selected_line < max_elements - 1) {
848 selected_line++;
849 selected_in_list++;
850 } else if (selected_in_list < (max_elements - 1)
851 && (list_offset < (max_elements - max_center_lines))) {
852 selected_in_list++;
853 list_offset++;
854 }
855 update_current_view();
856 }
857 break;
858 case KEY_NPAGE:
1fc22eb4
JD
859 break;
860 case KEY_UP:
861 if (perf_panel_visible) {
862 if (perf_line_selected > 0)
863 perf_line_selected--;
864 update_perf_panel(perf_line_selected, 0, 0);
865 } else {
866 if (selected_line > 0) {
867 selected_line--;
868 selected_in_list--;
869 } else if (selected_in_list > 0 && list_offset > 0) {
870 selected_in_list--;
871 list_offset--;
872 }
873 update_current_view();
874 }
875 break;
876 case KEY_PPAGE:
1fc22eb4
JD
877 break;
878
879 /* Navigate the history with arrows */
880 case KEY_LEFT:
881 if (currently_displayed_index > 0) {
882 currently_displayed_index--;
883 print_log("Going back in time");
884 } else {
885 print_log("Cannot rewind, last data is already displayed");
886 }
887 data = g_ptr_array_index(copies, currently_displayed_index);
888 max_elements = data->process_table->len;
889
890 /* we force to pause the display when moving in time */
891 if (toggle_pause < 0)
892 pause_display();
893
894 update_current_view();
895 update_footer();
896 break;
897 case KEY_RIGHT:
898 if (currently_displayed_index < last_display_index) {
899 currently_displayed_index++;
900 print_log("Going forward in time");
901 data = g_ptr_array_index(copies, currently_displayed_index);
902 max_elements = data->process_table->len;
903 update_current_view();
904 update_footer();
905 } else {
906 print_log("Manually moving forward");
907 sem_post(&timer);
908 /* we force to resume the refresh when moving forward */
909 if (toggle_pause > 0)
910 resume_display();
911 }
912
913 break;
914 case ' ':
635dc837 915 if (perf_panel_visible) {
1fc22eb4 916 update_perf_panel(perf_line_selected, 1, 0);
635dc837
JD
917 } else {
918 update_selected_processes();
919 update_current_view();
920 }
1fc22eb4
JD
921 break;
922 case 's':
923 if (perf_panel_visible)
924 update_perf_panel(perf_line_selected, 0, 1);
925 break;
926
927 case 13: /* FIXME : KEY_ENTER ?? */
041aa219
JD
928 if (current_view != process_details) {
929 previous_view = current_view;
1fc22eb4 930 current_view = process_details;
041aa219
JD
931 } else {
932 current_view = previous_view;
933 previous_view = process_details;
1fc22eb4
JD
934 }
935 update_current_view();
936 break;
937
938 case KEY_F(1):
1fc22eb4 939 current_view = cpu;
af54ebcf 940 selected_line = 0;
1fc22eb4
JD
941 update_current_view();
942 break;
943 case KEY_F(2):
944 current_view = cpu;
af54ebcf 945 selected_line = 0;
1fc22eb4
JD
946 update_current_view();
947 break;
948 case KEY_F(3):
949 current_view = perf;
af54ebcf 950 selected_line = 0;
1fc22eb4
JD
951 update_current_view();
952 break;
953 case KEY_F(4):
1fc22eb4 954 current_view = iostream;
af54ebcf 955 selected_line = 0;
1fc22eb4
JD
956 update_current_view();
957 break;
958 case KEY_F(10):
959 case 'q':
960 reset_ncurses();
961 break;
962 case 't':
963 toggle_threads *= -1;
964 update_current_view();
965 break;
966 case 'p':
967 if (toggle_pause < 0) {
968 pause_display();
969 } else {
970 resume_display();
971 }
972 break;
973 case 'P':
974 toggle_perf_panel();
975 break;
976 default:
b093de8a
MB
977 if (data)
978 update_current_view();
1fc22eb4
JD
979 break;
980 }
981 update_footer();
982 }
983 return NULL;
984}
985
986void init_ncurses()
987{
635dc837 988 selected_processes = g_ptr_array_new();
1fc22eb4
JD
989 sem_init(&update_display_sem, 0, 1);
990 init_screen();
991
dc8f04dd
JD
992 header = create_window(5, COLS - 1, 0, 0);
993 center = create_window(LINES - 5 - 7, COLS - 1, 5, 0);
1fc22eb4
JD
994 status = create_window(MAX_LOG_LINES + 2, COLS - 1, LINES - 7, 0);
995 footer = create_window(1, COLS - 1, LINES - 1, 0);
996
997 print_log("Starting display");
998
999 main_panel = new_panel(center);
1000 setup_perf_panel();
1001
1002 current_view = cpu;
1003
1004 basic_header();
1005 update_footer();
1006
1007 pthread_create(&keyboard_thread, NULL, handle_keyboard, (void *)NULL);
1008}
1009
This page took 0.106369 seconds and 4 git commands to generate.