Changeset a35b458 in mainline for uspace/dist/src/c/demos/top
- Timestamp:
- 2018-03-02T20:10:49Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- Location:
- uspace/dist/src/c/demos/top
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/dist/src/c/demos/top/screen.c
r3061bc1 ra35b458 98 98 { 99 99 screen_style_normal(); 100 100 101 101 if (clear) { 102 102 console_flush(console); 103 103 console_clear(console); 104 104 } 105 105 106 106 screen_moveto(0, 0); 107 107 } … … 112 112 sysarg_t rows; 113 113 screen_get_size(&cols, &rows); 114 114 115 115 sysarg_t c; 116 116 sysarg_t r; 117 117 screen_get_pos(&c, &r); 118 118 119 119 sysarg_t i; 120 120 for (i = c + 1; i < cols; i++) 121 121 puts(" "); 122 122 123 123 if (r + 1 < rows) 124 124 puts("\n"); … … 128 128 { 129 129 console = console_init(stdin, stdout); 130 130 131 131 console_flush(console); 132 132 console_cursor_visibility(console, false); 133 133 134 134 screen_restart(true); 135 135 } … … 141 141 142 142 screen_restart(true); 143 143 144 144 console_flush(console); 145 145 console_cursor_visibility(console, true); … … 149 149 { 150 150 printf("%3" PRIu64 ".", ffloat.upper / ffloat.lower); 151 151 152 152 unsigned int i; 153 153 uint64_t rest = (ffloat.upper % ffloat.lower) * 10; … … 156 156 rest = (rest % ffloat.lower) * 10; 157 157 } 158 158 159 159 printf("%%"); 160 160 } … … 165 165 sysarg_t rows; 166 166 screen_get_size(&cols, &rows); 167 167 168 168 sysarg_t c; 169 169 sysarg_t r; 170 170 screen_get_pos(&c, &r); 171 171 172 172 if (c < cols) { 173 173 int pos = cols - c - 1; … … 183 183 data->hours, data->minutes, data->seconds, 184 184 data->udays, data->uhours, data->uminutes, data->useconds); 185 185 186 186 size_t i; 187 187 for (i = 0; i < data->load_count; i++) { … … 189 189 stats_print_load_fragment(data->load[i], 2); 190 190 } 191 191 192 192 screen_newline(); 193 193 } … … 208 208 size_t other = 0; 209 209 size_t invalid = 0; 210 210 211 211 size_t i; 212 212 for (i = 0; i < data->threads_count; i++) { 213 213 total++; 214 214 215 215 switch (data->threads[i].state) { 216 216 case Running: … … 234 234 } 235 235 } 236 236 237 237 printf("threads: %zu total, %zu running, %zu ready, " 238 238 "%zu sleeping, %zu lingering, %zu other, %zu invalid", … … 250 250 char busy_suffix; 251 251 char idle_suffix; 252 252 253 253 order_suffix(data->cpus[i].busy_cycles, &busy, &busy_suffix); 254 254 order_suffix(data->cpus[i].idle_cycles, &idle, &idle_suffix); 255 255 256 256 printf("cpu%u (%4" PRIu16 " MHz): busy cycles: " 257 257 "%" PRIu64 "%c, idle cycles: %" PRIu64 "%c", … … 264 264 } else 265 265 printf("cpu%u inactive", data->cpus[i].id); 266 266 267 267 screen_newline(); 268 268 } … … 279 279 const char *used_suffix; 280 280 const char *free_suffix; 281 281 282 282 bin_order_suffix(data->physmem->total, &total, &total_suffix, false); 283 283 bin_order_suffix(data->physmem->unavail, &unavail, &unavail_suffix, false); 284 284 bin_order_suffix(data->physmem->used, &used, &used_suffix, false); 285 285 bin_order_suffix(data->physmem->free, &free, &free_suffix, false); 286 286 287 287 printf("memory: %" PRIu64 "%s total, %" PRIu64 "%s unavail, %" 288 288 PRIu64 "%s used, %" PRIu64 "%s free", total, total_suffix, … … 304 304 sysarg_t rows; 305 305 screen_get_size(&cols, &rows); 306 307 screen_newline(); 308 306 307 screen_newline(); 308 309 309 printf("Operation modes:"); 310 310 screen_newline(); 311 311 312 312 printf(" t .. tasks statistics"); 313 313 screen_newline(); 314 314 315 315 printf(" i .. IPC statistics"); 316 316 screen_newline(); 317 317 318 318 printf(" e .. exceptions statistics"); 319 319 screen_newline(); 320 320 321 321 printf(" a .. toggle display of all/hot exceptions"); 322 322 screen_newline(); … … 329 329 printf("Other keys:"); 330 330 screen_newline(); 331 331 332 332 printf(" s .. choose column to sort by"); 333 333 screen_newline(); 334 334 335 335 printf(" r .. toggle reversed sorting"); 336 336 screen_newline(); 337 337 338 338 printf(" q .. quit"); 339 339 screen_newline(); 340 340 341 341 sysarg_t col; 342 342 sysarg_t row; 343 343 screen_get_pos(&col, &row); 344 344 345 345 while (row < rows) { 346 346 screen_newline(); … … 379 379 sysarg_t rows; 380 380 screen_get_size(&cols, &rows); 381 381 382 382 sysarg_t col; 383 383 sysarg_t row; 384 384 screen_get_pos(&col, &row); 385 385 386 386 size_t i; 387 387 for (i = 0; (i < table->num_fields) && (row < rows); i++) { … … 440 440 } 441 441 } 442 442 443 443 while (row < rows) { 444 444 screen_newline(); … … 452 452 sysarg_t rows; 453 453 screen_get_size(&cols, &rows); 454 454 455 455 sysarg_t col; 456 456 sysarg_t row; … … 463 463 row++; 464 464 } 465 465 466 466 while (row < rows) { 467 467 screen_newline(); … … 493 493 print_physmem_info(data); 494 494 print_warning(); 495 495 496 496 switch (screen_mode) { 497 497 case SCREEN_TABLE: … … 506 506 print_help(); 507 507 } 508 508 509 509 console_flush(console); 510 510 } … … 526 526 vsnprintf(warning_text, warning_text_size, fmt, args); 527 527 va_end(args); 528 528 529 529 warning_timeleft = 2 * USEC_COUNT; 530 530 … … 542 542 * Reset timeleft whenever it is not positive. 543 543 */ 544 544 545 545 if (timeleft <= 0) 546 546 timeleft = sec * USEC_COUNT; 547 547 548 548 /* 549 549 * Wait to see if there is any input. If so, take it and … … 552 552 * make timeleft zero and return -1. 553 553 */ 554 554 555 555 wchar_t c = 0; 556 556 557 557 while (c == 0) { 558 558 cons_event_t event; 559 559 560 560 warning_timeleft -= timeleft; 561 561 if (!console_get_event_timeout(console, &event, &timeleft)) { … … 564 564 } 565 565 warning_timeleft += timeleft; 566 566 567 567 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS) 568 568 c = event.ev.key.c; 569 569 } 570 570 571 571 return (int) c; 572 572 } -
uspace/dist/src/c/demos/top/top.c
r3061bc1 ra35b458 152 152 target->table.num_fields = 0; 153 153 target->table.fields = NULL; 154 154 155 155 /* Get current time */ 156 156 struct timeval time; 157 157 gettimeofday(&time, NULL); 158 158 159 159 target->hours = (time.tv_sec % DAY) / HOUR; 160 160 target->minutes = (time.tv_sec % HOUR) / MINUTE; 161 161 target->seconds = time.tv_sec % MINUTE; 162 162 163 163 /* Get uptime */ 164 164 struct timeval uptime; 165 165 getuptime(&uptime); 166 166 167 167 target->udays = uptime.tv_sec / DAY; 168 168 target->uhours = (uptime.tv_sec % DAY) / HOUR; 169 169 target->uminutes = (uptime.tv_sec % HOUR) / MINUTE; 170 170 target->useconds = uptime.tv_sec % MINUTE; 171 171 172 172 /* Get load */ 173 173 target->load = stats_get_load(&(target->load_count)); 174 174 if (target->load == NULL) 175 175 return "Cannot get system load"; 176 176 177 177 /* Get CPUs */ 178 178 target->cpus = stats_get_cpus(&(target->cpus_count)); 179 179 if (target->cpus == NULL) 180 180 return "Cannot get CPUs"; 181 181 182 182 target->cpus_perc = 183 183 (perc_cpu_t *) calloc(target->cpus_count, sizeof(perc_cpu_t)); 184 184 if (target->cpus_perc == NULL) 185 185 return "Not enough memory for CPU utilization"; 186 186 187 187 /* Get tasks */ 188 188 target->tasks = stats_get_tasks(&(target->tasks_count)); 189 189 if (target->tasks == NULL) 190 190 return "Cannot get tasks"; 191 191 192 192 target->tasks_perc = 193 193 (perc_task_t *) calloc(target->tasks_count, sizeof(perc_task_t)); 194 194 if (target->tasks_perc == NULL) 195 195 return "Not enough memory for task utilization"; 196 196 197 197 /* Get threads */ 198 198 target->threads = stats_get_threads(&(target->threads_count)); 199 199 if (target->threads == NULL) 200 200 return "Cannot get threads"; 201 201 202 202 /* Get Exceptions */ 203 203 target->exceptions = stats_get_exceptions(&(target->exceptions_count)); 204 204 if (target->exceptions == NULL) 205 205 return "Cannot get exceptions"; 206 206 207 207 target->exceptions_perc = 208 208 (perc_exc_t *) calloc(target->exceptions_count, sizeof(perc_exc_t)); 209 209 if (target->exceptions_perc == NULL) 210 210 return "Not enough memory for exception utilization"; 211 211 212 212 /* Get physical memory */ 213 213 target->physmem = stats_get_physmem(); 214 214 if (target->physmem == NULL) 215 215 return "Cannot get physical memory"; 216 216 217 217 target->ucycles_diff = calloc(target->tasks_count, 218 218 sizeof(uint64_t)); 219 219 if (target->ucycles_diff == NULL) 220 220 return "Not enough memory for user utilization"; 221 221 222 222 /* Allocate memory for computed values */ 223 223 target->kcycles_diff = calloc(target->tasks_count, … … 225 225 if (target->kcycles_diff == NULL) 226 226 return "Not enough memory for kernel utilization"; 227 227 228 228 target->ecycles_diff = calloc(target->exceptions_count, 229 229 sizeof(uint64_t)); 230 230 if (target->ecycles_diff == NULL) 231 231 return "Not enough memory for exception cycles utilization"; 232 232 233 233 target->ecount_diff = calloc(target->exceptions_count, 234 234 sizeof(uint64_t)); 235 235 if (target->ecount_diff == NULL) 236 236 return "Not enough memory for exception count utilization"; 237 237 238 238 return NULL; 239 239 } … … 249 249 /* For each CPU: Compute total cycles and divide it between 250 250 user and kernel */ 251 251 252 252 size_t i; 253 253 for (i = 0; i < new_data->cpus_count; i++) { … … 257 257 new_data->cpus[i].busy_cycles - old_data->cpus[i].busy_cycles; 258 258 uint64_t sum = idle + busy; 259 259 260 260 FRACTION_TO_FLOAT(new_data->cpus_perc[i].idle, idle * 100, sum); 261 261 FRACTION_TO_FLOAT(new_data->cpus_perc[i].busy, busy * 100, sum); 262 262 } 263 263 264 264 /* For all tasks compute sum and differencies of all cycles */ 265 265 266 266 uint64_t virtmem_total = 0; 267 267 uint64_t resmem_total = 0; 268 268 uint64_t ucycles_total = 0; 269 269 uint64_t kcycles_total = 0; 270 270 271 271 for (i = 0; i < new_data->tasks_count; i++) { 272 272 /* Match task with the previous instance */ 273 273 274 274 bool found = false; 275 275 size_t j; … … 280 280 } 281 281 } 282 282 283 283 if (!found) { 284 284 /* This is newly borned task, ignore it */ … … 287 287 continue; 288 288 } 289 289 290 290 new_data->ucycles_diff[i] = 291 291 new_data->tasks[i].ucycles - old_data->tasks[j].ucycles; 292 292 new_data->kcycles_diff[i] = 293 293 new_data->tasks[i].kcycles - old_data->tasks[j].kcycles; 294 294 295 295 virtmem_total += new_data->tasks[i].virtmem; 296 296 resmem_total += new_data->tasks[i].resmem; … … 298 298 kcycles_total += new_data->kcycles_diff[i]; 299 299 } 300 300 301 301 /* For each task compute percential change */ 302 302 303 303 for (i = 0; i < new_data->tasks_count; i++) { 304 304 FRACTION_TO_FLOAT(new_data->tasks_perc[i].virtmem, … … 311 311 new_data->kcycles_diff[i] * 100, kcycles_total); 312 312 } 313 313 314 314 /* For all exceptions compute sum and differencies of cycles */ 315 315 316 316 uint64_t ecycles_total = 0; 317 317 uint64_t ecount_total = 0; 318 318 319 319 for (i = 0; i < new_data->exceptions_count; i++) { 320 320 /* … … 323 323 * usually disappear, but it does not hurt. 324 324 */ 325 325 326 326 bool found = false; 327 327 size_t j; … … 332 332 } 333 333 } 334 334 335 335 if (!found) { 336 336 /* This is a new exception, ignore it */ … … 339 339 continue; 340 340 } 341 341 342 342 new_data->ecycles_diff[i] = 343 343 new_data->exceptions[i].cycles - old_data->exceptions[j].cycles; 344 344 new_data->ecount_diff[i] = 345 345 new_data->exceptions[i].count - old_data->exceptions[i].count; 346 346 347 347 ecycles_total += new_data->ecycles_diff[i]; 348 348 ecount_total += new_data->ecount_diff[i]; 349 349 } 350 350 351 351 /* For each exception compute percential change */ 352 352 353 353 for (i = 0; i < new_data->exceptions_count; i++) { 354 354 FRACTION_TO_FLOAT(new_data->exceptions_perc[i].cycles, … … 363 363 field_t *fa = (field_t *)a + sort_column; 364 364 field_t *fb = (field_t *)b + sort_column; 365 365 366 366 if (fa->type > fb->type) 367 367 return 1 * sort_reverse; … … 535 535 if (target->load != NULL) 536 536 free(target->load); 537 537 538 538 if (target->cpus != NULL) 539 539 free(target->cpus); 540 540 541 541 if (target->cpus_perc != NULL) 542 542 free(target->cpus_perc); 543 543 544 544 if (target->tasks != NULL) 545 545 free(target->tasks); 546 546 547 547 if (target->tasks_perc != NULL) 548 548 free(target->tasks_perc); 549 549 550 550 if (target->threads != NULL) 551 551 free(target->threads); 552 552 553 553 if (target->exceptions != NULL) 554 554 free(target->exceptions); 555 555 556 556 if (target->exceptions_perc != NULL) 557 557 free(target->exceptions_perc); 558 558 559 559 if (target->physmem != NULL) 560 560 free(target->physmem); 561 561 562 562 if (target->ucycles_diff != NULL) 563 563 free(target->ucycles_diff); 564 564 565 565 if (target->kcycles_diff != NULL) 566 566 free(target->kcycles_diff); 567 567 568 568 if (target->ecycles_diff != NULL) 569 569 free(target->ecycles_diff); 570 570 571 571 if (target->ecount_diff != NULL) 572 572 free(target->ecount_diff); … … 581 581 data_t data_prev; 582 582 const char *ret = NULL; 583 583 584 584 screen_init(); 585 585 printf("Reading initial data...\n"); 586 586 587 587 if ((ret = read_data(&data)) != NULL) 588 588 goto out; 589 589 590 590 /* Compute some rubbish to have initialised values */ 591 591 compute_percentages(&data, &data); 592 592 593 593 /* And paint screen until death */ 594 594 while (true) { … … 601 601 goto out; 602 602 } 603 603 604 604 compute_percentages(&data_prev, &data); 605 605 free_data(&data_prev); … … 671 671 print_data(&data); 672 672 } 673 673 674 674 out: 675 675 screen_done(); 676 676 free_data(&data); 677 677 678 678 if (ret != NULL) { 679 679 fprintf(stderr, "%s: %s\n", NAME, ret); 680 680 return 1; 681 681 } 682 682 683 683 return 0; 684 684 } -
uspace/dist/src/c/demos/top/top.h
r3061bc1 ra35b458 112 112 time_t minutes; 113 113 time_t seconds; 114 114 115 115 sysarg_t udays; 116 116 sysarg_t uhours; 117 117 sysarg_t uminutes; 118 118 sysarg_t useconds; 119 119 120 120 size_t load_count; 121 121 load_t *load; 122 122 123 123 size_t cpus_count; 124 124 stats_cpu_t *cpus; 125 125 perc_cpu_t *cpus_perc; 126 126 127 127 size_t tasks_count; 128 128 stats_task_t *tasks; 129 129 perc_task_t *tasks_perc; 130 130 131 131 size_t threads_count; 132 132 stats_thread_t *threads; 133 133 134 134 size_t exceptions_count; 135 135 stats_exc_t *exceptions; 136 136 perc_exc_t *exceptions_perc; 137 137 138 138 stats_physmem_t *physmem; 139 139 140 140 uint64_t *ucycles_diff; 141 141 uint64_t *kcycles_diff;
Note:
See TracChangeset
for help on using the changeset viewer.
