[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
|
---|
[c0379fc] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
[caa8a94] | 38 | #include <task.h>
|
---|
[9dae191e] | 39 | #include <stats.h>
|
---|
[caa8a94] | 40 | #include <errno.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
[38d150e] | 42 | #include <stdlib.h>
|
---|
[caa8a94] | 43 | #include <inttypes.h>
|
---|
[3e6a98c5] | 44 | #include <stdbool.h>
|
---|
[caa8a94] | 45 | #include <str.h>
|
---|
| 46 | #include <arg_parse.h>
|
---|
[9d491a7] | 47 |
|
---|
[caa8a94] | 48 | #define NAME "stats"
|
---|
[9dae191e] | 49 |
|
---|
| 50 | #define DAY 86400
|
---|
| 51 | #define HOUR 3600
|
---|
| 52 | #define MINUTE 60
|
---|
[9d491a7] | 53 |
|
---|
[caa8a94] | 54 | static void list_tasks(void)
|
---|
| 55 | {
|
---|
| 56 | size_t count;
|
---|
| 57 | stats_task_t *stats_tasks = stats_get_tasks(&count);
|
---|
[a35b458] | 58 |
|
---|
[caa8a94] | 59 | if (stats_tasks == NULL) {
|
---|
| 60 | fprintf(stderr, "%s: Unable to get tasks\n", NAME);
|
---|
| 61 | return;
|
---|
| 62 | }
|
---|
[a35b458] | 63 |
|
---|
[caa8a94] | 64 | printf("[taskid] [thrds] [resident] [virtual] [ucycles]"
|
---|
| 65 | " [kcycles] [name\n");
|
---|
[a35b458] | 66 |
|
---|
[caa8a94] | 67 | size_t i;
|
---|
| 68 | for (i = 0; i < count; i++) {
|
---|
[933cadf] | 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;
|
---|
[a35b458] | 77 |
|
---|
[933cadf] | 78 | bin_order_suffix(stats_tasks[i].resmem, &resmem, &resmem_suffix, true);
|
---|
| 79 | bin_order_suffix(stats_tasks[i].virtmem, &virtmem, &virtmem_suffix, true);
|
---|
[caa8a94] | 80 | order_suffix(stats_tasks[i].ucycles, &ucycles, &usuffix);
|
---|
| 81 | order_suffix(stats_tasks[i].kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 82 |
|
---|
[933cadf] | 83 | printf("%-8" PRIu64 " %7zu %7" PRIu64 "%s %6" PRIu64 "%s"
|
---|
[caa8a94] | 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 | }
|
---|
[a35b458] | 89 |
|
---|
[caa8a94] | 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);
|
---|
[a35b458] | 97 |
|
---|
[caa8a94] | 98 | if (stats_threads == NULL) {
|
---|
| 99 | fprintf(stderr, "%s: Unable to get threads\n", NAME);
|
---|
| 100 | return;
|
---|
| 101 | }
|
---|
[a35b458] | 102 |
|
---|
[caa8a94] | 103 | printf("[taskid] [threadid] [state ] [prio] [cpu ] [ucycles] [kcycles]\n");
|
---|
[a35b458] | 104 |
|
---|
[caa8a94] | 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;
|
---|
[a35b458] | 110 |
|
---|
[caa8a94] | 111 | order_suffix(stats_threads[i].ucycles, &ucycles, &usuffix);
|
---|
| 112 | order_suffix(stats_threads[i].kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 113 |
|
---|
[caa8a94] | 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);
|
---|
[a35b458] | 118 |
|
---|
[caa8a94] | 119 | if (stats_threads[i].on_cpu)
|
---|
| 120 | printf("%6u ", stats_threads[i].cpu);
|
---|
| 121 | else
|
---|
| 122 | printf("(none) ");
|
---|
[a35b458] | 123 |
|
---|
[3bacee1] | 124 | printf("%8" PRIu64 "%c %8" PRIu64 "%c\n",
|
---|
[caa8a94] | 125 | ucycles, usuffix, kcycles, ksuffix);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
[a35b458] | 128 |
|
---|
[caa8a94] | 129 | free(stats_threads);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | static void list_cpus(void)
|
---|
[c0379fc] | 133 | {
|
---|
[caa8a94] | 134 | size_t count;
|
---|
| 135 | stats_cpu_t *cpus = stats_get_cpus(&count);
|
---|
[a35b458] | 136 |
|
---|
[caa8a94] | 137 | if (cpus == NULL) {
|
---|
| 138 | fprintf(stderr, "%s: Unable to get CPU statistics\n", NAME);
|
---|
| 139 | return;
|
---|
[9d491a7] | 140 | }
|
---|
[a35b458] | 141 |
|
---|
[caa8a94] | 142 | printf("[id] [MHz ] [busy cycles] [idle cycles]\n");
|
---|
[a35b458] | 143 |
|
---|
[caa8a94] | 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;
|
---|
[a35b458] | 150 |
|
---|
[caa8a94] | 151 | order_suffix(cpus[i].busy_cycles, &bcycles, &bsuffix);
|
---|
| 152 | order_suffix(cpus[i].idle_cycles, &icycles, &isuffix);
|
---|
[a35b458] | 153 |
|
---|
[caa8a94] | 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 | }
|
---|
[a35b458] | 160 |
|
---|
[caa8a94] | 161 | free(cpus);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | static void print_load(void)
|
---|
| 165 | {
|
---|
| 166 | size_t count;
|
---|
| 167 | load_t *load = stats_get_load(&count);
|
---|
[a35b458] | 168 |
|
---|
[caa8a94] | 169 | if (load == NULL) {
|
---|
| 170 | fprintf(stderr, "%s: Unable to get load\n", NAME);
|
---|
| 171 | return;
|
---|
| 172 | }
|
---|
[a35b458] | 173 |
|
---|
[caa8a94] | 174 | printf("%s: Load average: ", NAME);
|
---|
[a35b458] | 175 |
|
---|
[caa8a94] | 176 | size_t i;
|
---|
| 177 | for (i = 0; i < count; i++) {
|
---|
| 178 | if (i > 0)
|
---|
| 179 | printf(" ");
|
---|
[a35b458] | 180 |
|
---|
[caa8a94] | 181 | stats_print_load_fragment(load[i], 2);
|
---|
| 182 | }
|
---|
[a35b458] | 183 |
|
---|
[caa8a94] | 184 | printf("\n");
|
---|
[a35b458] | 185 |
|
---|
[caa8a94] | 186 | free(load);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | static void print_uptime(void)
|
---|
| 190 | {
|
---|
[bd41ac52] | 191 | struct timespec uptime;
|
---|
[1ab8539] | 192 | getuptime(&uptime);
|
---|
[a35b458] | 193 |
|
---|
[bd41ac52] | 194 | printf("%s: Up %lld days, %lld hours, %lld minutes, %lld seconds\n",
|
---|
[1ab8539] | 195 | NAME, uptime.tv_sec / DAY, (uptime.tv_sec % DAY) / HOUR,
|
---|
| 196 | (uptime.tv_sec % HOUR) / MINUTE, uptime.tv_sec % MINUTE);
|
---|
[caa8a94] | 197 | }
|
---|
| 198 |
|
---|
| 199 | static void usage(const char *name)
|
---|
| 200 | {
|
---|
| 201 | printf(
|
---|
[5ef16903] | 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"
|
---|
[caa8a94] | 227 | "\t\tPrint this usage information\n"
|
---|
[5ef16903] | 228 | "\n"
|
---|
[caa8a94] | 229 | "Without any options all tasks are listed\n",
|
---|
[3bacee1] | 230 | name);
|
---|
[caa8a94] | 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;
|
---|
[a35b458] | 241 |
|
---|
[caa8a94] | 242 | task_id_t task_id = 0;
|
---|
[a35b458] | 243 |
|
---|
[caa8a94] | 244 | int i;
|
---|
| 245 | for (i = 1; i < argc; i++) {
|
---|
| 246 | int off;
|
---|
[a35b458] | 247 |
|
---|
[caa8a94] | 248 | /* Usage */
|
---|
| 249 | if ((off = arg_parse_short_long(argv[i], "-h", "--help")) != -1) {
|
---|
| 250 | usage(argv[0]);
|
---|
| 251 | return 0;
|
---|
| 252 | }
|
---|
[a35b458] | 253 |
|
---|
[caa8a94] | 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 | }
|
---|
[a35b458] | 261 |
|
---|
[caa8a94] | 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 | }
|
---|
[a35b458] | 268 |
|
---|
[caa8a94] | 269 | /* Threads */
|
---|
| 270 | if ((off = arg_parse_short_long(argv[i], "-t", "--task=")) != -1) {
|
---|
[ccca251] | 271 | // TODO: Support for 64b range
|
---|
[caa8a94] | 272 | int tmp;
|
---|
[b7fd2a0] | 273 | errno_t ret = arg_parse_int(argc, argv, &i, &tmp, off);
|
---|
[caa8a94] | 274 | if (ret != EOK) {
|
---|
| 275 | printf("%s: Malformed task_id '%s'\n", NAME, argv[i]);
|
---|
| 276 | return -1;
|
---|
| 277 | }
|
---|
[a35b458] | 278 |
|
---|
[caa8a94] | 279 | task_id = tmp;
|
---|
[a35b458] | 280 |
|
---|
[caa8a94] | 281 | toggle_tasks = false;
|
---|
| 282 | toggle_threads = true;
|
---|
| 283 | continue;
|
---|
[9dae191e] | 284 | }
|
---|
[a35b458] | 285 |
|
---|
[caa8a94] | 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 | }
|
---|
[a35b458] | 292 |
|
---|
[caa8a94] | 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 | }
|
---|
[9dae191e] | 299 | }
|
---|
[a35b458] | 300 |
|
---|
[caa8a94] | 301 | if (toggle_tasks)
|
---|
| 302 | list_tasks();
|
---|
[a35b458] | 303 |
|
---|
[caa8a94] | 304 | if (toggle_threads)
|
---|
| 305 | list_threads(task_id, toggle_all);
|
---|
[a35b458] | 306 |
|
---|
[caa8a94] | 307 | if (toggle_cpus)
|
---|
| 308 | list_cpus();
|
---|
[a35b458] | 309 |
|
---|
[caa8a94] | 310 | if (toggle_load)
|
---|
| 311 | print_load();
|
---|
[a35b458] | 312 |
|
---|
[caa8a94] | 313 | if (toggle_uptime)
|
---|
| 314 | print_uptime();
|
---|
[a35b458] | 315 |
|
---|
[c0379fc] | 316 | return 0;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | /** @}
|
---|
| 320 | */
|
---|