source: mainline/uspace/app/top/screen.c@ 3193c05

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

distinguish between "hot" and "cold" exceptions
display only "hot" exceptions by default
add top to help

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