source: mainline/uspace/app/top/screen.c@ 9539be6

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

perfect CPU cycles accounting, cherry-picked and adopted from lp:~ersin/helenos/measure2

  • Property mode set to 100644
File size: 11.5 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_percent(fixed_float ffloat, unsigned int precision)
129{
130 printf("%3" 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 printf("%%");
140}
141
142static void print_string(const char *str)
143{
144 ipcarg_t cols;
145 ipcarg_t rows;
146 screen_get_size(&cols, &rows);
147
148 ipcarg_t c;
149 ipcarg_t r;
150 screen_get_pos(&c, &r);
151
152 if (c < cols)
153 printf("%.*s", cols - c - 1, str);
154}
155
156static inline void print_global_head(data_t *data)
157{
158 printf("top - %02lu:%02lu:%02lu up %u days, %02u:%02u:%02u, load average:",
159 data->hours, data->minutes, data->seconds,
160 data->udays, data->uhours, data->uminutes, data->useconds);
161
162 size_t i;
163 for (i = 0; i < data->load_count; i++) {
164 puts(" ");
165 stats_print_load_fragment(data->load[i], 2);
166 }
167
168 screen_newline();
169}
170
171static inline void print_task_summary(data_t *data)
172{
173 printf("tasks: %u total", data->tasks_count);
174 screen_newline();
175}
176
177static inline void print_thread_summary(data_t *data)
178{
179 size_t total = 0;
180 size_t running = 0;
181 size_t ready = 0;
182 size_t sleeping = 0;
183 size_t lingering = 0;
184 size_t other = 0;
185 size_t invalid = 0;
186
187 size_t i;
188 for (i = 0; i < data->threads_count; i++) {
189 total++;
190
191 switch (data->threads[i].state) {
192 case Running:
193 running++;
194 break;
195 case Ready:
196 ready++;
197 break;
198 case Sleeping:
199 sleeping++;
200 break;
201 case Lingering:
202 lingering++;
203 break;
204 case Entering:
205 case Exiting:
206 other++;
207 break;
208 default:
209 invalid++;
210 }
211 }
212
213 printf("threads: %u total, %u running, %u ready, %u sleeping, %u lingering, "
214 "%u other, %u invalid",
215 total, running, ready, sleeping, lingering, other, invalid);
216 screen_newline();
217}
218
219static inline void print_cpu_info(data_t *data)
220{
221 size_t i;
222 for (i = 0; i < data->cpus_count; i++) {
223 if (data->cpus[i].active) {
224 uint64_t busy;
225 uint64_t idle;
226 char busy_suffix;
227 char idle_suffix;
228
229 order_suffix(data->cpus[i].busy_cycles, &busy, &busy_suffix);
230 order_suffix(data->cpus[i].idle_cycles, &idle, &idle_suffix);
231
232 printf("cpu%u (%4" PRIu16 " MHz): busy cycles: "
233 "%" PRIu64 "%c, idle cycles: %" PRIu64 "%c",
234 data->cpus[i].id, data->cpus[i].frequency_mhz,
235 busy, busy_suffix, idle, idle_suffix);
236 puts(", idle: ");
237 print_percent(data->cpus_perc[i].idle, 2);
238 puts(", busy: ");
239 print_percent(data->cpus_perc[i].busy, 2);
240 } else
241 printf("cpu%u inactive", data->cpus[i].id);
242
243 screen_newline();
244 }
245}
246
247static inline void print_physmem_info(data_t *data)
248{
249 uint64_t total;
250 uint64_t unavail;
251 uint64_t used;
252 uint64_t free;
253 char total_suffix;
254 char unavail_suffix;
255 char used_suffix;
256 char free_suffix;
257
258 order_suffix(data->physmem->total, &total, &total_suffix);
259 order_suffix(data->physmem->unavail, &unavail, &unavail_suffix);
260 order_suffix(data->physmem->used, &used, &used_suffix);
261 order_suffix(data->physmem->free, &free, &free_suffix);
262
263 printf("memory: %" PRIu64 "%c total, %" PRIu64 "%c unavail, %"
264 PRIu64 "%c used, %" PRIu64 "%c free", total, total_suffix,
265 unavail, unavail_suffix, used, used_suffix, free, free_suffix);
266 screen_newline();
267}
268
269static inline void print_tasks_head(void)
270{
271 screen_style_inverted();
272 printf("[taskid] [threads] [virtual] [%%virt] [%%user]"
273 " [%%kernel] [name");
274 screen_newline();
275 screen_style_normal();
276}
277
278static inline void print_tasks(data_t *data)
279{
280 ipcarg_t cols;
281 ipcarg_t rows;
282 screen_get_size(&cols, &rows);
283
284 ipcarg_t col;
285 ipcarg_t row;
286 screen_get_pos(&col, &row);
287
288 size_t i;
289 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
290 uint64_t virtmem;
291 char virtmem_suffix;
292 order_suffix(data->tasks[i].virtmem, &virtmem, &virtmem_suffix);
293
294 printf("%-8" PRIu64 " %9u %8" PRIu64 "%c ", data->tasks[i].task_id,
295 data->tasks[i].threads, virtmem, virtmem_suffix);
296 print_percent(data->tasks_perc[i].virtmem, 2);
297 puts(" ");
298 print_percent(data->tasks_perc[i].ucycles, 2);
299 puts(" ");
300 print_percent(data->tasks_perc[i].kcycles, 2);
301 puts(" ");
302 print_string(data->tasks[i].name);
303
304 screen_newline();
305 }
306
307 while (row < rows) {
308 screen_newline();
309 row++;
310 }
311}
312
313static inline void print_ipc_head(void)
314{
315 screen_style_inverted();
316 printf("[taskid] [cls snt] [cls rcv] [ans snt]"
317 " [ans rcv] [irq rcv] [forward] [name");
318 screen_newline();
319 screen_style_normal();
320}
321
322static inline void print_ipc(data_t *data)
323{
324 ipcarg_t cols;
325 ipcarg_t rows;
326 screen_get_size(&cols, &rows);
327
328 ipcarg_t col;
329 ipcarg_t row;
330 screen_get_pos(&col, &row);
331
332 size_t i;
333 for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
334 uint64_t call_sent;
335 uint64_t call_received;
336 uint64_t answer_sent;
337 uint64_t answer_received;
338 uint64_t irq_notif_received;
339 uint64_t forwarded;
340
341 char call_sent_suffix;
342 char call_received_suffix;
343 char answer_sent_suffix;
344 char answer_received_suffix;
345 char irq_notif_received_suffix;
346 char forwarded_suffix;
347
348 order_suffix(data->tasks[i].ipc_info.call_sent, &call_sent,
349 &call_sent_suffix);
350 order_suffix(data->tasks[i].ipc_info.call_received,
351 &call_received, &call_received_suffix);
352 order_suffix(data->tasks[i].ipc_info.answer_sent,
353 &answer_sent, &answer_sent_suffix);
354 order_suffix(data->tasks[i].ipc_info.answer_received,
355 &answer_received, &answer_received_suffix);
356 order_suffix(data->tasks[i].ipc_info.irq_notif_received,
357 &irq_notif_received, &irq_notif_received_suffix);
358 order_suffix(data->tasks[i].ipc_info.forwarded, &forwarded,
359 &forwarded_suffix);
360
361 printf("%-8" PRIu64 " %8" PRIu64 "%c %8" PRIu64 "%c"
362 " %8" PRIu64 "%c %8" PRIu64 "%c %8" PRIu64 "%c"
363 " %8" PRIu64 "%c ", data->tasks[i].task_id,
364 call_sent, call_sent_suffix,
365 call_received, call_received_suffix,
366 answer_sent, answer_sent_suffix,
367 answer_received, answer_received_suffix,
368 irq_notif_received, irq_notif_received_suffix,
369 forwarded, forwarded_suffix);
370 print_string(data->tasks[i].name);
371
372 screen_newline();
373 }
374
375 while (row < rows) {
376 screen_newline();
377 row++;
378 }
379}
380
381static inline void print_excs_head(void)
382{
383 screen_style_inverted();
384 printf("[exc ] [count ] [%%count] [cycles ] [%%cycles] [description");
385 screen_newline();
386 screen_style_normal();
387}
388
389static inline void print_excs(data_t *data)
390{
391 ipcarg_t cols;
392 ipcarg_t rows;
393 screen_get_size(&cols, &rows);
394
395 ipcarg_t col;
396 ipcarg_t row;
397 screen_get_pos(&col, &row);
398
399 size_t i;
400 for (i = 0; (i < data->exceptions_count) && (row < rows); i++) {
401 /* Filter-out cold exceptions if not instructed otherwise */
402 if ((!excs_all) && (!data->exceptions[i].hot))
403 continue;
404
405 uint64_t count;
406 uint64_t cycles;
407
408 char count_suffix;
409 char cycles_suffix;
410
411 order_suffix(data->exceptions[i].count, &count, &count_suffix);
412 order_suffix(data->exceptions[i].cycles, &cycles, &cycles_suffix);
413
414 printf("%-8u %9" PRIu64 "%c ",
415 data->exceptions[i].id, count, count_suffix);
416 print_percent(data->exceptions_perc[i].count, 2);
417 printf(" %9" PRIu64 "%c ", cycles, cycles_suffix);
418 print_percent(data->exceptions_perc[i].cycles, 2);
419 puts(" ");
420 print_string(data->exceptions[i].desc);
421
422 screen_newline();
423 row++;
424 }
425
426 while (row < rows) {
427 screen_newline();
428 row++;
429 }
430}
431
432static void print_help(void)
433{
434 ipcarg_t cols;
435 ipcarg_t rows;
436 screen_get_size(&cols, &rows);
437
438 ipcarg_t col;
439 ipcarg_t row;
440 screen_get_pos(&col, &row);
441
442 screen_newline();
443
444 printf("Operation modes:");
445 screen_newline();
446
447 printf(" t .. tasks statistics");
448 screen_newline();
449
450 printf(" i .. IPC statistics");
451 screen_newline();
452
453 printf(" e .. exceptions statistics");
454 screen_newline();
455
456 printf(" a .. toggle display of all/hot exceptions");
457 screen_newline();
458
459 row += 6;
460
461 while (row < rows) {
462 screen_newline();
463 row++;
464 }
465}
466
467void print_data(data_t *data)
468{
469 screen_restart(false);
470 print_global_head(data);
471 print_task_summary(data);
472 print_thread_summary(data);
473 print_cpu_info(data);
474 print_physmem_info(data);
475
476 /* Empty row for warnings */
477 screen_get_pos(&warn_col, &warn_row);
478 screen_newline();
479
480 switch (op_mode) {
481 case OP_TASKS:
482 print_tasks_head();
483 print_tasks(data);
484 break;
485 case OP_IPC:
486 print_ipc_head();
487 print_ipc(data);
488 break;
489 case OP_EXCS:
490 print_excs_head();
491 print_excs(data);
492 break;
493 case OP_HELP:
494 print_tasks_head();
495 print_help();
496 }
497
498 fflush(stdout);
499}
500
501void print_warning(const char *fmt, ...)
502{
503 screen_moveto(warn_col, warn_row);
504
505 va_list args;
506 va_start(args, fmt);
507 vprintf(fmt, args);
508 va_end(args);
509
510 screen_newline();
511 fflush(stdout);
512}
513
514/** @}
515 */
Note: See TracBrowser for help on using the repository browser.