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
Line 
1/*
2 * Copyright (c) 2010 Stanislav Kozina
3 * Copyright (c) 2010 Martin Decky
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>
40#include <io/style.h>
41#include <vfs/vfs.h>
42#include <stdarg.h>
43#include <stats.h>
44#include <inttypes.h>
45#include "screen.h"
46#include "top.h"
47
48static sysarg_t warn_col = 0;
49static sysarg_t warn_row = 0;
50
51static void screen_style_normal(void)
52{
53 fflush(stdout);
54 console_set_style(fphone(stdout), STYLE_NORMAL);
55}
56
57static void screen_style_inverted(void)
58{
59 fflush(stdout);
60 console_set_style(fphone(stdout), STYLE_INVERTED);
61}
62
63static void screen_moveto(sysarg_t col, sysarg_t row)
64{
65 fflush(stdout);
66 console_set_pos(fphone(stdout), col, row);
67}
68
69static void screen_get_pos(sysarg_t *col, sysarg_t *row)
70{
71 fflush(stdout);
72 console_get_pos(fphone(stdout), col, row);
73}
74
75static void screen_get_size(sysarg_t *col, sysarg_t *row)
76{
77 fflush(stdout);
78 console_get_size(fphone(stdout), col, row);
79}
80
81static void screen_restart(bool clear)
82{
83 screen_style_normal();
84
85 if (clear) {
86 fflush(stdout);
87 console_clear(fphone(stdout));
88 }
89
90 screen_moveto(0, 0);
91}
92
93static void screen_newline(void)
94{
95 sysarg_t cols;
96 sysarg_t rows;
97 screen_get_size(&cols, &rows);
98
99 sysarg_t c;
100 sysarg_t r;
101 screen_get_pos(&c, &r);
102
103 sysarg_t i;
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);
117}
118
119void screen_done(void)
120{
121 screen_restart(true);
122
123 fflush(stdout);
124 console_cursor_visibility(fphone(stdout), true);
125}
126
127static void print_percent(fixed_float ffloat, unsigned int precision)
128{
129 printf("%3" PRIu64 ".", ffloat.upper / ffloat.lower);
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 }
137
138 printf("%%");
139}
140
141static void print_string(const char *str)
142{
143 sysarg_t cols;
144 sysarg_t rows;
145 screen_get_size(&cols, &rows);
146
147 sysarg_t c;
148 sysarg_t r;
149 screen_get_pos(&c, &r);
150
151 if (c < cols) {
152 int pos = cols - c - 1;
153 printf("%.*s", pos, str);
154 }
155}
156
157static inline void print_global_head(data_t *data)
158{
159 printf("top - %02lu:%02lu:%02lu up "
160 "%" PRIun " days, %02" PRIun ":%02" PRIun ":%02" PRIun ", "
161 "load average:",
162 data->hours, data->minutes, data->seconds,
163 data->udays, data->uhours, data->uminutes, data->useconds);
164
165 size_t i;
166 for (i = 0; i < data->load_count; i++) {
167 puts(" ");
168 stats_print_load_fragment(data->load[i], 2);
169 }
170
171 screen_newline();
172}
173
174static inline void print_task_summary(data_t *data)
175{
176 printf("tasks: %zu total", data->tasks_count);
177 screen_newline();
178}
179
180static inline void print_thread_summary(data_t *data)
181{
182 size_t total = 0;
183 size_t running = 0;
184 size_t ready = 0;
185 size_t sleeping = 0;
186 size_t lingering = 0;
187 size_t other = 0;
188 size_t invalid = 0;
189
190 size_t i;
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++;
213 }
214 }
215
216 printf("threads: %zu total, %zu running, %zu ready, "
217 "%zu sleeping, %zu lingering, %zu other, %zu invalid",
218 total, running, ready, sleeping, lingering, other, invalid);
219 screen_newline();
220}
221
222static inline void print_cpu_info(data_t *data)
223{
224 size_t i;
225 for (i = 0; i < data->cpus_count; i++) {
226 if (data->cpus[i].active) {
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",
237 data->cpus[i].id, data->cpus[i].frequency_mhz,
238 busy, busy_suffix, idle, idle_suffix);
239 puts(", idle: ");
240 print_percent(data->cpus_perc[i].idle, 2);
241 puts(", busy: ");
242 print_percent(data->cpus_perc[i].busy, 2);
243 } else
244 printf("cpu%u inactive", data->cpus[i].id);
245
246 screen_newline();
247 }
248}
249
250static inline void print_physmem_info(data_t *data)
251{
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);
269 screen_newline();
270}
271
272static inline void print_tasks_head(void)
273{
274 screen_style_inverted();
275 printf("[taskid] [thrds] [resident] [%%resi] [virtual] [%%virt]"
276 " [%%user] [%%kern] [name");
277 screen_newline();
278 screen_style_normal();
279}
280
281static inline void print_tasks(data_t *data)
282{
283 sysarg_t cols;
284 sysarg_t rows;
285 screen_get_size(&cols, &rows);
286
287 sysarg_t col;
288 sysarg_t row;
289 screen_get_pos(&col, &row);
290
291 size_t i;
292 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
293 stats_task_t *task = data->tasks + data->tasks_map[i];
294 perc_task_t *perc = data->tasks_perc + data->tasks_map[i];
295
296 uint64_t resmem;
297 char resmem_suffix;
298 order_suffix(task->resmem, &resmem, &resmem_suffix);
299
300 uint64_t virtmem;
301 char virtmem_suffix;
302 order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
303
304 printf("%-8" PRIu64 " %7zu %9" PRIu64 "%c ",
305 task->task_id, task->threads, resmem, resmem_suffix);
306 print_percent(perc->resmem, 2);
307 printf(" %8" PRIu64 "%c ", virtmem, virtmem_suffix);
308 print_percent(perc->virtmem, 2);
309 puts(" ");
310 print_percent(perc->ucycles, 2);
311 puts(" ");
312 print_percent(perc->kcycles, 2);
313 puts(" ");
314 print_string(task->name);
315
316 screen_newline();
317 }
318
319 while (row < rows) {
320 screen_newline();
321 row++;
322 }
323}
324
325static inline void print_ipc_head(void)
326{
327 screen_style_inverted();
328 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
329 " [ans rcv] [irq rcv] [forward] [name");
330 screen_newline();
331 screen_style_normal();
332}
333
334static inline void print_ipc(data_t *data)
335{
336 sysarg_t cols;
337 sysarg_t rows;
338 screen_get_size(&cols, &rows);
339
340 sysarg_t col;
341 sysarg_t row;
342 screen_get_pos(&col, &row);
343
344 size_t i;
345 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
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);
383
384 screen_newline();
385 }
386
387 while (row < rows) {
388 screen_newline();
389 row++;
390 }
391}
392
393static inline void print_excs_head(void)
394{
395 screen_style_inverted();
396 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
397 screen_newline();
398 screen_style_normal();
399}
400
401static inline void print_excs(data_t *data)
402{
403 sysarg_t cols;
404 sysarg_t rows;
405 screen_get_size(&cols, &rows);
406
407 sysarg_t col;
408 sysarg_t row;
409 screen_get_pos(&col, &row);
410
411 size_t i;
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
417 uint64_t count;
418 uint64_t cycles;
419
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);
433
434 screen_newline();
435 row++;
436 }
437
438 while (row < rows) {
439 screen_newline();
440 row++;
441 }
442}
443
444static void print_help(void)
445{
446 sysarg_t cols;
447 sysarg_t rows;
448 screen_get_size(&cols, &rows);
449
450 sysarg_t col;
451 sysarg_t row;
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;
472
473 while (row < rows) {
474 screen_newline();
475 row++;
476 }
477}
478
479void print_data(data_t *data)
480{
481 screen_restart(false);
482 print_global_head(data);
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 */
489 screen_get_pos(&warn_col, &warn_row);
490 screen_newline();
491
492 switch (op_mode) {
493 case OP_TASKS:
494 print_tasks_head();
495 print_tasks(data);
496 break;
497 case OP_IPC:
498 print_ipc_head();
499 print_ipc(data);
500 break;
501 case OP_EXCS:
502 print_excs_head();
503 print_excs(data);
504 break;
505 case OP_HELP:
506 print_tasks_head();
507 print_help();
508 }
509
510 fflush(stdout);
511}
512
513void print_warning(const char *fmt, ...)
514{
515 screen_moveto(warn_col, warn_row);
516
517 va_list args;
518 va_start(args, fmt);
519 vprintf(fmt, args);
520 va_end(args);
521
522 screen_newline();
523 fflush(stdout);
524}
525
526/** @}
527 */
Note: See TracBrowser for help on using the repository browser.