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