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
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 int pos = cols - c - 1;
154 printf("%.*s", pos, str);
155 }
156}
157
158static inline void print_global_head(data_t *data)
159{
160 printf("top - %02lu:%02lu:%02lu up "
161 "%" PRIun " days, %02" PRIun ":%02" PRIun ":%02" PRIun ", "
162 "load average:",
163 data->hours, data->minutes, data->seconds,
164 data->udays, data->uhours, data->uminutes, data->useconds);
165
166 size_t i;
167 for (i = 0; i < data->load_count; i++) {
168 puts(" ");
169 stats_print_load_fragment(data->load[i], 2);
170 }
171
172 screen_newline();
173}
174
175static inline void print_task_summary(data_t *data)
176{
177 printf("tasks: %zu total", data->tasks_count);
178 screen_newline();
179}
180
181static inline void print_thread_summary(data_t *data)
182{
183 size_t total = 0;
184 size_t running = 0;
185 size_t ready = 0;
186 size_t sleeping = 0;
187 size_t lingering = 0;
188 size_t other = 0;
189 size_t invalid = 0;
190
191 size_t i;
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++;
214 }
215 }
216
217 printf("threads: %zu total, %zu running, %zu ready, "
218 "%zu sleeping, %zu lingering, %zu other, %zu invalid",
219 total, running, ready, sleeping, lingering, other, invalid);
220 screen_newline();
221}
222
223static inline void print_cpu_info(data_t *data)
224{
225 size_t i;
226 for (i = 0; i < data->cpus_count; i++) {
227 if (data->cpus[i].active) {
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",
238 data->cpus[i].id, data->cpus[i].frequency_mhz,
239 busy, busy_suffix, idle, idle_suffix);
240 puts(", idle: ");
241 print_percent(data->cpus_perc[i].idle, 2);
242 puts(", busy: ");
243 print_percent(data->cpus_perc[i].busy, 2);
244 } else
245 printf("cpu%u inactive", data->cpus[i].id);
246
247 screen_newline();
248 }
249}
250
251static inline void print_physmem_info(data_t *data)
252{
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);
270 screen_newline();
271}
272
273static inline void print_tasks_head(void)
274{
275 screen_style_inverted();
276 printf("[taskid] [threads] [virtual] [%%virt] [%%user]"
277 " [%%kernel] [name");
278 screen_newline();
279 screen_style_normal();
280}
281
282static inline void print_tasks(data_t *data)
283{
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
292 size_t i;
293 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
294 stats_task_t *task = data->tasks + data->tasks_map[i];
295 perc_task_t *perc = data->tasks_perc + data->tasks_map[i];
296
297 uint64_t virtmem;
298 char virtmem_suffix;
299 order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
300
301 printf("%-8" PRIu64 " %9zu %8" PRIu64 "%c ", task->task_id,
302 task->threads, virtmem, virtmem_suffix);
303 print_percent(perc->virtmem, 2);
304 puts(" ");
305 print_percent(perc->ucycles, 2);
306 puts(" ");
307 print_percent(perc->kcycles, 2);
308 puts(" ");
309 print_string(task->name);
310
311 screen_newline();
312 }
313
314 while (row < rows) {
315 screen_newline();
316 row++;
317 }
318}
319
320static inline void print_ipc_head(void)
321{
322 screen_style_inverted();
323 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
324 " [ans rcv] [irq rcv] [forward] [name");
325 screen_newline();
326 screen_style_normal();
327}
328
329static inline void print_ipc(data_t *data)
330{
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
339 size_t i;
340 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
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);
378
379 screen_newline();
380 }
381
382 while (row < rows) {
383 screen_newline();
384 row++;
385 }
386}
387
388static inline void print_excs_head(void)
389{
390 screen_style_inverted();
391 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
392 screen_newline();
393 screen_style_normal();
394}
395
396static inline void print_excs(data_t *data)
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;
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
412 uint64_t count;
413 uint64_t cycles;
414
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);
428
429 screen_newline();
430 row++;
431 }
432
433 while (row < rows) {
434 screen_newline();
435 row++;
436 }
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;
467
468 while (row < rows) {
469 screen_newline();
470 row++;
471 }
472}
473
474void print_data(data_t *data)
475{
476 screen_restart(false);
477 print_global_head(data);
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 */
484 screen_get_pos(&warn_col, &warn_row);
485 screen_newline();
486
487 switch (op_mode) {
488 case OP_TASKS:
489 print_tasks_head();
490 print_tasks(data);
491 break;
492 case OP_IPC:
493 print_ipc_head();
494 print_ipc(data);
495 break;
496 case OP_EXCS:
497 print_excs_head();
498 print_excs(data);
499 break;
500 case OP_HELP:
501 print_tasks_head();
502 print_help();
503 }
504
505 fflush(stdout);
506}
507
508void print_warning(const char *fmt, ...)
509{
510 screen_moveto(warn_col, warn_row);
511
512 va_list args;
513 va_start(args, fmt);
514 vprintf(fmt, args);
515 va_end(args);
516
517 screen_newline();
518 fflush(stdout);
519}
520
521/** @}
522 */
Note: See TracBrowser for help on using the repository browser.