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

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

top: improve help screen and add missing keys

  • Property mode set to 100644
File size: 13.7 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 <stdlib.h>
40#include <io/console.h>
41#include <io/style.h>
42#include <vfs/vfs.h>
43#include <stdarg.h>
44#include <stats.h>
45#include <inttypes.h>
46#include "screen.h"
47#include "top.h"
48
49#define USEC_COUNT 1000000
50
51static suseconds_t timeleft = 0;
52
53console_ctrl_t *console;
54
55static sysarg_t warning_col = 0;
56static sysarg_t warning_row = 0;
57static suseconds_t warning_timeleft = 0;
58static char *warning_text = NULL;
59
60static void screen_style_normal(void)
61{
62 console_flush(console);
63 console_set_style(console, STYLE_NORMAL);
64}
65
66static void screen_style_inverted(void)
67{
68 console_flush(console);
69 console_set_style(console, STYLE_INVERTED);
70}
71
72static void screen_style_emphasis(void)
73{
74 console_flush(console);
75 console_set_style(console, STYLE_EMPHASIS);
76}
77
78static void screen_moveto(sysarg_t col, sysarg_t row)
79{
80 console_flush(console);
81 console_set_pos(console, col, row);
82}
83
84static void screen_get_pos(sysarg_t *col, sysarg_t *row)
85{
86 console_flush(console);
87 console_get_pos(console, col, row);
88}
89
90static void screen_get_size(sysarg_t *col, sysarg_t *row)
91{
92 console_flush(console);
93 console_get_size(console, col, row);
94}
95
96static void screen_restart(bool clear)
97{
98 screen_style_normal();
99
100 if (clear) {
101 console_flush(console);
102 console_clear(console);
103 }
104
105 screen_moveto(0, 0);
106}
107
108static void screen_newline(void)
109{
110 sysarg_t cols;
111 sysarg_t rows;
112 screen_get_size(&cols, &rows);
113
114 sysarg_t c;
115 sysarg_t r;
116 screen_get_pos(&c, &r);
117
118 sysarg_t i;
119 for (i = c + 1; i < cols; i++)
120 puts(" ");
121
122 if (r + 1 < rows)
123 puts("\n");
124}
125
126void screen_init(void)
127{
128 console = console_init(stdin, stdout);
129
130 console_flush(console);
131 console_cursor_visibility(console, false);
132
133 screen_restart(true);
134}
135
136void screen_done(void)
137{
138 free(warning_text);
139 warning_text = NULL;
140
141 screen_restart(true);
142
143 console_flush(console);
144 console_cursor_visibility(console, true);
145}
146
147static void print_percent(fixed_float ffloat, unsigned int precision)
148{
149 printf("%3" PRIu64 ".", ffloat.upper / ffloat.lower);
150
151 unsigned int i;
152 uint64_t rest = (ffloat.upper % ffloat.lower) * 10;
153 for (i = 0; i < precision; i++) {
154 printf("%" PRIu64, rest / ffloat.lower);
155 rest = (rest % ffloat.lower) * 10;
156 }
157
158 printf("%%");
159}
160
161static void print_string(const char *str)
162{
163 sysarg_t cols;
164 sysarg_t rows;
165 screen_get_size(&cols, &rows);
166
167 sysarg_t c;
168 sysarg_t r;
169 screen_get_pos(&c, &r);
170
171 if (c < cols) {
172 int pos = cols - c - 1;
173 printf("%.*s", pos, str);
174 }
175}
176
177static inline void print_global_head(data_t *data)
178{
179 printf("top - %02lu:%02lu:%02lu up "
180 "%" PRIun " days, %02" PRIun ":%02" PRIun ":%02" PRIun ", "
181 "load average:",
182 data->hours, data->minutes, data->seconds,
183 data->udays, data->uhours, data->uminutes, data->useconds);
184
185 size_t i;
186 for (i = 0; i < data->load_count; i++) {
187 puts(" ");
188 stats_print_load_fragment(data->load[i], 2);
189 }
190
191 screen_newline();
192}
193
194static inline void print_task_summary(data_t *data)
195{
196 printf("tasks: %zu total", data->tasks_count);
197 screen_newline();
198}
199
200static inline void print_thread_summary(data_t *data)
201{
202 size_t total = 0;
203 size_t running = 0;
204 size_t ready = 0;
205 size_t sleeping = 0;
206 size_t lingering = 0;
207 size_t other = 0;
208 size_t invalid = 0;
209
210 size_t i;
211 for (i = 0; i < data->threads_count; i++) {
212 total++;
213
214 switch (data->threads[i].state) {
215 case Running:
216 running++;
217 break;
218 case Ready:
219 ready++;
220 break;
221 case Sleeping:
222 sleeping++;
223 break;
224 case Lingering:
225 lingering++;
226 break;
227 case Entering:
228 case Exiting:
229 other++;
230 break;
231 default:
232 invalid++;
233 }
234 }
235
236 printf("threads: %zu total, %zu running, %zu ready, "
237 "%zu sleeping, %zu lingering, %zu other, %zu invalid",
238 total, running, ready, sleeping, lingering, other, invalid);
239 screen_newline();
240}
241
242static inline void print_cpu_info(data_t *data)
243{
244 size_t i;
245 for (i = 0; i < data->cpus_count; i++) {
246 if (data->cpus[i].active) {
247 uint64_t busy;
248 uint64_t idle;
249 char busy_suffix;
250 char idle_suffix;
251
252 order_suffix(data->cpus[i].busy_cycles, &busy, &busy_suffix);
253 order_suffix(data->cpus[i].idle_cycles, &idle, &idle_suffix);
254
255 printf("cpu%u (%4" PRIu16 " MHz): busy cycles: "
256 "%" PRIu64 "%c, idle cycles: %" PRIu64 "%c",
257 data->cpus[i].id, data->cpus[i].frequency_mhz,
258 busy, busy_suffix, idle, idle_suffix);
259 puts(", idle: ");
260 print_percent(data->cpus_perc[i].idle, 2);
261 puts(", busy: ");
262 print_percent(data->cpus_perc[i].busy, 2);
263 } else
264 printf("cpu%u inactive", data->cpus[i].id);
265
266 screen_newline();
267 }
268}
269
270static inline void print_physmem_info(data_t *data)
271{
272 uint64_t total;
273 uint64_t unavail;
274 uint64_t used;
275 uint64_t free;
276 const char *total_suffix;
277 const char *unavail_suffix;
278 const char *used_suffix;
279 const char *free_suffix;
280
281 bin_order_suffix(data->physmem->total, &total, &total_suffix, false);
282 bin_order_suffix(data->physmem->unavail, &unavail, &unavail_suffix, false);
283 bin_order_suffix(data->physmem->used, &used, &used_suffix, false);
284 bin_order_suffix(data->physmem->free, &free, &free_suffix, false);
285
286 printf("memory: %" PRIu64 "%s total, %" PRIu64 "%s unavail, %"
287 PRIu64 "%s used, %" PRIu64 "%s free", total, total_suffix,
288 unavail, unavail_suffix, used, used_suffix, free, free_suffix);
289 screen_newline();
290}
291
292static inline void print_tasks_head(void)
293{
294 screen_style_inverted();
295 printf("[taskid] [thrds] [resident] [%%resi] [virtual] [%%virt]"
296 " [%%user] [%%kern] [name");
297 screen_newline();
298 screen_style_normal();
299}
300
301static inline void print_tasks(data_t *data)
302{
303 sysarg_t cols;
304 sysarg_t rows;
305 screen_get_size(&cols, &rows);
306
307 sysarg_t col;
308 sysarg_t row;
309 screen_get_pos(&col, &row);
310
311 size_t i;
312 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
313 stats_task_t *task = data->tasks + data->tasks_map[i];
314 perc_task_t *perc = data->tasks_perc + data->tasks_map[i];
315
316 uint64_t resmem;
317 const char *resmem_suffix;
318 bin_order_suffix(task->resmem, &resmem, &resmem_suffix, true);
319
320 uint64_t virtmem;
321 const char *virtmem_suffix;
322 bin_order_suffix(task->virtmem, &virtmem, &virtmem_suffix, true);
323
324 printf("%-8" PRIu64 " %7zu %7" PRIu64 "%s ",
325 task->task_id, task->threads, resmem, resmem_suffix);
326 print_percent(perc->resmem, 2);
327 printf(" %6" PRIu64 "%s ", virtmem, virtmem_suffix);
328 print_percent(perc->virtmem, 2);
329 puts(" ");
330 print_percent(perc->ucycles, 2);
331 puts(" ");
332 print_percent(perc->kcycles, 2);
333 puts(" ");
334 print_string(task->name);
335
336 screen_newline();
337 }
338
339 while (row < rows) {
340 screen_newline();
341 row++;
342 }
343}
344
345static inline void print_ipc_head(void)
346{
347 screen_style_inverted();
348 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
349 " [ans rcv] [irq rcv] [forward] [name");
350 screen_newline();
351 screen_style_normal();
352}
353
354static inline void print_ipc(data_t *data)
355{
356 sysarg_t cols;
357 sysarg_t rows;
358 screen_get_size(&cols, &rows);
359
360 sysarg_t col;
361 sysarg_t row;
362 screen_get_pos(&col, &row);
363
364 size_t i;
365 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
366 uint64_t call_sent;
367 uint64_t call_received;
368 uint64_t answer_sent;
369 uint64_t answer_received;
370 uint64_t irq_notif_received;
371 uint64_t forwarded;
372
373 char call_sent_suffix;
374 char call_received_suffix;
375 char answer_sent_suffix;
376 char answer_received_suffix;
377 char irq_notif_received_suffix;
378 char forwarded_suffix;
379
380 order_suffix(data->tasks[i].ipc_info.call_sent, &call_sent,
381 &call_sent_suffix);
382 order_suffix(data->tasks[i].ipc_info.call_received,
383 &call_received, &call_received_suffix);
384 order_suffix(data->tasks[i].ipc_info.answer_sent,
385 &answer_sent, &answer_sent_suffix);
386 order_suffix(data->tasks[i].ipc_info.answer_received,
387 &answer_received, &answer_received_suffix);
388 order_suffix(data->tasks[i].ipc_info.irq_notif_received,
389 &irq_notif_received, &irq_notif_received_suffix);
390 order_suffix(data->tasks[i].ipc_info.forwarded, &forwarded,
391 &forwarded_suffix);
392
393 printf("%-8" PRIu64 " %8" PRIu64 "%c %8" PRIu64 "%c"
394 " %8" PRIu64 "%c %8" PRIu64 "%c %8" PRIu64 "%c"
395 " %8" PRIu64 "%c ", data->tasks[i].task_id,
396 call_sent, call_sent_suffix,
397 call_received, call_received_suffix,
398 answer_sent, answer_sent_suffix,
399 answer_received, answer_received_suffix,
400 irq_notif_received, irq_notif_received_suffix,
401 forwarded, forwarded_suffix);
402 print_string(data->tasks[i].name);
403
404 screen_newline();
405 }
406
407 while (row < rows) {
408 screen_newline();
409 row++;
410 }
411}
412
413static inline void print_excs_head(void)
414{
415 screen_style_inverted();
416 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
417 screen_newline();
418 screen_style_normal();
419}
420
421static inline void print_excs(data_t *data)
422{
423 sysarg_t cols;
424 sysarg_t rows;
425 screen_get_size(&cols, &rows);
426
427 sysarg_t col;
428 sysarg_t row;
429 screen_get_pos(&col, &row);
430
431 size_t i;
432 for (i = 0; (i < data->exceptions_count) && (row < rows); i++) {
433 /* Filter-out cold exceptions if not instructed otherwise */
434 if ((!excs_all) && (!data->exceptions[i].hot))
435 continue;
436
437 uint64_t count;
438 uint64_t cycles;
439
440 char count_suffix;
441 char cycles_suffix;
442
443 order_suffix(data->exceptions[i].count, &count, &count_suffix);
444 order_suffix(data->exceptions[i].cycles, &cycles, &cycles_suffix);
445
446 printf("%-8u %9" PRIu64 "%c ",
447 data->exceptions[i].id, count, count_suffix);
448 print_percent(data->exceptions_perc[i].count, 2);
449 printf(" %9" PRIu64 "%c ", cycles, cycles_suffix);
450 print_percent(data->exceptions_perc[i].cycles, 2);
451 puts(" ");
452 print_string(data->exceptions[i].desc);
453
454 screen_newline();
455 row++;
456 }
457
458 while (row < rows) {
459 screen_newline();
460 row++;
461 }
462}
463
464static inline void print_help_head(void)
465{
466 screen_style_inverted();
467 printf("Help");
468 screen_newline();
469 screen_style_normal();
470}
471
472static void print_help(void)
473{
474 sysarg_t cols;
475 sysarg_t rows;
476 screen_get_size(&cols, &rows);
477
478 screen_newline();
479
480 printf("Operation modes:");
481 screen_newline();
482
483 printf(" t .. tasks statistics");
484 screen_newline();
485
486 printf(" i .. IPC statistics");
487 screen_newline();
488
489 printf(" e .. exceptions statistics");
490 screen_newline();
491
492 printf(" a .. toggle display of all/hot exceptions");
493 screen_newline();
494
495 printf(" h .. this help screen");
496 screen_newline();
497
498 screen_newline();
499
500 printf("Other keys:");
501 screen_newline();
502
503 printf(" q .. quit");
504 screen_newline();
505
506 sysarg_t col;
507 sysarg_t row;
508 screen_get_pos(&col, &row);
509
510 while (row < rows) {
511 screen_newline();
512 row++;
513 }
514}
515
516static void print_warning(void)
517{
518 screen_get_pos(&warning_col, &warning_row);
519 if (warning_timeleft > 0) {
520 screen_style_emphasis();
521 print_string(warning_text);
522 screen_style_normal();
523 } else {
524 free(warning_text);
525 warning_text = NULL;
526 }
527 screen_newline();
528}
529
530void print_data(data_t *data)
531{
532 screen_restart(false);
533 print_global_head(data);
534 print_task_summary(data);
535 print_thread_summary(data);
536 print_cpu_info(data);
537 print_physmem_info(data);
538 print_warning();
539
540 switch (op_mode) {
541 case OP_TASKS:
542 print_tasks_head();
543 print_tasks(data);
544 break;
545 case OP_IPC:
546 print_ipc_head();
547 print_ipc(data);
548 break;
549 case OP_EXCS:
550 print_excs_head();
551 print_excs(data);
552 break;
553 case OP_HELP:
554 print_help_head();
555 print_help();
556 }
557
558 console_flush(console);
559}
560
561void show_warning(const char *fmt, ...)
562{
563 sysarg_t cols;
564 sysarg_t rows;
565 screen_get_size(&cols, &rows);
566
567 size_t warning_text_size = 1 + cols * sizeof(*warning_text);
568 free(warning_text);
569 warning_text = malloc(warning_text_size);
570 if (!warning_text)
571 return;
572
573 va_list args;
574 va_start(args, fmt);
575 vsnprintf(warning_text, warning_text_size, fmt, args);
576 va_end(args);
577
578 warning_timeleft = 2 * USEC_COUNT;
579
580 screen_moveto(warning_col, warning_row);
581 print_warning();
582 console_flush(console);
583}
584
585/** Get char with timeout
586 *
587 */
588int tgetchar(unsigned int sec)
589{
590 /*
591 * Reset timeleft whenever it is not positive.
592 */
593
594 if (timeleft <= 0)
595 timeleft = sec * USEC_COUNT;
596
597 /*
598 * Wait to see if there is any input. If so, take it and
599 * update timeleft so that the next call to tgetchar()
600 * will not wait as long. If there is no input,
601 * make timeleft zero and return -1.
602 */
603
604 wchar_t c = 0;
605
606 while (c == 0) {
607 kbd_event_t event;
608
609 warning_timeleft -= timeleft;
610 if (!console_get_kbd_event_timeout(console, &event, &timeleft)) {
611 timeleft = 0;
612 return -1;
613 }
614 warning_timeleft += timeleft;
615
616 if (event.type == KEY_PRESS)
617 c = event.c;
618 }
619
620 return (int) c;
621}
622
623/** @}
624 */
Note: See TracBrowser for help on using the repository browser.