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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c47e1a8 was 80badbe, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Grammar and spelling fixes.

  • Property mode set to 100644
File size: 8.3 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
[9f1362d4]128static void print_float(fixed_float ffloat, unsigned int precision)
[79edc36]129{
[9f1362d4]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 }
[79edc36]138}
139
[9f1362d4]140static inline void print_global_head(data_t *data)
[dd6c71c]141{
[80badbe]142 printf("top - %02lu:%02lu:%02lu up %u days, %02u:%02u:%02u, load average:",
[9f1362d4]143 data->hours, data->minutes, data->seconds,
144 data->udays, data->uhours, data->uminutes, data->useconds);
[dec16a2]145
146 size_t i;
147 for (i = 0; i < data->load_count; i++) {
[9f1362d4]148 puts(" ");
[dec16a2]149 stats_print_load_fragment(data->load[i], 2);
150 }
[9f1362d4]151
152 screen_newline();
[dd6c71c]153}
154
[dec16a2]155static inline void print_task_summary(data_t *data)
[dd6c71c]156{
[dec16a2]157 printf("tasks: %u total", data->tasks_count);
[9f1362d4]158 screen_newline();
[dd6c71c]159}
160
[dec16a2]161static inline void print_thread_summary(data_t *data)
[638927a]162{
[dec16a2]163 size_t total = 0;
[638927a]164 size_t running = 0;
[dec16a2]165 size_t ready = 0;
166 size_t sleeping = 0;
167 size_t lingering = 0;
[638927a]168 size_t other = 0;
[dec16a2]169 size_t invalid = 0;
170
[638927a]171 size_t i;
[dec16a2]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++;
[638927a]194 }
195 }
[dec16a2]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);
[9f1362d4]200 screen_newline();
[638927a]201}
202
[dec16a2]203static inline void print_cpu_info(data_t *data)
[8b2aba5]204{
[dec16a2]205 size_t i;
206 for (i = 0; i < data->cpus_count; i++) {
[bd01a4e]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);
[9f1362d4]212 puts(", idle: ");
[bd01a4e]213 print_float(data->cpus_perc[i].idle, 2);
[9f1362d4]214 puts("%, busy: ");
[bd01a4e]215 print_float(data->cpus_perc[i].busy, 2);
[9f1362d4]216 puts("%");
[bd01a4e]217 } else
[9f1362d4]218 printf("cpu%u inactive", data->cpus[i].id);
[dec16a2]219
[9f1362d4]220 screen_newline();
[8b2aba5]221 }
222}
223
[dec16a2]224static inline void print_physmem_info(data_t *data)
[516adce]225{
[dec16a2]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);
[9f1362d4]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();
[516adce]252}
253
[9f1362d4]254static inline void print_tasks(data_t *data)
[8f56d93]255{
[9f1362d4]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
[dec16a2]264 size_t i;
[9f1362d4]265 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
[dec16a2]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);
[9f1362d4]272 puts(" ");
[dec16a2]273 print_float(data->tasks_perc[i].virtmem, 2);
[9f1362d4]274 puts("% ");
[dec16a2]275 print_float(data->tasks_perc[i].ucycles, 2);
[9f1362d4]276 puts("% ");
[dec16a2]277 print_float(data->tasks_perc[i].kcycles, 2);
[9f1362d4]278 printf("%% %s", data->tasks[i].name);
279
280 screen_newline();
[8f56d93]281 }
[369a5f8]282
283 while (row < rows) {
284 screen_newline();
285 row++;
286 }
[8f56d93]287}
288
[bdfd3c97]289static inline void print_ipc_head(void)
290{
[9f1362d4]291 screen_style_inverted();
[bdfd3c97]292 printf(" ID Calls sent Calls recv Answs sent Answs recv IRQn recv Forw Name");
[9f1362d4]293 screen_newline();
294 screen_style_normal();
[bdfd3c97]295}
296
[9f1362d4]297static inline void print_ipc(data_t *data)
[bdfd3c97]298{
[9f1362d4]299 ipcarg_t cols;
300 ipcarg_t rows;
301 screen_get_size(&cols, &rows);
302
303 ipcarg_t col;
304 ipcarg_t row;
305 screen_get_pos(&col, &row);
306
[dec16a2]307 size_t i;
[9f1362d4]308 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
[dec16a2]309 printf("%8" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64
[9f1362d4]310 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %s",
[dec16a2]311 data->tasks[i].task_id, data->tasks[i].ipc_info.call_sent,
312 data->tasks[i].ipc_info.call_recieved,
313 data->tasks[i].ipc_info.answer_sent,
314 data->tasks[i].ipc_info.answer_recieved,
315 data->tasks[i].ipc_info.irq_notif_recieved,
316 data->tasks[i].ipc_info.forwarded, data->tasks[i].name);
[9f1362d4]317
318 screen_newline();
[bdfd3c97]319 }
[369a5f8]320
321 while (row < rows) {
322 screen_newline();
323 row++;
324 }
[bdfd3c97]325}
326
[79edc36]327void print_data(data_t *data)
328{
[9f1362d4]329 screen_restart(false);
330 print_global_head(data);
[dec16a2]331 print_task_summary(data);
332 print_thread_summary(data);
333 print_cpu_info(data);
334 print_physmem_info(data);
335
336 /* Empty row for warnings */
[9f1362d4]337 screen_get_pos(&warn_col, &warn_row);
338 screen_newline();
[dec16a2]339
[bdfd3c97]340 if (operation_type == OP_IPC) {
341 print_ipc_head();
[9f1362d4]342 print_ipc(data);
[bdfd3c97]343 } else {
344 print_task_head();
[9f1362d4]345 print_tasks(data);
[bdfd3c97]346 }
[dec16a2]347
348 fflush(stdout);
349}
350
351void print_warning(const char *fmt, ...)
352{
[9f1362d4]353 screen_moveto(warn_col, warn_row);
[dec16a2]354
355 va_list args;
356 va_start(args, fmt);
357 vprintf(fmt, args);
358 va_end(args);
359
[9f1362d4]360 screen_newline();
[79edc36]361 fflush(stdout);
362}
363
[5c058d50]364/** @}
365 */
Note: See TracBrowser for help on using the repository browser.