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