source: mainline/uspace/dist/src/c/demos/top/screen.c

Last change on this file was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 12.0 KB
RevLine 
[6b9355a]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>
[5042706]39#include <stdlib.h>
[6b9355a]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>
[5042706]46#include <macros.h>
[338d54a7]47#include <str.h>
[6b9355a]48#include "screen.h"
49#include "top.h"
50
51#define USEC_COUNT 1000000
52
53static suseconds_t timeleft = 0;
54
55console_ctrl_t *console;
56
[5042706]57static sysarg_t warning_col = 0;
58static sysarg_t warning_row = 0;
59static suseconds_t warning_timeleft = 0;
60static char *warning_text = NULL;
61
[6b9355a]62static void screen_style_normal(void)
63{
64 console_flush(console);
65 console_set_style(console, STYLE_NORMAL);
66}
67
68static void screen_style_inverted(void)
69{
70 console_flush(console);
71 console_set_style(console, STYLE_INVERTED);
72}
73
[5042706]74static void screen_style_emphasis(void)
75{
76 console_flush(console);
77 console_set_style(console, STYLE_EMPHASIS);
78}
79
[6b9355a]80static void screen_moveto(sysarg_t col, sysarg_t row)
81{
82 console_flush(console);
83 console_set_pos(console, col, row);
84}
85
86static void screen_get_pos(sysarg_t *col, sysarg_t *row)
87{
88 console_flush(console);
89 console_get_pos(console, col, row);
90}
91
92static void screen_get_size(sysarg_t *col, sysarg_t *row)
93{
94 console_flush(console);
95 console_get_size(console, col, row);
96}
97
98static void screen_restart(bool clear)
99{
100 screen_style_normal();
[a35b458]101
[6b9355a]102 if (clear) {
103 console_flush(console);
104 console_clear(console);
105 }
[a35b458]106
[6b9355a]107 screen_moveto(0, 0);
108}
109
110static void screen_newline(void)
111{
112 sysarg_t cols;
113 sysarg_t rows;
114 screen_get_size(&cols, &rows);
[a35b458]115
[6b9355a]116 sysarg_t c;
117 sysarg_t r;
118 screen_get_pos(&c, &r);
[a35b458]119
[6b9355a]120 sysarg_t i;
121 for (i = c + 1; i < cols; i++)
[1abcf1d]122 fputs(" ", stdout);
[a35b458]123
[6b9355a]124 if (r + 1 < rows)
[1abcf1d]125 puts("");
[6b9355a]126}
127
128void screen_init(void)
129{
130 console = console_init(stdin, stdout);
[a35b458]131
[6b9355a]132 console_flush(console);
133 console_cursor_visibility(console, false);
[a35b458]134
[6b9355a]135 screen_restart(true);
136}
137
138void screen_done(void)
139{
[5042706]140 free(warning_text);
141 warning_text = NULL;
142
[6b9355a]143 screen_restart(true);
[a35b458]144
[6b9355a]145 console_flush(console);
146 console_cursor_visibility(console, true);
147}
148
149static void print_percent(fixed_float ffloat, unsigned int precision)
150{
151 printf("%3" PRIu64 ".", ffloat.upper / ffloat.lower);
[a35b458]152
[6b9355a]153 unsigned int i;
154 uint64_t rest = (ffloat.upper % ffloat.lower) * 10;
155 for (i = 0; i < precision; i++) {
156 printf("%" PRIu64, rest / ffloat.lower);
157 rest = (rest % ffloat.lower) * 10;
158 }
[a35b458]159
[6b9355a]160 printf("%%");
161}
162
163static void print_string(const char *str)
164{
165 sysarg_t cols;
166 sysarg_t rows;
167 screen_get_size(&cols, &rows);
[a35b458]168
[6b9355a]169 sysarg_t c;
170 sysarg_t r;
171 screen_get_pos(&c, &r);
[a35b458]172
[6b9355a]173 if (c < cols) {
174 int pos = cols - c - 1;
175 printf("%.*s", pos, str);
176 }
177}
178
179static inline void print_global_head(data_t *data)
180{
181 printf("top - %02lu:%02lu:%02lu up "
182 "%" PRIun " days, %02" PRIun ":%02" PRIun ":%02" PRIun ", "
183 "load average:",
184 data->hours, data->minutes, data->seconds,
185 data->udays, data->uhours, data->uminutes, data->useconds);
[a35b458]186
[6b9355a]187 size_t i;
188 for (i = 0; i < data->load_count; i++) {
[1abcf1d]189 fputs(" ", stdout);
[6b9355a]190 stats_print_load_fragment(data->load[i], 2);
191 }
[a35b458]192
[6b9355a]193 screen_newline();
194}
195
196static inline void print_task_summary(data_t *data)
197{
198 printf("tasks: %zu total", data->tasks_count);
199 screen_newline();
200}
201
202static inline void print_thread_summary(data_t *data)
203{
204 size_t total = 0;
205 size_t running = 0;
206 size_t ready = 0;
207 size_t sleeping = 0;
208 size_t lingering = 0;
209 size_t other = 0;
210 size_t invalid = 0;
[a35b458]211
[6b9355a]212 size_t i;
213 for (i = 0; i < data->threads_count; i++) {
214 total++;
[a35b458]215
[6b9355a]216 switch (data->threads[i].state) {
217 case Running:
218 running++;
219 break;
220 case Ready:
221 ready++;
222 break;
223 case Sleeping:
224 sleeping++;
225 break;
226 case Lingering:
227 lingering++;
228 break;
229 case Entering:
230 case Exiting:
231 other++;
232 break;
233 default:
234 invalid++;
235 }
236 }
[a35b458]237
[6b9355a]238 printf("threads: %zu total, %zu running, %zu ready, "
239 "%zu sleeping, %zu lingering, %zu other, %zu invalid",
240 total, running, ready, sleeping, lingering, other, invalid);
241 screen_newline();
242}
243
244static inline void print_cpu_info(data_t *data)
245{
246 size_t i;
247 for (i = 0; i < data->cpus_count; i++) {
248 if (data->cpus[i].active) {
249 uint64_t busy;
250 uint64_t idle;
251 char busy_suffix;
252 char idle_suffix;
[a35b458]253
[6b9355a]254 order_suffix(data->cpus[i].busy_cycles, &busy, &busy_suffix);
255 order_suffix(data->cpus[i].idle_cycles, &idle, &idle_suffix);
[a35b458]256
[6b9355a]257 printf("cpu%u (%4" PRIu16 " MHz): busy cycles: "
258 "%" PRIu64 "%c, idle cycles: %" PRIu64 "%c",
259 data->cpus[i].id, data->cpus[i].frequency_mhz,
260 busy, busy_suffix, idle, idle_suffix);
[1abcf1d]261 fputs(", idle: ", stdout);
[6b9355a]262 print_percent(data->cpus_perc[i].idle, 2);
[1abcf1d]263 fputs(", busy: ", stdout);
[6b9355a]264 print_percent(data->cpus_perc[i].busy, 2);
265 } else
266 printf("cpu%u inactive", data->cpus[i].id);
[a35b458]267
[6b9355a]268 screen_newline();
269 }
270}
271
272static inline void print_physmem_info(data_t *data)
273{
274 uint64_t total;
275 uint64_t unavail;
276 uint64_t used;
277 uint64_t free;
278 const char *total_suffix;
279 const char *unavail_suffix;
280 const char *used_suffix;
281 const char *free_suffix;
[a35b458]282
[6b9355a]283 bin_order_suffix(data->physmem->total, &total, &total_suffix, false);
284 bin_order_suffix(data->physmem->unavail, &unavail, &unavail_suffix, false);
285 bin_order_suffix(data->physmem->used, &used, &used_suffix, false);
286 bin_order_suffix(data->physmem->free, &free, &free_suffix, false);
[a35b458]287
[6b9355a]288 printf("memory: %" PRIu64 "%s total, %" PRIu64 "%s unavail, %"
289 PRIu64 "%s used, %" PRIu64 "%s free", total, total_suffix,
290 unavail, unavail_suffix, used, used_suffix, free, free_suffix);
291 screen_newline();
292}
293
[5042706]294static inline void print_help_head(void)
[6b9355a]295{
296 screen_style_inverted();
[5042706]297 printf("Help");
[6b9355a]298 screen_newline();
299 screen_style_normal();
300}
301
[5042706]302static inline void print_help(void)
[6b9355a]303{
304 sysarg_t cols;
305 sysarg_t rows;
306 screen_get_size(&cols, &rows);
[a35b458]307
[5042706]308 screen_newline();
[a35b458]309
[5042706]310 printf("Operation modes:");
311 screen_newline();
[a35b458]312
[5042706]313 printf(" t .. tasks statistics");
314 screen_newline();
[a35b458]315
[5042706]316 printf(" i .. IPC statistics");
317 screen_newline();
[a35b458]318
[5042706]319 printf(" e .. exceptions statistics");
320 screen_newline();
[a35b458]321
[5042706]322 printf(" a .. toggle display of all/hot exceptions");
323 screen_newline();
324
325 printf(" h .. toggle this help screen");
326 screen_newline();
327
328 screen_newline();
329
330 printf("Other keys:");
331 screen_newline();
[a35b458]332
[5042706]333 printf(" s .. choose column to sort by");
334 screen_newline();
[a35b458]335
[5042706]336 printf(" r .. toggle reversed sorting");
337 screen_newline();
[a35b458]338
[5042706]339 printf(" q .. quit");
340 screen_newline();
[a35b458]341
[6b9355a]342 sysarg_t col;
343 sysarg_t row;
344 screen_get_pos(&col, &row);
[a35b458]345
[6b9355a]346 while (row < rows) {
347 screen_newline();
348 row++;
349 }
350}
351
[5042706]352static inline void print_table_head(const table_t *table)
[6b9355a]353{
[5042706]354 sysarg_t cols;
355 sysarg_t rows;
356 screen_get_size(&cols, &rows);
357
[6b9355a]358 screen_style_inverted();
[5042706]359 for (size_t i = 0; i < table->num_columns; i++) {
360 const char *name = table->columns[i].name;
361 int width = table->columns[i].width;
362 if (i != 0) {
[1abcf1d]363 fputs(" ", stdout);
[5042706]364 }
365 if (width == 0) {
366 sysarg_t col;
367 sysarg_t row;
368 screen_get_pos(&col, &row);
369 width = cols - col - 1;
370 }
371 printf("[%-*.*s]", width - 2, width - 2, name);
372 }
[6b9355a]373 screen_newline();
374 screen_style_normal();
375}
376
[5042706]377static inline void print_table(const table_t *table)
[6b9355a]378{
379 sysarg_t cols;
380 sysarg_t rows;
381 screen_get_size(&cols, &rows);
[a35b458]382
[6b9355a]383 sysarg_t col;
384 sysarg_t row;
385 screen_get_pos(&col, &row);
[a35b458]386
[6b9355a]387 size_t i;
[5042706]388 for (i = 0; (i < table->num_fields) && (row < rows); i++) {
389 size_t column_index = i % table->num_columns;
390 int width = table->columns[column_index].width;
391 field_t *field = &table->fields[i];
[338d54a7]392 uint64_t val;
393 const char *psuffix;
394 char suffix;
[5042706]395
396 if (column_index != 0) {
[1abcf1d]397 fputs(" ", stdout);
[5042706]398 }
399
400 if (width == 0) {
401 screen_get_pos(&col, &row);
402 width = cols - col - 1;
403 }
404
405 switch (field->type) {
406 case FIELD_EMPTY:
407 printf("%*s", width, "");
408 break;
409 case FIELD_UINT:
410 printf("%*" PRIu64, width, field->uint);
411 break;
[338d54a7]412 case FIELD_UINT_SUFFIX_BIN:
413 val = field->uint;
[5042706]414 width -= 3;
[338d54a7]415 bin_order_suffix(val, &val, &psuffix, true);
416 printf("%*" PRIu64 "%s", width, val, psuffix);
[5042706]417 break;
[338d54a7]418 case FIELD_UINT_SUFFIX_DEC:
419 val = field->uint;
[5042706]420 width -= 1;
421 order_suffix(val, &val, &suffix);
422 printf("%*" PRIu64 "%c", width, val, suffix);
423 break;
424 case FIELD_PERCENT:
425 width -= 5; /* nnn.% */
426 if (width > 2) {
427 printf("%*s", width - 2, "");
428 width = 2;
429 }
430 print_percent(field->fixed, width);
431 break;
432 case FIELD_STRING:
433 printf("%-*.*s", width, width, field->string);
434 break;
435 }
436
437 if (column_index == table->num_columns - 1) {
438 screen_newline();
439 row++;
440 }
[6b9355a]441 }
[a35b458]442
[6b9355a]443 while (row < rows) {
444 screen_newline();
445 row++;
446 }
447}
448
[5042706]449static inline void print_sort(table_t *table)
[6b9355a]450{
451 sysarg_t cols;
452 sysarg_t rows;
453 screen_get_size(&cols, &rows);
[a35b458]454
[6b9355a]455 sysarg_t col;
456 sysarg_t row;
457 screen_get_pos(&col, &row);
[5042706]458
459 size_t num = min(table->num_columns, rows - row);
460 for (size_t i = 0; i < num; i++) {
461 printf("%c - %s", table->columns[i].key, table->columns[i].name);
[6b9355a]462 screen_newline();
463 row++;
464 }
[a35b458]465
[6b9355a]466 while (row < rows) {
467 screen_newline();
468 row++;
469 }
470}
471
[5042706]472static inline void print_warning(void)
[6b9355a]473{
[5042706]474 screen_get_pos(&warning_col, &warning_row);
475 if (warning_timeleft > 0) {
476 screen_style_emphasis();
477 print_string(warning_text);
478 screen_style_normal();
479 } else {
480 free(warning_text);
481 warning_text = NULL;
[6b9355a]482 }
[5042706]483 screen_newline();
[6b9355a]484}
485
486void print_data(data_t *data)
487{
488 screen_restart(false);
489 print_global_head(data);
490 print_task_summary(data);
491 print_thread_summary(data);
492 print_cpu_info(data);
493 print_physmem_info(data);
[5042706]494 print_warning();
[a35b458]495
[5042706]496 switch (screen_mode) {
497 case SCREEN_TABLE:
498 print_table_head(&data->table);
499 print_table(&data->table);
[6b9355a]500 break;
[5042706]501 case SCREEN_SORT:
502 print_sort(&data->table);
[6b9355a]503 break;
[5042706]504 case SCREEN_HELP:
505 print_help_head();
[6b9355a]506 print_help();
507 }
[a35b458]508
[6b9355a]509 console_flush(console);
510}
511
[5042706]512void show_warning(const char *fmt, ...)
[6b9355a]513{
[5042706]514 sysarg_t cols;
515 sysarg_t rows;
516 screen_get_size(&cols, &rows);
517
518 size_t warning_text_size = 1 + cols * sizeof(*warning_text);
519 free(warning_text);
520 warning_text = malloc(warning_text_size);
521 if (!warning_text)
522 return;
523
[6b9355a]524 va_list args;
525 va_start(args, fmt);
[5042706]526 vsnprintf(warning_text, warning_text_size, fmt, args);
[6b9355a]527 va_end(args);
[a35b458]528
[5042706]529 warning_timeleft = 2 * USEC_COUNT;
530
531 screen_moveto(warning_col, warning_row);
532 print_warning();
[6b9355a]533 console_flush(console);
534}
535
536/** Get char with timeout
537 *
538 */
[338d54a7]539int tgetchar(unsigned int sec)
[6b9355a]540{
541 /*
542 * Reset timeleft whenever it is not positive.
543 */
[a35b458]544
[6b9355a]545 if (timeleft <= 0)
546 timeleft = sec * USEC_COUNT;
[a35b458]547
[6b9355a]548 /*
549 * Wait to see if there is any input. If so, take it and
550 * update timeleft so that the next call to tgetchar()
551 * will not wait as long. If there is no input,
552 * make timeleft zero and return -1.
553 */
[a35b458]554
[28a5ebd]555 char32_t c = 0;
[a35b458]556
[6b9355a]557 while (c == 0) {
[07b7c48]558 cons_event_t event;
[a35b458]559
[5042706]560 warning_timeleft -= timeleft;
[07b7c48]561 if (!console_get_event_timeout(console, &event, &timeleft)) {
[6b9355a]562 timeleft = 0;
563 return -1;
564 }
[5042706]565 warning_timeleft += timeleft;
[a35b458]566
[07b7c48]567 if (event.type == CEV_KEY && event.ev.key.type == KEY_PRESS)
568 c = event.ev.key.c;
[6b9355a]569 }
[a35b458]570
[6b9355a]571 return (int) c;
572}
573
574/** @}
575 */
Note: See TracBrowser for help on using the repository browser.