source: mainline/uspace/app/top/screen.c@ 0b0f4bb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0b0f4bb was 0b0f4bb, checked in by Martin Decky <martin@…>, 15 years ago
  • correct printf() formatting strings and corresponding arguments
  • add explicit overflow checking to printf-related code
  • Property mode set to 100644
File size: 11.6 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
[9f1362d4]49static ipcarg_t warn_col = 0;
50static ipcarg_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
[9f1362d4]64static void screen_moveto(ipcarg_t col, ipcarg_t row)
[5c058d50]65{
66 fflush(stdout);
[9f1362d4]67 console_set_pos(fphone(stdout), col, row);
[5c058d50]68}
69
[9f1362d4]70static void screen_get_pos(ipcarg_t *col, ipcarg_t *row)
[5c058d50]71{
[dec16a2]72 fflush(stdout);
[9f1362d4]73 console_get_pos(fphone(stdout), col, row);
[5c058d50]74}
75
[9f1362d4]76static void screen_get_size(ipcarg_t *col, ipcarg_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{
[9f1362d4]96 ipcarg_t cols;
97 ipcarg_t rows;
98 screen_get_size(&cols, &rows);
99
100 ipcarg_t c;
101 ipcarg_t r;
102 screen_get_pos(&c, &r);
103
104 ipcarg_t i;
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{
144 ipcarg_t cols;
145 ipcarg_t rows;
146 screen_get_size(&cols, &rows);
147
148 ipcarg_t c;
149 ipcarg_t r;
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();
[be06914]276 printf("[taskid] [threads] [virtual] [%%virt] [%%user]"
277 " [%%kernel] [name");
[9f1362d4]278 screen_newline();
279 screen_style_normal();
[516adce]280}
281
[9f1362d4]282static inline void print_tasks(data_t *data)
[8f56d93]283{
[9f1362d4]284 ipcarg_t cols;
285 ipcarg_t rows;
286 screen_get_size(&cols, &rows);
287
288 ipcarg_t col;
289 ipcarg_t row;
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
[dec16a2]297 uint64_t virtmem;
298 char virtmem_suffix;
[172aad6]299 order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
[dec16a2]300
[0b0f4bb]301 printf("%-8" PRIu64 " %9zu %8" PRIu64 "%c ", task->task_id,
[172aad6]302 task->threads, virtmem, virtmem_suffix);
303 print_percent(perc->virtmem, 2);
[be06914]304 puts(" ");
[172aad6]305 print_percent(perc->ucycles, 2);
[9f1362d4]306 puts(" ");
[172aad6]307 print_percent(perc->kcycles, 2);
[be06914]308 puts(" ");
[172aad6]309 print_string(task->name);
[9f1362d4]310
311 screen_newline();
[8f56d93]312 }
[369a5f8]313
314 while (row < rows) {
315 screen_newline();
316 row++;
317 }
[8f56d93]318}
319
[bdfd3c97]320static inline void print_ipc_head(void)
321{
[9f1362d4]322 screen_style_inverted();
[be06914]323 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
324 " [ans rcv] [irq rcv] [forward] [name");
[9f1362d4]325 screen_newline();
326 screen_style_normal();
[bdfd3c97]327}
328
[9f1362d4]329static inline void print_ipc(data_t *data)
[bdfd3c97]330{
[9f1362d4]331 ipcarg_t cols;
332 ipcarg_t rows;
333 screen_get_size(&cols, &rows);
334
335 ipcarg_t col;
336 ipcarg_t row;
337 screen_get_pos(&col, &row);
338
[dec16a2]339 size_t i;
[9f1362d4]340 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
[be06914]341 uint64_t call_sent;
342 uint64_t call_received;
343 uint64_t answer_sent;
344 uint64_t answer_received;
345 uint64_t irq_notif_received;
346 uint64_t forwarded;
347
348 char call_sent_suffix;
349 char call_received_suffix;
350 char answer_sent_suffix;
351 char answer_received_suffix;
352 char irq_notif_received_suffix;
353 char forwarded_suffix;
354
355 order_suffix(data->tasks[i].ipc_info.call_sent, &call_sent,
356 &call_sent_suffix);
357 order_suffix(data->tasks[i].ipc_info.call_received,
358 &call_received, &call_received_suffix);
359 order_suffix(data->tasks[i].ipc_info.answer_sent,
360 &answer_sent, &answer_sent_suffix);
361 order_suffix(data->tasks[i].ipc_info.answer_received,
362 &answer_received, &answer_received_suffix);
363 order_suffix(data->tasks[i].ipc_info.irq_notif_received,
364 &irq_notif_received, &irq_notif_received_suffix);
365 order_suffix(data->tasks[i].ipc_info.forwarded, &forwarded,
366 &forwarded_suffix);
367
368 printf("%-8" PRIu64 " %8" PRIu64 "%c %8" PRIu64 "%c"
369 " %8" PRIu64 "%c %8" PRIu64 "%c %8" PRIu64 "%c"
370 " %8" PRIu64 "%c ", data->tasks[i].task_id,
371 call_sent, call_sent_suffix,
372 call_received, call_received_suffix,
373 answer_sent, answer_sent_suffix,
374 answer_received, answer_received_suffix,
375 irq_notif_received, irq_notif_received_suffix,
376 forwarded, forwarded_suffix);
377 print_string(data->tasks[i].name);
[9f1362d4]378
379 screen_newline();
[bdfd3c97]380 }
[369a5f8]381
382 while (row < rows) {
383 screen_newline();
384 row++;
385 }
[bdfd3c97]386}
387
[b3b7e14a]388static inline void print_excs_head(void)
[8eec3c8]389{
390 screen_style_inverted();
[be06914]391 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
[8eec3c8]392 screen_newline();
393 screen_style_normal();
394}
395
[b3b7e14a]396static inline void print_excs(data_t *data)
[8eec3c8]397{
398 ipcarg_t cols;
399 ipcarg_t rows;
400 screen_get_size(&cols, &rows);
401
402 ipcarg_t col;
403 ipcarg_t row;
404 screen_get_pos(&col, &row);
405
406 size_t i;
[b3b7e14a]407 for (i = 0; (i < data->exceptions_count) && (row < rows); i++) {
408 /* Filter-out cold exceptions if not instructed otherwise */
409 if ((!excs_all) && (!data->exceptions[i].hot))
410 continue;
411
[be06914]412 uint64_t count;
[8eec3c8]413 uint64_t cycles;
414
[be06914]415 char count_suffix;
416 char cycles_suffix;
417
418 order_suffix(data->exceptions[i].count, &count, &count_suffix);
419 order_suffix(data->exceptions[i].cycles, &cycles, &cycles_suffix);
420
421 printf("%-8u %9" PRIu64 "%c ",
422 data->exceptions[i].id, count, count_suffix);
423 print_percent(data->exceptions_perc[i].count, 2);
424 printf(" %9" PRIu64 "%c ", cycles, cycles_suffix);
425 print_percent(data->exceptions_perc[i].cycles, 2);
426 puts(" ");
427 print_string(data->exceptions[i].desc);
[8eec3c8]428
429 screen_newline();
[b3b7e14a]430 row++;
431 }
432
433 while (row < rows) {
434 screen_newline();
435 row++;
[8eec3c8]436 }
[b3b7e14a]437}
438
439static void print_help(void)
440{
441 ipcarg_t cols;
442 ipcarg_t rows;
443 screen_get_size(&cols, &rows);
444
445 ipcarg_t col;
446 ipcarg_t row;
447 screen_get_pos(&col, &row);
448
449 screen_newline();
450
451 printf("Operation modes:");
452 screen_newline();
453
454 printf(" t .. tasks statistics");
455 screen_newline();
456
457 printf(" i .. IPC statistics");
458 screen_newline();
459
460 printf(" e .. exceptions statistics");
461 screen_newline();
462
463 printf(" a .. toggle display of all/hot exceptions");
464 screen_newline();
465
466 row += 6;
[8eec3c8]467
468 while (row < rows) {
469 screen_newline();
470 row++;
471 }
472}
473
[79edc36]474void print_data(data_t *data)
475{
[9f1362d4]476 screen_restart(false);
477 print_global_head(data);
[dec16a2]478 print_task_summary(data);
479 print_thread_summary(data);
480 print_cpu_info(data);
481 print_physmem_info(data);
482
483 /* Empty row for warnings */
[9f1362d4]484 screen_get_pos(&warn_col, &warn_row);
485 screen_newline();
[dec16a2]486
[b3b7e14a]487 switch (op_mode) {
[8eec3c8]488 case OP_TASKS:
[b3b7e14a]489 print_tasks_head();
[9f1362d4]490 print_tasks(data);
[8eec3c8]491 break;
492 case OP_IPC:
493 print_ipc_head();
494 print_ipc(data);
495 break;
[b3b7e14a]496 case OP_EXCS:
497 print_excs_head();
498 print_excs(data);
[8eec3c8]499 break;
[b3b7e14a]500 case OP_HELP:
501 print_tasks_head();
502 print_help();
[bdfd3c97]503 }
[dec16a2]504
505 fflush(stdout);
506}
507
508void print_warning(const char *fmt, ...)
509{
[9f1362d4]510 screen_moveto(warn_col, warn_row);
[dec16a2]511
512 va_list args;
513 va_start(args, fmt);
514 vprintf(fmt, args);
515 va_end(args);
516
[9f1362d4]517 screen_newline();
[79edc36]518 fflush(stdout);
519}
520
[5c058d50]521/** @}
522 */
Note: See TracBrowser for help on using the repository browser.