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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ca07cd3 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
RevLine 
[5c058d50]1/*
2 * Copyright (c) 2010 Stanislav Kozina
[dec16a2]3 * Copyright (c) 2010 Martin Decky
[5c058d50]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>
[9f1362d4]39#include <ipc/ipc.h>
[5c058d50]40#include <io/console.h>
[9f1362d4]41#include <io/style.h>
[5c058d50]42#include <vfs/vfs.h>
[dec16a2]43#include <stdarg.h>
44#include <stats.h>
45#include <inttypes.h>
[5c058d50]46#include "screen.h"
[79edc36]47#include "top.h"
[5c058d50]48
[9f1362d4]49static ipcarg_t warn_col = 0;
50static ipcarg_t warn_row = 0;
[8f56d93]51
[9f1362d4]52static void screen_style_normal(void)
53{
54 fflush(stdout);
55 console_set_style(fphone(stdout), STYLE_NORMAL);
56}
[8f56d93]57
[9f1362d4]58static void screen_style_inverted(void)
[ee35ba0b]59{
[9f1362d4]60 fflush(stdout);
61 console_set_style(fphone(stdout), STYLE_INVERTED);
[ee35ba0b]62}
63
[9f1362d4]64static void screen_moveto(ipcarg_t col, ipcarg_t row)
[5c058d50]65{
66 fflush(stdout);
[9f1362d4]67 console_set_pos(fphone(stdout), col, row);
[5c058d50]68}
69
[9f1362d4]70static void screen_get_pos(ipcarg_t *col, ipcarg_t *row)
[5c058d50]71{
[dec16a2]72 fflush(stdout);
[9f1362d4]73 console_get_pos(fphone(stdout), col, row);
[5c058d50]74}
75
[9f1362d4]76static void screen_get_size(ipcarg_t *col, ipcarg_t *row)
[5c058d50]77{
[8b2aba5]78 fflush(stdout);
[9f1362d4]79 console_get_size(fphone(stdout), col, row);
[5c058d50]80}
81
[9f1362d4]82static void screen_restart(bool clear)
[5c058d50]83{
[9f1362d4]84 screen_style_normal();
85
86 if (clear) {
87 fflush(stdout);
88 console_clear(fphone(stdout));
89 }
[dec16a2]90
[9f1362d4]91 screen_moveto(0, 0);
[dec16a2]92}
93
[9f1362d4]94static void screen_newline(void)
[dec16a2]95{
[9f1362d4]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);
[5c058d50]118}
119
[9f1362d4]120void screen_done(void)
[79edc36]121{
[9f1362d4]122 screen_restart(true);
123
124 fflush(stdout);
125 console_cursor_visibility(fphone(stdout), true);
[79edc36]126}
127
[be06914]128static void print_percent(fixed_float ffloat, unsigned int precision)
[79edc36]129{
[be06914]130 printf("%3" PRIu64 ".", ffloat.upper / ffloat.lower);
[9f1362d4]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 }
[be06914]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);
[79edc36]154}
155
[9f1362d4]156static inline void print_global_head(data_t *data)
[dd6c71c]157{
[80badbe]158 printf("top - %02lu:%02lu:%02lu up %u days, %02u:%02u:%02u, load average:",
[9f1362d4]159 data->hours, data->minutes, data->seconds,
160 data->udays, data->uhours, data->uminutes, data->useconds);
[dec16a2]161
162 size_t i;
163 for (i = 0; i < data->load_count; i++) {
[9f1362d4]164 puts(" ");
[dec16a2]165 stats_print_load_fragment(data->load[i], 2);
166 }
[9f1362d4]167
168 screen_newline();
[dd6c71c]169}
170
[dec16a2]171static inline void print_task_summary(data_t *data)
[dd6c71c]172{
[dec16a2]173 printf("tasks: %u total", data->tasks_count);
[9f1362d4]174 screen_newline();
[dd6c71c]175}
176
[dec16a2]177static inline void print_thread_summary(data_t *data)
[638927a]178{
[dec16a2]179 size_t total = 0;
[638927a]180 size_t running = 0;
[dec16a2]181 size_t ready = 0;
182 size_t sleeping = 0;
183 size_t lingering = 0;
[638927a]184 size_t other = 0;
[dec16a2]185 size_t invalid = 0;
186
[638927a]187 size_t i;
[dec16a2]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++;
[638927a]210 }
211 }
[dec16a2]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);
[9f1362d4]216 screen_newline();
[638927a]217}
218
[dec16a2]219static inline void print_cpu_info(data_t *data)
[8b2aba5]220{
[dec16a2]221 size_t i;
222 for (i = 0; i < data->cpus_count; i++) {
[bd01a4e]223 if (data->cpus[i].active) {
[d0c82c5]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",
[bd01a4e]234 data->cpus[i].id, data->cpus[i].frequency_mhz,
[d0c82c5]235 busy, busy_suffix, idle, idle_suffix);
[9f1362d4]236 puts(", idle: ");
[be06914]237 print_percent(data->cpus_perc[i].idle, 2);
238 puts(", busy: ");
239 print_percent(data->cpus_perc[i].busy, 2);
[bd01a4e]240 } else
[9f1362d4]241 printf("cpu%u inactive", data->cpus[i].id);
[dec16a2]242
[9f1362d4]243 screen_newline();
[8b2aba5]244 }
245}
246
[dec16a2]247static inline void print_physmem_info(data_t *data)
[516adce]248{
[dec16a2]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);
[9f1362d4]266 screen_newline();
267}
268
[b3b7e14a]269static inline void print_tasks_head(void)
[9f1362d4]270{
271 screen_style_inverted();
[be06914]272 printf("[taskid] [threads] [virtual] [%%virt] [%%user]"
273 " [%%kernel] [name");
[9f1362d4]274 screen_newline();
275 screen_style_normal();
[516adce]276}
277
[9f1362d4]278static inline void print_tasks(data_t *data)
[8f56d93]279{
[9f1362d4]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
[dec16a2]288 size_t i;
[9f1362d4]289 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
[172aad6]290 stats_task_t *task = data->tasks + data->tasks_map[i];
291 perc_task_t *perc = data->tasks_perc + data->tasks_map[i];
292
[dec16a2]293 uint64_t virtmem;
294 char virtmem_suffix;
[172aad6]295 order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
[dec16a2]296
[172aad6]297 printf("%-8" PRIu64 " %9u %8" PRIu64 "%c ", task->task_id,
298 task->threads, virtmem, virtmem_suffix);
299 print_percent(perc->virtmem, 2);
[be06914]300 puts(" ");
[172aad6]301 print_percent(perc->ucycles, 2);
[9f1362d4]302 puts(" ");
[172aad6]303 print_percent(perc->kcycles, 2);
[be06914]304 puts(" ");
[172aad6]305 print_string(task->name);
[9f1362d4]306
307 screen_newline();
[8f56d93]308 }
[369a5f8]309
310 while (row < rows) {
311 screen_newline();
312 row++;
313 }
[8f56d93]314}
315
[bdfd3c97]316static inline void print_ipc_head(void)
317{
[9f1362d4]318 screen_style_inverted();
[be06914]319 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
320 " [ans rcv] [irq rcv] [forward] [name");
[9f1362d4]321 screen_newline();
322 screen_style_normal();
[bdfd3c97]323}
324
[9f1362d4]325static inline void print_ipc(data_t *data)
[bdfd3c97]326{
[9f1362d4]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
[dec16a2]335 size_t i;
[9f1362d4]336 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
[be06914]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);
[9f1362d4]374
375 screen_newline();
[bdfd3c97]376 }
[369a5f8]377
378 while (row < rows) {
379 screen_newline();
380 row++;
381 }
[bdfd3c97]382}
383
[b3b7e14a]384static inline void print_excs_head(void)
[8eec3c8]385{
386 screen_style_inverted();
[be06914]387 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
[8eec3c8]388 screen_newline();
389 screen_style_normal();
390}
391
[b3b7e14a]392static inline void print_excs(data_t *data)
[8eec3c8]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;
[b3b7e14a]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
[be06914]408 uint64_t count;
[8eec3c8]409 uint64_t cycles;
410
[be06914]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);
[8eec3c8]424
425 screen_newline();
[b3b7e14a]426 row++;
427 }
428
429 while (row < rows) {
430 screen_newline();
431 row++;
[8eec3c8]432 }
[b3b7e14a]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;
[8eec3c8]463
464 while (row < rows) {
465 screen_newline();
466 row++;
467 }
468}
469
[79edc36]470void print_data(data_t *data)
471{
[9f1362d4]472 screen_restart(false);
473 print_global_head(data);
[dec16a2]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 */
[9f1362d4]480 screen_get_pos(&warn_col, &warn_row);
481 screen_newline();
[dec16a2]482
[b3b7e14a]483 switch (op_mode) {
[8eec3c8]484 case OP_TASKS:
[b3b7e14a]485 print_tasks_head();
[9f1362d4]486 print_tasks(data);
[8eec3c8]487 break;
488 case OP_IPC:
489 print_ipc_head();
490 print_ipc(data);
491 break;
[b3b7e14a]492 case OP_EXCS:
493 print_excs_head();
494 print_excs(data);
[8eec3c8]495 break;
[b3b7e14a]496 case OP_HELP:
497 print_tasks_head();
498 print_help();
[bdfd3c97]499 }
[dec16a2]500
501 fflush(stdout);
502}
503
504void print_warning(const char *fmt, ...)
505{
[9f1362d4]506 screen_moveto(warn_col, warn_row);
[dec16a2]507
508 va_list args;
509 va_start(args, fmt);
510 vprintf(fmt, args);
511 va_end(args);
512
[9f1362d4]513 screen_newline();
[79edc36]514 fflush(stdout);
515}
516
[5c058d50]517/** @}
518 */
Note: See TracBrowser for help on using the repository browser.