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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f682f5a was f682f5a, checked in by Sean Bartell <wingedtachikoma@…>, 14 years ago

top: use generic handling for tables

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