source: mainline/uspace/app/top/screen.c@ 9f1362d4

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

console output improvements

  • define new generic styles (STYLE_INVERTED for inverted print and STYLE_SELECTION for selections), use them primarily instead of specifying colors or RGBs
  • use console_set_style(fphone(stdout), STYLE_NORMAL) as the correct mean for reseting console settings (instead of specifying conrete hardcoded colors)
  • rename console_goto() to console_set_pos() (consistency with console_get_pos())
  • use semantically correct unsigned types for console sizes and cursor positions (instead of signed types)
  • use unsigned types for sizes and positions in libclui
  • top: nicer screen redrawing (do not use console_clear() which causes flickering, but repaint the screen properly — not entirely finished yet)
  • initialize mouse pointer coordinates (so the mouse cursor does not behave erratic after boot, unfortunatelly this does not solve ticket #223)
  • Property mode set to 100644
File size: 8.2 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_float(fixed_float ffloat, unsigned int precision)
129{
130 printf("%2" 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
140static inline void print_global_head(data_t *data)
141{
142 printf("top - %02lu:%02lu:%02lu up %u days, %02u:%02u:%02u, load avarage:",
143 data->hours, data->minutes, data->seconds,
144 data->udays, data->uhours, data->uminutes, data->useconds);
145
146 size_t i;
147 for (i = 0; i < data->load_count; i++) {
148 puts(" ");
149 stats_print_load_fragment(data->load[i], 2);
150 }
151
152 screen_newline();
153}
154
155static inline void print_task_summary(data_t *data)
156{
157 printf("tasks: %u total", data->tasks_count);
158 screen_newline();
159}
160
161static inline void print_thread_summary(data_t *data)
162{
163 size_t total = 0;
164 size_t running = 0;
165 size_t ready = 0;
166 size_t sleeping = 0;
167 size_t lingering = 0;
168 size_t other = 0;
169 size_t invalid = 0;
170
171 size_t i;
172 for (i = 0; i < data->threads_count; i++) {
173 total++;
174
175 switch (data->threads[i].state) {
176 case Running:
177 running++;
178 break;
179 case Ready:
180 ready++;
181 break;
182 case Sleeping:
183 sleeping++;
184 break;
185 case Lingering:
186 lingering++;
187 break;
188 case Entering:
189 case Exiting:
190 other++;
191 break;
192 default:
193 invalid++;
194 }
195 }
196
197 printf("threads: %u total, %u running, %u ready, %u sleeping, %u lingering, "
198 "%u other, %u invalid",
199 total, running, ready, sleeping, lingering, other, invalid);
200 screen_newline();
201}
202
203static inline void print_cpu_info(data_t *data)
204{
205 size_t i;
206 for (i = 0; i < data->cpus_count; i++) {
207 if (data->cpus[i].active) {
208 printf("cpu%u (%4" PRIu16 " MHz): busy ticks: "
209 "%" PRIu64 ", idle ticks: %" PRIu64,
210 data->cpus[i].id, data->cpus[i].frequency_mhz,
211 data->cpus[i].busy_ticks, data->cpus[i].idle_ticks);
212 puts(", idle: ");
213 print_float(data->cpus_perc[i].idle, 2);
214 puts("%, busy: ");
215 print_float(data->cpus_perc[i].busy, 2);
216 puts("%");
217 } else
218 printf("cpu%u inactive", data->cpus[i].id);
219
220 screen_newline();
221 }
222}
223
224static inline void print_physmem_info(data_t *data)
225{
226 uint64_t total;
227 uint64_t unavail;
228 uint64_t used;
229 uint64_t free;
230 char total_suffix;
231 char unavail_suffix;
232 char used_suffix;
233 char free_suffix;
234
235 order_suffix(data->physmem->total, &total, &total_suffix);
236 order_suffix(data->physmem->unavail, &unavail, &unavail_suffix);
237 order_suffix(data->physmem->used, &used, &used_suffix);
238 order_suffix(data->physmem->free, &free, &free_suffix);
239
240 printf("memory: %" PRIu64 "%c total, %" PRIu64 "%c unavail, %"
241 PRIu64 "%c used, %" PRIu64 "%c free", total, total_suffix,
242 unavail, unavail_suffix, used, used_suffix, free, free_suffix);
243 screen_newline();
244}
245
246static inline void print_task_head(void)
247{
248 screen_style_inverted();
249 printf(" ID Threads Mem %%Mem %%uCycles %%kCycles Name");
250 screen_newline();
251 screen_style_normal();
252}
253
254static inline void print_tasks(data_t *data)
255{
256 ipcarg_t cols;
257 ipcarg_t rows;
258 screen_get_size(&cols, &rows);
259
260 ipcarg_t col;
261 ipcarg_t row;
262 screen_get_pos(&col, &row);
263
264 size_t i;
265 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
266 uint64_t virtmem;
267 char virtmem_suffix;
268 order_suffix(data->tasks[i].virtmem, &virtmem, &virtmem_suffix);
269
270 printf("%8" PRIu64 " %8u %8" PRIu64 "%c ", data->tasks[i].task_id,
271 data->tasks[i].threads, virtmem, virtmem_suffix);
272 puts(" ");
273 print_float(data->tasks_perc[i].virtmem, 2);
274 puts("% ");
275 print_float(data->tasks_perc[i].ucycles, 2);
276 puts("% ");
277 print_float(data->tasks_perc[i].kcycles, 2);
278 printf("%% %s", data->tasks[i].name);
279
280 screen_newline();
281 }
282}
283
284static inline void print_ipc_head(void)
285{
286 screen_style_inverted();
287 printf(" ID Calls sent Calls recv Answs sent Answs recv IRQn recv Forw Name");
288 screen_newline();
289 screen_style_normal();
290}
291
292static inline void print_ipc(data_t *data)
293{
294 ipcarg_t cols;
295 ipcarg_t rows;
296 screen_get_size(&cols, &rows);
297
298 ipcarg_t col;
299 ipcarg_t row;
300 screen_get_pos(&col, &row);
301
302 size_t i;
303 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
304 printf("%8" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64
305 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %s",
306 data->tasks[i].task_id, data->tasks[i].ipc_info.call_sent,
307 data->tasks[i].ipc_info.call_recieved,
308 data->tasks[i].ipc_info.answer_sent,
309 data->tasks[i].ipc_info.answer_recieved,
310 data->tasks[i].ipc_info.irq_notif_recieved,
311 data->tasks[i].ipc_info.forwarded, data->tasks[i].name);
312
313 screen_newline();
314 }
315}
316
317void print_data(data_t *data)
318{
319 screen_restart(false);
320 print_global_head(data);
321 print_task_summary(data);
322 print_thread_summary(data);
323 print_cpu_info(data);
324 print_physmem_info(data);
325
326 /* Empty row for warnings */
327 screen_get_pos(&warn_col, &warn_row);
328 screen_newline();
329
330 if (operation_type == OP_IPC) {
331 print_ipc_head();
332 print_ipc(data);
333 } else {
334 print_task_head();
335 print_tasks(data);
336 }
337
338 fflush(stdout);
339}
340
341void print_warning(const char *fmt, ...)
342{
343 screen_moveto(warn_col, warn_row);
344
345 va_list args;
346 va_start(args, fmt);
347 vprintf(fmt, args);
348 va_end(args);
349
350 screen_newline();
351 fflush(stdout);
352}
353
354/** @}
355 */
Note: See TracBrowser for help on using the repository browser.