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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e435537 was 059a8e4, checked in by Martin Decky <martin@…>, 13 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
Line 
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 <abi/sysinfo.h>
38#include <sysinfo/stats.h>
39#include <sysinfo/sysinfo.h>
40#include <synch/spinlock.h>
41#include <synch/mutex.h>
42#include <time/clock.h>
43#include <mm/frame.h>
44#include <proc/task.h>
45#include <proc/thread.h>
46#include <interrupt.h>
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
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
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
67/** Fixed-point representation of
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};
75
76/** Running average of the number of ready threads */
77static load_t avenrdy[LOAD_STEPS] = {0, 0, 0};
78
79/** Load calculation lock */
80static mutex_t load_lock;
81
82/** Get system uptime
83 *
84 * @param item Sysinfo item (unused).
85 * @param data Unused.
86 *
87 * @return System uptime (in secords).
88 *
89 */
90static sysarg_t get_stats_uptime(struct sysinfo_item *item, void *data)
91{
92 /* This doesn't have to be very accurate */
93 return uptime->seconds1;
94}
95
96/** Get statistics of all CPUs
97 *
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.
101 * @param data Unused.
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 */
107static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
108 bool dry_run, void *data)
109{
110 *size = sizeof(stats_cpu_t) * config.cpu_count;
111 if (dry_run)
112 return NULL;
113
114 /* Assumption: config.cpu_count is constant */
115 stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size, FRAME_ATOMIC);
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++) {
123 irq_spinlock_lock(&cpus[i].lock, true);
124
125 stats_cpus[i].id = cpus[i].id;
126 stats_cpus[i].active = cpus[i].active;
127 stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
128 stats_cpus[i].busy_cycles = cpus[i].busy_cycles;
129 stats_cpus[i].idle_cycles = cpus[i].idle_cycles;
130
131 irq_spinlock_unlock(&cpus[i].lock, true);
132 }
133
134 return ((void *) stats_cpus);
135}
136
137/** Count number of nodes in an AVL tree
138 *
139 * AVL tree walker for counting nodes.
140 *
141 * @param node AVL tree node (unused).
142 * @param arg Pointer to the counter (size_t).
143 *
144 * @param Always true (continue the walk).
145 *
146 */
147static bool avl_count_walker(avltree_node_t *node, void *arg)
148{
149 size_t *count = (size_t *) arg;
150 (*count)++;
151
152 return true;
153}
154
155/** Get the size of a virtual address space
156 *
157 * @param as Address space.
158 *
159 * @return Size of the mapped virtual address space (bytes).
160 *
161 */
162static size_t get_task_virtmem(as_t *as)
163{
164 /*
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.
169 */
170
171 if (SYNCH_FAILED(mutex_trylock(&as->lock)))
172 return 0;
173
174 size_t pages = 0;
175
176 /* Walk the B+ tree and count pages */
177 list_foreach(as->as_area_btree.leaf_list, cur) {
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
185 if (SYNCH_FAILED(mutex_trylock(&area->lock)))
186 continue;
187
188 pages += area->pages;
189 mutex_unlock(&area->lock);
190 }
191 }
192
193 mutex_unlock(&as->lock);
194
195 return (pages << PAGE_WIDTH);
196}
197
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 /*
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.
212 */
213
214 if (SYNCH_FAILED(mutex_trylock(&as->lock)))
215 return 0;
216
217 size_t pages = 0;
218
219 /* Walk the B+ tree and count pages */
220 list_foreach(as->as_area_btree.leaf_list, cur) {
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
231 pages += area->resident;
232 mutex_unlock(&area->lock);
233 }
234 }
235
236 mutex_unlock(&as->lock);
237
238 return (pages << PAGE_WIDTH);
239}
240
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{
251 ASSERT(interrupts_disabled());
252 ASSERT(irq_spinlock_locked(&task->lock));
253
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);
257 stats_task->resmem = get_task_resmem(task->as);
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
267 * be already disabled while walking the tree.
268 *
269 * @param node AVL task tree node.
270 * @param arg Pointer to the iterator into the array of stats_task_t.
271 *
272 * @param Always true (continue the walk).
273 *
274 */
275static bool task_serialize_walker(avltree_node_t *node, void *arg)
276{
277 stats_task_t **iterator = (stats_task_t **) arg;
278 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
279
280 /* Interrupts are already disabled */
281 irq_spinlock_lock(&(task->lock), false);
282
283 /* Record the statistics and increment the iterator */
284 produce_stats_task(task, *iterator);
285 (*iterator)++;
286
287 irq_spinlock_unlock(&(task->lock), false);
288
289 return true;
290}
291
292/** Get task statistics
293 *
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.
297 * @param data Unused.
298 *
299 * @return Data containing several stats_task_t structures.
300 * If the return value is not NULL, it should be freed
301 * in the context of the sysinfo request.
302 */
303static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
304 bool dry_run, void *data)
305{
306 /* Messing with task structures, avoid deadlock */
307 irq_spinlock_lock(&tasks_lock, true);
308
309 /* First walk the task tree to count the tasks */
310 size_t count = 0;
311 avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
312
313 if (count == 0) {
314 /* No tasks found (strange) */
315 irq_spinlock_unlock(&tasks_lock, true);
316 *size = 0;
317 return NULL;
318 }
319
320 *size = sizeof(stats_task_t) * count;
321 if (dry_run) {
322 irq_spinlock_unlock(&tasks_lock, true);
323 return NULL;
324 }
325
326 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
327 if (stats_tasks == NULL) {
328 /* No free space for allocation */
329 irq_spinlock_unlock(&tasks_lock, true);
330 *size = 0;
331 return NULL;
332 }
333
334 /* Walk tha task tree again to gather the statistics */
335 stats_task_t *iterator = stats_tasks;
336 avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
337
338 irq_spinlock_unlock(&tasks_lock, true);
339
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{
353 ASSERT(interrupts_disabled());
354 ASSERT(irq_spinlock_locked(&thread->lock));
355
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;
368}
369
370/** Gather statistics of all threads
371 *
372 * AVL three tree walker for gathering thread statistics. Interrupts should
373 * be already disabled while walking the tree.
374 *
375 * @param node AVL thread tree node.
376 * @param arg Pointer to the iterator into the array of thread statistics.
377 *
378 * @param Always true (continue the walk).
379 *
380 */
381static bool thread_serialize_walker(avltree_node_t *node, void *arg)
382{
383 stats_thread_t **iterator = (stats_thread_t **) arg;
384 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
385
386 /* Interrupts are already disabled */
387 irq_spinlock_lock(&thread->lock, false);
388
389 /* Record the statistics and increment the iterator */
390 produce_stats_thread(thread, *iterator);
391 (*iterator)++;
392
393 irq_spinlock_unlock(&thread->lock, false);
394
395 return true;
396}
397
398/** Get thread statistics
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.
403 * @param data Unused.
404 *
405 * @return Data containing several stats_task_t structures.
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, void *data)
411{
412 /* Messing with threads structures, avoid deadlock */
413 irq_spinlock_lock(&threads_lock, true);
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) */
421 irq_spinlock_unlock(&threads_lock, true);
422 *size = 0;
423 return NULL;
424 }
425
426 *size = sizeof(stats_thread_t) * count;
427 if (dry_run) {
428 irq_spinlock_unlock(&threads_lock, true);
429 return NULL;
430 }
431
432 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
433 if (stats_threads == NULL) {
434 /* No free space for allocation */
435 irq_spinlock_unlock(&threads_lock, true);
436 *size = 0;
437 return NULL;
438 }
439
440 /* Walk tha thread tree again to gather the statistics */
441 stats_thread_t *iterator = stats_threads;
442 avltree_walk(&threads_tree, thread_serialize_walker, (void *) &iterator);
443
444 irq_spinlock_unlock(&threads_lock, true);
445
446 return ((void *) stats_threads);
447}
448
449/** Get a single task statistics
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 *
455 * @param name Task ID (string-encoded number).
456 * @param dry_run Do not get the data, just calculate the size.
457 * @param data Unused.
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 */
467static sysinfo_return_t get_stats_task(const char *name, bool dry_run,
468 void *data)
469{
470 /* Initially no return value */
471 sysinfo_return_t ret;
472 ret.tag = SYSINFO_VAL_UNDEFINED;
473
474 /* Parse the task ID */
475 task_id_t task_id;
476 if (str_uint64_t(name, NULL, 0, true, &task_id) != EOK)
477 return ret;
478
479 /* Messing with task structures, avoid deadlock */
480 irq_spinlock_lock(&tasks_lock, true);
481
482 task_t *task = task_find_by_id(task_id);
483 if (task == NULL) {
484 /* No task with this ID */
485 irq_spinlock_unlock(&tasks_lock, true);
486 return ret;
487 }
488
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
494 irq_spinlock_unlock(&tasks_lock, true);
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) {
500 irq_spinlock_unlock(&tasks_lock, true);
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);
508
509 /* Hand-over-hand locking */
510 irq_spinlock_exchange(&tasks_lock, &task->lock);
511
512 produce_stats_task(task, stats_task);
513
514 irq_spinlock_unlock(&task->lock, true);
515 }
516
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.
528 * @param data Unused.
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 */
538static sysinfo_return_t get_stats_thread(const char *name, bool dry_run,
539 void *data)
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;
547 if (str_uint64_t(name, NULL, 0, true, &thread_id) != EOK)
548 return ret;
549
550 /* Messing with threads structures, avoid deadlock */
551 irq_spinlock_lock(&threads_lock, true);
552
553 thread_t *thread = thread_find_by_id(thread_id);
554 if (thread == NULL) {
555 /* No thread with this ID */
556 irq_spinlock_unlock(&threads_lock, true);
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
565 irq_spinlock_unlock(&threads_lock, true);
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) {
571 irq_spinlock_unlock(&threads_lock, true);
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);
579
580 /* Hand-over-hand locking */
581 irq_spinlock_exchange(&threads_lock, &thread->lock);
582
583 produce_stats_thread(thread, stats_thread);
584
585 irq_spinlock_unlock(&thread->lock, true);
586 }
587
588 return ret;
589}
590
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.
596 * @param data Unused.
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,
603 bool dry_run, void *data)
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
618#if (IVT_ITEMS > 0)
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);
626 stats_exceptions[i].hot = exc_table[i].hot;
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);
632#endif
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.
645 * @param data Unused.
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 */
655static sysinfo_return_t get_stats_exception(const char *name, bool dry_run,
656 void *data)
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;
664 if (str_uint64_t(name, NULL, 0, true, &excn) != EOK)
665 return ret;
666
667#if (IVT_FIRST > 0)
668 if (excn < IVT_FIRST)
669 return ret;
670#endif
671
672#if (IVT_ITEMS + IVT_FIRST == 0)
673 return ret;
674#else
675 if (excn >= IVT_ITEMS + IVT_FIRST)
676 return ret;
677#endif
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);
703 stats_exception->hot = exc_table[excn].hot;
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
713/** Get physical memory statistics
714 *
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.
718 * @param data Unused.
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 */
724static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
725 bool dry_run, void *data)
726{
727 *size = sizeof(stats_physmem_t);
728 if (dry_run)
729 return NULL;
730
731 stats_physmem_t *stats_physmem =
732 (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
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
744/** Get system load
745 *
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.
749 * @param data Unused.
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 */
755static void *get_stats_load(struct sysinfo_item *item, size_t *size,
756 bool dry_run, void *data)
757{
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);
763 if (stats_load == NULL) {
764 *size = 0;
765 return NULL;
766 }
767
768 /* To always get consistent values acquire the mutex */
769 mutex_lock(&load_lock);
770
771 unsigned int i;
772 for (i = 0; i < LOAD_STEPS; i++)
773 stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
774
775 mutex_unlock(&load_lock);
776
777 return ((void *) stats_load);
778}
779
780/** Calculate load
781 *
782 */
783static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
784{
785 load *= exp;
786 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
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) {
803 atomic_count_t ready = atomic_get(&nrdy);
804
805 /* Mutually exclude with get_stats_load() */
806 mutex_lock(&load_lock);
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
812 mutex_unlock(&load_lock);
813
814 thread_sleep(LOAD_INTERVAL);
815 }
816}
817
818/** Register sysinfo statistical items
819 *
820 */
821void stats_init(void)
822{
823 mutex_initialize(&load_lock, MUTEX_PASSIVE);
824
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);
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);
835}
836
837/** @}
838 */
Note: See TracBrowser for help on using the repository browser.