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