[9dae191e] | 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>
|
---|
[6e121b8] | 40 | #include <synch/spinlock.h>
|
---|
| 41 | #include <synch/mutex.h>
|
---|
[9dae191e] | 42 | #include <time/clock.h>
|
---|
| 43 | #include <mm/frame.h>
|
---|
[e1b6742] | 44 | #include <proc/task.h>
|
---|
[9dae191e] | 45 | #include <proc/thread.h>
|
---|
[8eec3c8] | 46 | #include <interrupt.h>
|
---|
[9dae191e] | 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 |
|
---|
[fd3a631f] | 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 |
|
---|
[9dae191e] | 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 |
|
---|
[80bfb601] | 67 | /** Fixed-point representation of
|
---|
[9dae191e] | 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};
|
---|
[80bfb601] | 75 |
|
---|
| 76 | /** Running average of the number of ready threads */
|
---|
[9dae191e] | 77 | static load_t avenrdy[LOAD_STEPS] = {0, 0, 0};
|
---|
[80bfb601] | 78 |
|
---|
[6e121b8] | 79 | /** Load calculation lock */
|
---|
| 80 | static mutex_t load_lock;
|
---|
[9dae191e] | 81 |
|
---|
[80bfb601] | 82 | /** Get system uptime
|
---|
| 83 | *
|
---|
| 84 | * @param item Sysinfo item (unused).
|
---|
| 85 | *
|
---|
| 86 | * @return System uptime (in secords).
|
---|
| 87 | *
|
---|
| 88 | */
|
---|
[9dae191e] | 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 |
|
---|
[80bfb601] | 95 | /** Get statistics of all CPUs
|
---|
| 96 | *
|
---|
[70e2b2d] | 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.
|
---|
[80bfb601] | 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 | */
|
---|
[70e2b2d] | 105 | static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
|
---|
| 106 | bool dry_run)
|
---|
[9dae191e] | 107 | {
|
---|
[70e2b2d] | 108 | *size = sizeof(stats_cpu_t) * config.cpu_count;
|
---|
| 109 | if (dry_run)
|
---|
| 110 | return NULL;
|
---|
| 111 |
|
---|
[80bfb601] | 112 | /* Assumption: config.cpu_count is constant */
|
---|
[70e2b2d] | 113 | stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size, FRAME_ATOMIC);
|
---|
[9dae191e] | 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++) {
|
---|
[da1bafb] | 121 | irq_spinlock_lock(&cpus[i].lock, true);
|
---|
[9dae191e] | 122 |
|
---|
| 123 | stats_cpus[i].id = cpus[i].id;
|
---|
[bd01a4e] | 124 | stats_cpus[i].active = cpus[i].active;
|
---|
[9dae191e] | 125 | stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
|
---|
[d0c82c5] | 126 | stats_cpus[i].busy_cycles = cpus[i].busy_cycles;
|
---|
| 127 | stats_cpus[i].idle_cycles = cpus[i].idle_cycles;
|
---|
[9dae191e] | 128 |
|
---|
[da1bafb] | 129 | irq_spinlock_unlock(&cpus[i].lock, true);
|
---|
[9dae191e] | 130 | }
|
---|
| 131 |
|
---|
| 132 | return ((void *) stats_cpus);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[e1b6742] | 135 | /** Count number of nodes in an AVL tree
|
---|
[80bfb601] | 136 | *
|
---|
[e1b6742] | 137 | * AVL tree walker for counting nodes.
|
---|
[80bfb601] | 138 | *
|
---|
[e1b6742] | 139 | * @param node AVL tree node (unused).
|
---|
| 140 | * @param arg Pointer to the counter (size_t).
|
---|
[80bfb601] | 141 | *
|
---|
| 142 | * @param Always true (continue the walk).
|
---|
| 143 | *
|
---|
| 144 | */
|
---|
[e1b6742] | 145 | static bool avl_count_walker(avltree_node_t *node, void *arg)
|
---|
[9dae191e] | 146 | {
|
---|
| 147 | size_t *count = (size_t *) arg;
|
---|
| 148 | (*count)++;
|
---|
| 149 |
|
---|
| 150 | return true;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[dec16a2] | 153 | /** Get the size of a virtual address space
|
---|
| 154 | *
|
---|
| 155 | * @param as Address space.
|
---|
[80bfb601] | 156 | *
|
---|
[dec16a2] | 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;
|
---|
[d69f959] | 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 |
|
---|
[acda8f0] | 175 | if (SYNCH_FAILED(mutex_trylock(&as->lock)))
|
---|
[d69f959] | 176 | return result * PAGE_SIZE;
|
---|
[dec16a2] | 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 |
|
---|
[acda8f0] | 189 | if (SYNCH_FAILED(mutex_trylock(&area->lock)))
|
---|
[d69f959] | 190 | continue;
|
---|
[dec16a2] | 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 | {
|
---|
[1d432f9] | 211 | ASSERT(interrupts_disabled());
|
---|
| 212 | ASSERT(irq_spinlock_locked(&task->lock));
|
---|
| 213 |
|
---|
[dec16a2] | 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
|
---|
[80bfb601] | 226 | * be already disabled while walking the tree.
|
---|
| 227 | *
|
---|
| 228 | * @param node AVL task tree node.
|
---|
[dec16a2] | 229 | * @param arg Pointer to the iterator into the array of stats_task_t.
|
---|
[80bfb601] | 230 | *
|
---|
| 231 | * @param Always true (continue the walk).
|
---|
| 232 | *
|
---|
| 233 | */
|
---|
[9dae191e] | 234 | static bool task_serialize_walker(avltree_node_t *node, void *arg)
|
---|
| 235 | {
|
---|
[dec16a2] | 236 | stats_task_t **iterator = (stats_task_t **) arg;
|
---|
[9dae191e] | 237 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
| 238 |
|
---|
[80bfb601] | 239 | /* Interrupts are already disabled */
|
---|
[da1bafb] | 240 | irq_spinlock_lock(&(task->lock), false);
|
---|
[9dae191e] | 241 |
|
---|
[dec16a2] | 242 | /* Record the statistics and increment the iterator */
|
---|
| 243 | produce_stats_task(task, *iterator);
|
---|
| 244 | (*iterator)++;
|
---|
[9dae191e] | 245 |
|
---|
[da1bafb] | 246 | irq_spinlock_unlock(&(task->lock), false);
|
---|
[9dae191e] | 247 |
|
---|
| 248 | return true;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[dec16a2] | 251 | /** Get task statistics
|
---|
[80bfb601] | 252 | *
|
---|
[70e2b2d] | 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.
|
---|
[80bfb601] | 256 | *
|
---|
[dec16a2] | 257 | * @return Data containing several stats_task_t structures.
|
---|
[80bfb601] | 258 | * If the return value is not NULL, it should be freed
|
---|
| 259 | * in the context of the sysinfo request.
|
---|
| 260 | */
|
---|
[70e2b2d] | 261 | static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
|
---|
| 262 | bool dry_run)
|
---|
[9dae191e] | 263 | {
|
---|
| 264 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 265 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[9dae191e] | 266 |
|
---|
[80bfb601] | 267 | /* First walk the task tree to count the tasks */
|
---|
[9dae191e] | 268 | size_t count = 0;
|
---|
[e1b6742] | 269 | avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
|
---|
[9dae191e] | 270 |
|
---|
| 271 | if (count == 0) {
|
---|
[80bfb601] | 272 | /* No tasks found (strange) */
|
---|
[da1bafb] | 273 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[9dae191e] | 274 | *size = 0;
|
---|
| 275 | return NULL;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[dec16a2] | 278 | *size = sizeof(stats_task_t) * count;
|
---|
[70e2b2d] | 279 | if (dry_run) {
|
---|
[da1bafb] | 280 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[70e2b2d] | 281 | return NULL;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[dec16a2] | 284 | stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
|
---|
| 285 | if (stats_tasks == NULL) {
|
---|
[80bfb601] | 286 | /* No free space for allocation */
|
---|
[da1bafb] | 287 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[9dae191e] | 288 | *size = 0;
|
---|
| 289 | return NULL;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[dec16a2] | 292 | /* Walk tha task tree again to gather the statistics */
|
---|
| 293 | stats_task_t *iterator = stats_tasks;
|
---|
[9dae191e] | 294 | avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
|
---|
| 295 |
|
---|
[da1bafb] | 296 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[9dae191e] | 297 |
|
---|
[dec16a2] | 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 | {
|
---|
[1d432f9] | 311 | ASSERT(interrupts_disabled());
|
---|
| 312 | ASSERT(irq_spinlock_locked(&thread->lock));
|
---|
| 313 |
|
---|
[dec16a2] | 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;
|
---|
[9dae191e] | 326 | }
|
---|
| 327 |
|
---|
[dec16a2] | 328 | /** Gather statistics of all threads
|
---|
[e1b6742] | 329 | *
|
---|
[dec16a2] | 330 | * AVL three tree walker for gathering thread statistics. Interrupts should
|
---|
[e1b6742] | 331 | * be already disabled while walking the tree.
|
---|
| 332 | *
|
---|
| 333 | * @param node AVL thread tree node.
|
---|
[dec16a2] | 334 | * @param arg Pointer to the iterator into the array of thread statistics.
|
---|
[e1b6742] | 335 | *
|
---|
| 336 | * @param Always true (continue the walk).
|
---|
| 337 | *
|
---|
| 338 | */
|
---|
| 339 | static bool thread_serialize_walker(avltree_node_t *node, void *arg)
|
---|
| 340 | {
|
---|
[dec16a2] | 341 | stats_thread_t **iterator = (stats_thread_t **) arg;
|
---|
[e1b6742] | 342 | thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
|
---|
| 343 |
|
---|
| 344 | /* Interrupts are already disabled */
|
---|
[da1bafb] | 345 | irq_spinlock_lock(&thread->lock, false);
|
---|
[e1b6742] | 346 |
|
---|
[dec16a2] | 347 | /* Record the statistics and increment the iterator */
|
---|
| 348 | produce_stats_thread(thread, *iterator);
|
---|
| 349 | (*iterator)++;
|
---|
[e1b6742] | 350 |
|
---|
[da1bafb] | 351 | irq_spinlock_unlock(&thread->lock, false);
|
---|
[e1b6742] | 352 |
|
---|
| 353 | return true;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
[dec16a2] | 356 | /** Get thread statistics
|
---|
[e1b6742] | 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 | *
|
---|
[dec16a2] | 362 | * @return Data containing several stats_task_t structures.
|
---|
[e1b6742] | 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 */
|
---|
[da1bafb] | 370 | irq_spinlock_lock(&threads_lock, true);
|
---|
[e1b6742] | 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) */
|
---|
[da1bafb] | 378 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[e1b6742] | 379 | *size = 0;
|
---|
| 380 | return NULL;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[dec16a2] | 383 | *size = sizeof(stats_thread_t) * count;
|
---|
[e1b6742] | 384 | if (dry_run) {
|
---|
[da1bafb] | 385 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[e1b6742] | 386 | return NULL;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[dec16a2] | 389 | stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
|
---|
| 390 | if (stats_threads == NULL) {
|
---|
[e1b6742] | 391 | /* No free space for allocation */
|
---|
[da1bafb] | 392 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[e1b6742] | 393 | *size = 0;
|
---|
| 394 | return NULL;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[dec16a2] | 397 | /* Walk tha thread tree again to gather the statistics */
|
---|
| 398 | stats_thread_t *iterator = stats_threads;
|
---|
[e1b6742] | 399 | avltree_walk(&threads_tree, thread_serialize_walker, (void *) &iterator);
|
---|
| 400 |
|
---|
[da1bafb] | 401 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[e1b6742] | 402 |
|
---|
[dec16a2] | 403 | return ((void *) stats_threads);
|
---|
[e1b6742] | 404 | }
|
---|
| 405 |
|
---|
[dec16a2] | 406 | /** Get a single task statistics
|
---|
[80bfb601] | 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 | *
|
---|
[e1b6742] | 412 | * @param name Task ID (string-encoded number).
|
---|
| 413 | * @param dry_run Do not get the data, just calculate the size.
|
---|
[80bfb601] | 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 | */
|
---|
[e1b6742] | 423 | static sysinfo_return_t get_stats_task(const char *name, bool dry_run)
|
---|
[9dae191e] | 424 | {
|
---|
[80bfb601] | 425 | /* Initially no return value */
|
---|
[9dae191e] | 426 | sysinfo_return_t ret;
|
---|
| 427 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
---|
| 428 |
|
---|
[80bfb601] | 429 | /* Parse the task ID */
|
---|
[9dae191e] | 430 | task_id_t task_id;
|
---|
| 431 | if (str_uint64(name, NULL, 0, true, &task_id) != EOK)
|
---|
| 432 | return ret;
|
---|
| 433 |
|
---|
[80bfb601] | 434 | /* Messing with task structures, avoid deadlock */
|
---|
[da1bafb] | 435 | irq_spinlock_lock(&tasks_lock, true);
|
---|
[9dae191e] | 436 |
|
---|
| 437 | task_t *task = task_find_by_id(task_id);
|
---|
| 438 | if (task == NULL) {
|
---|
[80bfb601] | 439 | /* No task with this ID */
|
---|
[da1bafb] | 440 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[9dae191e] | 441 | return ret;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[e1b6742] | 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 |
|
---|
[da1bafb] | 449 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[e1b6742] | 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) {
|
---|
[da1bafb] | 455 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
[e1b6742] | 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);
|
---|
[da1bafb] | 463 |
|
---|
[e1b6742] | 464 | /* Hand-over-hand locking */
|
---|
[da1bafb] | 465 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
[e1b6742] | 466 |
|
---|
[dec16a2] | 467 | produce_stats_task(task, stats_task);
|
---|
[e1b6742] | 468 |
|
---|
[da1bafb] | 469 | irq_spinlock_unlock(&task->lock, true);
|
---|
[e1b6742] | 470 | }
|
---|
[9dae191e] | 471 |
|
---|
[e1b6742] | 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 */
|
---|
[da1bafb] | 504 | irq_spinlock_lock(&threads_lock, true);
|
---|
[e1b6742] | 505 |
|
---|
| 506 | thread_t *thread = thread_find_by_id(thread_id);
|
---|
| 507 | if (thread == NULL) {
|
---|
| 508 | /* No thread with this ID */
|
---|
[da1bafb] | 509 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[e1b6742] | 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 |
|
---|
[da1bafb] | 518 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[e1b6742] | 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) {
|
---|
[da1bafb] | 524 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[e1b6742] | 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);
|
---|
[dec16a2] | 532 |
|
---|
[e1b6742] | 533 | /* Hand-over-hand locking */
|
---|
[da1bafb] | 534 | irq_spinlock_exchange(&threads_lock, &thread->lock);
|
---|
[e1b6742] | 535 |
|
---|
[dec16a2] | 536 | produce_stats_thread(thread, stats_thread);
|
---|
[e1b6742] | 537 |
|
---|
[da1bafb] | 538 | irq_spinlock_unlock(&thread->lock, true);
|
---|
[e1b6742] | 539 | }
|
---|
| 540 |
|
---|
[9dae191e] | 541 | return ret;
|
---|
| 542 | }
|
---|
| 543 |
|
---|
[8eec3c8] | 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 |
|
---|
[b3b7e14a] | 570 | #if (IVT_ITEMS > 0)
|
---|
[8eec3c8] | 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);
|
---|
[b3b7e14a] | 578 | stats_exceptions[i].hot = exc_table[i].hot;
|
---|
[8eec3c8] | 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);
|
---|
[b3b7e14a] | 584 | #endif
|
---|
[8eec3c8] | 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 |
|
---|
[b3b7e14a] | 617 | #if (IVT_FIRST > 0)
|
---|
[8eec3c8] | 618 | if (excn < IVT_FIRST)
|
---|
| 619 | return ret;
|
---|
| 620 | #endif
|
---|
| 621 |
|
---|
[b3b7e14a] | 622 | #if (IVT_ITEMS + IVT_FIRST == 0)
|
---|
| 623 | return ret;
|
---|
| 624 | #else
|
---|
[8eec3c8] | 625 | if (excn >= IVT_ITEMS + IVT_FIRST)
|
---|
| 626 | return ret;
|
---|
[b3b7e14a] | 627 | #endif
|
---|
[8eec3c8] | 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);
|
---|
[b3b7e14a] | 653 | stats_exception->hot = exc_table[excn].hot;
|
---|
[8eec3c8] | 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 |
|
---|
[80bfb601] | 663 | /** Get physical memory statistics
|
---|
| 664 | *
|
---|
[70e2b2d] | 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.
|
---|
[80bfb601] | 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 | */
|
---|
[70e2b2d] | 673 | static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
|
---|
| 674 | bool dry_run)
|
---|
[9dae191e] | 675 | {
|
---|
[70e2b2d] | 676 | *size = sizeof(stats_physmem_t);
|
---|
| 677 | if (dry_run)
|
---|
| 678 | return NULL;
|
---|
| 679 |
|
---|
[9dae191e] | 680 | stats_physmem_t *stats_physmem =
|
---|
[70e2b2d] | 681 | (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
|
---|
[9dae191e] | 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 |
|
---|
[80bfb601] | 693 | /** Get system load
|
---|
| 694 | *
|
---|
[70e2b2d] | 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.
|
---|
[80bfb601] | 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 | */
|
---|
[70e2b2d] | 703 | static void *get_stats_load(struct sysinfo_item *item, size_t *size,
|
---|
| 704 | bool dry_run)
|
---|
[9dae191e] | 705 | {
|
---|
[70e2b2d] | 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);
|
---|
[9dae191e] | 711 | if (stats_load == NULL) {
|
---|
| 712 | *size = 0;
|
---|
| 713 | return NULL;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
[6e121b8] | 716 | /* To always get consistent values acquire the mutex */
|
---|
| 717 | mutex_lock(&load_lock);
|
---|
[9dae191e] | 718 |
|
---|
| 719 | unsigned int i;
|
---|
| 720 | for (i = 0; i < LOAD_STEPS; i++)
|
---|
[fd3a631f] | 721 | stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
|
---|
[9dae191e] | 722 |
|
---|
[6e121b8] | 723 | mutex_unlock(&load_lock);
|
---|
[9dae191e] | 724 |
|
---|
| 725 | return ((void *) stats_load);
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | /** Calculate load
|
---|
| 729 | *
|
---|
| 730 | */
|
---|
[9efff92] | 731 | static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
|
---|
[9dae191e] | 732 | {
|
---|
| 733 | load *= exp;
|
---|
[9efff92] | 734 | load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
|
---|
[9dae191e] | 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) {
|
---|
[9efff92] | 751 | atomic_count_t ready = atomic_get(&nrdy);
|
---|
| 752 |
|
---|
[80bfb601] | 753 | /* Mutually exclude with get_stats_load() */
|
---|
[6e121b8] | 754 | mutex_lock(&load_lock);
|
---|
[9dae191e] | 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 |
|
---|
[6e121b8] | 760 | mutex_unlock(&load_lock);
|
---|
[9dae191e] | 761 |
|
---|
| 762 | thread_sleep(LOAD_INTERVAL);
|
---|
| 763 | }
|
---|
| 764 | }
|
---|
| 765 |
|
---|
[80bfb601] | 766 | /** Register sysinfo statistical items
|
---|
| 767 | *
|
---|
| 768 | */
|
---|
[9dae191e] | 769 | void stats_init(void)
|
---|
| 770 | {
|
---|
[6e121b8] | 771 | mutex_initialize(&load_lock, MUTEX_PASSIVE);
|
---|
[da1bafb] | 772 |
|
---|
[9dae191e] | 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);
|
---|
[e1b6742] | 778 | sysinfo_set_item_fn_data("system.threads", NULL, get_stats_threads);
|
---|
[8eec3c8] | 779 | sysinfo_set_item_fn_data("system.exceptions", NULL, get_stats_exceptions);
|
---|
[9dae191e] | 780 | sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task);
|
---|
[e1b6742] | 781 | sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread);
|
---|
[8eec3c8] | 782 | sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception);
|
---|
[9dae191e] | 783 | }
|
---|
| 784 |
|
---|
| 785 | /** @}
|
---|
| 786 | */
|
---|