Pref panel introduction + perf list rewrite
[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;
0d91c12a
JD
41WINDOW *pref_panel_window = NULL;
42PANEL *pref_panel, *main_panel;
1fc22eb4 43
0d91c12a 44int pref_panel_visible = 0;
1fc22eb4
JD
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
69362330
JD
304static void scale_unit(uint64_t bytes, char *ret)
305{
306 if (bytes >= 1000000000)
307 sprintf(ret, "%" PRIu64 "G", bytes/1000000000);
308 if (bytes >= 1000000)
309 sprintf(ret, "%" PRIu64 "M", bytes/1000000);
310 else if (bytes >= 1000)
311 sprintf(ret, "%" PRIu64 "K", bytes/1000);
312 else
313 sprintf(ret, "%" PRIu64, bytes);
314}
315uint64_t total_io()
316{
317 int i;
318 struct processtop *tmp;
319 uint64_t total = 0;
320
321 for (i = 0; i < data->process_table->len; i++) {
322 tmp = g_ptr_array_index(data->process_table, i);
323 total += tmp->fileread;
324 total += tmp->filewrite;
325 }
326
327 return total;
328}
329
1fc22eb4
JD
330void update_header()
331{
91be6bb5
JD
332 struct tm start, end;
333 uint64_t ts_nsec_start, ts_nsec_end;
69362330 334 char io[4];
91be6bb5
JD
335
336 ts_nsec_start = data->start % NSEC_PER_SEC;
337 start = format_timestamp(data->start);
338
339 ts_nsec_end = data->end % NSEC_PER_SEC;
340 end = format_timestamp(data->end);
341
1fc22eb4
JD
342 werase(header);
343 box(header, 0 , 0);
344 set_window_title(header, "Statistics for interval ");
345 wattron(header, A_BOLD);
91be6bb5
JD
346
347 wprintw(header, "[%02d:%02d:%02d.%09" PRIu64 ", %02d:%02d:%02d.%09" PRIu64 "[",
348 start.tm_hour, start.tm_min, start.tm_sec, ts_nsec_start,
349 end.tm_hour, end.tm_min, end.tm_sec, ts_nsec_end);
1fc22eb4
JD
350 mvwprintw(header, 1, 4, "CPUs");
351 wattroff(header, A_BOLD);
352 wprintw(header, "\t%d\t(max/cpu : %0.2f%)", data->cpu_table->len,
353 100.0/data->cpu_table->len);
e05a35a6 354 print_headers(2, "Threads", data->nbthreads, data->nbnewthreads,
1fc22eb4 355 -1*(data->nbdeadthreads));
dc8f04dd 356 print_headers(3, "FDs", data->nbfiles, data->nbnewfiles,
1fc22eb4 357 -1*(data->nbclosedfiles));
69362330
JD
358 scale_unit(total_io(), io);
359 mvwprintw(header, 3, 43, "%sB/sec", io);
1fc22eb4
JD
360 wrefresh(header);
361}
362
363gint sort_by_cpu_desc(gconstpointer p1, gconstpointer p2)
364{
365 struct processtop *n1 = *(struct processtop **)p1;
366 struct processtop *n2 = *(struct processtop **)p2;
367 unsigned long totaln1 = n1->totalcpunsec;
368 unsigned long totaln2 = n2->totalcpunsec;
369
370 if (totaln1 < totaln2)
371 return 1;
372 if (totaln1 == totaln2)
373 return 0;
374 return -1;
375}
376
377gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2)
378{
379 struct processtop *n1 = *(struct processtop **)p1;
380 struct processtop *n2 = *(struct processtop **)p2;
381 unsigned long totaln1 = n1->threadstotalcpunsec;
382 unsigned long totaln2 = n2->threadstotalcpunsec;
383
384 if (totaln1 < totaln2)
385 return 1;
386 if (totaln1 == totaln2)
387 return 0;
388 return -1;
389}
390
391void update_cputop_display()
392{
393 int i;
394 int header_offset = 2;
395 struct processtop *tmp;
396 unsigned long elapsed;
397 double maxcputime;
398 int nblinedisplayed = 0;
399 int current_line = 0;
400
401 elapsed = data->end - data->start;
402 maxcputime = elapsed * data->cpu_table->len / 100.0;
403
404 g_ptr_array_sort(data->process_table, sort_by_cpu_desc);
405
406 set_window_title(center, "CPU Top");
407 wattron(center, A_BOLD);
408 mvwprintw(center, 1, 1, "CPU(%)");
409 mvwprintw(center, 1, 12, "TGID");
410 mvwprintw(center, 1, 22, "PID");
411 mvwprintw(center, 1, 32, "NAME");
412 wattroff(center, A_BOLD);
413
dc8f04dd 414 max_center_lines = LINES - 5 - 7 - 1 - header_offset;
1fc22eb4
JD
415
416 /* iterate the process (thread) list */
417 for (i = list_offset; i < data->process_table->len &&
418 nblinedisplayed < max_center_lines; i++) {
419 tmp = g_ptr_array_index(data->process_table, i);
420
97514683 421 /* FIXME : random segfault here */
635dc837
JD
422 if (process_selected(tmp)) {
423 wattron(center, COLOR_PAIR(6));
424 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
425 }
1fc22eb4
JD
426 if (current_line == selected_line) {
427 selected_process = tmp;
1fc22eb4
JD
428 wattron(center, COLOR_PAIR(5));
429 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
430 }
431 /* CPU(%) */
432 mvwprintw(center, current_line + header_offset, 1, "%1.2f",
433 tmp->totalcpunsec / maxcputime);
434 /* TGID */
435 mvwprintw(center, current_line + header_offset, 12, "%d", tmp->pid);
436 /* PID */
437 mvwprintw(center, current_line + header_offset, 22, "%d", tmp->tid);
438 /* NAME */
439 mvwprintw(center, current_line + header_offset, 32, "%s", tmp->comm);
635dc837 440 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
441 wattroff(center, COLOR_PAIR(5));
442 nblinedisplayed++;
443 current_line++;
444 }
445}
446
447gint sort_perf(gconstpointer p1, gconstpointer p2, gpointer key)
448{
449 struct processtop *n1 = *(struct processtop **) p1;
450 struct processtop *n2 = *(struct processtop **) p2;
451
452 struct perfcounter *tmp1, *tmp2;
453 unsigned long totaln2 = 0;
454 unsigned long totaln1 = 0;
455
456 if (!key)
457 return 0;
458
459 tmp1 = g_hash_table_lookup(n1->perf, key);
460 if (!tmp1)
461 totaln1 = 0;
462 else
463 totaln1 = tmp1->count;
464
465 tmp2 = g_hash_table_lookup(n2->perf, key);
466 if (!tmp2)
467 totaln2 = 0;
468 else
469 totaln2 = tmp2->count;
470
471 if (totaln1 < totaln2)
472 return 1;
473 if (totaln1 == totaln2) {
474 totaln1 = n1->tid;
475 totaln2 = n2->tid;
476 if (totaln1 < totaln2)
477 return 1;
478 return -1;
479 }
480 return -1;
481}
482
483void print_key_title(char *key, int line)
484{
485 wattron(center, A_BOLD);
486 mvwprintw(center, line, 1, "%s\t", key);
487 wattroff(center, A_BOLD);
488}
489
490void update_process_details()
491{
492 unsigned long elapsed;
493 double maxcputime;
d26643ed 494 struct processtop *tmp;
b093de8a
MB
495 struct files *file_tmp;
496 int i, j = 0;
69362330 497 char unit[4];
97514683 498 char filename_buf[COLS];
1fc22eb4
JD
499
500 set_window_title(center, "Process details");
501
502
d26643ed
JD
503 tmp = find_process_tid(data,
504 selected_process->tid,
505 selected_process->comm);
1fc22eb4
JD
506 elapsed = data->end - data->start;
507 maxcputime = elapsed * data->cpu_table->len / 100.0;
508
509 print_key_title("Name", 1);
d26643ed 510 wprintw(center, "%s", selected_process->comm);
1fc22eb4 511 print_key_title("TID", 2);
d26643ed 512 wprintw(center, "%d", selected_process->tid);
1fc22eb4
JD
513 if (!tmp) {
514 print_key_title("Does not exit at this time", 3);
515 return;
516 }
517
518 print_key_title("PID", 3);
519 wprintw(center, "%d", tmp->pid);
520 print_key_title("PPID", 4);
521 wprintw(center, "%d", tmp->ppid);
522 print_key_title("CPU", 5);
523 wprintw(center, "%1.2f %%", tmp->totalcpunsec/maxcputime);
b093de8a
MB
524
525 print_key_title("READ B/s", 6);
69362330
JD
526 scale_unit(tmp->fileread, unit);
527 wprintw(center, "%s", unit);
b093de8a
MB
528
529 print_key_title("WRITE B/s", 7);
69362330
JD
530 scale_unit(tmp->filewrite, unit);
531 wprintw(center, "%s", unit);
b093de8a 532
93d80d35
JD
533 wattron(center, A_BOLD);
534 mvwprintw(center, 8, 1, "FD");
69362330
JD
535 mvwprintw(center, 8, 10, "READ");
536 mvwprintw(center, 8, 17, "WRITE");
537 mvwprintw(center, 8, 24, "FILENAME");
93d80d35
JD
538 wattroff(center, A_BOLD);
539
97514683
JD
540 for (i = selected_line; i < tmp->process_files_table->len &&
541 i < (selected_line + max_center_lines - 7); i++) {
b093de8a
MB
542 file_tmp = get_file(tmp, i);
543 if (file_tmp != NULL) {
93d80d35 544 mvwprintw(center, 9 + j, 1, "%d", i);
69362330
JD
545 scale_unit(file_tmp->read, unit);
546 mvwprintw(center, 9 + j, 10, "%s", unit);
547 scale_unit(file_tmp->write, unit);
548 mvwprintw(center, 9 + j, 17, "%s", unit);
97514683
JD
549 snprintf(filename_buf, COLS - 25, "%s", file_tmp->name);
550 mvwprintw(center, 9 + j, 24, "%s", filename_buf);
b093de8a
MB
551 j++;
552 }
553 }
1fc22eb4
JD
554}
555
556void update_perf()
557{
bb053abb 558 int i;
1fc22eb4
JD
559 int nblinedisplayed = 0;
560 int current_line = 0;
561 struct processtop *tmp;
562 int header_offset = 2;
563 int perf_row = 40;
564 struct perfcounter *perfn1, *perfn2;
1fc22eb4
JD
565 char *perf_key = NULL;
566 int value;
85db4618
JD
567 GHashTableIter iter;
568 gpointer key;
1fc22eb4
JD
569
570 set_window_title(center, "Perf Top");
571 wattron(center, A_BOLD);
572 mvwprintw(center, 1, 1, "PID");
573 mvwprintw(center, 1, 11, "TID");
574 mvwprintw(center, 1, 22, "NAME");
575
576 perf_row = 40;
0d91c12a 577 g_hash_table_iter_init(&iter, global_perf_liszt);
85db4618 578 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
1fc22eb4 579 if (perfn1->visible) {
6419078d 580 /* + 5 to strip the "perf_" prefix */
1fc22eb4 581 mvwprintw(center, 1, perf_row, "%s",
6419078d 582 (char *) key + 5);
1fc22eb4
JD
583 perf_row += 20;
584 }
585 if (perfn1->sort) {
85db4618 586 perf_key = (char *) key;
1fc22eb4 587 }
1fc22eb4 588 }
85db4618 589
1fc22eb4
JD
590 wattroff(center, A_BOLD);
591
592 g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key);
85db4618 593
91be6bb5 594 for (i = 0; i < data->process_table->len &&
1fc22eb4 595 nblinedisplayed < max_center_lines; i++) {
1fc22eb4
JD
596 tmp = g_ptr_array_index(data->process_table, i);
597
635dc837
JD
598 if (process_selected(tmp)) {
599 wattron(center, COLOR_PAIR(6));
600 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
601 }
1fc22eb4
JD
602 if (current_line == selected_line) {
603 selected_process = tmp;
604 wattron(center, COLOR_PAIR(5));
605 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
606 }
607
608 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
609 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
610 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
611
0d91c12a 612 g_hash_table_iter_init(&iter, global_perf_liszt);
1fc22eb4
JD
613
614 perf_row = 40;
85db4618 615 while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) {
1fc22eb4 616 if (perfn1->visible) {
85db4618 617 perfn2 = g_hash_table_lookup(tmp->perf, (char *) key);
1fc22eb4
JD
618 if (perfn2)
619 value = perfn2->count;
620 else
621 value = 0;
b093de8a
MB
622 mvwprintw(center, current_line + header_offset,
623 perf_row, "%d", value);
1fc22eb4
JD
624 perf_row += 20;
625 }
1fc22eb4
JD
626 }
627
635dc837 628 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
629 wattroff(center, COLOR_PAIR(5));
630 nblinedisplayed++;
631 current_line++;
632 }
633}
634
1fc22eb4
JD
635gint sort_by_ret_desc(gconstpointer p1, gconstpointer p2)
636{
637 struct processtop *n1 = *(struct processtop **)p1;
638 struct processtop *n2 = *(struct processtop **)p2;
b093de8a
MB
639
640 unsigned long totaln1 = n1->totalfileread + n1->totalfilewrite;
641 unsigned long totaln2 = n2->totalfileread + n2->totalfilewrite;
1fc22eb4
JD
642
643 if (totaln1 < totaln2)
644 return 1;
645 if (totaln1 == totaln2)
646 return 0;
647 return -1;
648}
649
650void update_iostream()
651{
652 int i;
653 int header_offset = 2;
654 struct processtop *tmp;
655 int nblinedisplayed = 0;
656 int current_line = 0;
b093de8a 657 int total = 0;
69362330 658 char unit[4];
1fc22eb4
JD
659
660 set_window_title(center, "IO Top");
661 wattron(center, A_BOLD);
69362330
JD
662 mvwprintw(center, 1, 1, "PID");
663 mvwprintw(center, 1, 11, "TID");
664 mvwprintw(center, 1, 22, "NAME");
665 mvwprintw(center, 1, 40, "R (B/sec)");
666 mvwprintw(center, 1, 52, "W (B/sec)");
667 mvwprintw(center, 1, 64, "Total");
1fc22eb4
JD
668 wattroff(center, A_BOLD);
669
670 g_ptr_array_sort(data->process_table, sort_by_ret_desc);
671
672 for (i = list_offset; i < data->process_table->len &&
673 nblinedisplayed < max_center_lines; i++) {
674 tmp = g_ptr_array_index(data->process_table, i);
675
635dc837
JD
676 if (process_selected(tmp)) {
677 wattron(center, COLOR_PAIR(6));
678 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
679 }
1fc22eb4
JD
680 if (current_line == selected_line) {
681 selected_process = tmp;
1fc22eb4
JD
682 wattron(center, COLOR_PAIR(5));
683 mvwhline(center, current_line + header_offset, 1, ' ', COLS-3);
684 }
69362330
JD
685 /* TGID */
686 mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid);
687 /* PID */
688 mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid);
689 /* NAME */
690 mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm);
b093de8a 691
1fc22eb4 692 /* READ (bytes/sec) */
69362330
JD
693 scale_unit(tmp->fileread, unit);
694 mvwprintw(center, current_line + header_offset, 40, "%s", unit);
1fc22eb4
JD
695
696 /* WRITE (bytes/sec) */
69362330
JD
697 scale_unit(tmp->filewrite, unit);
698 mvwprintw(center, current_line + header_offset, 52, "%s", unit);
1fc22eb4
JD
699
700 /* TOTAL STREAM */
b093de8a
MB
701 total = tmp->totalfileread + tmp->totalfilewrite;
702
69362330
JD
703 scale_unit(total, unit);
704 mvwprintw(center, current_line + header_offset, 64, "%s", unit);
b093de8a 705
635dc837 706 wattroff(center, COLOR_PAIR(6));
1fc22eb4
JD
707 wattroff(center, COLOR_PAIR(5));
708 nblinedisplayed++;
709 current_line++;
710 }
711}
712
713void update_current_view()
714{
715 sem_wait(&update_display_sem);
716 if (!data)
717 return;
718 update_header();
719
720 werase(center);
721 box(center, 0, 0);
722 switch (current_view) {
723 case cpu:
724 update_cputop_display();
725 break;
726 case perf:
727 update_perf();
728 break;
729 case process_details:
730 update_process_details();
731 break;
1fc22eb4
JD
732 case iostream:
733 update_iostream();
734 break;
735 case tree:
736 update_cputop_display();
737 break;
738 default:
739 break;
740 }
741 update_panels();
742 doupdate();
743 sem_post(&update_display_sem);
744}
745
0d91c12a 746void setup_pref_panel()
1fc22eb4
JD
747{
748 int size;
749 if (!data)
750 return;
0d91c12a
JD
751 if (pref_panel_window) {
752 del_panel(pref_panel);
753 delwin(pref_panel_window);
1fc22eb4 754 }
0d91c12a
JD
755 size = g_hash_table_size(global_perf_liszt);
756 pref_panel_window = create_window(size + 2, 30, 10, 10);
757 pref_panel = new_panel(pref_panel_window);
758 pref_panel_visible = 0;
759 hide_panel(pref_panel);
1fc22eb4
JD
760}
761
762void update_perf_panel(int line_selected, int toggle_view, int toggle_sort)
763{
764 int i;
765 struct perfcounter *perf;
766 GList *perflist;
767
768 if (!data)
769 return;
770
0d91c12a
JD
771 werase(pref_panel_window);
772 box(pref_panel_window, 0 , 0);
773 set_window_title(pref_panel_window, "Perf Preferences ");
774 wattron(pref_panel_window, A_BOLD);
775 mvwprintw(pref_panel_window, g_hash_table_size(global_perf_liszt) + 1, 1,
b093de8a 776 " 's' to sort");
0d91c12a 777 wattroff(pref_panel_window, A_BOLD);
1fc22eb4
JD
778
779 if (toggle_sort == 1) {
780 i = 0;
0d91c12a 781 perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
1fc22eb4 782 while (perflist) {
0d91c12a 783 perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
1fc22eb4
JD
784 if (i != line_selected)
785 perf->sort = 0;
786 else
787 perf->sort = 1;
788 i++;
789 perflist = g_list_next(perflist);
790 }
791 update_current_view();
792 }
793
794 i = 0;
0d91c12a 795 perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt));
1fc22eb4 796 while (perflist) {
0d91c12a 797 perf = g_hash_table_lookup(global_perf_liszt, perflist->data);
1fc22eb4
JD
798 if (i == line_selected && toggle_view == 1) {
799 perf->visible = perf->visible == 1 ? 0:1;
800 update_current_view();
801 }
802 if (i == line_selected) {
0d91c12a
JD
803 wattron(pref_panel_window, COLOR_PAIR(5));
804 mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2);
1fc22eb4
JD
805 }
806 if (perf->sort == 1)
0d91c12a
JD
807 wattron(pref_panel_window, A_BOLD);
808 mvwprintw(pref_panel_window, i + 1, 1, "[%c] %s",
1fc22eb4 809 perf->visible == 1 ? 'x' : ' ',
b8a1df45 810 (char *) perflist->data + 5);
0d91c12a
JD
811 wattroff(pref_panel_window, A_BOLD);
812 wattroff(pref_panel_window, COLOR_PAIR(5));
1fc22eb4
JD
813 i++;
814 perflist = g_list_next(perflist);
815 }
816 update_panels();
817 doupdate();
818}
819
0d91c12a 820void update_preference_panel(int line_selected, int toggle_view, int toggle_sort)
1fc22eb4 821{
0d91c12a
JD
822 switch(current_view) {
823 case perf:
824 update_perf_panel(line_selected, toggle_view, toggle_sort);
825 break;
826 default:
827 break;
828 }
829}
830
831void toggle_pref_panel(void)
832{
833 if (pref_panel_visible) {
834 hide_panel(pref_panel);
835 pref_panel_visible = 0;
1fc22eb4 836 } else {
0d91c12a
JD
837 setup_pref_panel();
838 update_preference_panel(perf_line_selected, 0, 0);
839 show_panel(pref_panel);
840 pref_panel_visible = 1;
1fc22eb4
JD
841 }
842 update_panels();
843 doupdate();
844}
845
846void display(unsigned int index)
847{
848 last_display_index = index;
849 currently_displayed_index = index;
850 data = g_ptr_array_index(copies, index);
851 if (!data)
852 return;
853 max_elements = data->process_table->len;
854 update_current_view();
855 update_footer();
856 update_panels();
857 doupdate();
858}
859
860void pause_display()
861{
862 toggle_pause = 1;
863 print_log("Pause");
864 sem_wait(&pause_sem);
865}
866
867void resume_display()
868{
869 toggle_pause = -1;
870 print_log("Resume");
871 sem_post(&pause_sem);
872}
873
874void *handle_keyboard(void *p)
875{
876 int ch;
877 while((ch = getch())) {
878 switch(ch) {
879 /* Move the cursor and scroll */
880 case KEY_DOWN:
0d91c12a
JD
881 if (pref_panel_visible) {
882 if (perf_line_selected < g_hash_table_size(global_perf_liszt) - 1)
1fc22eb4 883 perf_line_selected++;
0d91c12a 884 update_preference_panel(perf_line_selected, 0, 0);
1fc22eb4
JD
885 } else {
886 if (selected_line < (max_center_lines - 1) &&
887 selected_line < max_elements - 1) {
888 selected_line++;
889 selected_in_list++;
890 } else if (selected_in_list < (max_elements - 1)
891 && (list_offset < (max_elements - max_center_lines))) {
892 selected_in_list++;
893 list_offset++;
894 }
895 update_current_view();
896 }
897 break;
898 case KEY_NPAGE:
1fc22eb4
JD
899 break;
900 case KEY_UP:
0d91c12a 901 if (pref_panel_visible) {
1fc22eb4
JD
902 if (perf_line_selected > 0)
903 perf_line_selected--;
0d91c12a 904 update_preference_panel(perf_line_selected, 0, 0);
1fc22eb4
JD
905 } else {
906 if (selected_line > 0) {
907 selected_line--;
908 selected_in_list--;
909 } else if (selected_in_list > 0 && list_offset > 0) {
910 selected_in_list--;
911 list_offset--;
912 }
913 update_current_view();
914 }
915 break;
916 case KEY_PPAGE:
1fc22eb4
JD
917 break;
918
919 /* Navigate the history with arrows */
920 case KEY_LEFT:
921 if (currently_displayed_index > 0) {
922 currently_displayed_index--;
923 print_log("Going back in time");
924 } else {
925 print_log("Cannot rewind, last data is already displayed");
926 }
927 data = g_ptr_array_index(copies, currently_displayed_index);
928 max_elements = data->process_table->len;
929
930 /* we force to pause the display when moving in time */
931 if (toggle_pause < 0)
932 pause_display();
933
934 update_current_view();
935 update_footer();
936 break;
937 case KEY_RIGHT:
938 if (currently_displayed_index < last_display_index) {
939 currently_displayed_index++;
940 print_log("Going forward in time");
941 data = g_ptr_array_index(copies, currently_displayed_index);
942 max_elements = data->process_table->len;
943 update_current_view();
944 update_footer();
945 } else {
946 print_log("Manually moving forward");
947 sem_post(&timer);
948 /* we force to resume the refresh when moving forward */
949 if (toggle_pause > 0)
950 resume_display();
951 }
952
953 break;
954 case ' ':
0d91c12a
JD
955 if (pref_panel_visible) {
956 update_preference_panel(perf_line_selected, 1, 0);
635dc837
JD
957 } else {
958 update_selected_processes();
959 update_current_view();
960 }
1fc22eb4
JD
961 break;
962 case 's':
0d91c12a
JD
963 if (pref_panel_visible)
964 update_preference_panel(perf_line_selected, 0, 1);
1fc22eb4
JD
965 break;
966
967 case 13: /* FIXME : KEY_ENTER ?? */
041aa219
JD
968 if (current_view != process_details) {
969 previous_view = current_view;
1fc22eb4 970 current_view = process_details;
041aa219
JD
971 } else {
972 current_view = previous_view;
973 previous_view = process_details;
1fc22eb4
JD
974 }
975 update_current_view();
976 break;
977
978 case KEY_F(1):
1fc22eb4 979 current_view = cpu;
af54ebcf 980 selected_line = 0;
1fc22eb4
JD
981 update_current_view();
982 break;
983 case KEY_F(2):
984 current_view = cpu;
af54ebcf 985 selected_line = 0;
1fc22eb4
JD
986 update_current_view();
987 break;
988 case KEY_F(3):
989 current_view = perf;
af54ebcf 990 selected_line = 0;
1fc22eb4
JD
991 update_current_view();
992 break;
993 case KEY_F(4):
1fc22eb4 994 current_view = iostream;
af54ebcf 995 selected_line = 0;
1fc22eb4
JD
996 update_current_view();
997 break;
998 case KEY_F(10):
999 case 'q':
1000 reset_ncurses();
1001 break;
1002 case 't':
1003 toggle_threads *= -1;
1004 update_current_view();
1005 break;
1006 case 'p':
1007 if (toggle_pause < 0) {
1008 pause_display();
1009 } else {
1010 resume_display();
1011 }
1012 break;
1013 case 'P':
0d91c12a 1014 toggle_pref_panel();
1fc22eb4
JD
1015 break;
1016 default:
b093de8a
MB
1017 if (data)
1018 update_current_view();
1fc22eb4
JD
1019 break;
1020 }
1021 update_footer();
1022 }
1023 return NULL;
1024}
1025
1026void init_ncurses()
1027{
635dc837 1028 selected_processes = g_ptr_array_new();
1fc22eb4
JD
1029 sem_init(&update_display_sem, 0, 1);
1030 init_screen();
1031
dc8f04dd
JD
1032 header = create_window(5, COLS - 1, 0, 0);
1033 center = create_window(LINES - 5 - 7, COLS - 1, 5, 0);
1fc22eb4
JD
1034 status = create_window(MAX_LOG_LINES + 2, COLS - 1, LINES - 7, 0);
1035 footer = create_window(1, COLS - 1, LINES - 1, 0);
1036
1037 print_log("Starting display");
1038
1039 main_panel = new_panel(center);
0d91c12a 1040 setup_pref_panel();
1fc22eb4
JD
1041
1042 current_view = cpu;
1043
1044 basic_header();
1045 update_footer();
1046
1047 pthread_create(&keyboard_thread, NULL, handle_keyboard, (void *)NULL);
1048}
This page took 0.110586 seconds and 4 git commands to generate.