source: mainline/uspace/app/top/screen.c@ 2fa10f6

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

add initial support for sorting the output of top

  • 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 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 uint64_t busy;
225 uint64_t idle;
226 char busy_suffix;
227 char idle_suffix;
228
229 order_suffix(data->cpus[i].busy_cycles, &busy, &busy_suffix);
230 order_suffix(data->cpus[i].idle_cycles, &idle, &idle_suffix);
231
232 printf("cpu%u (%4" PRIu16 " MHz): busy cycles: "
233 "%" PRIu64 "%c, idle cycles: %" PRIu64 "%c",
234 data->cpus[i].id, data->cpus[i].frequency_mhz,
235 busy, busy_suffix, idle, idle_suffix);
236 puts(", idle: ");
237 print_percent(data->cpus_perc[i].idle, 2);
238 puts(", busy: ");
239 print_percent(data->cpus_perc[i].busy, 2);
240 } else
241 printf("cpu%u inactive", data->cpus[i].id);
242
243 screen_newline();
244 }
245}
246
247static inline void print_physmem_info(data_t *data)
248{
249 uint64_t total;
250 uint64_t unavail;
251 uint64_t used;
252 uint64_t free;
253 char total_suffix;
254 char unavail_suffix;
255 char used_suffix;
256 char free_suffix;
257
258 order_suffix(data->physmem->total, &total, &total_suffix);
259 order_suffix(data->physmem->unavail, &unavail, &unavail_suffix);
260 order_suffix(data->physmem->used, &used, &used_suffix);
261 order_suffix(data->physmem->free, &free, &free_suffix);
262
263 printf("memory: %" PRIu64 "%c total, %" PRIu64 "%c unavail, %"
264 PRIu64 "%c used, %" PRIu64 "%c free", total, total_suffix,
265 unavail, unavail_suffix, used, used_suffix, free, free_suffix);
266 screen_newline();
267}
268
269static inline void print_tasks_head(void)
270{
271 screen_style_inverted();
272 printf("[taskid] [threads] [virtual] [%%virt] [%%user]"
273 " [%%kernel] [name");
274 screen_newline();
275 screen_style_normal();
276}
277
278static inline void print_tasks(data_t *data)
279{
280 ipcarg_t cols;
281 ipcarg_t rows;
282 screen_get_size(&cols, &rows);
283
284 ipcarg_t col;
285 ipcarg_t row;
286 screen_get_pos(&col, &row);
287
288 size_t i;
289 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
290 stats_task_t *task = data->tasks + data->tasks_map[i];
291 perc_task_t *perc = data->tasks_perc + data->tasks_map[i];
292
293 uint64_t virtmem;
294 char virtmem_suffix;
295 order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
296
297 printf("%-8" PRIu64 " %9u %8" PRIu64 "%c ", task->task_id,
298 task->threads, virtmem, virtmem_suffix);
299 print_percent(perc->virtmem, 2);
300 puts(" ");
301 print_percent(perc->ucycles, 2);
302 puts(" ");
303 print_percent(perc->kcycles, 2);
304 puts(" ");
305 print_string(task->name);
306
307 screen_newline();
308 }
309
310 while (row < rows) {
311 screen_newline();
312 row++;
313 }
314}
315
316static inline void print_ipc_head(void)
317{
318 screen_style_inverted();
319 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
320 " [ans rcv] [irq rcv] [forward] [name");
321 screen_newline();
322 screen_style_normal();
323}
324
325static inline void print_ipc(data_t *data)
326{
327 ipcarg_t cols;
328 ipcarg_t rows;
329 screen_get_size(&cols, &rows);
330
331 ipcarg_t col;
332 ipcarg_t row;
333 screen_get_pos(&col, &row);
334
335 size_t i;
336 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
337 uint64_t call_sent;
338 uint64_t call_received;
339 uint64_t answer_sent;
340 uint64_t answer_received;
341 uint64_t irq_notif_received;
342 uint64_t forwarded;
343
344 char call_sent_suffix;
345 char call_received_suffix;
346 char answer_sent_suffix;
347 char answer_received_suffix;
348 char irq_notif_received_suffix;
349 char forwarded_suffix;
350
351 order_suffix(data->tasks[i].ipc_info.call_sent, &call_sent,
352 &call_sent_suffix);
353 order_suffix(data->tasks[i].ipc_info.call_received,
354 &call_received, &call_received_suffix);
355 order_suffix(data->tasks[i].ipc_info.answer_sent,
356 &answer_sent, &answer_sent_suffix);
357 order_suffix(data->tasks[i].ipc_info.answer_received,
358 &answer_received, &answer_received_suffix);
359 order_suffix(data->tasks[i].ipc_info.irq_notif_received,
360 &irq_notif_received, &irq_notif_received_suffix);
361 order_suffix(data->tasks[i].ipc_info.forwarded, &forwarded,
362 &forwarded_suffix);
363
364 printf("%-8" PRIu64 " %8" PRIu64 "%c %8" PRIu64 "%c"
365 " %8" PRIu64 "%c %8" PRIu64 "%c %8" PRIu64 "%c"
366 " %8" PRIu64 "%c ", data->tasks[i].task_id,
367 call_sent, call_sent_suffix,
368 call_received, call_received_suffix,
369 answer_sent, answer_sent_suffix,
370 answer_received, answer_received_suffix,
371 irq_notif_received, irq_notif_received_suffix,
372 forwarded, forwarded_suffix);
373 print_string(data->tasks[i].name);
374
375 screen_newline();
376 }
377
378 while (row < rows) {
379 screen_newline();
380 row++;
381 }
382}
383
384static inline void print_excs_head(void)
385{
386 screen_style_inverted();
387 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
388 screen_newline();
389 screen_style_normal();
390}
391
392static inline void print_excs(data_t *data)
393{
394 ipcarg_t cols;
395 ipcarg_t rows;
396 screen_get_size(&cols, &rows);
397
398 ipcarg_t col;
399 ipcarg_t row;
400 screen_get_pos(&col, &row);
401
402 size_t i;
403 for (i = 0; (i < data->exceptions_count) && (row < rows); i++) {
404 /* Filter-out cold exceptions if not instructed otherwise */
405 if ((!excs_all) && (!data->exceptions[i].hot))
406 continue;
407
408 uint64_t count;
409 uint64_t cycles;
410
411 char count_suffix;
412 char cycles_suffix;
413
414 order_suffix(data->exceptions[i].count, &count, &count_suffix);
415 order_suffix(data->exceptions[i].cycles, &cycles, &cycles_suffix);
416
417 printf("%-8u %9" PRIu64 "%c ",
418 data->exceptions[i].id, count, count_suffix);
419 print_percent(data->exceptions_perc[i].count, 2);
420 printf(" %9" PRIu64 "%c ", cycles, cycles_suffix);
421 print_percent(data->exceptions_perc[i].cycles, 2);
422 puts(" ");
423 print_string(data->exceptions[i].desc);
424
425 screen_newline();
426 row++;
427 }
428
429 while (row < rows) {
430 screen_newline();
431 row++;
432 }
433}
434
435static void print_help(void)
436{
437 ipcarg_t cols;
438 ipcarg_t rows;
439 screen_get_size(&cols, &rows);
440
441 ipcarg_t col;
442 ipcarg_t row;
443 screen_get_pos(&col, &row);
444
445 screen_newline();
446
447 printf("Operation modes:");
448 screen_newline();
449
450 printf(" t .. tasks statistics");
451 screen_newline();
452
453 printf(" i .. IPC statistics");
454 screen_newline();
455
456 printf(" e .. exceptions statistics");
457 screen_newline();
458
459 printf(" a .. toggle display of all/hot exceptions");
460 screen_newline();
461
462 row += 6;
463
464 while (row < rows) {
465 screen_newline();
466 row++;
467 }
468}
469
470void print_data(data_t *data)
471{
472 screen_restart(false);
473 print_global_head(data);
474 print_task_summary(data);
475 print_thread_summary(data);
476 print_cpu_info(data);
477 print_physmem_info(data);
478
479 /* Empty row for warnings */
480 screen_get_pos(&warn_col, &warn_row);
481 screen_newline();
482
483 switch (op_mode) {
484 case OP_TASKS:
485 print_tasks_head();
486 print_tasks(data);
487 break;
488 case OP_IPC:
489 print_ipc_head();
490 print_ipc(data);
491 break;
492 case OP_EXCS:
493 print_excs_head();
494 print_excs(data);
495 break;
496 case OP_HELP:
497 print_tasks_head();
498 print_help();
499 }
500
501 fflush(stdout);
502}
503
504void print_warning(const char *fmt, ...)
505{
506 screen_moveto(warn_col, warn_row);
507
508 va_list args;
509 va_start(args, fmt);
510 vprintf(fmt, args);
511 va_end(args);
512
513 screen_newline();
514 fflush(stdout);
515}
516
517/** @}
518 */
Note: See TracBrowser for help on using the repository browser.