| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Stanislav Kozina
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup top
|
|---|
| 30 | * @brief Top utility.
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <io/console.h>
|
|---|
| 39 | #include <vfs/vfs.h>
|
|---|
| 40 | #include <load.h>
|
|---|
| 41 | #include <kernel/ps/taskinfo.h>
|
|---|
| 42 | #include <ps.h>
|
|---|
| 43 | #include "screen.h"
|
|---|
| 44 | #include "top.h"
|
|---|
| 45 |
|
|---|
| 46 | int rows;
|
|---|
| 47 | int colls;
|
|---|
| 48 |
|
|---|
| 49 | #define WHITE 0xf0f0f0
|
|---|
| 50 | #define BLACK 0x000000
|
|---|
| 51 |
|
|---|
| 52 | static void resume_normal(void)
|
|---|
| 53 | {
|
|---|
| 54 | fflush(stdout);
|
|---|
| 55 | console_set_rgb_color(fphone(stdout), 0, WHITE);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void screen_init(void)
|
|---|
| 59 | {
|
|---|
| 60 | console_get_size(fphone(stdout), &colls, &rows);
|
|---|
| 61 | console_cursor_visibility(fphone(stdout), 0);
|
|---|
| 62 | resume_normal();
|
|---|
| 63 | clear_screen();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void clear_screen(void)
|
|---|
| 67 | {
|
|---|
| 68 | console_clear(fphone(stdout));
|
|---|
| 69 | moveto(0, 0);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void moveto(int r, int c)
|
|---|
| 73 | {
|
|---|
| 74 | fflush(stdout);
|
|---|
| 75 | console_goto(fphone(stdout), c, r);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | static inline void print_time(data_t *data)
|
|---|
| 79 | {
|
|---|
| 80 | printf("%02d:%02d:%02d ", data->hours, data->minutes, data->seconds);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | static inline void print_uptime(data_t *data)
|
|---|
| 84 | {
|
|---|
| 85 | printf("up %4d days, %02d:%02d:%02d, ", data->uptime_d, data->uptime_h,
|
|---|
| 86 | data->uptime_m, data->uptime_s);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | static inline void print_load(data_t *data)
|
|---|
| 90 | {
|
|---|
| 91 | puts("load avarage: ");
|
|---|
| 92 | print_load_fragment(data->load[0], 2);
|
|---|
| 93 | puts(" ");
|
|---|
| 94 | print_load_fragment(data->load[1], 2);
|
|---|
| 95 | puts(" ");
|
|---|
| 96 | print_load_fragment(data->load[2], 2);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | static inline void print_taskstat(data_t *data)
|
|---|
| 100 | {
|
|---|
| 101 | puts("Tasks: ");
|
|---|
| 102 | printf("%4u total", data->task_count);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | static inline void print_tasks(data_t *data, int row)
|
|---|
| 106 | {
|
|---|
| 107 | int i;
|
|---|
| 108 | for (i = 0; i < (int)data->task_count; ++i) {
|
|---|
| 109 | if (row + i > rows)
|
|---|
| 110 | return;
|
|---|
| 111 | task_info_t taskinfo;
|
|---|
| 112 | get_task_info(data->tasks[i], &taskinfo);
|
|---|
| 113 | printf("%8llu %8u %8u %12llu %12llu %s\n", taskinfo.taskid,
|
|---|
| 114 | taskinfo.thread_count, taskinfo.pages, taskinfo.ucycles / 1000 / 1000,
|
|---|
| 115 | taskinfo.kcycles / 1000 / 1000, taskinfo.name);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | static inline void print_head(void)
|
|---|
| 121 | {
|
|---|
| 122 | fflush(stdout);
|
|---|
| 123 | console_set_rgb_color(fphone(stdout), WHITE, BLACK);
|
|---|
| 124 | printf(" ID Threads Pages uCycles kCycles Name");
|
|---|
| 125 | int i;
|
|---|
| 126 | for (i = 60; i < colls; ++i)
|
|---|
| 127 | puts(" ");
|
|---|
| 128 | fflush(stdout);
|
|---|
| 129 | console_set_rgb_color(fphone(stdout), BLACK, WHITE);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void print_data(data_t *data)
|
|---|
| 133 | {
|
|---|
| 134 | clear_screen();
|
|---|
| 135 | fflush(stdout);
|
|---|
| 136 | printf("top - ");
|
|---|
| 137 | print_time(data);
|
|---|
| 138 | print_uptime(data);
|
|---|
| 139 | print_load(data);
|
|---|
| 140 | puts("\n");
|
|---|
| 141 | print_taskstat(data);
|
|---|
| 142 | puts("\n\n");
|
|---|
| 143 | print_head();
|
|---|
| 144 | puts("\n");
|
|---|
| 145 | print_tasks(data, 4);
|
|---|
| 146 | fflush(stdout);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /** @}
|
|---|
| 150 | */
|
|---|