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

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

rename str_uint64() to str_uint64_t() for better consistency with the str_<type>() family of functions

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