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

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

keep track of the number of resident pages on-the-fly (do not traverse the B+ tree while reading sysinfo)
cstyle updates (mostly update of comments)

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