Changeset a35b458 in mainline for uspace/dist/src/c/demos/top/top.c
- Timestamp:
- 2018-03-02T20:10:49Z (7 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.