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 |
|
---|
49 | static ipcarg_t warn_col = 0;
|
---|
50 | static ipcarg_t warn_row = 0;
|
---|
51 |
|
---|
52 | static void screen_style_normal(void)
|
---|
53 | {
|
---|
54 | fflush(stdout);
|
---|
55 | console_set_style(fphone(stdout), STYLE_NORMAL);
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void screen_style_inverted(void)
|
---|
59 | {
|
---|
60 | fflush(stdout);
|
---|
61 | console_set_style(fphone(stdout), STYLE_INVERTED);
|
---|
62 | }
|
---|
63 |
|
---|
64 | static void screen_moveto(ipcarg_t col, ipcarg_t row)
|
---|
65 | {
|
---|
66 | fflush(stdout);
|
---|
67 | console_set_pos(fphone(stdout), col, row);
|
---|
68 | }
|
---|
69 |
|
---|
70 | static void screen_get_pos(ipcarg_t *col, ipcarg_t *row)
|
---|
71 | {
|
---|
72 | fflush(stdout);
|
---|
73 | console_get_pos(fphone(stdout), col, row);
|
---|
74 | }
|
---|
75 |
|
---|
76 | static void screen_get_size(ipcarg_t *col, ipcarg_t *row)
|
---|
77 | {
|
---|
78 | fflush(stdout);
|
---|
79 | console_get_size(fphone(stdout), col, row);
|
---|
80 | }
|
---|
81 |
|
---|
82 | static 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 |
|
---|
94 | static 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 |
|
---|
112 | void screen_init(void)
|
---|
113 | {
|
---|
114 | fflush(stdout);
|
---|
115 | console_cursor_visibility(fphone(stdout), false);
|
---|
116 |
|
---|
117 | screen_restart(true);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void screen_done(void)
|
---|
121 | {
|
---|
122 | screen_restart(true);
|
---|
123 |
|
---|
124 | fflush(stdout);
|
---|
125 | console_cursor_visibility(fphone(stdout), true);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static 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 |
|
---|
140 | static 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 |
|
---|
155 | static inline void print_task_summary(data_t *data)
|
---|
156 | {
|
---|
157 | printf("tasks: %u total", data->tasks_count);
|
---|
158 | screen_newline();
|
---|
159 | }
|
---|
160 |
|
---|
161 | static 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 |
|
---|
203 | static 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 |
|
---|
224 | static 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 |
|
---|
246 | static 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 |
|
---|
254 | static 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 | while (row < rows) {
|
---|
284 | screen_newline();
|
---|
285 | row++;
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | static inline void print_ipc_head(void)
|
---|
290 | {
|
---|
291 | screen_style_inverted();
|
---|
292 | printf(" ID Calls sent Calls recv Answs sent Answs recv IRQn recv Forw Name");
|
---|
293 | screen_newline();
|
---|
294 | screen_style_normal();
|
---|
295 | }
|
---|
296 |
|
---|
297 | static inline void print_ipc(data_t *data)
|
---|
298 | {
|
---|
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 |
|
---|
307 | size_t i;
|
---|
308 | for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
|
---|
309 | printf("%8" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64
|
---|
310 | " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %s",
|
---|
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);
|
---|
317 |
|
---|
318 | screen_newline();
|
---|
319 | }
|
---|
320 |
|
---|
321 | while (row < rows) {
|
---|
322 | screen_newline();
|
---|
323 | row++;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | void print_data(data_t *data)
|
---|
328 | {
|
---|
329 | screen_restart(false);
|
---|
330 | print_global_head(data);
|
---|
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 */
|
---|
337 | screen_get_pos(&warn_col, &warn_row);
|
---|
338 | screen_newline();
|
---|
339 |
|
---|
340 | if (operation_type == OP_IPC) {
|
---|
341 | print_ipc_head();
|
---|
342 | print_ipc(data);
|
---|
343 | } else {
|
---|
344 | print_task_head();
|
---|
345 | print_tasks(data);
|
---|
346 | }
|
---|
347 |
|
---|
348 | fflush(stdout);
|
---|
349 | }
|
---|
350 |
|
---|
351 | void print_warning(const char *fmt, ...)
|
---|
352 | {
|
---|
353 | screen_moveto(warn_col, warn_row);
|
---|
354 |
|
---|
355 | va_list args;
|
---|
356 | va_start(args, fmt);
|
---|
357 | vprintf(fmt, args);
|
---|
358 | va_end(args);
|
---|
359 |
|
---|
360 | screen_newline();
|
---|
361 | fflush(stdout);
|
---|
362 | }
|
---|
363 |
|
---|
364 | /** @}
|
---|
365 | */
|
---|