source: mainline/uspace/app/top/screen.c@ ffa2c8ef

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

do not intermix low-level IPC methods with async framework methods

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