source: mainline/kernel/generic/src/sysinfo/stats.c@ 1ab8539

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1ab8539 was 1ab8539, checked in by Martin Decky <martin@…>, 11 years ago

remove system.uptime sysinfo entry since it is redundant
cleanup the time handling routines

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