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