| 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 generic
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <typedefs.h>
|
|---|
| 37 | #include <abi/sysinfo.h>
|
|---|
| 38 | #include <sysinfo/stats.h>
|
|---|
| 39 | #include <sysinfo/sysinfo.h>
|
|---|
| 40 | #include <synch/spinlock.h>
|
|---|
| 41 | #include <synch/mutex.h>
|
|---|
| 42 | #include <time/clock.h>
|
|---|
| 43 | #include <mm/frame.h>
|
|---|
| 44 | #include <proc/task.h>
|
|---|
| 45 | #include <proc/thread.h>
|
|---|
| 46 | #include <interrupt.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <errno.h>
|
|---|
| 49 | #include <cpu.h>
|
|---|
| 50 | #include <arch.h>
|
|---|
| 51 |
|
|---|
| 52 | /** Bits of fixed-point precision for load */
|
|---|
| 53 | #define LOAD_FIXED_SHIFT 11
|
|---|
| 54 |
|
|---|
| 55 | /** Uspace load fixed-point precision */
|
|---|
| 56 | #define LOAD_USPACE_SHIFT 6
|
|---|
| 57 |
|
|---|
| 58 | /** Kernel load shift */
|
|---|
| 59 | #define LOAD_KERNEL_SHIFT (LOAD_FIXED_SHIFT - LOAD_USPACE_SHIFT)
|
|---|
| 60 |
|
|---|
| 61 | /** 1.0 as fixed-point for load */
|
|---|
| 62 | #define LOAD_FIXED_1 (1 << LOAD_FIXED_SHIFT)
|
|---|
| 63 |
|
|---|
| 64 | /** Compute load in 5 second intervals */
|
|---|
| 65 | #define LOAD_INTERVAL 5
|
|---|
| 66 |
|
|---|
| 67 | /** Fixed-point representation of
|
|---|
| 68 | *
|
|---|
| 69 | * 1 / exp(5 sec / 1 min)
|
|---|
| 70 | * 1 / exp(5 sec / 5 min)
|
|---|
| 71 | * 1 / exp(5 sec / 15 min)
|
|---|
| 72 | *
|
|---|
| 73 | */
|
|---|
| 74 | static load_t load_exp[LOAD_STEPS] = {1884, 2014, 2037};
|
|---|
| 75 |
|
|---|
| 76 | /** Running average of the number of ready threads */
|
|---|
| 77 | static load_t avenrdy[LOAD_STEPS] = {0, 0, 0};
|
|---|
| 78 |
|
|---|
| 79 | /** Load calculation lock */
|
|---|
| 80 | static mutex_t load_lock;
|
|---|
| 81 |
|
|---|
| 82 | /** Get system uptime
|
|---|
| 83 | *
|
|---|
| 84 | * @param item Sysinfo item (unused).
|
|---|
| 85 | * @param data Unused.
|
|---|
| 86 | *
|
|---|
| 87 | * @return System uptime (in secords).
|
|---|
| 88 | *
|
|---|
| 89 | */
|
|---|
| 90 | static sysarg_t get_stats_uptime(struct sysinfo_item *item, void *data)
|
|---|
| 91 | {
|
|---|
| 92 | /* This doesn't have to be very accurate */
|
|---|
| 93 | return uptime->seconds1;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Get statistics of all CPUs
|
|---|
| 97 | *
|
|---|
| 98 | * @param item Sysinfo item (unused).
|
|---|
| 99 | * @param size Size of the returned data.
|
|---|
| 100 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 101 | * @param data Unused.
|
|---|
| 102 | *
|
|---|
| 103 | * @return Data containing several stats_cpu_t structures.
|
|---|
| 104 | * If the return value is not NULL, it should be freed
|
|---|
| 105 | * in the context of the sysinfo request.
|
|---|
| 106 | */
|
|---|
| 107 | static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
|
|---|
| 108 | bool dry_run, void *data)
|
|---|
| 109 | {
|
|---|
| 110 | *size = sizeof(stats_cpu_t) * config.cpu_count;
|
|---|
| 111 | if (dry_run)
|
|---|
| 112 | return NULL;
|
|---|
| 113 |
|
|---|
| 114 | /* Assumption: config.cpu_count is constant */
|
|---|
| 115 | stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size, FRAME_ATOMIC);
|
|---|
| 116 | if (stats_cpus == NULL) {
|
|---|
| 117 | *size = 0;
|
|---|
| 118 | return NULL;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | size_t i;
|
|---|
| 122 | for (i = 0; i < config.cpu_count; i++) {
|
|---|
| 123 | irq_spinlock_lock(&cpus[i].lock, true);
|
|---|
| 124 |
|
|---|
| 125 | stats_cpus[i].id = cpus[i].id;
|
|---|
| 126 | stats_cpus[i].active = cpus[i].active;
|
|---|
| 127 | stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
|
|---|
| 128 | stats_cpus[i].busy_cycles = cpus[i].busy_cycles;
|
|---|
| 129 | stats_cpus[i].idle_cycles = cpus[i].idle_cycles;
|
|---|
| 130 |
|
|---|
| 131 | irq_spinlock_unlock(&cpus[i].lock, true);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | return ((void *) stats_cpus);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Count number of nodes in an AVL tree
|
|---|
| 138 | *
|
|---|
| 139 | * AVL tree walker for counting nodes.
|
|---|
| 140 | *
|
|---|
| 141 | * @param node AVL tree node (unused).
|
|---|
| 142 | * @param arg Pointer to the counter (size_t).
|
|---|
| 143 | *
|
|---|
| 144 | * @param Always true (continue the walk).
|
|---|
| 145 | *
|
|---|
| 146 | */
|
|---|
| 147 | static bool avl_count_walker(avltree_node_t *node, void *arg)
|
|---|
| 148 | {
|
|---|
| 149 | size_t *count = (size_t *) arg;
|
|---|
| 150 | (*count)++;
|
|---|
| 151 |
|
|---|
| 152 | return true;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /** Get the size of a virtual address space
|
|---|
| 156 | *
|
|---|
| 157 | * @param as Address space.
|
|---|
| 158 | *
|
|---|
| 159 | * @return Size of the mapped virtual address space (bytes).
|
|---|
| 160 | *
|
|---|
| 161 | */
|
|---|
| 162 | static size_t get_task_virtmem(as_t *as)
|
|---|
| 163 | {
|
|---|
| 164 | /*
|
|---|
| 165 | * We are holding spinlocks here and therefore are not allowed to
|
|---|
| 166 | * block. Only attempt to lock the address space and address space
|
|---|
| 167 | * area mutexes conditionally. If it is not possible to lock either
|
|---|
| 168 | * object, return inexact statistics by skipping the respective object.
|
|---|
| 169 | */
|
|---|
| 170 |
|
|---|
| 171 | if (SYNCH_FAILED(mutex_trylock(&as->lock)))
|
|---|
| 172 | return 0;
|
|---|
| 173 |
|
|---|
| 174 | size_t pages = 0;
|
|---|
| 175 |
|
|---|
| 176 | /* Walk the B+ tree and count pages */
|
|---|
| 177 | list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t,
|
|---|
| 178 | node) {
|
|---|
| 179 | unsigned int i;
|
|---|
| 180 | for (i = 0; i < node->keys; i++) {
|
|---|
| 181 | as_area_t *area = node->value[i];
|
|---|
| 182 |
|
|---|
| 183 | if (SYNCH_FAILED(mutex_trylock(&area->lock)))
|
|---|
| 184 | continue;
|
|---|
| 185 |
|
|---|
| 186 | pages += area->pages;
|
|---|
| 187 | mutex_unlock(&area->lock);
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | mutex_unlock(&as->lock);
|
|---|
| 192 |
|
|---|
| 193 | return (pages << PAGE_WIDTH);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** Get the resident (used) size of a virtual address space
|
|---|
| 197 | *
|
|---|
| 198 | * @param as Address space.
|
|---|
| 199 | *
|
|---|
| 200 | * @return Size of the resident (used) virtual address space (bytes).
|
|---|
| 201 | *
|
|---|
| 202 | */
|
|---|
| 203 | static size_t get_task_resmem(as_t *as)
|
|---|
| 204 | {
|
|---|
| 205 | /*
|
|---|
| 206 | * We are holding spinlocks here and therefore are not allowed to
|
|---|
| 207 | * block. Only attempt to lock the address space and address space
|
|---|
| 208 | * area mutexes conditionally. If it is not possible to lock either
|
|---|
| 209 | * object, return inexact statistics by skipping the respective object.
|
|---|
| 210 | */
|
|---|
| 211 |
|
|---|
| 212 | if (SYNCH_FAILED(mutex_trylock(&as->lock)))
|
|---|
| 213 | return 0;
|
|---|
| 214 |
|
|---|
| 215 | size_t pages = 0;
|
|---|
| 216 |
|
|---|
| 217 | /* Walk the B+ tree and count pages */
|
|---|
| 218 | list_foreach(as->as_area_btree.leaf_list, leaf_link, btree_node_t, node) {
|
|---|
| 219 | unsigned int i;
|
|---|
| 220 | for (i = 0; i < node->keys; i++) {
|
|---|
| 221 | as_area_t *area = node->value[i];
|
|---|
| 222 |
|
|---|
| 223 | if (SYNCH_FAILED(mutex_trylock(&area->lock)))
|
|---|
| 224 | continue;
|
|---|
| 225 |
|
|---|
| 226 | pages += area->resident;
|
|---|
| 227 | mutex_unlock(&area->lock);
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | mutex_unlock(&as->lock);
|
|---|
| 232 |
|
|---|
| 233 | return (pages << PAGE_WIDTH);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /* Produce task statistics
|
|---|
| 237 | *
|
|---|
| 238 | * Summarize task information into task statistics.
|
|---|
| 239 | *
|
|---|
| 240 | * @param task Task.
|
|---|
| 241 | * @param stats_task Task statistics.
|
|---|
| 242 | *
|
|---|
| 243 | */
|
|---|
| 244 | static void produce_stats_task(task_t *task, stats_task_t *stats_task)
|
|---|
| 245 | {
|
|---|
| 246 | ASSERT(interrupts_disabled());
|
|---|
| 247 | ASSERT(irq_spinlock_locked(&task->lock));
|
|---|
| 248 |
|
|---|
| 249 | stats_task->task_id = task->taskid;
|
|---|
| 250 | str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
|
|---|
| 251 | stats_task->virtmem = get_task_virtmem(task->as);
|
|---|
| 252 | stats_task->resmem = get_task_resmem(task->as);
|
|---|
| 253 | stats_task->threads = atomic_get(&task->refcount);
|
|---|
| 254 | task_get_accounting(task, &(stats_task->ucycles),
|
|---|
| 255 | &(stats_task->kcycles));
|
|---|
| 256 | stats_task->ipc_info = task->ipc_info;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /** Gather statistics of all tasks
|
|---|
| 260 | *
|
|---|
| 261 | * AVL task tree walker for gathering task statistics. Interrupts should
|
|---|
| 262 | * be already disabled while walking the tree.
|
|---|
| 263 | *
|
|---|
| 264 | * @param node AVL task tree node.
|
|---|
| 265 | * @param arg Pointer to the iterator into the array of stats_task_t.
|
|---|
| 266 | *
|
|---|
| 267 | * @param Always true (continue the walk).
|
|---|
| 268 | *
|
|---|
| 269 | */
|
|---|
| 270 | static bool task_serialize_walker(avltree_node_t *node, void *arg)
|
|---|
| 271 | {
|
|---|
| 272 | stats_task_t **iterator = (stats_task_t **) arg;
|
|---|
| 273 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
|---|
| 274 |
|
|---|
| 275 | /* Interrupts are already disabled */
|
|---|
| 276 | irq_spinlock_lock(&(task->lock), false);
|
|---|
| 277 |
|
|---|
| 278 | /* Record the statistics and increment the iterator */
|
|---|
| 279 | produce_stats_task(task, *iterator);
|
|---|
| 280 | (*iterator)++;
|
|---|
| 281 |
|
|---|
| 282 | irq_spinlock_unlock(&(task->lock), false);
|
|---|
| 283 |
|
|---|
| 284 | return true;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /** Get task statistics
|
|---|
| 288 | *
|
|---|
| 289 | * @param item Sysinfo item (unused).
|
|---|
| 290 | * @param size Size of the returned data.
|
|---|
| 291 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 292 | * @param data Unused.
|
|---|
| 293 | *
|
|---|
| 294 | * @return Data containing several stats_task_t structures.
|
|---|
| 295 | * If the return value is not NULL, it should be freed
|
|---|
| 296 | * in the context of the sysinfo request.
|
|---|
| 297 | */
|
|---|
| 298 | static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
|
|---|
| 299 | bool dry_run, void *data)
|
|---|
| 300 | {
|
|---|
| 301 | /* Messing with task structures, avoid deadlock */
|
|---|
| 302 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 303 |
|
|---|
| 304 | /* First walk the task tree to count the tasks */
|
|---|
| 305 | size_t count = 0;
|
|---|
| 306 | avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
|
|---|
| 307 |
|
|---|
| 308 | if (count == 0) {
|
|---|
| 309 | /* No tasks found (strange) */
|
|---|
| 310 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 311 | *size = 0;
|
|---|
| 312 | return NULL;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | *size = sizeof(stats_task_t) * count;
|
|---|
| 316 | if (dry_run) {
|
|---|
| 317 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 318 | return NULL;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
|
|---|
| 322 | if (stats_tasks == NULL) {
|
|---|
| 323 | /* No free space for allocation */
|
|---|
| 324 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 325 | *size = 0;
|
|---|
| 326 | return NULL;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /* Walk tha task tree again to gather the statistics */
|
|---|
| 330 | stats_task_t *iterator = stats_tasks;
|
|---|
| 331 | avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
|
|---|
| 332 |
|
|---|
| 333 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 334 |
|
|---|
| 335 | return ((void *) stats_tasks);
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /* Produce thread statistics
|
|---|
| 339 | *
|
|---|
| 340 | * Summarize thread information into thread statistics.
|
|---|
| 341 | *
|
|---|
| 342 | * @param thread Thread.
|
|---|
| 343 | * @param stats_thread Thread statistics.
|
|---|
| 344 | *
|
|---|
| 345 | */
|
|---|
| 346 | static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
|
|---|
| 347 | {
|
|---|
| 348 | ASSERT(interrupts_disabled());
|
|---|
| 349 | ASSERT(irq_spinlock_locked(&thread->lock));
|
|---|
| 350 |
|
|---|
| 351 | stats_thread->thread_id = thread->tid;
|
|---|
| 352 | stats_thread->task_id = thread->task->taskid;
|
|---|
| 353 | stats_thread->state = thread->state;
|
|---|
| 354 | stats_thread->priority = thread->priority;
|
|---|
| 355 | stats_thread->ucycles = thread->ucycles;
|
|---|
| 356 | stats_thread->kcycles = thread->kcycles;
|
|---|
| 357 |
|
|---|
| 358 | if (thread->cpu != NULL) {
|
|---|
| 359 | stats_thread->on_cpu = true;
|
|---|
| 360 | stats_thread->cpu = thread->cpu->id;
|
|---|
| 361 | } else
|
|---|
| 362 | stats_thread->on_cpu = false;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | /** Gather statistics of all threads
|
|---|
| 366 | *
|
|---|
| 367 | * AVL three tree walker for gathering thread statistics. Interrupts should
|
|---|
| 368 | * be already disabled while walking the tree.
|
|---|
| 369 | *
|
|---|
| 370 | * @param node AVL thread tree node.
|
|---|
| 371 | * @param arg Pointer to the iterator into the array of thread statistics.
|
|---|
| 372 | *
|
|---|
| 373 | * @param Always true (continue the walk).
|
|---|
| 374 | *
|
|---|
| 375 | */
|
|---|
| 376 | static bool thread_serialize_walker(avltree_node_t *node, void *arg)
|
|---|
| 377 | {
|
|---|
| 378 | stats_thread_t **iterator = (stats_thread_t **) arg;
|
|---|
| 379 | thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
|
|---|
| 380 |
|
|---|
| 381 | /* Interrupts are already disabled */
|
|---|
| 382 | irq_spinlock_lock(&thread->lock, false);
|
|---|
| 383 |
|
|---|
| 384 | /* Record the statistics and increment the iterator */
|
|---|
| 385 | produce_stats_thread(thread, *iterator);
|
|---|
| 386 | (*iterator)++;
|
|---|
| 387 |
|
|---|
| 388 | irq_spinlock_unlock(&thread->lock, false);
|
|---|
| 389 |
|
|---|
| 390 | return true;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | /** Get thread statistics
|
|---|
| 394 | *
|
|---|
| 395 | * @param item Sysinfo item (unused).
|
|---|
| 396 | * @param size Size of the returned data.
|
|---|
| 397 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 398 | * @param data Unused.
|
|---|
| 399 | *
|
|---|
| 400 | * @return Data containing several stats_task_t structures.
|
|---|
| 401 | * If the return value is not NULL, it should be freed
|
|---|
| 402 | * in the context of the sysinfo request.
|
|---|
| 403 | */
|
|---|
| 404 | static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
|
|---|
| 405 | bool dry_run, void *data)
|
|---|
| 406 | {
|
|---|
| 407 | /* Messing with threads structures, avoid deadlock */
|
|---|
| 408 | irq_spinlock_lock(&threads_lock, true);
|
|---|
| 409 |
|
|---|
| 410 | /* First walk the thread tree to count the threads */
|
|---|
| 411 | size_t count = 0;
|
|---|
| 412 | avltree_walk(&threads_tree, avl_count_walker, (void *) &count);
|
|---|
| 413 |
|
|---|
| 414 | if (count == 0) {
|
|---|
| 415 | /* No threads found (strange) */
|
|---|
| 416 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 417 | *size = 0;
|
|---|
| 418 | return NULL;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | *size = sizeof(stats_thread_t) * count;
|
|---|
| 422 | if (dry_run) {
|
|---|
| 423 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 424 | return NULL;
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
|
|---|
| 428 | if (stats_threads == NULL) {
|
|---|
| 429 | /* No free space for allocation */
|
|---|
| 430 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 431 | *size = 0;
|
|---|
| 432 | return NULL;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | /* Walk tha thread tree again to gather the statistics */
|
|---|
| 436 | stats_thread_t *iterator = stats_threads;
|
|---|
| 437 | avltree_walk(&threads_tree, thread_serialize_walker, (void *) &iterator);
|
|---|
| 438 |
|
|---|
| 439 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 440 |
|
|---|
| 441 | return ((void *) stats_threads);
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /** Get a single task statistics
|
|---|
| 445 | *
|
|---|
| 446 | * Get statistics of a given task. The task ID is passed
|
|---|
| 447 | * as a string (current limitation of the sysinfo interface,
|
|---|
| 448 | * but it is still reasonable for the given purpose).
|
|---|
| 449 | *
|
|---|
| 450 | * @param name Task ID (string-encoded number).
|
|---|
| 451 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 452 | * @param data Unused.
|
|---|
| 453 | *
|
|---|
| 454 | * @return Sysinfo return holder. The type of the returned
|
|---|
| 455 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
|---|
| 456 | * task ID or memory allocation error) or
|
|---|
| 457 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
|---|
| 458 | * generated data should be freed within the
|
|---|
| 459 | * sysinfo request context).
|
|---|
| 460 | *
|
|---|
| 461 | */
|
|---|
| 462 | static sysinfo_return_t get_stats_task(const char *name, bool dry_run,
|
|---|
| 463 | void *data)
|
|---|
| 464 | {
|
|---|
| 465 | /* Initially no return value */
|
|---|
| 466 | sysinfo_return_t ret;
|
|---|
| 467 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 468 |
|
|---|
| 469 | /* Parse the task ID */
|
|---|
| 470 | task_id_t task_id;
|
|---|
| 471 | if (str_uint64_t(name, NULL, 0, true, &task_id) != EOK)
|
|---|
| 472 | return ret;
|
|---|
| 473 |
|
|---|
| 474 | /* Messing with task structures, avoid deadlock */
|
|---|
| 475 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 476 |
|
|---|
| 477 | task_t *task = task_find_by_id(task_id);
|
|---|
| 478 | if (task == NULL) {
|
|---|
| 479 | /* No task with this ID */
|
|---|
| 480 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 481 | return ret;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | if (dry_run) {
|
|---|
| 485 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 486 | ret.data.data = NULL;
|
|---|
| 487 | ret.data.size = sizeof(stats_task_t);
|
|---|
| 488 |
|
|---|
| 489 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 490 | } else {
|
|---|
| 491 | /* Allocate stats_task_t structure */
|
|---|
| 492 | stats_task_t *stats_task =
|
|---|
| 493 | (stats_task_t *) malloc(sizeof(stats_task_t), FRAME_ATOMIC);
|
|---|
| 494 | if (stats_task == NULL) {
|
|---|
| 495 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 496 | return ret;
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | /* Correct return value */
|
|---|
| 500 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 501 | ret.data.data = (void *) stats_task;
|
|---|
| 502 | ret.data.size = sizeof(stats_task_t);
|
|---|
| 503 |
|
|---|
| 504 | /* Hand-over-hand locking */
|
|---|
| 505 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
|---|
| 506 |
|
|---|
| 507 | produce_stats_task(task, stats_task);
|
|---|
| 508 |
|
|---|
| 509 | irq_spinlock_unlock(&task->lock, true);
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | return ret;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | /** Get thread statistics
|
|---|
| 516 | *
|
|---|
| 517 | * Get statistics of a given thread. The thread ID is passed
|
|---|
| 518 | * as a string (current limitation of the sysinfo interface,
|
|---|
| 519 | * but it is still reasonable for the given purpose).
|
|---|
| 520 | *
|
|---|
| 521 | * @param name Thread ID (string-encoded number).
|
|---|
| 522 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 523 | * @param data Unused.
|
|---|
| 524 | *
|
|---|
| 525 | * @return Sysinfo return holder. The type of the returned
|
|---|
| 526 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
|---|
| 527 | * thread ID or memory allocation error) or
|
|---|
| 528 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
|---|
| 529 | * generated data should be freed within the
|
|---|
| 530 | * sysinfo request context).
|
|---|
| 531 | *
|
|---|
| 532 | */
|
|---|
| 533 | static sysinfo_return_t get_stats_thread(const char *name, bool dry_run,
|
|---|
| 534 | void *data)
|
|---|
| 535 | {
|
|---|
| 536 | /* Initially no return value */
|
|---|
| 537 | sysinfo_return_t ret;
|
|---|
| 538 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 539 |
|
|---|
| 540 | /* Parse the thread ID */
|
|---|
| 541 | thread_id_t thread_id;
|
|---|
| 542 | if (str_uint64_t(name, NULL, 0, true, &thread_id) != EOK)
|
|---|
| 543 | return ret;
|
|---|
| 544 |
|
|---|
| 545 | /* Messing with threads structures, avoid deadlock */
|
|---|
| 546 | irq_spinlock_lock(&threads_lock, true);
|
|---|
| 547 |
|
|---|
| 548 | thread_t *thread = thread_find_by_id(thread_id);
|
|---|
| 549 | if (thread == NULL) {
|
|---|
| 550 | /* No thread with this ID */
|
|---|
| 551 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 552 | return ret;
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | if (dry_run) {
|
|---|
| 556 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 557 | ret.data.data = NULL;
|
|---|
| 558 | ret.data.size = sizeof(stats_thread_t);
|
|---|
| 559 |
|
|---|
| 560 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 561 | } else {
|
|---|
| 562 | /* Allocate stats_thread_t structure */
|
|---|
| 563 | stats_thread_t *stats_thread =
|
|---|
| 564 | (stats_thread_t *) malloc(sizeof(stats_thread_t), FRAME_ATOMIC);
|
|---|
| 565 | if (stats_thread == NULL) {
|
|---|
| 566 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 567 | return ret;
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | /* Correct return value */
|
|---|
| 571 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 572 | ret.data.data = (void *) stats_thread;
|
|---|
| 573 | ret.data.size = sizeof(stats_thread_t);
|
|---|
| 574 |
|
|---|
| 575 | /* Hand-over-hand locking */
|
|---|
| 576 | irq_spinlock_exchange(&threads_lock, &thread->lock);
|
|---|
| 577 |
|
|---|
| 578 | produce_stats_thread(thread, stats_thread);
|
|---|
| 579 |
|
|---|
| 580 | irq_spinlock_unlock(&thread->lock, true);
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | return ret;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | /** Get exceptions statistics
|
|---|
| 587 | *
|
|---|
| 588 | * @param item Sysinfo item (unused).
|
|---|
| 589 | * @param size Size of the returned data.
|
|---|
| 590 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 591 | * @param data Unused.
|
|---|
| 592 | *
|
|---|
| 593 | * @return Data containing several stats_exc_t structures.
|
|---|
| 594 | * If the return value is not NULL, it should be freed
|
|---|
| 595 | * in the context of the sysinfo request.
|
|---|
| 596 | */
|
|---|
| 597 | static void *get_stats_exceptions(struct sysinfo_item *item, size_t *size,
|
|---|
| 598 | bool dry_run, void *data)
|
|---|
| 599 | {
|
|---|
| 600 | *size = sizeof(stats_exc_t) * IVT_ITEMS;
|
|---|
| 601 |
|
|---|
| 602 | if ((dry_run) || (IVT_ITEMS == 0))
|
|---|
| 603 | return NULL;
|
|---|
| 604 |
|
|---|
| 605 | stats_exc_t *stats_exceptions =
|
|---|
| 606 | (stats_exc_t *) malloc(*size, FRAME_ATOMIC);
|
|---|
| 607 | if (stats_exceptions == NULL) {
|
|---|
| 608 | /* No free space for allocation */
|
|---|
| 609 | *size = 0;
|
|---|
| 610 | return NULL;
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | #if (IVT_ITEMS > 0)
|
|---|
| 614 | /* Messing with exception table, avoid deadlock */
|
|---|
| 615 | irq_spinlock_lock(&exctbl_lock, true);
|
|---|
| 616 |
|
|---|
| 617 | unsigned int i;
|
|---|
| 618 | for (i = 0; i < IVT_ITEMS; i++) {
|
|---|
| 619 | stats_exceptions[i].id = i + IVT_FIRST;
|
|---|
| 620 | str_cpy(stats_exceptions[i].desc, EXC_NAME_BUFLEN, exc_table[i].name);
|
|---|
| 621 | stats_exceptions[i].hot = exc_table[i].hot;
|
|---|
| 622 | stats_exceptions[i].cycles = exc_table[i].cycles;
|
|---|
| 623 | stats_exceptions[i].count = exc_table[i].count;
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | irq_spinlock_unlock(&exctbl_lock, true);
|
|---|
| 627 | #endif
|
|---|
| 628 |
|
|---|
| 629 | return ((void *) stats_exceptions);
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | /** Get exception statistics
|
|---|
| 633 | *
|
|---|
| 634 | * Get statistics of a given exception. The exception number
|
|---|
| 635 | * is passed as a string (current limitation of the sysinfo
|
|---|
| 636 | * interface, but it is still reasonable for the given purpose).
|
|---|
| 637 | *
|
|---|
| 638 | * @param name Exception number (string-encoded number).
|
|---|
| 639 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 640 | * @param data Unused.
|
|---|
| 641 | *
|
|---|
| 642 | * @return Sysinfo return holder. The type of the returned
|
|---|
| 643 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
|---|
| 644 | * exception number or memory allocation error) or
|
|---|
| 645 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
|---|
| 646 | * generated data should be freed within the
|
|---|
| 647 | * sysinfo request context).
|
|---|
| 648 | *
|
|---|
| 649 | */
|
|---|
| 650 | static sysinfo_return_t get_stats_exception(const char *name, bool dry_run,
|
|---|
| 651 | void *data)
|
|---|
| 652 | {
|
|---|
| 653 | /* Initially no return value */
|
|---|
| 654 | sysinfo_return_t ret;
|
|---|
| 655 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 656 |
|
|---|
| 657 | /* Parse the exception number */
|
|---|
| 658 | uint64_t excn;
|
|---|
| 659 | if (str_uint64_t(name, NULL, 0, true, &excn) != EOK)
|
|---|
| 660 | return ret;
|
|---|
| 661 |
|
|---|
| 662 | #if (IVT_FIRST > 0)
|
|---|
| 663 | if (excn < IVT_FIRST)
|
|---|
| 664 | return ret;
|
|---|
| 665 | #endif
|
|---|
| 666 |
|
|---|
| 667 | #if (IVT_ITEMS + IVT_FIRST == 0)
|
|---|
| 668 | return ret;
|
|---|
| 669 | #else
|
|---|
| 670 | if (excn >= IVT_ITEMS + IVT_FIRST)
|
|---|
| 671 | return ret;
|
|---|
| 672 | #endif
|
|---|
| 673 |
|
|---|
| 674 | if (dry_run) {
|
|---|
| 675 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 676 | ret.data.data = NULL;
|
|---|
| 677 | ret.data.size = sizeof(stats_thread_t);
|
|---|
| 678 | } else {
|
|---|
| 679 | /* Update excn index for accessing exc_table */
|
|---|
| 680 | excn -= IVT_FIRST;
|
|---|
| 681 |
|
|---|
| 682 | /* Allocate stats_exc_t structure */
|
|---|
| 683 | stats_exc_t *stats_exception =
|
|---|
| 684 | (stats_exc_t *) malloc(sizeof(stats_exc_t), FRAME_ATOMIC);
|
|---|
| 685 | if (stats_exception == NULL)
|
|---|
| 686 | return ret;
|
|---|
| 687 |
|
|---|
| 688 | /* Messing with exception table, avoid deadlock */
|
|---|
| 689 | irq_spinlock_lock(&exctbl_lock, true);
|
|---|
| 690 |
|
|---|
| 691 | /* Correct return value */
|
|---|
| 692 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 693 | ret.data.data = (void *) stats_exception;
|
|---|
| 694 | ret.data.size = sizeof(stats_exc_t);
|
|---|
| 695 |
|
|---|
| 696 | stats_exception->id = excn;
|
|---|
| 697 | str_cpy(stats_exception->desc, EXC_NAME_BUFLEN, exc_table[excn].name);
|
|---|
| 698 | stats_exception->hot = exc_table[excn].hot;
|
|---|
| 699 | stats_exception->cycles = exc_table[excn].cycles;
|
|---|
| 700 | stats_exception->count = exc_table[excn].count;
|
|---|
| 701 |
|
|---|
| 702 | irq_spinlock_unlock(&exctbl_lock, true);
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | return ret;
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | /** Get physical memory statistics
|
|---|
| 709 | *
|
|---|
| 710 | * @param item Sysinfo item (unused).
|
|---|
| 711 | * @param size Size of the returned data.
|
|---|
| 712 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 713 | * @param data Unused.
|
|---|
| 714 | *
|
|---|
| 715 | * @return Data containing stats_physmem_t.
|
|---|
| 716 | * If the return value is not NULL, it should be freed
|
|---|
| 717 | * in the context of the sysinfo request.
|
|---|
| 718 | */
|
|---|
| 719 | static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
|
|---|
| 720 | bool dry_run, void *data)
|
|---|
| 721 | {
|
|---|
| 722 | *size = sizeof(stats_physmem_t);
|
|---|
| 723 | if (dry_run)
|
|---|
| 724 | return NULL;
|
|---|
| 725 |
|
|---|
| 726 | stats_physmem_t *stats_physmem =
|
|---|
| 727 | (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
|
|---|
| 728 | if (stats_physmem == NULL) {
|
|---|
| 729 | *size = 0;
|
|---|
| 730 | return NULL;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
|
|---|
| 734 | &(stats_physmem->used), &(stats_physmem->free));
|
|---|
| 735 |
|
|---|
| 736 | return ((void *) stats_physmem);
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | /** Get system load
|
|---|
| 740 | *
|
|---|
| 741 | * @param item Sysinfo item (unused).
|
|---|
| 742 | * @param size Size of the returned data.
|
|---|
| 743 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 744 | * @param data Unused.
|
|---|
| 745 | *
|
|---|
| 746 | * @return Data several load_t values.
|
|---|
| 747 | * If the return value is not NULL, it should be freed
|
|---|
| 748 | * in the context of the sysinfo request.
|
|---|
| 749 | */
|
|---|
| 750 | static void *get_stats_load(struct sysinfo_item *item, size_t *size,
|
|---|
| 751 | bool dry_run, void *data)
|
|---|
| 752 | {
|
|---|
| 753 | *size = sizeof(load_t) * LOAD_STEPS;
|
|---|
| 754 | if (dry_run)
|
|---|
| 755 | return NULL;
|
|---|
| 756 |
|
|---|
| 757 | load_t *stats_load = (load_t *) malloc(*size, FRAME_ATOMIC);
|
|---|
| 758 | if (stats_load == NULL) {
|
|---|
| 759 | *size = 0;
|
|---|
| 760 | return NULL;
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | /* To always get consistent values acquire the mutex */
|
|---|
| 764 | mutex_lock(&load_lock);
|
|---|
| 765 |
|
|---|
| 766 | unsigned int i;
|
|---|
| 767 | for (i = 0; i < LOAD_STEPS; i++)
|
|---|
| 768 | stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
|
|---|
| 769 |
|
|---|
| 770 | mutex_unlock(&load_lock);
|
|---|
| 771 |
|
|---|
| 772 | return ((void *) stats_load);
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | /** Calculate load
|
|---|
| 776 | *
|
|---|
| 777 | */
|
|---|
| 778 | static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
|
|---|
| 779 | {
|
|---|
| 780 | load *= exp;
|
|---|
| 781 | load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
|
|---|
| 782 |
|
|---|
| 783 | return (load >> LOAD_FIXED_SHIFT);
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | /** Load computation thread.
|
|---|
| 787 | *
|
|---|
| 788 | * Compute system load every few seconds.
|
|---|
| 789 | *
|
|---|
| 790 | * @param arg Unused.
|
|---|
| 791 | *
|
|---|
| 792 | */
|
|---|
| 793 | void kload(void *arg)
|
|---|
| 794 | {
|
|---|
| 795 | thread_detach(THREAD);
|
|---|
| 796 |
|
|---|
| 797 | while (true) {
|
|---|
| 798 | atomic_count_t ready = atomic_get(&nrdy);
|
|---|
| 799 |
|
|---|
| 800 | /* Mutually exclude with get_stats_load() */
|
|---|
| 801 | mutex_lock(&load_lock);
|
|---|
| 802 |
|
|---|
| 803 | unsigned int i;
|
|---|
| 804 | for (i = 0; i < LOAD_STEPS; i++)
|
|---|
| 805 | avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
|
|---|
| 806 |
|
|---|
| 807 | mutex_unlock(&load_lock);
|
|---|
| 808 |
|
|---|
| 809 | thread_sleep(LOAD_INTERVAL);
|
|---|
| 810 | }
|
|---|
| 811 | }
|
|---|
| 812 |
|
|---|
| 813 | /** Register sysinfo statistical items
|
|---|
| 814 | *
|
|---|
| 815 | */
|
|---|
| 816 | void stats_init(void)
|
|---|
| 817 | {
|
|---|
| 818 | mutex_initialize(&load_lock, MUTEX_PASSIVE);
|
|---|
| 819 |
|
|---|
| 820 | sysinfo_set_item_gen_val("system.uptime", NULL, get_stats_uptime, NULL);
|
|---|
| 821 | sysinfo_set_item_gen_data("system.cpus", NULL, get_stats_cpus, NULL);
|
|---|
| 822 | sysinfo_set_item_gen_data("system.physmem", NULL, get_stats_physmem, NULL);
|
|---|
| 823 | sysinfo_set_item_gen_data("system.load", NULL, get_stats_load, NULL);
|
|---|
| 824 | sysinfo_set_item_gen_data("system.tasks", NULL, get_stats_tasks, NULL);
|
|---|
| 825 | sysinfo_set_item_gen_data("system.threads", NULL, get_stats_threads, NULL);
|
|---|
| 826 | sysinfo_set_item_gen_data("system.exceptions", NULL, get_stats_exceptions, NULL);
|
|---|
| 827 | sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task, NULL);
|
|---|
| 828 | sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread, NULL);
|
|---|
| 829 | sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception, NULL);
|
|---|
| 830 | }
|
|---|
| 831 |
|
|---|
| 832 | /** @}
|
|---|
| 833 | */
|
|---|