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