| 1 | /* |
| 2 | * Copyright (C) 2011-2012 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 *pref_panel_window = NULL; |
| 42 | PANEL *pref_panel, *main_panel; |
| 43 | |
| 44 | int pref_panel_visible = 0; |
| 45 | int pref_line_selected = 0; |
| 46 | int pref_current_sort = 0; |
| 47 | |
| 48 | int last_display_index, currently_displayed_index; |
| 49 | |
| 50 | struct processtop *selected_process = NULL; |
| 51 | int selected_ret; |
| 52 | |
| 53 | int selected_line = 0; /* select bar position */ |
| 54 | int selected_in_list = 0; /* selection relative to the whole list */ |
| 55 | int list_offset = 0; /* first index in the list to display (scroll) */ |
| 56 | int nb_log_lines = 0; |
| 57 | char log_lines[MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES]; |
| 58 | |
| 59 | int max_elements = 80; |
| 60 | |
| 61 | int toggle_threads = 1; |
| 62 | int toggle_virt = -1; |
| 63 | int toggle_pause = -1; |
| 64 | |
| 65 | int max_center_lines; |
| 66 | GPtrArray *selected_processes; |
| 67 | |
| 68 | pthread_t keyboard_thread; |
| 69 | |
| 70 | struct header_view cputopview[6]; |
| 71 | struct header_view iostreamtopview[3]; |
| 72 | struct header_view fileview[3]; |
| 73 | struct header_view kprobeview[2]; |
| 74 | |
| 75 | void reset_ncurses() |
| 76 | { |
| 77 | curs_set(1); |
| 78 | endwin(); |
| 79 | quit = 1; |
| 80 | sem_post(&pause_sem); |
| 81 | sem_post(&timer); |
| 82 | sem_post(&goodtodisplay); |
| 83 | sem_post(&end_trace_sem); |
| 84 | sem_post(&goodtoupdate); |
| 85 | } |
| 86 | |
| 87 | static void handle_sigterm(int signal) |
| 88 | { |
| 89 | pthread_cancel(keyboard_thread); |
| 90 | reset_ncurses(); |
| 91 | } |
| 92 | |
| 93 | void init_screen() |
| 94 | { |
| 95 | initscr(); |
| 96 | noecho(); |
| 97 | halfdelay(DEFAULT_DELAY); |
| 98 | nonl(); |
| 99 | intrflush(stdscr, false); |
| 100 | keypad(stdscr, true); |
| 101 | curs_set(0); |
| 102 | |
| 103 | if (has_colors()) { |
| 104 | start_color(); |
| 105 | init_pair(1, COLOR_RED, COLOR_BLACK); /* - */ |
| 106 | init_pair(2, COLOR_GREEN, COLOR_BLACK); /* + */ |
| 107 | init_pair(3, COLOR_BLACK, COLOR_WHITE); /* keys */ |
| 108 | init_pair(4, COLOR_WHITE, COLOR_GREEN); /* keys activated */ |
| 109 | init_pair(5, COLOR_WHITE, COLOR_BLUE); /* select line */ |
| 110 | init_pair(6, COLOR_WHITE, COLOR_GREEN); /* selected process */ |
| 111 | } |
| 112 | termtype = getenv("TERM"); |
| 113 | if (!strcmp(termtype, "xterm") || !strcmp(termtype, "xterm-color") || |
| 114 | !strcmp(termtype, "vt220")) { |
| 115 | define_key("\033[H", KEY_HOME); |
| 116 | define_key("\033[F", KEY_END); |
| 117 | define_key("\033OP", KEY_F(1)); |
| 118 | define_key("\033OQ", KEY_F(2)); |
| 119 | define_key("\033OR", KEY_F(3)); |
| 120 | define_key("\033OS", KEY_F(4)); |
| 121 | define_key("\0330U", KEY_F(6)); |
| 122 | define_key("\033[11~", KEY_F(1)); |
| 123 | define_key("\033[12~", KEY_F(2)); |
| 124 | define_key("\033[13~", KEY_F(3)); |
| 125 | define_key("\033[14~", KEY_F(4)); |
| 126 | define_key("\033[16~", KEY_F(6)); |
| 127 | define_key("\033[17;2~", KEY_F(18)); |
| 128 | } |
| 129 | signal(SIGTERM, handle_sigterm); |
| 130 | signal(SIGINT, handle_sigterm); |
| 131 | mousemask(BUTTON1_CLICKED, NULL); |
| 132 | refresh(); |
| 133 | } |
| 134 | |
| 135 | WINDOW *create_window(int height, int width, int startx, int starty) |
| 136 | { |
| 137 | WINDOW *win; |
| 138 | win = newwin(height, width, startx, starty); |
| 139 | box(win, 0 , 0); |
| 140 | wrefresh(win); |
| 141 | return win; |
| 142 | } |
| 143 | |
| 144 | WINDOW *create_window_no_border(int height, int width, int startx, int starty) |
| 145 | { |
| 146 | WINDOW *win; |
| 147 | win = newwin(height, width, startx, starty); |
| 148 | wrefresh(win); |
| 149 | return win; |
| 150 | } |
| 151 | |
| 152 | void print_digit(WINDOW *win, int digit) |
| 153 | { |
| 154 | if (digit < 0) { |
| 155 | wattron(win, COLOR_PAIR(1)); |
| 156 | wprintw(win, "%d", digit); |
| 157 | wattroff(win, COLOR_PAIR(1)); |
| 158 | } else if (digit > 0) { |
| 159 | wattron(win, COLOR_PAIR(2)); |
| 160 | wprintw(win, "+%d", digit); |
| 161 | wattroff(win, COLOR_PAIR(2)); |
| 162 | } else { |
| 163 | wprintw(win, "0"); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void print_digits(WINDOW *win, int first, int second) |
| 168 | { |
| 169 | wprintw(win, "("); |
| 170 | print_digit(win, first); |
| 171 | wprintw(win, ", "); |
| 172 | print_digit(win, second); |
| 173 | wprintw(win, ")"); |
| 174 | } |
| 175 | |
| 176 | void print_headers(int line, char *desc, int value, int first, int second) |
| 177 | { |
| 178 | wattron(header, A_BOLD); |
| 179 | mvwprintw(header, line, 4, "%s", desc); |
| 180 | wattroff(header, A_BOLD); |
| 181 | mvwprintw(header, line, 16, "%d", value); |
| 182 | wmove(header, line, 24); |
| 183 | print_digits(header, first, second); |
| 184 | wmove(header, line, 40); |
| 185 | } |
| 186 | |
| 187 | void set_window_title(WINDOW *win, char *title) |
| 188 | { |
| 189 | wattron(win, A_BOLD); |
| 190 | mvwprintw(win, 0, 1, title); |
| 191 | wattroff(win, A_BOLD); |
| 192 | } |
| 193 | |
| 194 | void print_log(char *str) |
| 195 | { |
| 196 | int i; |
| 197 | int current_line = 1; |
| 198 | int current_char = 1; |
| 199 | char *tmp, *tmp2; |
| 200 | /* rotate the line buffer */ |
| 201 | if (nb_log_lines >= MAX_LOG_LINES) { |
| 202 | tmp = strndup(log_lines, MAX_LINE_LENGTH * MAX_LOG_LINES + MAX_LOG_LINES); |
| 203 | tmp2 = strchr(tmp, '\n'); |
| 204 | memset(log_lines, '\0', strlen(log_lines)); |
| 205 | strncat(log_lines, tmp2 + 1, strlen(tmp2) - 1); |
| 206 | log_lines[strlen(log_lines)] = '\n'; |
| 207 | log_lines[strlen(log_lines)] = '\0'; |
| 208 | free(tmp); |
| 209 | } |
| 210 | nb_log_lines++; |
| 211 | |
| 212 | strncat(log_lines, str, MAX_LINE_LENGTH - 1); |
| 213 | |
| 214 | if (nb_log_lines < MAX_LOG_LINES) |
| 215 | log_lines[strlen(log_lines)] = '\n'; |
| 216 | log_lines[strlen(log_lines)] = '\0'; |
| 217 | |
| 218 | werase(status); |
| 219 | box(status, 0 , 0); |
| 220 | set_window_title(status, "Status"); |
| 221 | for (i = 0; i < strlen(log_lines); i++) { |
| 222 | if (log_lines[i] == '\n') { |
| 223 | wmove(status, ++current_line, 1); |
| 224 | current_char = 1; |
| 225 | } else { |
| 226 | mvwprintw(status, current_line, current_char++, "%c", |
| 227 | log_lines[i]); |
| 228 | } |
| 229 | } |
| 230 | wrefresh(status); |
| 231 | } |
| 232 | |
| 233 | int process_selected(struct processtop *process) |
| 234 | { |
| 235 | int i; |
| 236 | struct processtop *stored_process; |
| 237 | |
| 238 | for (i = 0; i < selected_processes->len; i++) { |
| 239 | stored_process = g_ptr_array_index(selected_processes, i); |
| 240 | if (!stored_process) |
| 241 | return 0; |
| 242 | if (stored_process->tid == process->tid) |
| 243 | return 1; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | void update_selected_processes() |
| 249 | { |
| 250 | int i; |
| 251 | struct processtop *stored_process; |
| 252 | |
| 253 | if (process_selected(selected_process)) { |
| 254 | for (i = 0; i < selected_processes->len; i++) { |
| 255 | stored_process = g_ptr_array_index(selected_processes, i); |
| 256 | if (!stored_process) |
| 257 | return; |
| 258 | if (stored_process->tid == selected_process->tid) |
| 259 | g_ptr_array_remove(selected_processes, |
| 260 | stored_process); |
| 261 | print_log("Process removed"); |
| 262 | } |
| 263 | } else { |
| 264 | g_ptr_array_add(selected_processes, selected_process); |
| 265 | print_log("Process added"); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void print_key(WINDOW *win, char *key, char *desc, int toggle) |
| 270 | { |
| 271 | int pair; |
| 272 | if (toggle > 0) |
| 273 | pair = 4; |
| 274 | else |
| 275 | pair = 3; |
| 276 | wattron(win, COLOR_PAIR(pair)); |
| 277 | wprintw(footer, "%s", key); |
| 278 | wattroff(win, COLOR_PAIR(pair)); |
| 279 | wprintw(footer, ":%s", desc); |
| 280 | } |
| 281 | |
| 282 | void update_footer() |
| 283 | { |
| 284 | sem_wait(&update_display_sem); |
| 285 | werase(footer); |
| 286 | wmove(footer, 1, 1); |
| 287 | print_key(footer, "F2", "CPUtop ", current_view == cpu); |
| 288 | print_key(footer, "F3", "PerfTop ", current_view == perf); |
| 289 | print_key(footer, "F4", "IOTop ", current_view == iostream); |
| 290 | print_key(footer, "Enter", "Details ", current_view == process_details); |
| 291 | print_key(footer, "Space", "Highlight ", 0); |
| 292 | print_key(footer, "q", "Quit ", 0); |
| 293 | print_key(footer, "r", "Pref ", 0); |
| 294 | print_key(footer, "t", "Threads ", toggle_threads); |
| 295 | print_key(footer, "v", "Virt ", toggle_virt); |
| 296 | print_key(footer, "p", "Pause ", toggle_pause); |
| 297 | |
| 298 | wrefresh(footer); |
| 299 | sem_post(&update_display_sem); |
| 300 | } |
| 301 | |
| 302 | void basic_header() |
| 303 | { |
| 304 | werase(header); |
| 305 | box(header, 0 , 0); |
| 306 | set_window_title(header, "Statistics for interval [gathering data...["); |
| 307 | wattron(header, A_BOLD); |
| 308 | mvwprintw(header, 1, 4, "CPUs"); |
| 309 | mvwprintw(header, 2, 4, "Threads"); |
| 310 | mvwprintw(header, 3, 4, "FDs"); |
| 311 | wattroff(header, A_BOLD); |
| 312 | wrefresh(header); |
| 313 | } |
| 314 | |
| 315 | static void scale_unit(uint64_t bytes, char *ret) |
| 316 | { |
| 317 | if (bytes >= 1000000000) |
| 318 | sprintf(ret, "%" PRIu64 "G", bytes/1000000000); |
| 319 | if (bytes >= 1000000) |
| 320 | sprintf(ret, "%" PRIu64 "M", bytes/1000000); |
| 321 | else if (bytes >= 1000) |
| 322 | sprintf(ret, "%" PRIu64 "K", bytes/1000); |
| 323 | else |
| 324 | sprintf(ret, "%" PRIu64, bytes); |
| 325 | } |
| 326 | |
| 327 | uint64_t total_io() |
| 328 | { |
| 329 | int i; |
| 330 | struct processtop *tmp; |
| 331 | uint64_t total = 0; |
| 332 | |
| 333 | for (i = 0; i < data->process_table->len; i++) { |
| 334 | tmp = g_ptr_array_index(data->process_table, i); |
| 335 | total += tmp->fileread; |
| 336 | total += tmp->filewrite; |
| 337 | } |
| 338 | |
| 339 | return total; |
| 340 | } |
| 341 | |
| 342 | void update_header() |
| 343 | { |
| 344 | struct tm start, end; |
| 345 | uint64_t ts_nsec_start, ts_nsec_end; |
| 346 | char io[4]; |
| 347 | |
| 348 | ts_nsec_start = data->start % NSEC_PER_SEC; |
| 349 | start = format_timestamp(data->start); |
| 350 | |
| 351 | ts_nsec_end = data->end % NSEC_PER_SEC; |
| 352 | end = format_timestamp(data->end); |
| 353 | |
| 354 | werase(header); |
| 355 | box(header, 0 , 0); |
| 356 | set_window_title(header, "Statistics for interval "); |
| 357 | wattron(header, A_BOLD); |
| 358 | |
| 359 | wprintw(header, "[%02d:%02d:%02d.%09" PRIu64 ", %02d:%02d:%02d.%09" PRIu64 "[", |
| 360 | start.tm_hour, start.tm_min, start.tm_sec, ts_nsec_start, |
| 361 | end.tm_hour, end.tm_min, end.tm_sec, ts_nsec_end); |
| 362 | mvwprintw(header, 1, 4, "CPUs"); |
| 363 | wattroff(header, A_BOLD); |
| 364 | wprintw(header, "\t%d\t(max/cpu : %0.2f%)", data->cpu_table->len, |
| 365 | 100.0/data->cpu_table->len); |
| 366 | print_headers(2, "Threads", data->nbthreads, data->nbnewthreads, |
| 367 | -1*(data->nbdeadthreads)); |
| 368 | print_headers(3, "FDs", data->nbfiles, data->nbnewfiles, |
| 369 | -1*(data->nbclosedfiles)); |
| 370 | scale_unit(total_io(), io); |
| 371 | mvwprintw(header, 3, 43, "%sB/sec", io); |
| 372 | wrefresh(header); |
| 373 | } |
| 374 | |
| 375 | gint sort_by_cpu_desc(gconstpointer p1, gconstpointer p2) |
| 376 | { |
| 377 | struct processtop *n1 = *(struct processtop **)p1; |
| 378 | struct processtop *n2 = *(struct processtop **)p2; |
| 379 | unsigned long totaln1 = n1->totalcpunsec; |
| 380 | unsigned long totaln2 = n2->totalcpunsec; |
| 381 | |
| 382 | if (totaln1 < totaln2) |
| 383 | return 1; |
| 384 | if (totaln1 == totaln2) |
| 385 | return 0; |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | gint sort_by_tid_desc(gconstpointer p1, gconstpointer p2) |
| 390 | { |
| 391 | struct processtop *n1 = *(struct processtop **)p1; |
| 392 | struct processtop *n2 = *(struct processtop **)p2; |
| 393 | unsigned long totaln1 = n1->tid; |
| 394 | unsigned long totaln2 = n2->tid; |
| 395 | |
| 396 | if (totaln1 < totaln2) |
| 397 | return 1; |
| 398 | if (totaln1 == totaln2) |
| 399 | return 0; |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | gint sort_by_pid_desc(gconstpointer p1, gconstpointer p2) |
| 404 | { |
| 405 | struct processtop *n1 = *(struct processtop **)p1; |
| 406 | struct processtop *n2 = *(struct processtop **)p2; |
| 407 | unsigned long totaln1 = n1->pid; |
| 408 | unsigned long totaln2 = n2->pid; |
| 409 | |
| 410 | if (totaln1 < totaln2) |
| 411 | return 1; |
| 412 | if (totaln1 == totaln2) |
| 413 | return 0; |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | gint sort_by_process_read_desc(gconstpointer p1, gconstpointer p2) |
| 418 | { |
| 419 | struct processtop *n1 = *(struct processtop **)p1; |
| 420 | struct processtop *n2 = *(struct processtop **)p2; |
| 421 | unsigned long totaln1 = n1->fileread; |
| 422 | unsigned long totaln2 = n2->fileread; |
| 423 | |
| 424 | if (totaln1 < totaln2) |
| 425 | return 1; |
| 426 | if (totaln1 == totaln2) |
| 427 | return 0; |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | gint sort_by_process_write_desc(gconstpointer p1, gconstpointer p2) |
| 432 | { |
| 433 | struct processtop *n1 = *(struct processtop **)p1; |
| 434 | struct processtop *n2 = *(struct processtop **)p2; |
| 435 | unsigned long totaln1 = n1->filewrite; |
| 436 | unsigned long totaln2 = n2->filewrite; |
| 437 | |
| 438 | if (totaln1 < totaln2) |
| 439 | return 1; |
| 440 | if (totaln1 == totaln2) |
| 441 | return 0; |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | gint sort_by_process_total_desc(gconstpointer p1, gconstpointer p2) |
| 446 | { |
| 447 | struct processtop *n1 = *(struct processtop **)p1; |
| 448 | struct processtop *n2 = *(struct processtop **)p2; |
| 449 | unsigned long totaln1 = n1->totalfilewrite + n1->totalfileread; |
| 450 | unsigned long totaln2 = n2->totalfilewrite + n2->totalfileread; |
| 451 | |
| 452 | if (totaln1 < totaln2) |
| 453 | return 1; |
| 454 | if (totaln1 == totaln2) |
| 455 | return 0; |
| 456 | return -1; |
| 457 | } |
| 458 | |
| 459 | gint sort_by_file_read_desc(gconstpointer p1, gconstpointer p2) |
| 460 | { |
| 461 | struct files *n1 = *(struct files **)p1; |
| 462 | struct files *n2 = *(struct files **)p2; |
| 463 | unsigned long totaln1; |
| 464 | unsigned long totaln2; |
| 465 | |
| 466 | totaln1 = n1->read; |
| 467 | totaln2 = n2->read; |
| 468 | |
| 469 | if (totaln1 < totaln2) |
| 470 | return 1; |
| 471 | if (totaln1 == totaln2) |
| 472 | return 0; |
| 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | gint sort_by_file_write_desc(gconstpointer p1, gconstpointer p2) |
| 477 | { |
| 478 | struct files *n1 = *(struct files **)p1; |
| 479 | struct files *n2 = *(struct files **)p2; |
| 480 | unsigned long totaln1; |
| 481 | unsigned long totaln2; |
| 482 | |
| 483 | totaln1 = n1->write; |
| 484 | totaln2 = n2->write; |
| 485 | |
| 486 | if (totaln1 < totaln2) |
| 487 | return 1; |
| 488 | if (totaln1 == totaln2) |
| 489 | return 0; |
| 490 | return -1; |
| 491 | } |
| 492 | |
| 493 | gint sort_by_file_fd_desc(gconstpointer p1, gconstpointer p2) |
| 494 | { |
| 495 | struct files *n1 = *(struct files **)p1; |
| 496 | struct files *n2 = *(struct files **)p2; |
| 497 | unsigned long totaln1; |
| 498 | unsigned long totaln2; |
| 499 | |
| 500 | totaln1 = n1->fd; |
| 501 | totaln2 = n2->fd; |
| 502 | |
| 503 | if (totaln1 < totaln2) |
| 504 | return 1; |
| 505 | if (totaln1 == totaln2) |
| 506 | return 0; |
| 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | gint sort_by_cpu_group_by_threads_desc(gconstpointer p1, gconstpointer p2) |
| 511 | { |
| 512 | struct processtop *n1 = *(struct processtop **)p1; |
| 513 | struct processtop *n2 = *(struct processtop **)p2; |
| 514 | unsigned long totaln1 = n1->threadstotalcpunsec; |
| 515 | unsigned long totaln2 = n2->threadstotalcpunsec; |
| 516 | |
| 517 | if (totaln1 < totaln2) |
| 518 | return 1; |
| 519 | if (totaln1 == totaln2) |
| 520 | return 0; |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | void update_kprobes_display() |
| 525 | { |
| 526 | int i, column; |
| 527 | struct kprobes *probe; |
| 528 | int header_offset = 2; |
| 529 | int current_line = 0; |
| 530 | |
| 531 | set_window_title(center, "Kprobes Top "); |
| 532 | wattron(center, A_BOLD); |
| 533 | column = 1; |
| 534 | for (i = 0; i < 2; i++) { |
| 535 | if (kprobeview[i].sort) { |
| 536 | wattron(center, A_UNDERLINE); |
| 537 | pref_current_sort = i; |
| 538 | } |
| 539 | mvwprintw(center, 1, column, "%s", kprobeview[i].title); |
| 540 | wattroff(center, A_UNDERLINE); |
| 541 | column += 30; |
| 542 | } |
| 543 | wattroff(center, A_BOLD); |
| 544 | |
| 545 | for (i = 0; i < data->kprobes_table->len; i++) { |
| 546 | column = 1; |
| 547 | probe = g_ptr_array_index(data->kprobes_table, i); |
| 548 | mvwprintw(center, current_line + header_offset, column, |
| 549 | "%s", probe->probe_name + 6); |
| 550 | column += 30; |
| 551 | mvwprintw(center, current_line + header_offset, column, |
| 552 | "%d", probe->count); |
| 553 | current_line++; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void update_cputop_display() |
| 558 | { |
| 559 | int i; |
| 560 | int header_offset = 2; |
| 561 | struct processtop *tmp; |
| 562 | unsigned long elapsed; |
| 563 | double maxcputime; |
| 564 | int nblinedisplayed = 0; |
| 565 | int current_line = 0; |
| 566 | int current_row_offset; |
| 567 | int column; |
| 568 | |
| 569 | elapsed = data->end - data->start; |
| 570 | maxcputime = elapsed * data->cpu_table->len / 100.0; |
| 571 | |
| 572 | if (cputopview[0].sort == 1) |
| 573 | g_ptr_array_sort(data->process_table, sort_by_cpu_desc); |
| 574 | else if (cputopview[1].sort == 1) |
| 575 | g_ptr_array_sort(data->process_table, sort_by_pid_desc); |
| 576 | else if (cputopview[2].sort == 1) |
| 577 | g_ptr_array_sort(data->process_table, sort_by_tid_desc); |
| 578 | else if (cputopview[3].sort == 1) |
| 579 | g_ptr_array_sort(data->process_table, sort_by_cpu_desc); |
| 580 | else |
| 581 | g_ptr_array_sort(data->process_table, sort_by_cpu_desc); |
| 582 | |
| 583 | set_window_title(center, "CPU Top"); |
| 584 | wattron(center, A_BOLD); |
| 585 | column = 1; |
| 586 | for (i = 0; i < 6; i++) { |
| 587 | if (toggle_virt < 0 && (i == 3 || i == 4)) { |
| 588 | continue; |
| 589 | } |
| 590 | if (cputopview[i].sort) { |
| 591 | wattron(center, A_UNDERLINE); |
| 592 | pref_current_sort = i; |
| 593 | } |
| 594 | mvwprintw(center, 1, column, cputopview[i].title); |
| 595 | wattroff(center, A_UNDERLINE); |
| 596 | column += 10; |
| 597 | } |
| 598 | wattroff(center, A_BOLD); |
| 599 | |
| 600 | max_center_lines = LINES - 5 - 7 - 1 - header_offset; |
| 601 | |
| 602 | /* iterate the process (thread) list */ |
| 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 | current_row_offset = 1; |
| 607 | if (!opt_tid && (opt_hostname && !lookup_hostname_list(tmp->hostname))) |
| 608 | continue; |
| 609 | if (!opt_hostname && (opt_tid && !lookup_tid_list(tmp->pid))) |
| 610 | continue; |
| 611 | if ((opt_tid && !lookup_tid_list(tmp->tid)) && |
| 612 | (opt_hostname && !lookup_hostname_list(tmp->hostname))) |
| 613 | continue; |
| 614 | |
| 615 | if (tmp->pid != tmp->tid) |
| 616 | if (toggle_threads == -1) |
| 617 | continue; |
| 618 | |
| 619 | if (process_selected(tmp)) { |
| 620 | wattron(center, COLOR_PAIR(6)); |
| 621 | mvwhline(center, current_line + header_offset, 1, ' ', COLS-3); |
| 622 | } |
| 623 | if (current_line == selected_line) { |
| 624 | selected_process = tmp; |
| 625 | wattron(center, COLOR_PAIR(5)); |
| 626 | mvwhline(center, current_line + header_offset, 1, ' ', COLS-3); |
| 627 | } |
| 628 | /* CPU(%) */ |
| 629 | mvwprintw(center, current_line + header_offset, |
| 630 | current_row_offset, "%1.2f", |
| 631 | tmp->totalcpunsec / maxcputime); |
| 632 | current_row_offset += 10; |
| 633 | /* PID */ |
| 634 | mvwprintw(center, current_line + header_offset, |
| 635 | current_row_offset, "%d", tmp->pid); |
| 636 | current_row_offset += 10; |
| 637 | /* TID */ |
| 638 | mvwprintw(center, current_line + header_offset, |
| 639 | current_row_offset, "%d", tmp->tid); |
| 640 | current_row_offset += 10; |
| 641 | if (toggle_virt > 0) { |
| 642 | /* VPID */ |
| 643 | mvwprintw(center, current_line + header_offset, |
| 644 | current_row_offset, "%d", tmp->vpid); |
| 645 | current_row_offset += 10; |
| 646 | /* VTID */ |
| 647 | mvwprintw(center, current_line + header_offset, |
| 648 | current_row_offset, "%d", tmp->vtid); |
| 649 | current_row_offset += 10; |
| 650 | } |
| 651 | /* NAME */ |
| 652 | mvwprintw(center, current_line + header_offset, |
| 653 | current_row_offset, "%s", tmp->comm); |
| 654 | wattroff(center, COLOR_PAIR(6)); |
| 655 | wattroff(center, COLOR_PAIR(5)); |
| 656 | nblinedisplayed++; |
| 657 | current_line++; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | gint sort_perf(gconstpointer p1, gconstpointer p2, gpointer key) |
| 662 | { |
| 663 | struct processtop *n1 = *(struct processtop **) p1; |
| 664 | struct processtop *n2 = *(struct processtop **) p2; |
| 665 | |
| 666 | struct perfcounter *tmp1, *tmp2; |
| 667 | unsigned long totaln2 = 0; |
| 668 | unsigned long totaln1 = 0; |
| 669 | |
| 670 | if (!key) |
| 671 | return 0; |
| 672 | |
| 673 | tmp1 = g_hash_table_lookup(n1->perf, key); |
| 674 | if (!tmp1) |
| 675 | totaln1 = 0; |
| 676 | else |
| 677 | totaln1 = tmp1->count; |
| 678 | |
| 679 | tmp2 = g_hash_table_lookup(n2->perf, key); |
| 680 | if (!tmp2) |
| 681 | totaln2 = 0; |
| 682 | else |
| 683 | totaln2 = tmp2->count; |
| 684 | |
| 685 | if (totaln1 < totaln2) |
| 686 | return 1; |
| 687 | if (totaln1 == totaln2) { |
| 688 | totaln1 = n1->tid; |
| 689 | totaln2 = n2->tid; |
| 690 | if (totaln1 < totaln2) |
| 691 | return 1; |
| 692 | return -1; |
| 693 | } |
| 694 | return -1; |
| 695 | } |
| 696 | |
| 697 | void print_key_title(char *key, int line) |
| 698 | { |
| 699 | wattron(center, A_BOLD); |
| 700 | mvwprintw(center, line, 1, "%s", key); |
| 701 | mvwprintw(center, line, 30, " "); |
| 702 | wattroff(center, A_BOLD); |
| 703 | } |
| 704 | |
| 705 | void update_process_details() |
| 706 | { |
| 707 | unsigned long elapsed; |
| 708 | double maxcputime; |
| 709 | struct processtop *tmp; |
| 710 | struct files *file_tmp; |
| 711 | int i, j = 0; |
| 712 | char unit[4]; |
| 713 | char filename_buf[COLS]; |
| 714 | int line = 1; |
| 715 | int column; |
| 716 | GPtrArray *newfilearray = g_ptr_array_new(); |
| 717 | GHashTableIter iter; |
| 718 | struct perfcounter *perfn1, *perfn2; |
| 719 | gpointer key; |
| 720 | |
| 721 | set_window_title(center, "Process details"); |
| 722 | |
| 723 | |
| 724 | tmp = find_process_tid(data, |
| 725 | selected_process->tid, |
| 726 | selected_process->comm); |
| 727 | elapsed = data->end - data->start; |
| 728 | maxcputime = elapsed * data->cpu_table->len / 100.0; |
| 729 | |
| 730 | print_key_title("Name", line++); |
| 731 | wprintw(center, "%s", selected_process->comm); |
| 732 | print_key_title("TID", line++); |
| 733 | wprintw(center, "%d", selected_process->tid); |
| 734 | if (!tmp) { |
| 735 | print_key_title("Does not exit at this time", 3); |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | print_key_title("PID", line++); |
| 740 | wprintw(center, "%d", tmp->pid); |
| 741 | print_key_title("PPID", line++); |
| 742 | wprintw(center, "%d", tmp->ppid); |
| 743 | print_key_title("VPID", line++); |
| 744 | wprintw(center, "%d", tmp->vpid); |
| 745 | print_key_title("VTID", line++); |
| 746 | wprintw(center, "%d", tmp->vtid); |
| 747 | print_key_title("VPPID", line++); |
| 748 | wprintw(center, "%d", tmp->vppid); |
| 749 | print_key_title("CPU", line++); |
| 750 | wprintw(center, "%1.2f %%", tmp->totalcpunsec/maxcputime); |
| 751 | |
| 752 | print_key_title("READ B/s", line++); |
| 753 | scale_unit(tmp->fileread, unit); |
| 754 | wprintw(center, "%s", unit); |
| 755 | |
| 756 | print_key_title("WRITE B/s", line++); |
| 757 | scale_unit(tmp->filewrite, unit); |
| 758 | wprintw(center, "%s", unit); |
| 759 | |
| 760 | g_hash_table_iter_init(&iter, global_perf_liszt); |
| 761 | while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) { |
| 762 | print_key_title((char *) key, line++); |
| 763 | perfn2 = g_hash_table_lookup(tmp->perf, (char *) key); |
| 764 | wprintw(center, "%d", perfn2 ? perfn2->count : 0); |
| 765 | } |
| 766 | line++; |
| 767 | |
| 768 | wattron(center, A_BOLD); |
| 769 | column = 1; |
| 770 | for (i = 0; i < 3; i++) { |
| 771 | if (fileview[i].sort) { |
| 772 | pref_current_sort = i; |
| 773 | wattron(center, A_UNDERLINE); |
| 774 | } |
| 775 | mvwprintw(center, line, column, fileview[i].title); |
| 776 | wattroff(center, A_UNDERLINE); |
| 777 | column += 10; |
| 778 | } |
| 779 | mvwprintw(center, line++, column, "FILENAME"); |
| 780 | wattroff(center, A_BOLD); |
| 781 | |
| 782 | /* |
| 783 | * since the process_files_table array could contain NULL file structures, |
| 784 | * and that the positions inside the array is important (it is the FD), we |
| 785 | * need to create a temporary array that we can sort. |
| 786 | */ |
| 787 | for (i = 0; i < tmp->process_files_table->len; i++) { |
| 788 | file_tmp = g_ptr_array_index(tmp->process_files_table, i); |
| 789 | if (file_tmp) |
| 790 | g_ptr_array_add(newfilearray, file_tmp); |
| 791 | } |
| 792 | |
| 793 | if (fileview[0].sort == 1) |
| 794 | g_ptr_array_sort(newfilearray, sort_by_file_fd_desc); |
| 795 | else if (fileview[1].sort == 1) |
| 796 | g_ptr_array_sort(newfilearray, sort_by_file_read_desc); |
| 797 | else if (fileview[2].sort == 1) |
| 798 | g_ptr_array_sort(newfilearray, sort_by_file_write_desc); |
| 799 | else |
| 800 | g_ptr_array_sort(newfilearray, sort_by_file_read_desc); |
| 801 | |
| 802 | for (i = selected_line; i < newfilearray->len && |
| 803 | i < (selected_line + max_center_lines - line + 2); i++) { |
| 804 | file_tmp = g_ptr_array_index(newfilearray, i); |
| 805 | if (!file_tmp) |
| 806 | continue; |
| 807 | mvwprintw(center, line + j, 1, "%d", file_tmp->fd); |
| 808 | scale_unit(file_tmp->read, unit); |
| 809 | mvwprintw(center, line + j, 11, "%s", unit); |
| 810 | scale_unit(file_tmp->write, unit); |
| 811 | mvwprintw(center, line + j, 21, "%s", unit); |
| 812 | snprintf(filename_buf, COLS - 25, "%s", file_tmp->name); |
| 813 | mvwprintw(center, line + j, 31, "%s", filename_buf); |
| 814 | j++; |
| 815 | } |
| 816 | g_ptr_array_free(newfilearray, TRUE); |
| 817 | } |
| 818 | |
| 819 | void update_perf() |
| 820 | { |
| 821 | int i; |
| 822 | int nblinedisplayed = 0; |
| 823 | int current_line = 0; |
| 824 | struct processtop *tmp; |
| 825 | int header_offset = 2; |
| 826 | int perf_row = 40; |
| 827 | struct perfcounter *perfn1, *perfn2; |
| 828 | char *perf_key = NULL; |
| 829 | int value; |
| 830 | GHashTableIter iter; |
| 831 | gpointer key; |
| 832 | |
| 833 | set_window_title(center, "Perf Top"); |
| 834 | wattron(center, A_BOLD); |
| 835 | mvwprintw(center, 1, 1, "PID"); |
| 836 | mvwprintw(center, 1, 11, "TID"); |
| 837 | mvwprintw(center, 1, 22, "NAME"); |
| 838 | |
| 839 | perf_row = 40; |
| 840 | g_hash_table_iter_init(&iter, global_perf_liszt); |
| 841 | while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) { |
| 842 | if (perfn1->visible) { |
| 843 | if (perfn1->sort) { |
| 844 | /* pref_current_sort = i; */ |
| 845 | wattron(center, A_UNDERLINE); |
| 846 | } |
| 847 | /* + 5 to strip the "perf_" prefix */ |
| 848 | mvwprintw(center, 1, perf_row, "%s", |
| 849 | (char *) key + 5); |
| 850 | wattroff(center, A_UNDERLINE); |
| 851 | perf_row += 20; |
| 852 | } |
| 853 | if (perfn1->sort) { |
| 854 | perf_key = (char *) key; |
| 855 | } |
| 856 | } |
| 857 | wattroff(center, A_BOLD); |
| 858 | |
| 859 | g_ptr_array_sort_with_data(data->process_table, sort_perf, perf_key); |
| 860 | |
| 861 | for (i = 0; i < data->process_table->len && |
| 862 | nblinedisplayed < max_center_lines; i++) { |
| 863 | tmp = g_ptr_array_index(data->process_table, i); |
| 864 | |
| 865 | if (!opt_tid && (opt_hostname && !lookup_hostname_list(tmp->hostname))) |
| 866 | continue; |
| 867 | if (!opt_hostname && (opt_tid && !lookup_tid_list(tmp->pid))) |
| 868 | continue; |
| 869 | if ((opt_tid && !lookup_tid_list(tmp->tid)) && |
| 870 | (opt_hostname && !lookup_hostname_list(tmp->hostname))) |
| 871 | continue; |
| 872 | |
| 873 | if (tmp->pid != tmp->tid) |
| 874 | if (toggle_threads == -1) |
| 875 | continue; |
| 876 | |
| 877 | if (process_selected(tmp)) { |
| 878 | wattron(center, COLOR_PAIR(6)); |
| 879 | mvwhline(center, current_line + header_offset, 1, ' ', COLS-3); |
| 880 | } |
| 881 | if (current_line == selected_line) { |
| 882 | selected_process = tmp; |
| 883 | wattron(center, COLOR_PAIR(5)); |
| 884 | mvwhline(center, current_line + header_offset, 1, ' ', COLS-3); |
| 885 | } |
| 886 | |
| 887 | mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid); |
| 888 | mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid); |
| 889 | mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm); |
| 890 | |
| 891 | g_hash_table_iter_init(&iter, global_perf_liszt); |
| 892 | |
| 893 | perf_row = 40; |
| 894 | while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfn1)) { |
| 895 | if (perfn1->visible) { |
| 896 | perfn2 = g_hash_table_lookup(tmp->perf, (char *) key); |
| 897 | if (perfn2) |
| 898 | value = perfn2->count; |
| 899 | else |
| 900 | value = 0; |
| 901 | mvwprintw(center, current_line + header_offset, |
| 902 | perf_row, "%d", value); |
| 903 | perf_row += 20; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | wattroff(center, COLOR_PAIR(6)); |
| 908 | wattroff(center, COLOR_PAIR(5)); |
| 909 | nblinedisplayed++; |
| 910 | current_line++; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | void update_iostream() |
| 915 | { |
| 916 | int i; |
| 917 | int header_offset = 2; |
| 918 | struct processtop *tmp; |
| 919 | int nblinedisplayed = 0; |
| 920 | int current_line = 0; |
| 921 | int total = 0; |
| 922 | char unit[4]; |
| 923 | int column; |
| 924 | |
| 925 | set_window_title(center, "IO Top"); |
| 926 | wattron(center, A_BOLD); |
| 927 | mvwprintw(center, 1, 1, "PID"); |
| 928 | mvwprintw(center, 1, 11, "TID"); |
| 929 | mvwprintw(center, 1, 22, "NAME"); |
| 930 | column = 40; |
| 931 | for (i = 0; i < 3; i++) { |
| 932 | if (iostreamtopview[i].sort) { |
| 933 | pref_current_sort = i; |
| 934 | wattron(center, A_UNDERLINE); |
| 935 | } |
| 936 | mvwprintw(center, 1, column, iostreamtopview[i].title); |
| 937 | wattroff(center, A_UNDERLINE); |
| 938 | column += 12; |
| 939 | } |
| 940 | wattroff(center, A_BOLD); |
| 941 | wattroff(center, A_UNDERLINE); |
| 942 | |
| 943 | if (iostreamtopview[0].sort == 1) |
| 944 | g_ptr_array_sort(data->process_table, sort_by_process_read_desc); |
| 945 | else if (iostreamtopview[1].sort == 1) |
| 946 | g_ptr_array_sort(data->process_table, sort_by_process_write_desc); |
| 947 | else if (iostreamtopview[2].sort == 1) |
| 948 | g_ptr_array_sort(data->process_table, sort_by_process_total_desc); |
| 949 | else |
| 950 | g_ptr_array_sort(data->process_table, sort_by_process_total_desc); |
| 951 | |
| 952 | for (i = list_offset; i < data->process_table->len && |
| 953 | nblinedisplayed < max_center_lines; i++) { |
| 954 | tmp = g_ptr_array_index(data->process_table, i); |
| 955 | |
| 956 | if (!opt_tid && (opt_hostname && !lookup_hostname_list(tmp->hostname))) |
| 957 | continue; |
| 958 | if (!opt_hostname && (opt_tid && !lookup_tid_list(tmp->pid))) |
| 959 | continue; |
| 960 | if ((opt_tid && !lookup_tid_list(tmp->tid)) && |
| 961 | (opt_hostname && !lookup_hostname_list(tmp->hostname))) |
| 962 | continue; |
| 963 | |
| 964 | if (tmp->pid != tmp->tid) |
| 965 | if (toggle_threads == -1) |
| 966 | continue; |
| 967 | |
| 968 | if (process_selected(tmp)) { |
| 969 | wattron(center, COLOR_PAIR(6)); |
| 970 | mvwhline(center, current_line + header_offset, 1, ' ', COLS-3); |
| 971 | } |
| 972 | if (current_line == selected_line) { |
| 973 | selected_process = tmp; |
| 974 | wattron(center, COLOR_PAIR(5)); |
| 975 | mvwhline(center, current_line + header_offset, 1, ' ', COLS-3); |
| 976 | } |
| 977 | /* TGID */ |
| 978 | mvwprintw(center, current_line + header_offset, 1, "%d", tmp->pid); |
| 979 | /* PID */ |
| 980 | mvwprintw(center, current_line + header_offset, 11, "%d", tmp->tid); |
| 981 | /* NAME */ |
| 982 | mvwprintw(center, current_line + header_offset, 22, "%s", tmp->comm); |
| 983 | |
| 984 | /* READ (bytes/sec) */ |
| 985 | scale_unit(tmp->fileread, unit); |
| 986 | mvwprintw(center, current_line + header_offset, 40, "%s", unit); |
| 987 | |
| 988 | /* WRITE (bytes/sec) */ |
| 989 | scale_unit(tmp->filewrite, unit); |
| 990 | mvwprintw(center, current_line + header_offset, 52, "%s", unit); |
| 991 | |
| 992 | /* TOTAL STREAM */ |
| 993 | total = tmp->totalfileread + tmp->totalfilewrite; |
| 994 | |
| 995 | scale_unit(total, unit); |
| 996 | mvwprintw(center, current_line + header_offset, 64, "%s", unit); |
| 997 | |
| 998 | wattroff(center, COLOR_PAIR(6)); |
| 999 | wattroff(center, COLOR_PAIR(5)); |
| 1000 | nblinedisplayed++; |
| 1001 | current_line++; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | void update_current_view() |
| 1006 | { |
| 1007 | sem_wait(&update_display_sem); |
| 1008 | if (!data) |
| 1009 | return; |
| 1010 | update_header(); |
| 1011 | |
| 1012 | werase(center); |
| 1013 | box(center, 0, 0); |
| 1014 | switch (current_view) { |
| 1015 | case cpu: |
| 1016 | update_cputop_display(); |
| 1017 | break; |
| 1018 | case perf: |
| 1019 | update_perf(); |
| 1020 | break; |
| 1021 | case process_details: |
| 1022 | update_process_details(); |
| 1023 | break; |
| 1024 | case iostream: |
| 1025 | update_iostream(); |
| 1026 | break; |
| 1027 | case tree: |
| 1028 | update_cputop_display(); |
| 1029 | break; |
| 1030 | case kprobes: |
| 1031 | update_kprobes_display(); |
| 1032 | break; |
| 1033 | default: |
| 1034 | break; |
| 1035 | } |
| 1036 | update_panels(); |
| 1037 | doupdate(); |
| 1038 | sem_post(&update_display_sem); |
| 1039 | } |
| 1040 | |
| 1041 | void update_process_detail_sort(int *line_selected) |
| 1042 | { |
| 1043 | int i; |
| 1044 | int size; |
| 1045 | |
| 1046 | size = 3; |
| 1047 | |
| 1048 | if (*line_selected > (size - 1)) |
| 1049 | *line_selected = size - 1; |
| 1050 | else if (*line_selected < 0) |
| 1051 | *line_selected = 0; |
| 1052 | |
| 1053 | if (fileview[*line_selected].sort == 1) |
| 1054 | fileview[*line_selected].reverse = 1; |
| 1055 | for (i = 0; i < size; i++) |
| 1056 | fileview[i].sort = 0; |
| 1057 | fileview[*line_selected].sort = 1; |
| 1058 | } |
| 1059 | |
| 1060 | void update_process_detail_pref(int *line_selected, int toggle_view, int toggle_sort) |
| 1061 | { |
| 1062 | int i; |
| 1063 | int size; |
| 1064 | |
| 1065 | if (!data) |
| 1066 | return; |
| 1067 | if (pref_panel_window) { |
| 1068 | del_panel(pref_panel); |
| 1069 | delwin(pref_panel_window); |
| 1070 | } |
| 1071 | size = 3; |
| 1072 | |
| 1073 | pref_panel_window = create_window(size + 2, 30, 10, 10); |
| 1074 | pref_panel = new_panel(pref_panel_window); |
| 1075 | |
| 1076 | werase(pref_panel_window); |
| 1077 | box(pref_panel_window, 0 , 0); |
| 1078 | set_window_title(pref_panel_window, "Process Detail Preferences "); |
| 1079 | wattron(pref_panel_window, A_BOLD); |
| 1080 | mvwprintw(pref_panel_window, size + 1, 1, |
| 1081 | " 's' : sort, space : toggle"); |
| 1082 | wattroff(pref_panel_window, A_BOLD); |
| 1083 | |
| 1084 | if (*line_selected > (size - 1)) |
| 1085 | *line_selected = size - 1; |
| 1086 | else if (*line_selected < 0) |
| 1087 | *line_selected = 0; |
| 1088 | if (toggle_sort == 1) { |
| 1089 | update_process_detail_sort(line_selected); |
| 1090 | update_current_view(); |
| 1091 | } |
| 1092 | |
| 1093 | for (i = 0; i < size; i++) { |
| 1094 | if (i == *line_selected) { |
| 1095 | wattron(pref_panel_window, COLOR_PAIR(5)); |
| 1096 | mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2); |
| 1097 | } |
| 1098 | if (fileview[i].sort == 1) |
| 1099 | wattron(pref_panel_window, A_BOLD); |
| 1100 | mvwprintw(pref_panel_window, i + 1, 1, "[-] %s", |
| 1101 | fileview[i].title); |
| 1102 | wattroff(pref_panel_window, A_BOLD); |
| 1103 | wattroff(pref_panel_window, COLOR_PAIR(5)); |
| 1104 | |
| 1105 | } |
| 1106 | update_panels(); |
| 1107 | doupdate(); |
| 1108 | } |
| 1109 | |
| 1110 | void update_iostream_sort(int *line_selected) |
| 1111 | { |
| 1112 | int i; |
| 1113 | int size; |
| 1114 | |
| 1115 | size = 3; |
| 1116 | if (*line_selected > (size - 1)) |
| 1117 | *line_selected = size - 1; |
| 1118 | else if (*line_selected < 0) |
| 1119 | *line_selected = 0; |
| 1120 | if (iostreamtopview[*line_selected].sort == 1) |
| 1121 | iostreamtopview[*line_selected].reverse = 1; |
| 1122 | for (i = 0; i < size; i++) |
| 1123 | iostreamtopview[i].sort = 0; |
| 1124 | iostreamtopview[*line_selected].sort = 1; |
| 1125 | |
| 1126 | } |
| 1127 | |
| 1128 | void update_iostream_pref(int *line_selected, int toggle_view, int toggle_sort) |
| 1129 | { |
| 1130 | int i; |
| 1131 | int size; |
| 1132 | |
| 1133 | if (!data) |
| 1134 | return; |
| 1135 | if (pref_panel_window) { |
| 1136 | del_panel(pref_panel); |
| 1137 | delwin(pref_panel_window); |
| 1138 | } |
| 1139 | size = 3; |
| 1140 | |
| 1141 | pref_panel_window = create_window(size + 2, 30, 10, 10); |
| 1142 | pref_panel = new_panel(pref_panel_window); |
| 1143 | |
| 1144 | werase(pref_panel_window); |
| 1145 | box(pref_panel_window, 0 , 0); |
| 1146 | set_window_title(pref_panel_window, "IOTop Preferences "); |
| 1147 | wattron(pref_panel_window, A_BOLD); |
| 1148 | mvwprintw(pref_panel_window, size + 1, 1, |
| 1149 | " 's' : sort, space : toggle"); |
| 1150 | wattroff(pref_panel_window, A_BOLD); |
| 1151 | |
| 1152 | if (*line_selected > (size - 1)) |
| 1153 | *line_selected = size - 1; |
| 1154 | else if (*line_selected < 0) |
| 1155 | *line_selected = 0; |
| 1156 | if (toggle_sort == 1) { |
| 1157 | update_iostream_sort(line_selected); |
| 1158 | update_current_view(); |
| 1159 | } |
| 1160 | |
| 1161 | for (i = 0; i < size; i++) { |
| 1162 | if (i == *line_selected) { |
| 1163 | wattron(pref_panel_window, COLOR_PAIR(5)); |
| 1164 | mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2); |
| 1165 | } |
| 1166 | if (iostreamtopview[i].sort == 1) |
| 1167 | wattron(pref_panel_window, A_BOLD); |
| 1168 | mvwprintw(pref_panel_window, i + 1, 1, "[-] %s", |
| 1169 | iostreamtopview[i].title); |
| 1170 | wattroff(pref_panel_window, A_BOLD); |
| 1171 | wattroff(pref_panel_window, COLOR_PAIR(5)); |
| 1172 | |
| 1173 | } |
| 1174 | update_panels(); |
| 1175 | doupdate(); |
| 1176 | } |
| 1177 | |
| 1178 | void update_cpu_sort(int *line_selected) |
| 1179 | { |
| 1180 | int i; |
| 1181 | int size = 3; |
| 1182 | |
| 1183 | if (*line_selected > (size - 1)) |
| 1184 | *line_selected = size - 1; |
| 1185 | else if (*line_selected < 0) |
| 1186 | *line_selected = 0; |
| 1187 | |
| 1188 | /* special case, we don't support sorting by procname for now */ |
| 1189 | if (*line_selected != 3) { |
| 1190 | if (cputopview[*line_selected].sort == 1) |
| 1191 | cputopview[*line_selected].reverse = 1; |
| 1192 | for (i = 0; i < size; i++) |
| 1193 | cputopview[i].sort = 0; |
| 1194 | cputopview[*line_selected].sort = 1; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | void update_cpu_pref(int *line_selected, int toggle_view, int toggle_sort) |
| 1199 | { |
| 1200 | int i; |
| 1201 | int size; |
| 1202 | |
| 1203 | if (!data) |
| 1204 | return; |
| 1205 | if (pref_panel_window) { |
| 1206 | del_panel(pref_panel); |
| 1207 | delwin(pref_panel_window); |
| 1208 | } |
| 1209 | size = 4; |
| 1210 | |
| 1211 | pref_panel_window = create_window(size + 2, 30, 10, 10); |
| 1212 | pref_panel = new_panel(pref_panel_window); |
| 1213 | |
| 1214 | werase(pref_panel_window); |
| 1215 | box(pref_panel_window, 0 , 0); |
| 1216 | set_window_title(pref_panel_window, "CPUTop Preferences "); |
| 1217 | wattron(pref_panel_window, A_BOLD); |
| 1218 | mvwprintw(pref_panel_window, size + 1, 1, |
| 1219 | " 's' : sort, space : toggle"); |
| 1220 | wattroff(pref_panel_window, A_BOLD); |
| 1221 | |
| 1222 | if (*line_selected > (size - 1)) |
| 1223 | *line_selected = size - 1; |
| 1224 | else if (*line_selected < 0) |
| 1225 | *line_selected = 0; |
| 1226 | if (toggle_sort == 1) { |
| 1227 | update_cpu_sort(line_selected); |
| 1228 | update_current_view(); |
| 1229 | } |
| 1230 | |
| 1231 | for (i = 0; i < size; i++) { |
| 1232 | if (i == *line_selected) { |
| 1233 | wattron(pref_panel_window, COLOR_PAIR(5)); |
| 1234 | mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2); |
| 1235 | } |
| 1236 | if (cputopview[i].sort == 1) |
| 1237 | wattron(pref_panel_window, A_BOLD); |
| 1238 | mvwprintw(pref_panel_window, i + 1, 1, "[-] %s", |
| 1239 | cputopview[i].title); |
| 1240 | wattroff(pref_panel_window, A_BOLD); |
| 1241 | wattroff(pref_panel_window, COLOR_PAIR(5)); |
| 1242 | |
| 1243 | } |
| 1244 | update_panels(); |
| 1245 | doupdate(); |
| 1246 | } |
| 1247 | |
| 1248 | void update_perf_sort(int *line_selected) |
| 1249 | { |
| 1250 | int i; |
| 1251 | struct perfcounter *perf; |
| 1252 | GList *perflist; |
| 1253 | int size; |
| 1254 | |
| 1255 | size = g_hash_table_size(global_perf_liszt); |
| 1256 | if (*line_selected > (size - 1)) |
| 1257 | *line_selected = size - 1; |
| 1258 | else if (*line_selected < 0) |
| 1259 | *line_selected = 0; |
| 1260 | |
| 1261 | i = 0; |
| 1262 | perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt)); |
| 1263 | while (perflist) { |
| 1264 | perf = g_hash_table_lookup(global_perf_liszt, perflist->data); |
| 1265 | if (i != *line_selected) |
| 1266 | perf->sort = 0; |
| 1267 | else |
| 1268 | perf->sort = 1; |
| 1269 | i++; |
| 1270 | perflist = g_list_next(perflist); |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | void update_perf_pref(int *line_selected, int toggle_view, int toggle_sort) |
| 1275 | { |
| 1276 | int i; |
| 1277 | struct perfcounter *perf; |
| 1278 | GList *perflist; |
| 1279 | int size; |
| 1280 | |
| 1281 | if (!data) |
| 1282 | return; |
| 1283 | if (pref_panel_window) { |
| 1284 | del_panel(pref_panel); |
| 1285 | delwin(pref_panel_window); |
| 1286 | } |
| 1287 | size = g_hash_table_size(global_perf_liszt); |
| 1288 | |
| 1289 | pref_panel_window = create_window(size + 2, 30, 10, 10); |
| 1290 | pref_panel = new_panel(pref_panel_window); |
| 1291 | |
| 1292 | werase(pref_panel_window); |
| 1293 | box(pref_panel_window, 0 , 0); |
| 1294 | set_window_title(pref_panel_window, "Perf Preferences "); |
| 1295 | wattron(pref_panel_window, A_BOLD); |
| 1296 | mvwprintw(pref_panel_window, g_hash_table_size(global_perf_liszt) + 1, 1, |
| 1297 | " 's' : sort, space : toggle"); |
| 1298 | wattroff(pref_panel_window, A_BOLD); |
| 1299 | |
| 1300 | if (*line_selected > (size - 1)) |
| 1301 | *line_selected = size - 1; |
| 1302 | else if (*line_selected < 0) |
| 1303 | *line_selected = 0; |
| 1304 | |
| 1305 | if (toggle_sort == 1) { |
| 1306 | update_perf_sort(line_selected); |
| 1307 | update_current_view(); |
| 1308 | } |
| 1309 | |
| 1310 | i = 0; |
| 1311 | perflist = g_list_first(g_hash_table_get_keys(global_perf_liszt)); |
| 1312 | while (perflist) { |
| 1313 | perf = g_hash_table_lookup(global_perf_liszt, perflist->data); |
| 1314 | if (i == *line_selected && toggle_view == 1) { |
| 1315 | perf->visible = perf->visible == 1 ? 0:1; |
| 1316 | update_current_view(); |
| 1317 | } |
| 1318 | if (i == *line_selected) { |
| 1319 | wattron(pref_panel_window, COLOR_PAIR(5)); |
| 1320 | mvwhline(pref_panel_window, i + 1, 1, ' ', 30 - 2); |
| 1321 | } |
| 1322 | if (perf->sort == 1) |
| 1323 | wattron(pref_panel_window, A_BOLD); |
| 1324 | mvwprintw(pref_panel_window, i + 1, 1, "[%c] %s", |
| 1325 | perf->visible == 1 ? 'x' : ' ', |
| 1326 | (char *) perflist->data + 5); |
| 1327 | wattroff(pref_panel_window, A_BOLD); |
| 1328 | wattroff(pref_panel_window, COLOR_PAIR(5)); |
| 1329 | i++; |
| 1330 | perflist = g_list_next(perflist); |
| 1331 | } |
| 1332 | update_panels(); |
| 1333 | doupdate(); |
| 1334 | } |
| 1335 | |
| 1336 | int update_preference_panel(int *line_selected, int toggle_view, int toggle_sort) |
| 1337 | { |
| 1338 | int ret = 0; |
| 1339 | |
| 1340 | switch(current_view) { |
| 1341 | case perf: |
| 1342 | update_perf_pref(line_selected, toggle_view, toggle_sort); |
| 1343 | break; |
| 1344 | case cpu: |
| 1345 | update_cpu_pref(line_selected, toggle_view, toggle_sort); |
| 1346 | break; |
| 1347 | case iostream: |
| 1348 | update_iostream_pref(line_selected, toggle_view, toggle_sort); |
| 1349 | break; |
| 1350 | case process_details: |
| 1351 | update_process_detail_pref(line_selected, toggle_view, toggle_sort); |
| 1352 | break; |
| 1353 | default: |
| 1354 | ret = -1; |
| 1355 | break; |
| 1356 | } |
| 1357 | |
| 1358 | return ret; |
| 1359 | } |
| 1360 | |
| 1361 | int update_sort(int *line_selected) |
| 1362 | { |
| 1363 | int ret = 0; |
| 1364 | |
| 1365 | switch(current_view) { |
| 1366 | case perf: |
| 1367 | update_perf_sort(line_selected); |
| 1368 | break; |
| 1369 | case cpu: |
| 1370 | update_cpu_sort(line_selected); |
| 1371 | break; |
| 1372 | case iostream: |
| 1373 | update_iostream_sort(line_selected); |
| 1374 | break; |
| 1375 | case process_details: |
| 1376 | update_process_detail_sort(line_selected); |
| 1377 | break; |
| 1378 | default: |
| 1379 | ret = -1; |
| 1380 | break; |
| 1381 | } |
| 1382 | |
| 1383 | return ret; |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | void toggle_pref_panel(void) |
| 1388 | { |
| 1389 | int ret; |
| 1390 | |
| 1391 | if (pref_panel_visible) { |
| 1392 | hide_panel(pref_panel); |
| 1393 | pref_panel_visible = 0; |
| 1394 | } else { |
| 1395 | ret = update_preference_panel(&pref_line_selected, 0, 0); |
| 1396 | if (ret < 0) |
| 1397 | return; |
| 1398 | show_panel(pref_panel); |
| 1399 | pref_panel_visible = 1; |
| 1400 | } |
| 1401 | update_panels(); |
| 1402 | doupdate(); |
| 1403 | } |
| 1404 | |
| 1405 | void display(unsigned int index) |
| 1406 | { |
| 1407 | last_display_index = index; |
| 1408 | currently_displayed_index = index; |
| 1409 | data = g_ptr_array_index(copies, index); |
| 1410 | if (!data) |
| 1411 | return; |
| 1412 | max_elements = data->process_table->len; |
| 1413 | update_current_view(); |
| 1414 | update_footer(); |
| 1415 | update_panels(); |
| 1416 | doupdate(); |
| 1417 | } |
| 1418 | |
| 1419 | void pause_display() |
| 1420 | { |
| 1421 | toggle_pause = 1; |
| 1422 | print_log("Pause"); |
| 1423 | sem_wait(&pause_sem); |
| 1424 | } |
| 1425 | |
| 1426 | void resume_display() |
| 1427 | { |
| 1428 | toggle_pause = -1; |
| 1429 | print_log("Resume"); |
| 1430 | sem_post(&pause_sem); |
| 1431 | } |
| 1432 | |
| 1433 | void *handle_keyboard(void *p) |
| 1434 | { |
| 1435 | int ch; |
| 1436 | while((ch = getch())) { |
| 1437 | switch(ch) { |
| 1438 | /* Move the cursor and scroll */ |
| 1439 | case 'j': |
| 1440 | case KEY_DOWN: |
| 1441 | if (pref_panel_visible) { |
| 1442 | pref_line_selected++; |
| 1443 | update_preference_panel(&pref_line_selected, 0, 0); |
| 1444 | } else { |
| 1445 | if (selected_line < (max_center_lines - 1) && |
| 1446 | selected_line < max_elements - 1) { |
| 1447 | selected_line++; |
| 1448 | selected_in_list++; |
| 1449 | } else if (selected_in_list < (max_elements - 1) |
| 1450 | && (list_offset < (max_elements - max_center_lines))) { |
| 1451 | selected_in_list++; |
| 1452 | list_offset++; |
| 1453 | } |
| 1454 | update_current_view(); |
| 1455 | } |
| 1456 | break; |
| 1457 | case KEY_NPAGE: |
| 1458 | break; |
| 1459 | case 'k': |
| 1460 | case KEY_UP: |
| 1461 | if (pref_panel_visible) { |
| 1462 | if (pref_line_selected > 0) |
| 1463 | pref_line_selected--; |
| 1464 | update_preference_panel(&pref_line_selected, 0, 0); |
| 1465 | } else { |
| 1466 | if (selected_line > 0) { |
| 1467 | selected_line--; |
| 1468 | selected_in_list--; |
| 1469 | } else if (selected_in_list > 0 && list_offset > 0) { |
| 1470 | selected_in_list--; |
| 1471 | list_offset--; |
| 1472 | } |
| 1473 | update_current_view(); |
| 1474 | } |
| 1475 | break; |
| 1476 | case KEY_PPAGE: |
| 1477 | break; |
| 1478 | |
| 1479 | /* Navigate the history with arrows */ |
| 1480 | case KEY_LEFT: |
| 1481 | if (currently_displayed_index > 0) { |
| 1482 | currently_displayed_index--; |
| 1483 | print_log("Going back in time"); |
| 1484 | } else { |
| 1485 | print_log("Cannot rewind, last data is already displayed"); |
| 1486 | } |
| 1487 | data = g_ptr_array_index(copies, currently_displayed_index); |
| 1488 | max_elements = data->process_table->len; |
| 1489 | |
| 1490 | /* we force to pause the display when moving in time */ |
| 1491 | if (toggle_pause < 0) |
| 1492 | pause_display(); |
| 1493 | |
| 1494 | update_current_view(); |
| 1495 | update_footer(); |
| 1496 | break; |
| 1497 | case KEY_RIGHT: |
| 1498 | if (currently_displayed_index < last_display_index) { |
| 1499 | currently_displayed_index++; |
| 1500 | print_log("Going forward in time"); |
| 1501 | data = g_ptr_array_index(copies, currently_displayed_index); |
| 1502 | max_elements = data->process_table->len; |
| 1503 | update_current_view(); |
| 1504 | update_footer(); |
| 1505 | } else { |
| 1506 | print_log("Manually moving forward"); |
| 1507 | sem_post(&timer); |
| 1508 | if (toggle_pause > 0) { |
| 1509 | sem_post(&pause_sem); |
| 1510 | update_current_view(); |
| 1511 | sem_wait(&pause_sem); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | break; |
| 1516 | case ' ': |
| 1517 | if (pref_panel_visible) { |
| 1518 | update_preference_panel(&pref_line_selected, 1, 0); |
| 1519 | } else { |
| 1520 | update_selected_processes(); |
| 1521 | update_current_view(); |
| 1522 | } |
| 1523 | break; |
| 1524 | case 's': |
| 1525 | if (pref_panel_visible) |
| 1526 | update_preference_panel(&pref_line_selected, 0, 1); |
| 1527 | break; |
| 1528 | case '>': |
| 1529 | /* perf uses a hashtable, it is ordered backward */ |
| 1530 | if (current_view == perf) { |
| 1531 | pref_current_sort--; |
| 1532 | } else if (!pref_panel_visible) { |
| 1533 | pref_current_sort++; |
| 1534 | } |
| 1535 | update_sort(&pref_current_sort); |
| 1536 | update_current_view(); |
| 1537 | break; |
| 1538 | case '<': |
| 1539 | /* perf uses a hashtable, it is ordered backward */ |
| 1540 | if (current_view == perf) { |
| 1541 | pref_current_sort++; |
| 1542 | } else if (!pref_panel_visible) { |
| 1543 | pref_current_sort--; |
| 1544 | } |
| 1545 | update_sort(&pref_current_sort); |
| 1546 | update_current_view(); |
| 1547 | break; |
| 1548 | |
| 1549 | case 13: /* FIXME : KEY_ENTER ?? */ |
| 1550 | if (pref_panel_visible) |
| 1551 | break; |
| 1552 | if (current_view != process_details) { |
| 1553 | previous_view = current_view; |
| 1554 | current_view = process_details; |
| 1555 | } else { |
| 1556 | current_view = previous_view; |
| 1557 | previous_view = process_details; |
| 1558 | } |
| 1559 | selected_line = 0; |
| 1560 | update_current_view(); |
| 1561 | break; |
| 1562 | |
| 1563 | case KEY_F(1): |
| 1564 | if (pref_panel_visible) |
| 1565 | toggle_pref_panel(); |
| 1566 | current_view = cpu; |
| 1567 | selected_line = 0; |
| 1568 | update_current_view(); |
| 1569 | break; |
| 1570 | case KEY_F(2): |
| 1571 | if (pref_panel_visible) |
| 1572 | toggle_pref_panel(); |
| 1573 | current_view = cpu; |
| 1574 | selected_line = 0; |
| 1575 | update_current_view(); |
| 1576 | break; |
| 1577 | case KEY_F(3): |
| 1578 | if (pref_panel_visible) |
| 1579 | toggle_pref_panel(); |
| 1580 | current_view = perf; |
| 1581 | selected_line = 0; |
| 1582 | update_current_view(); |
| 1583 | break; |
| 1584 | case KEY_F(4): |
| 1585 | if (pref_panel_visible) |
| 1586 | toggle_pref_panel(); |
| 1587 | current_view = iostream; |
| 1588 | selected_line = 0; |
| 1589 | update_current_view(); |
| 1590 | break; |
| 1591 | case KEY_F(5): |
| 1592 | if (pref_panel_visible) |
| 1593 | toggle_pref_panel(); |
| 1594 | current_view = kprobes; |
| 1595 | selected_line = 0; |
| 1596 | update_current_view(); |
| 1597 | break; |
| 1598 | case KEY_F(10): |
| 1599 | case 'q': |
| 1600 | reset_ncurses(); |
| 1601 | /* exit keyboard thread */ |
| 1602 | pthread_exit(0); |
| 1603 | break; |
| 1604 | case 't': |
| 1605 | toggle_threads *= -1; |
| 1606 | update_current_view(); |
| 1607 | break; |
| 1608 | case 'p': |
| 1609 | if (toggle_pause < 0) { |
| 1610 | pause_display(); |
| 1611 | } else { |
| 1612 | resume_display(); |
| 1613 | } |
| 1614 | break; |
| 1615 | case 'r': |
| 1616 | toggle_pref_panel(); |
| 1617 | break; |
| 1618 | case 'v': |
| 1619 | toggle_virt *= -1; |
| 1620 | update_current_view(); |
| 1621 | break; |
| 1622 | /* ESCAPE, but slow to process, don't know why */ |
| 1623 | case 27: |
| 1624 | if (pref_panel_visible) |
| 1625 | toggle_pref_panel(); |
| 1626 | else if (current_view == process_details) { |
| 1627 | current_view = previous_view; |
| 1628 | previous_view = process_details; |
| 1629 | } |
| 1630 | update_current_view(); |
| 1631 | break; |
| 1632 | default: |
| 1633 | if (data) |
| 1634 | update_current_view(); |
| 1635 | break; |
| 1636 | } |
| 1637 | update_footer(); |
| 1638 | } |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | |
| 1642 | void init_view_headers() |
| 1643 | { |
| 1644 | cputopview[0].title = strdup("CPU(%)"); |
| 1645 | cputopview[0].sort = 1; |
| 1646 | cputopview[1].title = strdup("PID"); |
| 1647 | cputopview[2].title = strdup("TID"); |
| 1648 | cputopview[3].title = strdup("VPID"); |
| 1649 | cputopview[4].title = strdup("VTID"); |
| 1650 | cputopview[5].title = strdup("NAME"); |
| 1651 | |
| 1652 | iostreamtopview[0].title = strdup("R (B/sec)"); |
| 1653 | iostreamtopview[1].title = strdup("W (B/sec)"); |
| 1654 | iostreamtopview[2].title = strdup("Total (B)"); |
| 1655 | iostreamtopview[2].sort = 1; |
| 1656 | |
| 1657 | fileview[0].title = strdup("FD"); |
| 1658 | fileview[1].title = strdup("READ"); |
| 1659 | fileview[1].sort = 1; |
| 1660 | fileview[2].title = strdup("WRITE"); |
| 1661 | |
| 1662 | kprobeview[0].title = strdup("NAME"); |
| 1663 | kprobeview[1].title = strdup("HIT"); |
| 1664 | kprobeview[1].sort = 1; |
| 1665 | } |
| 1666 | |
| 1667 | void init_ncurses() |
| 1668 | { |
| 1669 | selected_processes = g_ptr_array_new(); |
| 1670 | sem_init(&update_display_sem, 0, 1); |
| 1671 | init_view_headers(); |
| 1672 | init_screen(); |
| 1673 | |
| 1674 | header = create_window(5, COLS - 1, 0, 0); |
| 1675 | center = create_window(LINES - 5 - 7, COLS - 1, 5, 0); |
| 1676 | status = create_window(MAX_LOG_LINES + 2, COLS - 1, LINES - 7, 0); |
| 1677 | footer = create_window(1, COLS - 1, LINES - 1, 0); |
| 1678 | |
| 1679 | print_log("Starting display"); |
| 1680 | |
| 1681 | main_panel = new_panel(center); |
| 1682 | |
| 1683 | current_view = cpu; |
| 1684 | |
| 1685 | basic_header(); |
| 1686 | update_footer(); |
| 1687 | |
| 1688 | pthread_create(&keyboard_thread, NULL, handle_keyboard, (void *)NULL); |
| 1689 | } |