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