Changes in uspace/app/top/top.c [d76a329:9d58539] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/top/top.c
rd76a329 r9d58539 55 55 #define MINUTE 60 56 56 57 typedef enum { 58 OP_TASKS, 59 OP_IPC, 60 OP_EXCS, 61 } op_mode_t; 62 63 static const column_t task_columns[] = { 64 {"taskid", 't', 8}, 65 {"thrds", 'h', 7}, 66 {"resident", 'r', 10}, 67 {"%resi", 'R', 7}, 68 {"virtual", 'v', 9}, 69 {"%virt", 'V', 7}, 70 {"%user", 'U', 7}, 71 {"%kern", 'K', 7}, 72 {"name", 'd', 0}, 73 }; 74 75 enum { 76 TASK_COL_ID = 0, 77 TASK_COL_NUM_THREADS, 78 TASK_COL_RESIDENT, 79 TASK_COL_PERCENT_RESIDENT, 80 TASK_COL_VIRTUAL, 81 TASK_COL_PERCENT_VIRTUAL, 82 TASK_COL_PERCENT_USER, 83 TASK_COL_PERCENT_KERNEL, 84 TASK_COL_NAME, 85 TASK_NUM_COLUMNS, 86 }; 87 88 static const column_t ipc_columns[] = { 89 {"taskid", 't', 8}, 90 {"cls snt", 'c', 9}, 91 {"cls rcv", 'C', 9}, 92 {"ans snt", 'a', 9}, 93 {"ans rcv", 'A', 9}, 94 {"forward", 'f', 9}, 95 {"name", 'd', 0}, 96 }; 97 98 enum { 99 IPC_COL_TASKID = 0, 100 IPC_COL_CLS_SNT, 101 IPC_COL_CLS_RCV, 102 IPC_COL_ANS_SNT, 103 IPC_COL_ANS_RCV, 104 IPC_COL_FORWARD, 105 IPC_COL_NAME, 106 IPC_NUM_COLUMNS, 107 }; 108 109 static const column_t exception_columns[] = { 110 {"exc", 'e', 8}, 111 {"count", 'n', 10}, 112 {"%count", 'N', 8}, 113 {"cycles", 'c', 10}, 114 {"%cycles", 'C', 9}, 115 {"description", 'd', 0}, 116 }; 117 118 enum { 119 EXCEPTION_COL_ID = 0, 120 EXCEPTION_COL_COUNT, 121 EXCEPTION_COL_PERCENT_COUNT, 122 EXCEPTION_COL_CYCLES, 123 EXCEPTION_COL_PERCENT_CYCLES, 124 EXCEPTION_COL_DESCRIPTION, 125 EXCEPTION_NUM_COLUMNS, 126 }; 127 128 screen_mode_t screen_mode = SCREEN_TABLE; 129 static op_mode_t op_mode = OP_TASKS; 130 static size_t sort_column = TASK_COL_PERCENT_USER; 131 static int sort_reverse = -1; 132 static bool excs_all = false; 57 op_mode_t op_mode = OP_TASKS; 58 sort_mode_t sort_mode = SORT_TASK_CYCLES; 59 bool excs_all = false; 133 60 134 61 static const char *read_data(data_t *target) … … 140 67 target->tasks = NULL; 141 68 target->tasks_perc = NULL; 69 target->tasks_map = NULL; 142 70 target->threads = NULL; 143 71 target->exceptions = NULL; … … 148 76 target->ecycles_diff = NULL; 149 77 target->ecount_diff = NULL; 150 target->table.name = NULL;151 target->table.num_columns = 0;152 target->table.columns = NULL;153 target->table.num_fields = 0;154 target->table.fields = NULL;155 78 156 79 /* Get current time */ … … 194 117 if (target->tasks_perc == NULL) 195 118 return "Not enough memory for task utilization"; 119 120 target->tasks_map = 121 (size_t *) calloc(target->tasks_count, sizeof(size_t)); 122 if (target->tasks_map == NULL) 123 return "Not enough memory for task map"; 196 124 197 125 /* Get threads */ … … 361 289 static int cmp_data(void *a, void *b, void *arg) 362 290 { 363 field_t *fa = (field_t *)a + sort_column; 364 field_t *fb = (field_t *)b + sort_column; 365 366 if (fa->type > fb->type) 367 return 1 * sort_reverse; 368 369 if (fa->type < fb->type) 370 return -1 * sort_reverse; 371 372 switch (fa->type) { 373 case FIELD_EMPTY: 374 return 0; 375 case FIELD_UINT_SUFFIX_BIN: /* fallthrough */ 376 case FIELD_UINT_SUFFIX_DEC: /* fallthrough */ 377 case FIELD_UINT: 378 if (fa->uint > fb->uint) 379 return 1 * sort_reverse; 380 if (fa->uint < fb->uint) 381 return -1 * sort_reverse; 382 return 0; 383 case FIELD_PERCENT: 384 if (fa->fixed.upper * fb->fixed.lower 385 > fb->fixed.upper * fa->fixed.lower) 386 return 1 * sort_reverse; 387 if (fa->fixed.upper * fb->fixed.lower 388 < fb->fixed.upper * fa->fixed.lower) 389 return -1 * sort_reverse; 390 return 0; 391 case FIELD_STRING: 392 return str_cmp(fa->string, fb->string) * sort_reverse; 393 } 394 291 size_t ia = *((size_t *) a); 292 size_t ib = *((size_t *) b); 293 data_t *data = (data_t *) arg; 294 295 uint64_t acycles = data->ucycles_diff[ia] + data->kcycles_diff[ia]; 296 uint64_t bcycles = data->ucycles_diff[ib] + data->kcycles_diff[ib]; 297 298 if (acycles > bcycles) 299 return -1; 300 301 if (acycles < bcycles) 302 return 1; 303 395 304 return 0; 396 305 } 397 306 398 static void sort_table(table_t *table) 399 { 400 if (sort_column >= table->num_columns) 401 sort_column = 0; 402 /* stable sort is probably best, so we use gsort */ 403 gsort((void *) table->fields, table->num_fields / table->num_columns, 404 sizeof(field_t) * table->num_columns, cmp_data, NULL); 405 } 406 407 static const char *fill_task_table(data_t *data) 408 { 409 data->table.name = "Tasks"; 410 data->table.num_columns = TASK_NUM_COLUMNS; 411 data->table.columns = task_columns; 412 data->table.num_fields = data->tasks_count * TASK_NUM_COLUMNS; 413 data->table.fields = calloc(data->table.num_fields, 414 sizeof(field_t)); 415 if (data->table.fields == NULL) 416 return "Not enough memory for table fields"; 417 418 field_t *field = data->table.fields; 419 for (size_t i = 0; i < data->tasks_count; i++) { 420 stats_task_t *task = &data->tasks[i]; 421 perc_task_t *perc = &data->tasks_perc[i]; 422 field[TASK_COL_ID].type = FIELD_UINT; 423 field[TASK_COL_ID].uint = task->task_id; 424 field[TASK_COL_NUM_THREADS].type = FIELD_UINT; 425 field[TASK_COL_NUM_THREADS].uint = task->threads; 426 field[TASK_COL_RESIDENT].type = FIELD_UINT_SUFFIX_BIN; 427 field[TASK_COL_RESIDENT].uint = task->resmem; 428 field[TASK_COL_PERCENT_RESIDENT].type = FIELD_PERCENT; 429 field[TASK_COL_PERCENT_RESIDENT].fixed = perc->resmem; 430 field[TASK_COL_VIRTUAL].type = FIELD_UINT_SUFFIX_BIN; 431 field[TASK_COL_VIRTUAL].uint = task->virtmem; 432 field[TASK_COL_PERCENT_VIRTUAL].type = FIELD_PERCENT; 433 field[TASK_COL_PERCENT_VIRTUAL].fixed = perc->virtmem; 434 field[TASK_COL_PERCENT_USER].type = FIELD_PERCENT; 435 field[TASK_COL_PERCENT_USER].fixed = perc->ucycles; 436 field[TASK_COL_PERCENT_KERNEL].type = FIELD_PERCENT; 437 field[TASK_COL_PERCENT_KERNEL].fixed = perc->kcycles; 438 field[TASK_COL_NAME].type = FIELD_STRING; 439 field[TASK_COL_NAME].string = task->name; 440 field += TASK_NUM_COLUMNS; 441 } 442 443 return NULL; 444 } 445 446 static const char *fill_ipc_table(data_t *data) 447 { 448 data->table.name = "IPC"; 449 data->table.num_columns = IPC_NUM_COLUMNS; 450 data->table.columns = ipc_columns; 451 data->table.num_fields = data->tasks_count * IPC_NUM_COLUMNS; 452 data->table.fields = calloc(data->table.num_fields, 453 sizeof(field_t)); 454 if (data->table.fields == NULL) 455 return "Not enough memory for table fields"; 456 457 field_t *field = data->table.fields; 458 for (size_t i = 0; i < data->tasks_count; i++) { 459 field[IPC_COL_TASKID].type = FIELD_UINT; 460 field[IPC_COL_TASKID].uint = data->tasks[i].task_id; 461 field[IPC_COL_CLS_SNT].type = FIELD_UINT_SUFFIX_DEC; 462 field[IPC_COL_CLS_SNT].uint = data->tasks[i].ipc_info.call_sent; 463 field[IPC_COL_CLS_RCV].type = FIELD_UINT_SUFFIX_DEC; 464 field[IPC_COL_CLS_RCV].uint = data->tasks[i].ipc_info.call_received; 465 field[IPC_COL_ANS_SNT].type = FIELD_UINT_SUFFIX_DEC; 466 field[IPC_COL_ANS_SNT].uint = data->tasks[i].ipc_info.answer_sent; 467 field[IPC_COL_ANS_RCV].type = FIELD_UINT_SUFFIX_DEC; 468 field[IPC_COL_ANS_RCV].uint = data->tasks[i].ipc_info.answer_received; 469 field[IPC_COL_FORWARD].type = FIELD_UINT_SUFFIX_DEC; 470 field[IPC_COL_FORWARD].uint = data->tasks[i].ipc_info.forwarded; 471 field[IPC_COL_NAME].type = FIELD_STRING; 472 field[IPC_COL_NAME].string = data->tasks[i].name; 473 field += IPC_NUM_COLUMNS; 474 } 475 476 return NULL; 477 } 478 479 static const char *fill_exception_table(data_t *data) 480 { 481 data->table.name = "Exceptions"; 482 data->table.num_columns = EXCEPTION_NUM_COLUMNS; 483 data->table.columns = exception_columns; 484 data->table.num_fields = data->exceptions_count * 485 EXCEPTION_NUM_COLUMNS; 486 data->table.fields = calloc(data->table.num_fields, sizeof(field_t)); 487 if (data->table.fields == NULL) 488 return "Not enough memory for table fields"; 489 490 field_t *field = data->table.fields; 491 for (size_t i = 0; i < data->exceptions_count; i++) { 492 if (!excs_all && !data->exceptions[i].hot) 493 continue; 494 field[EXCEPTION_COL_ID].type = FIELD_UINT; 495 field[EXCEPTION_COL_ID].uint = data->exceptions[i].id; 496 field[EXCEPTION_COL_COUNT].type = FIELD_UINT_SUFFIX_DEC; 497 field[EXCEPTION_COL_COUNT].uint = data->exceptions[i].count; 498 field[EXCEPTION_COL_PERCENT_COUNT].type = FIELD_PERCENT; 499 field[EXCEPTION_COL_PERCENT_COUNT].fixed = data->exceptions_perc[i].count; 500 field[EXCEPTION_COL_CYCLES].type = FIELD_UINT_SUFFIX_DEC; 501 field[EXCEPTION_COL_CYCLES].uint = data->exceptions[i].cycles; 502 field[EXCEPTION_COL_PERCENT_CYCLES].type = FIELD_PERCENT; 503 field[EXCEPTION_COL_PERCENT_CYCLES].fixed = data->exceptions_perc[i].cycles; 504 field[EXCEPTION_COL_DESCRIPTION].type = FIELD_STRING; 505 field[EXCEPTION_COL_DESCRIPTION].string = data->exceptions[i].desc; 506 field += EXCEPTION_NUM_COLUMNS; 507 } 508 509 /* in case any cold exceptions were ignored */ 510 data->table.num_fields = field - data->table.fields; 511 512 return NULL; 513 } 514 515 static const char *fill_table(data_t *data) 516 { 517 if (data->table.fields != NULL) { 518 free(data->table.fields); 519 data->table.fields = NULL; 520 } 521 522 switch (op_mode) { 523 case OP_TASKS: 524 return fill_task_table(data); 525 case OP_IPC: 526 return fill_ipc_table(data); 527 case OP_EXCS: 528 return fill_exception_table(data); 529 } 530 return NULL; 307 static void sort_data(data_t *data) 308 { 309 size_t i; 310 311 for (i = 0; i < data->tasks_count; i++) 312 data->tasks_map[i] = i; 313 314 qsort((void *) data->tasks_map, data->tasks_count, 315 sizeof(size_t), cmp_data, (void *) data); 531 316 } 532 317 … … 571 356 if (target->ecount_diff != NULL) 572 357 free(target->ecount_diff); 573 574 if (target->table.fields != NULL)575 free(target->table.fields);576 358 } 577 359 … … 585 367 printf("Reading initial data...\n"); 586 368 587 if ((ret = read_data(&data )) != NULL)369 if ((ret = read_data(&data_prev)) != NULL) 588 370 goto out; 589 371 590 372 /* Compute some rubbish to have initialised values */ 591 compute_percentages(&data , &data);373 compute_percentages(&data_prev, &data_prev); 592 374 593 375 /* And paint screen until death */ 594 376 while (true) { 595 377 int c = tgetchar(UPDATE_INTERVAL); 596 597 if (c < 0) { /* timeout */ 598 data_prev = data; 378 if (c < 0) { 599 379 if ((ret = read_data(&data)) != NULL) { 600 free_data(&data _prev);380 free_data(&data); 601 381 goto out; 602 382 } 603 383 604 384 compute_percentages(&data_prev, &data); 385 sort_data(&data); 386 print_data(&data); 605 387 free_data(&data_prev); 606 607 c = -1; 608 } 609 610 if (screen_mode == SCREEN_HELP && c >= 0) { 611 if (c == 'h' || c == '?') 612 c = -1; 613 /* go back to table and handle the key */ 614 screen_mode = SCREEN_TABLE; 615 } 616 617 if (screen_mode == SCREEN_SORT && c >= 0) { 618 for (size_t i = 0; i < data.table.num_columns; i++) { 619 if (data.table.columns[i].key == c) { 620 sort_column = i; 621 screen_mode = SCREEN_TABLE; 388 data_prev = data; 389 390 continue; 391 } 392 393 switch (c) { 394 case 't': 395 print_warning("Showing task statistics"); 396 op_mode = OP_TASKS; 397 break; 398 case 'i': 399 print_warning("Showing IPC statistics"); 400 op_mode = OP_IPC; 401 break; 402 case 'e': 403 print_warning("Showing exception statistics"); 404 op_mode = OP_EXCS; 405 break; 406 case 'h': 407 print_warning("Showing help"); 408 op_mode = OP_HELP; 409 break; 410 case 'q': 411 goto out; 412 case 'a': 413 if (op_mode == OP_EXCS) { 414 excs_all = !excs_all; 415 if (excs_all) 416 print_warning("Showing all exceptions"); 417 else 418 print_warning("Showing only hot exceptions"); 419 break; 622 420 } 623 } 624 625 c = -1; 626 } 627 628 switch (c) { 629 case -1: /* do nothing */ 630 break; 631 case 't': 632 op_mode = OP_TASKS; 633 break; 634 case 'i': 635 op_mode = OP_IPC; 636 break; 637 case 'e': 638 op_mode = OP_EXCS; 639 break; 640 case 's': 641 screen_mode = SCREEN_SORT; 642 break; 643 case 'r': 644 sort_reverse = -sort_reverse; 645 break; 646 case 'h': 647 case '?': 648 screen_mode = SCREEN_HELP; 649 break; 650 case 'q': 651 goto out; 652 case 'a': 653 if (op_mode == OP_EXCS) { 654 excs_all = !excs_all; 655 if (excs_all) 656 show_warning("Showing all exceptions"); 657 else 658 show_warning("Showing only hot exceptions"); 659 break; 660 } 661 /* fallthrough */ 662 default: 663 show_warning("Unknown command \"%c\", use \"h\" for help", c); 664 continue; /* don't redraw */ 665 } 666 667 if ((ret = fill_table(&data)) != NULL) { 668 goto out; 669 } 670 sort_table(&data.table); 671 print_data(&data); 421 default: 422 print_warning("Unknown command \"%c\", use \"h\" for help", c); 423 break; 424 } 672 425 } 673 426 674 427 out: 675 428 screen_done(); 676 free_data(&data );429 free_data(&data_prev); 677 430 678 431 if (ret != NULL) {
Note:
See TracChangeset
for help on using the changeset viewer.