[c0379fc] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Stanislav Kozina
|
---|
[caa8a94] | 3 | * Copyright (c) 2010 Martin Decky
|
---|
[c0379fc] | 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 |
|
---|
[3d482e0] | 30 | /** @addtogroup stats
|
---|
| 31 | * @brief Print system statistics.
|
---|
[c0379fc] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /**
|
---|
| 35 | * @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <stdio.h>
|
---|
[caa8a94] | 39 | #include <task.h>
|
---|
| 40 | #include <thread.h>
|
---|
[9dae191e] | 41 | #include <stats.h>
|
---|
[caa8a94] | 42 | #include <errno.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
[70e2b2d] | 44 | #include <malloc.h>
|
---|
[caa8a94] | 45 | #include <inttypes.h>
|
---|
| 46 | #include <bool.h>
|
---|
| 47 | #include <str.h>
|
---|
| 48 | #include <arg_parse.h>
|
---|
[9d491a7] | 49 |
|
---|
[caa8a94] | 50 | #define NAME "stats"
|
---|
[9dae191e] | 51 |
|
---|
| 52 | #define DAY 86400
|
---|
| 53 | #define HOUR 3600
|
---|
| 54 | #define MINUTE 60
|
---|
[9d491a7] | 55 |
|
---|
[caa8a94] | 56 | static void list_tasks(void)
|
---|
| 57 | {
|
---|
| 58 | size_t count;
|
---|
| 59 | stats_task_t *stats_tasks = stats_get_tasks(&count);
|
---|
| 60 |
|
---|
| 61 | if (stats_tasks == NULL) {
|
---|
| 62 | fprintf(stderr, "%s: Unable to get tasks\n", NAME);
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | printf("[taskid] [thrds] [resident] [virtual] [ucycles]"
|
---|
| 67 | " [kcycles] [name\n");
|
---|
| 68 |
|
---|
| 69 | size_t i;
|
---|
| 70 | for (i = 0; i < count; i++) {
|
---|
| 71 | uint64_t resmem, virtmem, ucycles, kcycles;
|
---|
| 72 | char resmem_suffix, virtmem_suffix, usuffix, ksuffix;
|
---|
| 73 |
|
---|
| 74 | order_suffix(stats_tasks[i].resmem, &resmem, &resmem_suffix);
|
---|
| 75 | order_suffix(stats_tasks[i].virtmem, &virtmem, &virtmem_suffix);
|
---|
| 76 | order_suffix(stats_tasks[i].ucycles, &ucycles, &usuffix);
|
---|
| 77 | order_suffix(stats_tasks[i].kcycles, &kcycles, &ksuffix);
|
---|
| 78 |
|
---|
| 79 | printf("%-8" PRIu64 " %7zu %9" PRIu64 "%c %8" PRIu64 "%c"
|
---|
| 80 | " %8" PRIu64 "%c %8" PRIu64 "%c %s\n",
|
---|
| 81 | stats_tasks[i].task_id, stats_tasks[i].threads,
|
---|
| 82 | resmem, resmem_suffix, virtmem, virtmem_suffix,
|
---|
| 83 | ucycles, usuffix, kcycles, ksuffix, stats_tasks[i].name);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | free(stats_tasks);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | static void list_threads(task_id_t task_id, bool all)
|
---|
| 90 | {
|
---|
| 91 | size_t count;
|
---|
| 92 | stats_thread_t *stats_threads = stats_get_threads(&count);
|
---|
| 93 |
|
---|
| 94 | if (stats_threads == NULL) {
|
---|
| 95 | fprintf(stderr, "%s: Unable to get threads\n", NAME);
|
---|
| 96 | return;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | printf("[taskid] [threadid] [state ] [prio] [cpu ] [ucycles] [kcycles]\n");
|
---|
| 100 |
|
---|
| 101 | size_t i;
|
---|
| 102 | for (i = 0; i < count; i++) {
|
---|
| 103 | if ((all) || (stats_threads[i].task_id == task_id)) {
|
---|
| 104 | uint64_t ucycles, kcycles;
|
---|
| 105 | char usuffix, ksuffix;
|
---|
| 106 |
|
---|
| 107 | order_suffix(stats_threads[i].ucycles, &ucycles, &usuffix);
|
---|
| 108 | order_suffix(stats_threads[i].kcycles, &kcycles, &ksuffix);
|
---|
| 109 |
|
---|
| 110 | printf("%-8" PRIu64 " %-10" PRIu64 " %-8s %6d ",
|
---|
| 111 | stats_threads[i].task_id, stats_threads[i].thread_id,
|
---|
| 112 | thread_get_state(stats_threads[i].state),
|
---|
| 113 | stats_threads[i].priority);
|
---|
| 114 |
|
---|
| 115 | if (stats_threads[i].on_cpu)
|
---|
| 116 | printf("%6u ", stats_threads[i].cpu);
|
---|
| 117 | else
|
---|
| 118 | printf("(none) ");
|
---|
| 119 |
|
---|
| 120 | printf("%8" PRIu64"%c %8" PRIu64"%c\n",
|
---|
| 121 | ucycles, usuffix, kcycles, ksuffix);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | free(stats_threads);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | static void list_cpus(void)
|
---|
[c0379fc] | 129 | {
|
---|
[caa8a94] | 130 | size_t count;
|
---|
| 131 | stats_cpu_t *cpus = stats_get_cpus(&count);
|
---|
| 132 |
|
---|
| 133 | if (cpus == NULL) {
|
---|
| 134 | fprintf(stderr, "%s: Unable to get CPU statistics\n", NAME);
|
---|
| 135 | return;
|
---|
[9d491a7] | 136 | }
|
---|
[9dae191e] | 137 |
|
---|
[caa8a94] | 138 | printf("[id] [MHz ] [busy cycles] [idle cycles]\n");
|
---|
| 139 |
|
---|
| 140 | size_t i;
|
---|
| 141 | for (i = 0; i < count; i++) {
|
---|
| 142 | printf("%-4u ", cpus[i].id);
|
---|
| 143 | if (cpus[i].active) {
|
---|
| 144 | uint64_t bcycles, icycles;
|
---|
| 145 | char bsuffix, isuffix;
|
---|
| 146 |
|
---|
| 147 | order_suffix(cpus[i].busy_cycles, &bcycles, &bsuffix);
|
---|
| 148 | order_suffix(cpus[i].idle_cycles, &icycles, &isuffix);
|
---|
| 149 |
|
---|
| 150 | printf("%10" PRIu16 " %12" PRIu64 "%c %12" PRIu64 "%c\n",
|
---|
| 151 | cpus[i].frequency_mhz, bcycles, bsuffix,
|
---|
| 152 | icycles, isuffix);
|
---|
| 153 | } else
|
---|
| 154 | printf("inactive\n");
|
---|
| 155 | }
|
---|
[9dae191e] | 156 |
|
---|
[caa8a94] | 157 | free(cpus);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | static void print_load(void)
|
---|
| 161 | {
|
---|
| 162 | size_t count;
|
---|
| 163 | load_t *load = stats_get_load(&count);
|
---|
| 164 |
|
---|
| 165 | if (load == NULL) {
|
---|
| 166 | fprintf(stderr, "%s: Unable to get load\n", NAME);
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | printf("%s: Load average: ", NAME);
|
---|
| 171 |
|
---|
| 172 | size_t i;
|
---|
| 173 | for (i = 0; i < count; i++) {
|
---|
| 174 | if (i > 0)
|
---|
| 175 | printf(" ");
|
---|
| 176 |
|
---|
| 177 | stats_print_load_fragment(load[i], 2);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | printf("\n");
|
---|
| 181 |
|
---|
| 182 | free(load);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | static void print_uptime(void)
|
---|
| 186 | {
|
---|
[c3d4bb45] | 187 | sysarg_t uptime = stats_get_uptime();
|
---|
[caa8a94] | 188 | printf("%s: Up %" PRIun " days, %" PRIun " hours, "
|
---|
| 189 | "%" PRIun " minutes, %" PRIun " seconds\n", NAME,
|
---|
[7e752b2] | 190 | uptime / DAY, (uptime % DAY) / HOUR,
|
---|
| 191 | (uptime % HOUR) / MINUTE, uptime % MINUTE);
|
---|
[caa8a94] | 192 | }
|
---|
| 193 |
|
---|
| 194 | static void usage(const char *name)
|
---|
| 195 | {
|
---|
| 196 | printf(
|
---|
| 197 | "Usage: %s [-t task_id] [-a] [-c] [-l] [-u]\n" \
|
---|
| 198 | "\n" \
|
---|
| 199 | "Options:\n" \
|
---|
| 200 | "\t-t task_id\n" \
|
---|
| 201 | "\t--task=task_id\n" \
|
---|
| 202 | "\t\tList threads of the given task\n" \
|
---|
| 203 | "\n" \
|
---|
| 204 | "\t-a\n" \
|
---|
| 205 | "\t--all\n" \
|
---|
| 206 | "\t\tList all threads\n" \
|
---|
| 207 | "\n" \
|
---|
| 208 | "\t-c\n" \
|
---|
| 209 | "\t--cpus\n" \
|
---|
| 210 | "\t\tList CPUs\n" \
|
---|
| 211 | "\n" \
|
---|
| 212 | "\t-l\n" \
|
---|
| 213 | "\t--load\n" \
|
---|
| 214 | "\t\tPrint system load\n" \
|
---|
| 215 | "\n" \
|
---|
| 216 | "\t-u\n" \
|
---|
| 217 | "\t--uptime\n" \
|
---|
| 218 | "\t\tPrint system uptime\n" \
|
---|
| 219 | "\n" \
|
---|
| 220 | "\t-h\n" \
|
---|
| 221 | "\t--help\n" \
|
---|
| 222 | "\t\tPrint this usage information\n"
|
---|
| 223 | "\n" \
|
---|
| 224 | "Without any options all tasks are listed\n",
|
---|
| 225 | name
|
---|
| 226 | );
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | int main(int argc, char *argv[])
|
---|
| 230 | {
|
---|
| 231 | bool toggle_tasks = true;
|
---|
| 232 | bool toggle_threads = false;
|
---|
| 233 | bool toggle_all = false;
|
---|
| 234 | bool toggle_cpus = false;
|
---|
| 235 | bool toggle_load = false;
|
---|
| 236 | bool toggle_uptime = false;
|
---|
[9dae191e] | 237 |
|
---|
[caa8a94] | 238 | task_id_t task_id = 0;
|
---|
| 239 |
|
---|
| 240 | int i;
|
---|
| 241 | for (i = 1; i < argc; i++) {
|
---|
| 242 | int off;
|
---|
| 243 |
|
---|
| 244 | /* Usage */
|
---|
| 245 | if ((off = arg_parse_short_long(argv[i], "-h", "--help")) != -1) {
|
---|
| 246 | usage(argv[0]);
|
---|
| 247 | return 0;
|
---|
| 248 | }
|
---|
[9dae191e] | 249 |
|
---|
[caa8a94] | 250 | /* All threads */
|
---|
| 251 | if ((off = arg_parse_short_long(argv[i], "-a", "--all")) != -1) {
|
---|
| 252 | toggle_tasks = false;
|
---|
| 253 | toggle_threads = true;
|
---|
| 254 | toggle_all = true;
|
---|
| 255 | continue;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /* CPUs */
|
---|
| 259 | if ((off = arg_parse_short_long(argv[i], "-c", "--cpus")) != -1) {
|
---|
| 260 | toggle_tasks = false;
|
---|
| 261 | toggle_cpus = true;
|
---|
| 262 | continue;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /* Threads */
|
---|
| 266 | if ((off = arg_parse_short_long(argv[i], "-t", "--task=")) != -1) {
|
---|
[ccca251] | 267 | // TODO: Support for 64b range
|
---|
[caa8a94] | 268 | int tmp;
|
---|
| 269 | int ret = arg_parse_int(argc, argv, &i, &tmp, off);
|
---|
| 270 | if (ret != EOK) {
|
---|
| 271 | printf("%s: Malformed task_id '%s'\n", NAME, argv[i]);
|
---|
| 272 | return -1;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | task_id = tmp;
|
---|
[9dae191e] | 276 |
|
---|
[caa8a94] | 277 | toggle_tasks = false;
|
---|
| 278 | toggle_threads = true;
|
---|
| 279 | continue;
|
---|
[9dae191e] | 280 | }
|
---|
[70e2b2d] | 281 |
|
---|
[caa8a94] | 282 | /* Load */
|
---|
| 283 | if ((off = arg_parse_short_long(argv[i], "-l", "--load")) != -1) {
|
---|
| 284 | toggle_tasks = false;
|
---|
| 285 | toggle_load = true;
|
---|
| 286 | continue;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /* Uptime */
|
---|
| 290 | if ((off = arg_parse_short_long(argv[i], "-u", "--uptime")) != -1) {
|
---|
| 291 | toggle_tasks = false;
|
---|
| 292 | toggle_uptime = true;
|
---|
| 293 | continue;
|
---|
| 294 | }
|
---|
[9dae191e] | 295 | }
|
---|
| 296 |
|
---|
[caa8a94] | 297 | if (toggle_tasks)
|
---|
| 298 | list_tasks();
|
---|
| 299 |
|
---|
| 300 | if (toggle_threads)
|
---|
| 301 | list_threads(task_id, toggle_all);
|
---|
| 302 |
|
---|
| 303 | if (toggle_cpus)
|
---|
| 304 | list_cpus();
|
---|
| 305 |
|
---|
| 306 | if (toggle_load)
|
---|
| 307 | print_load();
|
---|
| 308 |
|
---|
| 309 | if (toggle_uptime)
|
---|
| 310 | print_uptime();
|
---|
| 311 |
|
---|
[c0379fc] | 312 | return 0;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | /** @}
|
---|
| 316 | */
|
---|