source: mainline/uspace/app/top/screen.c@ 2a827f4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2a827f4 was 2a827f4, checked in by Martin Decky <martin@…>, 14 years ago

shorten "threads" to "thrds"

  • Property mode set to 100644
File size: 11.8 KB
RevLine 
[5c058d50]1/*
2 * Copyright (c) 2010 Stanislav Kozina
[dec16a2]3 * Copyright (c) 2010 Martin Decky
[5c058d50]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup top
31 * @brief Top utility.
32 * @{
33 */
34/**
35 * @file
36 */
37
38#include <stdio.h>
[9f1362d4]39#include <ipc/ipc.h>
[5c058d50]40#include <io/console.h>
[9f1362d4]41#include <io/style.h>
[5c058d50]42#include <vfs/vfs.h>
[dec16a2]43#include <stdarg.h>
44#include <stats.h>
45#include <inttypes.h>
[5c058d50]46#include "screen.h"
[79edc36]47#include "top.h"
[5c058d50]48
[96b02eb9]49static sysarg_t warn_col = 0;
50static sysarg_t warn_row = 0;
[8f56d93]51
[9f1362d4]52static void screen_style_normal(void)
53{
54 fflush(stdout);
55 console_set_style(fphone(stdout), STYLE_NORMAL);
56}
[8f56d93]57
[9f1362d4]58static void screen_style_inverted(void)
[ee35ba0b]59{
[9f1362d4]60 fflush(stdout);
61 console_set_style(fphone(stdout), STYLE_INVERTED);
[ee35ba0b]62}
63
[96b02eb9]64static void screen_moveto(sysarg_t col, sysarg_t row)
[5c058d50]65{
66 fflush(stdout);
[9f1362d4]67 console_set_pos(fphone(stdout), col, row);
[5c058d50]68}
69
[96b02eb9]70static void screen_get_pos(sysarg_t *col, sysarg_t *row)
[5c058d50]71{
[dec16a2]72 fflush(stdout);
[9f1362d4]73 console_get_pos(fphone(stdout), col, row);
[5c058d50]74}
75
[96b02eb9]76static void screen_get_size(sysarg_t *col, sysarg_t *row)
[5c058d50]77{
[8b2aba5]78 fflush(stdout);
[9f1362d4]79 console_get_size(fphone(stdout), col, row);
[5c058d50]80}
81
[9f1362d4]82static void screen_restart(bool clear)
[5c058d50]83{
[9f1362d4]84 screen_style_normal();
85
86 if (clear) {
87 fflush(stdout);
88 console_clear(fphone(stdout));
89 }
[dec16a2]90
[9f1362d4]91 screen_moveto(0, 0);
[dec16a2]92}
93
[9f1362d4]94static void screen_newline(void)
[dec16a2]95{
[96b02eb9]96 sysarg_t cols;
97 sysarg_t rows;
[9f1362d4]98 screen_get_size(&cols, &rows);
99
[96b02eb9]100 sysarg_t c;
101 sysarg_t r;
[9f1362d4]102 screen_get_pos(&c, &r);
103
[96b02eb9]104 sysarg_t i;
[9f1362d4]105 for (i = c + 1; i < cols; i++)
106 puts(" ");
107
108 if (r + 1 < rows)
109 puts("\n");
110}
111
112void screen_init(void)
113{
114 fflush(stdout);
115 console_cursor_visibility(fphone(stdout), false);
116
117 screen_restart(true);
[5c058d50]118}
119
[9f1362d4]120void screen_done(void)
[79edc36]121{
[9f1362d4]122 screen_restart(true);
123
124 fflush(stdout);
125 console_cursor_visibility(fphone(stdout), true);
[79edc36]126}
127
[be06914]128static void print_percent(fixed_float ffloat, unsigned int precision)
[79edc36]129{
[be06914]130 printf("%3" PRIu64 ".", ffloat.upper / ffloat.lower);
[9f1362d4]131
132 unsigned int i;
133 uint64_t rest = (ffloat.upper % ffloat.lower) * 10;
134 for (i = 0; i < precision; i++) {
135 printf("%" PRIu64, rest / ffloat.lower);
136 rest = (rest % ffloat.lower) * 10;
137 }
[be06914]138
139 printf("%%");
140}
141
142static void print_string(const char *str)
143{
[96b02eb9]144 sysarg_t cols;
145 sysarg_t rows;
[be06914]146 screen_get_size(&cols, &rows);
147
[96b02eb9]148 sysarg_t c;
149 sysarg_t r;
[be06914]150 screen_get_pos(&c, &r);
151
[0b0f4bb]152 if (c < cols) {
153 int pos = cols - c - 1;
154 printf("%.*s", pos, str);
155 }
[79edc36]156}
157
[9f1362d4]158static inline void print_global_head(data_t *data)
[dd6c71c]159{
[0b0f4bb]160 printf("top - %02lu:%02lu:%02lu up "
161 "%" PRIun " days, %02" PRIun ":%02" PRIun ":%02" PRIun ", "
162 "load average:",
[9f1362d4]163 data->hours, data->minutes, data->seconds,
164 data->udays, data->uhours, data->uminutes, data->useconds);
[dec16a2]165
166 size_t i;
167 for (i = 0; i < data->load_count; i++) {
[9f1362d4]168 puts(" ");
[dec16a2]169 stats_print_load_fragment(data->load[i], 2);
170 }
[9f1362d4]171
172 screen_newline();
[dd6c71c]173}
174
[dec16a2]175static inline void print_task_summary(data_t *data)
[dd6c71c]176{
[0b0f4bb]177 printf("tasks: %zu total", data->tasks_count);
[9f1362d4]178 screen_newline();
[dd6c71c]179}
180
[dec16a2]181static inline void print_thread_summary(data_t *data)
[638927a]182{
[dec16a2]183 size_t total = 0;
[638927a]184 size_t running = 0;
[dec16a2]185 size_t ready = 0;
186 size_t sleeping = 0;
187 size_t lingering = 0;
[638927a]188 size_t other = 0;
[dec16a2]189 size_t invalid = 0;
190
[638927a]191 size_t i;
[dec16a2]192 for (i = 0; i < data->threads_count; i++) {
193 total++;
194
195 switch (data->threads[i].state) {
196 case Running:
197 running++;
198 break;
199 case Ready:
200 ready++;
201 break;
202 case Sleeping:
203 sleeping++;
204 break;
205 case Lingering:
206 lingering++;
207 break;
208 case Entering:
209 case Exiting:
210 other++;
211 break;
212 default:
213 invalid++;
[638927a]214 }
215 }
[dec16a2]216
[0b0f4bb]217 printf("threads: %zu total, %zu running, %zu ready, "
218 "%zu sleeping, %zu lingering, %zu other, %zu invalid",
[dec16a2]219 total, running, ready, sleeping, lingering, other, invalid);
[9f1362d4]220 screen_newline();
[638927a]221}
222
[dec16a2]223static inline void print_cpu_info(data_t *data)
[8b2aba5]224{
[dec16a2]225 size_t i;
226 for (i = 0; i < data->cpus_count; i++) {
[bd01a4e]227 if (data->cpus[i].active) {
[d0c82c5]228 uint64_t busy;
229 uint64_t idle;
230 char busy_suffix;
231 char idle_suffix;
232
233 order_suffix(data->cpus[i].busy_cycles, &busy, &busy_suffix);
234 order_suffix(data->cpus[i].idle_cycles, &idle, &idle_suffix);
235
236 printf("cpu%u (%4" PRIu16 " MHz): busy cycles: "
237 "%" PRIu64 "%c, idle cycles: %" PRIu64 "%c",
[bd01a4e]238 data->cpus[i].id, data->cpus[i].frequency_mhz,
[d0c82c5]239 busy, busy_suffix, idle, idle_suffix);
[9f1362d4]240 puts(", idle: ");
[be06914]241 print_percent(data->cpus_perc[i].idle, 2);
242 puts(", busy: ");
243 print_percent(data->cpus_perc[i].busy, 2);
[bd01a4e]244 } else
[9f1362d4]245 printf("cpu%u inactive", data->cpus[i].id);
[dec16a2]246
[9f1362d4]247 screen_newline();
[8b2aba5]248 }
249}
250
[dec16a2]251static inline void print_physmem_info(data_t *data)
[516adce]252{
[dec16a2]253 uint64_t total;
254 uint64_t unavail;
255 uint64_t used;
256 uint64_t free;
257 char total_suffix;
258 char unavail_suffix;
259 char used_suffix;
260 char free_suffix;
261
262 order_suffix(data->physmem->total, &total, &total_suffix);
263 order_suffix(data->physmem->unavail, &unavail, &unavail_suffix);
264 order_suffix(data->physmem->used, &used, &used_suffix);
265 order_suffix(data->physmem->free, &free, &free_suffix);
266
267 printf("memory: %" PRIu64 "%c total, %" PRIu64 "%c unavail, %"
268 PRIu64 "%c used, %" PRIu64 "%c free", total, total_suffix,
269 unavail, unavail_suffix, used, used_suffix, free, free_suffix);
[9f1362d4]270 screen_newline();
271}
272
[b3b7e14a]273static inline void print_tasks_head(void)
[9f1362d4]274{
275 screen_style_inverted();
[2a827f4]276 printf("[taskid] [thrds] [resident] [%%resi] [virtual] [%%virt]"
[a0ce870]277 " [%%user] [%%kern] [name");
[9f1362d4]278 screen_newline();
279 screen_style_normal();
[516adce]280}
281
[9f1362d4]282static inline void print_tasks(data_t *data)
[8f56d93]283{
[96b02eb9]284 sysarg_t cols;
285 sysarg_t rows;
[9f1362d4]286 screen_get_size(&cols, &rows);
287
[96b02eb9]288 sysarg_t col;
289 sysarg_t row;
[9f1362d4]290 screen_get_pos(&col, &row);
291
[dec16a2]292 size_t i;
[9f1362d4]293 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
[172aad6]294 stats_task_t *task = data->tasks + data->tasks_map[i];
295 perc_task_t *perc = data->tasks_perc + data->tasks_map[i];
296
[a0ce870]297 uint64_t resmem;
298 char resmem_suffix;
299 order_suffix(task->resmem, &resmem, &resmem_suffix);
300
[dec16a2]301 uint64_t virtmem;
302 char virtmem_suffix;
[172aad6]303 order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
[dec16a2]304
[2a827f4]305 printf("%-8" PRIu64 " %7zu %9" PRIu64 "%c ",
[a0ce870]306 task->task_id, task->threads, resmem, resmem_suffix);
307 print_percent(perc->resmem, 2);
308 printf(" %8" PRIu64 "%c ", virtmem, virtmem_suffix);
[172aad6]309 print_percent(perc->virtmem, 2);
[be06914]310 puts(" ");
[172aad6]311 print_percent(perc->ucycles, 2);
[a0ce870]312 puts(" ");
[172aad6]313 print_percent(perc->kcycles, 2);
[be06914]314 puts(" ");
[172aad6]315 print_string(task->name);
[9f1362d4]316
317 screen_newline();
[8f56d93]318 }
[369a5f8]319
320 while (row < rows) {
321 screen_newline();
322 row++;
323 }
[8f56d93]324}
325
[bdfd3c97]326static inline void print_ipc_head(void)
327{
[9f1362d4]328 screen_style_inverted();
[be06914]329 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
330 " [ans rcv] [irq rcv] [forward] [name");
[9f1362d4]331 screen_newline();
332 screen_style_normal();
[bdfd3c97]333}
334
[9f1362d4]335static inline void print_ipc(data_t *data)
[bdfd3c97]336{
[96b02eb9]337 sysarg_t cols;
338 sysarg_t rows;
[9f1362d4]339 screen_get_size(&cols, &rows);
340
[96b02eb9]341 sysarg_t col;
342 sysarg_t row;
[9f1362d4]343 screen_get_pos(&col, &row);
344
[dec16a2]345 size_t i;
[9f1362d4]346 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
[be06914]347 uint64_t call_sent;
348 uint64_t call_received;
349 uint64_t answer_sent;
350 uint64_t answer_received;
351 uint64_t irq_notif_received;
352 uint64_t forwarded;
353
354 char call_sent_suffix;
355 char call_received_suffix;
356 char answer_sent_suffix;
357 char answer_received_suffix;
358 char irq_notif_received_suffix;
359 char forwarded_suffix;
360
361 order_suffix(data->tasks[i].ipc_info.call_sent, &call_sent,
362 &call_sent_suffix);
363 order_suffix(data->tasks[i].ipc_info.call_received,
364 &call_received, &call_received_suffix);
365 order_suffix(data->tasks[i].ipc_info.answer_sent,
366 &answer_sent, &answer_sent_suffix);
367 order_suffix(data->tasks[i].ipc_info.answer_received,
368 &answer_received, &answer_received_suffix);
369 order_suffix(data->tasks[i].ipc_info.irq_notif_received,
370 &irq_notif_received, &irq_notif_received_suffix);
371 order_suffix(data->tasks[i].ipc_info.forwarded, &forwarded,
372 &forwarded_suffix);
373
374 printf("%-8" PRIu64 " %8" PRIu64 "%c %8" PRIu64 "%c"
375 " %8" PRIu64 "%c %8" PRIu64 "%c %8" PRIu64 "%c"
376 " %8" PRIu64 "%c ", data->tasks[i].task_id,
377 call_sent, call_sent_suffix,
378 call_received, call_received_suffix,
379 answer_sent, answer_sent_suffix,
380 answer_received, answer_received_suffix,
381 irq_notif_received, irq_notif_received_suffix,
382 forwarded, forwarded_suffix);
383 print_string(data->tasks[i].name);
[9f1362d4]384
385 screen_newline();
[bdfd3c97]386 }
[369a5f8]387
388 while (row < rows) {
389 screen_newline();
390 row++;
391 }
[bdfd3c97]392}
393
[b3b7e14a]394static inline void print_excs_head(void)
[8eec3c8]395{
396 screen_style_inverted();
[be06914]397 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
[8eec3c8]398 screen_newline();
399 screen_style_normal();
400}
401
[b3b7e14a]402static inline void print_excs(data_t *data)
[8eec3c8]403{
[96b02eb9]404 sysarg_t cols;
405 sysarg_t rows;
[8eec3c8]406 screen_get_size(&cols, &rows);
407
[96b02eb9]408 sysarg_t col;
409 sysarg_t row;
[8eec3c8]410 screen_get_pos(&col, &row);
411
412 size_t i;
[b3b7e14a]413 for (i = 0; (i < data->exceptions_count) && (row < rows); i++) {
414 /* Filter-out cold exceptions if not instructed otherwise */
415 if ((!excs_all) && (!data->exceptions[i].hot))
416 continue;
417
[be06914]418 uint64_t count;
[8eec3c8]419 uint64_t cycles;
420
[be06914]421 char count_suffix;
422 char cycles_suffix;
423
424 order_suffix(data->exceptions[i].count, &count, &count_suffix);
425 order_suffix(data->exceptions[i].cycles, &cycles, &cycles_suffix);
426
427 printf("%-8u %9" PRIu64 "%c ",
428 data->exceptions[i].id, count, count_suffix);
429 print_percent(data->exceptions_perc[i].count, 2);
430 printf(" %9" PRIu64 "%c ", cycles, cycles_suffix);
431 print_percent(data->exceptions_perc[i].cycles, 2);
432 puts(" ");
433 print_string(data->exceptions[i].desc);
[8eec3c8]434
435 screen_newline();
[b3b7e14a]436 row++;
437 }
438
439 while (row < rows) {
440 screen_newline();
441 row++;
[8eec3c8]442 }
[b3b7e14a]443}
444
445static void print_help(void)
446{
[96b02eb9]447 sysarg_t cols;
448 sysarg_t rows;
[b3b7e14a]449 screen_get_size(&cols, &rows);
450
[96b02eb9]451 sysarg_t col;
452 sysarg_t row;
[b3b7e14a]453 screen_get_pos(&col, &row);
454
455 screen_newline();
456
457 printf("Operation modes:");
458 screen_newline();
459
460 printf(" t .. tasks statistics");
461 screen_newline();
462
463 printf(" i .. IPC statistics");
464 screen_newline();
465
466 printf(" e .. exceptions statistics");
467 screen_newline();
468
469 printf(" a .. toggle display of all/hot exceptions");
470 screen_newline();
471
472 row += 6;
[8eec3c8]473
474 while (row < rows) {
475 screen_newline();
476 row++;
477 }
478}
479
[79edc36]480void print_data(data_t *data)
481{
[9f1362d4]482 screen_restart(false);
483 print_global_head(data);
[dec16a2]484 print_task_summary(data);
485 print_thread_summary(data);
486 print_cpu_info(data);
487 print_physmem_info(data);
488
489 /* Empty row for warnings */
[9f1362d4]490 screen_get_pos(&warn_col, &warn_row);
491 screen_newline();
[dec16a2]492
[b3b7e14a]493 switch (op_mode) {
[8eec3c8]494 case OP_TASKS:
[b3b7e14a]495 print_tasks_head();
[9f1362d4]496 print_tasks(data);
[8eec3c8]497 break;
498 case OP_IPC:
499 print_ipc_head();
500 print_ipc(data);
501 break;
[b3b7e14a]502 case OP_EXCS:
503 print_excs_head();
504 print_excs(data);
[8eec3c8]505 break;
[b3b7e14a]506 case OP_HELP:
507 print_tasks_head();
508 print_help();
[bdfd3c97]509 }
[dec16a2]510
511 fflush(stdout);
512}
513
514void print_warning(const char *fmt, ...)
515{
[9f1362d4]516 screen_moveto(warn_col, warn_row);
[dec16a2]517
518 va_list args;
519 va_start(args, fmt);
520 vprintf(fmt, args);
521 va_end(args);
522
[9f1362d4]523 screen_newline();
[79edc36]524 fflush(stdout);
525}
526
[5c058d50]527/** @}
528 */
Note: See TracBrowser for help on using the repository browser.