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 |
|
---|
49 | static sysarg_t warn_col = 0;
|
---|
50 | static sysarg_t warn_row = 0;
|
---|
51 |
|
---|
52 | static void screen_style_normal(void)
|
---|
53 | {
|
---|
54 | fflush(stdout);
|
---|
55 | console_set_style(fphone(stdout), STYLE_NORMAL);
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void screen_style_inverted(void)
|
---|
59 | {
|
---|
60 | fflush(stdout);
|
---|
61 | console_set_style(fphone(stdout), STYLE_INVERTED);
|
---|
62 | }
|
---|
63 |
|
---|
64 | static void screen_moveto(sysarg_t col, sysarg_t row)
|
---|
65 | {
|
---|
66 | fflush(stdout);
|
---|
67 | console_set_pos(fphone(stdout), col, row);
|
---|
68 | }
|
---|
69 |
|
---|
70 | static void screen_get_pos(sysarg_t *col, sysarg_t *row)
|
---|
71 | {
|
---|
72 | fflush(stdout);
|
---|
73 | console_get_pos(fphone(stdout), col, row);
|
---|
74 | }
|
---|
75 |
|
---|
76 | static void screen_get_size(sysarg_t *col, sysarg_t *row)
|
---|
77 | {
|
---|
78 | fflush(stdout);
|
---|
79 | console_get_size(fphone(stdout), col, row);
|
---|
80 | }
|
---|
81 |
|
---|
82 | static 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 |
|
---|
94 | static void screen_newline(void)
|
---|
95 | {
|
---|
96 | sysarg_t cols;
|
---|
97 | sysarg_t rows;
|
---|
98 | screen_get_size(&cols, &rows);
|
---|
99 |
|
---|
100 | sysarg_t c;
|
---|
101 | sysarg_t r;
|
---|
102 | screen_get_pos(&c, &r);
|
---|
103 |
|
---|
104 | sysarg_t i;
|
---|
105 | for (i = c + 1; i < cols; i++)
|
---|
106 | puts(" ");
|
---|
107 |
|
---|
108 | if (r + 1 < rows)
|
---|
109 | puts("\n");
|
---|
110 | }
|
---|
111 |
|
---|
112 | void screen_init(void)
|
---|
113 | {
|
---|
114 | fflush(stdout);
|
---|
115 | console_cursor_visibility(fphone(stdout), false);
|
---|
116 |
|
---|
117 | screen_restart(true);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void screen_done(void)
|
---|
121 | {
|
---|
122 | screen_restart(true);
|
---|
123 |
|
---|
124 | fflush(stdout);
|
---|
125 | console_cursor_visibility(fphone(stdout), true);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static 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 |
|
---|
142 | static void print_string(const char *str)
|
---|
143 | {
|
---|
144 | sysarg_t cols;
|
---|
145 | sysarg_t rows;
|
---|
146 | screen_get_size(&cols, &rows);
|
---|
147 |
|
---|
148 | sysarg_t c;
|
---|
149 | sysarg_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 |
|
---|
158 | static 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 |
|
---|
175 | static inline void print_task_summary(data_t *data)
|
---|
176 | {
|
---|
177 | printf("tasks: %zu total", data->tasks_count);
|
---|
178 | screen_newline();
|
---|
179 | }
|
---|
180 |
|
---|
181 | static 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 |
|
---|
223 | static 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 |
|
---|
251 | static 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 |
|
---|
273 | static inline void print_tasks_head(void)
|
---|
274 | {
|
---|
275 | screen_style_inverted();
|
---|
276 | printf("[taskid] [thrds] [resident] [%%resi] [virtual] [%%virt]"
|
---|
277 | " [%%user] [%%kern] [name");
|
---|
278 | screen_newline();
|
---|
279 | screen_style_normal();
|
---|
280 | }
|
---|
281 |
|
---|
282 | static inline void print_tasks(data_t *data)
|
---|
283 | {
|
---|
284 | sysarg_t cols;
|
---|
285 | sysarg_t rows;
|
---|
286 | screen_get_size(&cols, &rows);
|
---|
287 |
|
---|
288 | sysarg_t col;
|
---|
289 | sysarg_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 resmem;
|
---|
298 | char resmem_suffix;
|
---|
299 | order_suffix(task->resmem, &resmem, &resmem_suffix);
|
---|
300 |
|
---|
301 | uint64_t virtmem;
|
---|
302 | char virtmem_suffix;
|
---|
303 | order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
|
---|
304 |
|
---|
305 | printf("%-8" PRIu64 " %7zu %9" PRIu64 "%c ",
|
---|
306 | task->task_id, task->threads, resmem, resmem_suffix);
|
---|
307 | print_percent(perc->resmem, 2);
|
---|
308 | printf(" %8" PRIu64 "%c ", virtmem, virtmem_suffix);
|
---|
309 | print_percent(perc->virtmem, 2);
|
---|
310 | puts(" ");
|
---|
311 | print_percent(perc->ucycles, 2);
|
---|
312 | puts(" ");
|
---|
313 | print_percent(perc->kcycles, 2);
|
---|
314 | puts(" ");
|
---|
315 | print_string(task->name);
|
---|
316 |
|
---|
317 | screen_newline();
|
---|
318 | }
|
---|
319 |
|
---|
320 | while (row < rows) {
|
---|
321 | screen_newline();
|
---|
322 | row++;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | static inline void print_ipc_head(void)
|
---|
327 | {
|
---|
328 | screen_style_inverted();
|
---|
329 | printf("[taskid] [cls snt] [cls rcv] [ans snt]"
|
---|
330 | " [ans rcv] [irq rcv] [forward] [name");
|
---|
331 | screen_newline();
|
---|
332 | screen_style_normal();
|
---|
333 | }
|
---|
334 |
|
---|
335 | static inline void print_ipc(data_t *data)
|
---|
336 | {
|
---|
337 | sysarg_t cols;
|
---|
338 | sysarg_t rows;
|
---|
339 | screen_get_size(&cols, &rows);
|
---|
340 |
|
---|
341 | sysarg_t col;
|
---|
342 | sysarg_t row;
|
---|
343 | screen_get_pos(&col, &row);
|
---|
344 |
|
---|
345 | size_t i;
|
---|
346 | for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
|
---|
347 | uint64_t call_sent;
|
---|
348 | uint64_t call_received;
|
---|
349 | uint64_t answer_sent;
|
---|
350 | uint64_t answer_received;
|
---|
351 | uint64_t irq_notif_received;
|
---|
352 | uint64_t forwarded;
|
---|
353 |
|
---|
354 | char call_sent_suffix;
|
---|
355 | char call_received_suffix;
|
---|
356 | char answer_sent_suffix;
|
---|
357 | char answer_received_suffix;
|
---|
358 | char irq_notif_received_suffix;
|
---|
359 | char forwarded_suffix;
|
---|
360 |
|
---|
361 | order_suffix(data->tasks[i].ipc_info.call_sent, &call_sent,
|
---|
362 | &call_sent_suffix);
|
---|
363 | order_suffix(data->tasks[i].ipc_info.call_received,
|
---|
364 | &call_received, &call_received_suffix);
|
---|
365 | order_suffix(data->tasks[i].ipc_info.answer_sent,
|
---|
366 | &answer_sent, &answer_sent_suffix);
|
---|
367 | order_suffix(data->tasks[i].ipc_info.answer_received,
|
---|
368 | &answer_received, &answer_received_suffix);
|
---|
369 | order_suffix(data->tasks[i].ipc_info.irq_notif_received,
|
---|
370 | &irq_notif_received, &irq_notif_received_suffix);
|
---|
371 | order_suffix(data->tasks[i].ipc_info.forwarded, &forwarded,
|
---|
372 | &forwarded_suffix);
|
---|
373 |
|
---|
374 | printf("%-8" PRIu64 " %8" PRIu64 "%c %8" PRIu64 "%c"
|
---|
375 | " %8" PRIu64 "%c %8" PRIu64 "%c %8" PRIu64 "%c"
|
---|
376 | " %8" PRIu64 "%c ", data->tasks[i].task_id,
|
---|
377 | call_sent, call_sent_suffix,
|
---|
378 | call_received, call_received_suffix,
|
---|
379 | answer_sent, answer_sent_suffix,
|
---|
380 | answer_received, answer_received_suffix,
|
---|
381 | irq_notif_received, irq_notif_received_suffix,
|
---|
382 | forwarded, forwarded_suffix);
|
---|
383 | print_string(data->tasks[i].name);
|
---|
384 |
|
---|
385 | screen_newline();
|
---|
386 | }
|
---|
387 |
|
---|
388 | while (row < rows) {
|
---|
389 | screen_newline();
|
---|
390 | row++;
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | static inline void print_excs_head(void)
|
---|
395 | {
|
---|
396 | screen_style_inverted();
|
---|
397 | printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
|
---|
398 | screen_newline();
|
---|
399 | screen_style_normal();
|
---|
400 | }
|
---|
401 |
|
---|
402 | static inline void print_excs(data_t *data)
|
---|
403 | {
|
---|
404 | sysarg_t cols;
|
---|
405 | sysarg_t rows;
|
---|
406 | screen_get_size(&cols, &rows);
|
---|
407 |
|
---|
408 | sysarg_t col;
|
---|
409 | sysarg_t row;
|
---|
410 | screen_get_pos(&col, &row);
|
---|
411 |
|
---|
412 | size_t i;
|
---|
413 | for (i = 0; (i < data->exceptions_count) && (row < rows); i++) {
|
---|
414 | /* Filter-out cold exceptions if not instructed otherwise */
|
---|
415 | if ((!excs_all) && (!data->exceptions[i].hot))
|
---|
416 | continue;
|
---|
417 |
|
---|
418 | uint64_t count;
|
---|
419 | uint64_t cycles;
|
---|
420 |
|
---|
421 | char count_suffix;
|
---|
422 | char cycles_suffix;
|
---|
423 |
|
---|
424 | order_suffix(data->exceptions[i].count, &count, &count_suffix);
|
---|
425 | order_suffix(data->exceptions[i].cycles, &cycles, &cycles_suffix);
|
---|
426 |
|
---|
427 | printf("%-8u %9" PRIu64 "%c ",
|
---|
428 | data->exceptions[i].id, count, count_suffix);
|
---|
429 | print_percent(data->exceptions_perc[i].count, 2);
|
---|
430 | printf(" %9" PRIu64 "%c ", cycles, cycles_suffix);
|
---|
431 | print_percent(data->exceptions_perc[i].cycles, 2);
|
---|
432 | puts(" ");
|
---|
433 | print_string(data->exceptions[i].desc);
|
---|
434 |
|
---|
435 | screen_newline();
|
---|
436 | row++;
|
---|
437 | }
|
---|
438 |
|
---|
439 | while (row < rows) {
|
---|
440 | screen_newline();
|
---|
441 | row++;
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | static void print_help(void)
|
---|
446 | {
|
---|
447 | sysarg_t cols;
|
---|
448 | sysarg_t rows;
|
---|
449 | screen_get_size(&cols, &rows);
|
---|
450 |
|
---|
451 | sysarg_t col;
|
---|
452 | sysarg_t row;
|
---|
453 | screen_get_pos(&col, &row);
|
---|
454 |
|
---|
455 | screen_newline();
|
---|
456 |
|
---|
457 | printf("Operation modes:");
|
---|
458 | screen_newline();
|
---|
459 |
|
---|
460 | printf(" t .. tasks statistics");
|
---|
461 | screen_newline();
|
---|
462 |
|
---|
463 | printf(" i .. IPC statistics");
|
---|
464 | screen_newline();
|
---|
465 |
|
---|
466 | printf(" e .. exceptions statistics");
|
---|
467 | screen_newline();
|
---|
468 |
|
---|
469 | printf(" a .. toggle display of all/hot exceptions");
|
---|
470 | screen_newline();
|
---|
471 |
|
---|
472 | row += 6;
|
---|
473 |
|
---|
474 | while (row < rows) {
|
---|
475 | screen_newline();
|
---|
476 | row++;
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | void print_data(data_t *data)
|
---|
481 | {
|
---|
482 | screen_restart(false);
|
---|
483 | print_global_head(data);
|
---|
484 | print_task_summary(data);
|
---|
485 | print_thread_summary(data);
|
---|
486 | print_cpu_info(data);
|
---|
487 | print_physmem_info(data);
|
---|
488 |
|
---|
489 | /* Empty row for warnings */
|
---|
490 | screen_get_pos(&warn_col, &warn_row);
|
---|
491 | screen_newline();
|
---|
492 |
|
---|
493 | switch (op_mode) {
|
---|
494 | case OP_TASKS:
|
---|
495 | print_tasks_head();
|
---|
496 | print_tasks(data);
|
---|
497 | break;
|
---|
498 | case OP_IPC:
|
---|
499 | print_ipc_head();
|
---|
500 | print_ipc(data);
|
---|
501 | break;
|
---|
502 | case OP_EXCS:
|
---|
503 | print_excs_head();
|
---|
504 | print_excs(data);
|
---|
505 | break;
|
---|
506 | case OP_HELP:
|
---|
507 | print_tasks_head();
|
---|
508 | print_help();
|
---|
509 | }
|
---|
510 |
|
---|
511 | fflush(stdout);
|
---|
512 | }
|
---|
513 |
|
---|
514 | void print_warning(const char *fmt, ...)
|
---|
515 | {
|
---|
516 | screen_moveto(warn_col, warn_row);
|
---|
517 |
|
---|
518 | va_list args;
|
---|
519 | va_start(args, fmt);
|
---|
520 | vprintf(fmt, args);
|
---|
521 | va_end(args);
|
---|
522 |
|
---|
523 | screen_newline();
|
---|
524 | fflush(stdout);
|
---|
525 | }
|
---|
526 |
|
---|
527 | /** @}
|
---|
528 | */
|
---|