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

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

do not provide general access to kernel headers from uspace, only allow specific headers to be accessed or shared
externalize headers which serve as kernel/uspace API/ABI into a special tree

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