[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] | 49 | static ipcarg_t warn_col = 0;
|
---|
| 50 | static ipcarg_t warn_row = 0;
|
---|
[8f56d93] | 51 |
|
---|
[9f1362d4] | 52 | static void screen_style_normal(void)
|
---|
| 53 | {
|
---|
| 54 | fflush(stdout);
|
---|
| 55 | console_set_style(fphone(stdout), STYLE_NORMAL);
|
---|
| 56 | }
|
---|
[8f56d93] | 57 |
|
---|
[9f1362d4] | 58 | static void screen_style_inverted(void)
|
---|
[ee35ba0b] | 59 | {
|
---|
[9f1362d4] | 60 | fflush(stdout);
|
---|
| 61 | console_set_style(fphone(stdout), STYLE_INVERTED);
|
---|
[ee35ba0b] | 62 | }
|
---|
| 63 |
|
---|
[9f1362d4] | 64 | static 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] | 70 | static 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] | 76 | static 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] | 82 | static 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] | 94 | static 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 |
|
---|
| 112 | void screen_init(void)
|
---|
| 113 | {
|
---|
| 114 | fflush(stdout);
|
---|
| 115 | console_cursor_visibility(fphone(stdout), false);
|
---|
| 116 |
|
---|
| 117 | screen_restart(true);
|
---|
[5c058d50] | 118 | }
|
---|
| 119 |
|
---|
[9f1362d4] | 120 | void 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] | 128 | static 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] | 140 | static inline void print_global_head(data_t *data)
|
---|
[dd6c71c] | 141 | {
|
---|
[9f1362d4] | 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);
|
---|
[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] | 155 | static 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] | 161 | static 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] | 203 | static 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] | 224 | static 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 |
|
---|
| 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();
|
---|
[516adce] | 252 | }
|
---|
| 253 |
|
---|
[9f1362d4] | 254 | static 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 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[bdfd3c97] | 284 | static inline void print_ipc_head(void)
|
---|
| 285 | {
|
---|
[9f1362d4] | 286 | screen_style_inverted();
|
---|
[bdfd3c97] | 287 | printf(" ID Calls sent Calls recv Answs sent Answs recv IRQn recv Forw Name");
|
---|
[9f1362d4] | 288 | screen_newline();
|
---|
| 289 | screen_style_normal();
|
---|
[bdfd3c97] | 290 | }
|
---|
| 291 |
|
---|
[9f1362d4] | 292 | static inline void print_ipc(data_t *data)
|
---|
[bdfd3c97] | 293 | {
|
---|
[9f1362d4] | 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 |
|
---|
[dec16a2] | 302 | size_t i;
|
---|
[9f1362d4] | 303 | for (i = 0; (i < data->tasks_count) && (row < rows); i++, row++) {
|
---|
[dec16a2] | 304 | printf("%8" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64
|
---|
[9f1362d4] | 305 | " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %s",
|
---|
[dec16a2] | 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);
|
---|
[9f1362d4] | 312 |
|
---|
| 313 | screen_newline();
|
---|
[bdfd3c97] | 314 | }
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[79edc36] | 317 | void print_data(data_t *data)
|
---|
| 318 | {
|
---|
[9f1362d4] | 319 | screen_restart(false);
|
---|
| 320 | print_global_head(data);
|
---|
[dec16a2] | 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 */
|
---|
[9f1362d4] | 327 | screen_get_pos(&warn_col, &warn_row);
|
---|
| 328 | screen_newline();
|
---|
[dec16a2] | 329 |
|
---|
[bdfd3c97] | 330 | if (operation_type == OP_IPC) {
|
---|
| 331 | print_ipc_head();
|
---|
[9f1362d4] | 332 | print_ipc(data);
|
---|
[bdfd3c97] | 333 | } else {
|
---|
| 334 | print_task_head();
|
---|
[9f1362d4] | 335 | print_tasks(data);
|
---|
[bdfd3c97] | 336 | }
|
---|
[dec16a2] | 337 |
|
---|
| 338 | fflush(stdout);
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | void print_warning(const char *fmt, ...)
|
---|
| 342 | {
|
---|
[9f1362d4] | 343 | screen_moveto(warn_col, warn_row);
|
---|
[dec16a2] | 344 |
|
---|
| 345 | va_list args;
|
---|
| 346 | va_start(args, fmt);
|
---|
| 347 | vprintf(fmt, args);
|
---|
| 348 | va_end(args);
|
---|
| 349 |
|
---|
[9f1362d4] | 350 | screen_newline();
|
---|
[79edc36] | 351 | fflush(stdout);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[5c058d50] | 354 | /** @}
|
---|
| 355 | */
|
---|