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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c028b22 was 55b77d9, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Separate list_t typedef from link_t (kernel part).

  • list_t represents lists
  • Use list_first(), list_last(), list_empty() where appropriate
  • Use list_foreach() where possible
  • Replace improper uses of list_prepend() with list_insert_after()
  • Replace improper uses of list_append() with list_insert_before()
  • Property mode set to 100644
File size: 22.1 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 <sysinfo/abi.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 *
86 * @return System uptime (in secords).
87 *
88 */
89static sysarg_t get_stats_uptime(struct sysinfo_item *item)
90{
91 /* This doesn't have to be very accurate */
92 return uptime->seconds1;
93}
94
95/** Get statistics of all CPUs
96 *
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.
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 */
105static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
106 bool dry_run)
107{
108 *size = sizeof(stats_cpu_t) * config.cpu_count;
109 if (dry_run)
110 return NULL;
111
112 /* Assumption: config.cpu_count is constant */
113 stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size, FRAME_ATOMIC);
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++) {
121 irq_spinlock_lock(&cpus[i].lock, true);
122
123 stats_cpus[i].id = cpus[i].id;
124 stats_cpus[i].active = cpus[i].active;
125 stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
126 stats_cpus[i].busy_cycles = cpus[i].busy_cycles;
127 stats_cpus[i].idle_cycles = cpus[i].idle_cycles;
128
129 irq_spinlock_unlock(&cpus[i].lock, true);
130 }
131
132 return ((void *) stats_cpus);
133}
134
135/** Count number of nodes in an AVL tree
136 *
137 * AVL tree walker for counting nodes.
138 *
139 * @param node AVL tree node (unused).
140 * @param arg Pointer to the counter (size_t).
141 *
142 * @param Always true (continue the walk).
143 *
144 */
145static bool avl_count_walker(avltree_node_t *node, void *arg)
146{
147 size_t *count = (size_t *) arg;
148 (*count)++;
149
150 return true;
151}
152
153/** Get the size of a virtual address space
154 *
155 * @param as Address space.
156 *
157 * @return Size of the mapped virtual address space (bytes).
158 *
159 */
160static size_t get_task_virtmem(as_t *as)
161{
162 /*
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.
167 */
168
169 if (SYNCH_FAILED(mutex_trylock(&as->lock)))
170 return 0;
171
172 size_t pages = 0;
173
174 /* Walk the B+ tree and count pages */
175 list_foreach(as->as_area_btree.leaf_list, cur) {
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
183 if (SYNCH_FAILED(mutex_trylock(&area->lock)))
184 continue;
185
186 pages += area->pages;
187 mutex_unlock(&area->lock);
188 }
189 }
190
191 mutex_unlock(&as->lock);
192
193 return (pages << PAGE_WIDTH);
194}
195
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 /*
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.
210 */
211
212 if (SYNCH_FAILED(mutex_trylock(&as->lock)))
213 return 0;
214
215 size_t pages = 0;
216
217 /* Walk the B+ tree and count pages */
218 list_foreach(as->as_area_btree.leaf_list, cur) {
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
229 pages += area->resident;
230 mutex_unlock(&area->lock);
231 }
232 }
233
234 mutex_unlock(&as->lock);
235
236 return (pages << PAGE_WIDTH);
237}
238
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{
249 ASSERT(interrupts_disabled());
250 ASSERT(irq_spinlock_locked(&task->lock));
251
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);
255 stats_task->resmem = get_task_resmem(task->as);
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
265 * be already disabled while walking the tree.
266 *
267 * @param node AVL task tree node.
268 * @param arg Pointer to the iterator into the array of stats_task_t.
269 *
270 * @param Always true (continue the walk).
271 *
272 */
273static bool task_serialize_walker(avltree_node_t *node, void *arg)
274{
275 stats_task_t **iterator = (stats_task_t **) arg;
276 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
277
278 /* Interrupts are already disabled */
279 irq_spinlock_lock(&(task->lock), false);
280
281 /* Record the statistics and increment the iterator */
282 produce_stats_task(task, *iterator);
283 (*iterator)++;
284
285 irq_spinlock_unlock(&(task->lock), false);
286
287 return true;
288}
289
290/** Get task statistics
291 *
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.
295 *
296 * @return Data containing several stats_task_t structures.
297 * If the return value is not NULL, it should be freed
298 * in the context of the sysinfo request.
299 */
300static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
301 bool dry_run)
302{
303 /* Messing with task structures, avoid deadlock */
304 irq_spinlock_lock(&tasks_lock, true);
305
306 /* First walk the task tree to count the tasks */
307 size_t count = 0;
308 avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
309
310 if (count == 0) {
311 /* No tasks found (strange) */
312 irq_spinlock_unlock(&tasks_lock, true);
313 *size = 0;
314 return NULL;
315 }
316
317 *size = sizeof(stats_task_t) * count;
318 if (dry_run) {
319 irq_spinlock_unlock(&tasks_lock, true);
320 return NULL;
321 }
322
323 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
324 if (stats_tasks == NULL) {
325 /* No free space for allocation */
326 irq_spinlock_unlock(&tasks_lock, true);
327 *size = 0;
328 return NULL;
329 }
330
331 /* Walk tha task tree again to gather the statistics */
332 stats_task_t *iterator = stats_tasks;
333 avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
334
335 irq_spinlock_unlock(&tasks_lock, true);
336
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{
350 ASSERT(interrupts_disabled());
351 ASSERT(irq_spinlock_locked(&thread->lock));
352
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;
365}
366
367/** Gather statistics of all threads
368 *
369 * AVL three tree walker for gathering thread statistics. Interrupts should
370 * be already disabled while walking the tree.
371 *
372 * @param node AVL thread tree node.
373 * @param arg Pointer to the iterator into the array of thread statistics.
374 *
375 * @param Always true (continue the walk).
376 *
377 */
378static bool thread_serialize_walker(avltree_node_t *node, void *arg)
379{
380 stats_thread_t **iterator = (stats_thread_t **) arg;
381 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
382
383 /* Interrupts are already disabled */
384 irq_spinlock_lock(&thread->lock, false);
385
386 /* Record the statistics and increment the iterator */
387 produce_stats_thread(thread, *iterator);
388 (*iterator)++;
389
390 irq_spinlock_unlock(&thread->lock, false);
391
392 return true;
393}
394
395/** Get thread statistics
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 *
401 * @return Data containing several stats_task_t structures.
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 */
409 irq_spinlock_lock(&threads_lock, true);
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) */
417 irq_spinlock_unlock(&threads_lock, true);
418 *size = 0;
419 return NULL;
420 }
421
422 *size = sizeof(stats_thread_t) * count;
423 if (dry_run) {
424 irq_spinlock_unlock(&threads_lock, true);
425 return NULL;
426 }
427
428 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
429 if (stats_threads == NULL) {
430 /* No free space for allocation */
431 irq_spinlock_unlock(&threads_lock, true);
432 *size = 0;
433 return NULL;
434 }
435
436 /* Walk tha thread tree again to gather the statistics */
437 stats_thread_t *iterator = stats_threads;
438 avltree_walk(&threads_tree, thread_serialize_walker, (void *) &iterator);
439
440 irq_spinlock_unlock(&threads_lock, true);
441
442 return ((void *) stats_threads);
443}
444
445/** Get a single task statistics
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 *
451 * @param name Task ID (string-encoded number).
452 * @param dry_run Do not get the data, just calculate the size.
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 */
462static sysinfo_return_t get_stats_task(const char *name, bool dry_run)
463{
464 /* Initially no return value */
465 sysinfo_return_t ret;
466 ret.tag = SYSINFO_VAL_UNDEFINED;
467
468 /* Parse the task ID */
469 task_id_t task_id;
470 if (str_uint64(name, NULL, 0, true, &task_id) != EOK)
471 return ret;
472
473 /* Messing with task structures, avoid deadlock */
474 irq_spinlock_lock(&tasks_lock, true);
475
476 task_t *task = task_find_by_id(task_id);
477 if (task == NULL) {
478 /* No task with this ID */
479 irq_spinlock_unlock(&tasks_lock, true);
480 return ret;
481 }
482
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
488 irq_spinlock_unlock(&tasks_lock, true);
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) {
494 irq_spinlock_unlock(&tasks_lock, true);
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);
502
503 /* Hand-over-hand locking */
504 irq_spinlock_exchange(&tasks_lock, &task->lock);
505
506 produce_stats_task(task, stats_task);
507
508 irq_spinlock_unlock(&task->lock, true);
509 }
510
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 */
543 irq_spinlock_lock(&threads_lock, true);
544
545 thread_t *thread = thread_find_by_id(thread_id);
546 if (thread == NULL) {
547 /* No thread with this ID */
548 irq_spinlock_unlock(&threads_lock, true);
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
557 irq_spinlock_unlock(&threads_lock, true);
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) {
563 irq_spinlock_unlock(&threads_lock, true);
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);
571
572 /* Hand-over-hand locking */
573 irq_spinlock_exchange(&threads_lock, &thread->lock);
574
575 produce_stats_thread(thread, stats_thread);
576
577 irq_spinlock_unlock(&thread->lock, true);
578 }
579
580 return ret;
581}
582
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
609#if (IVT_ITEMS > 0)
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);
617 stats_exceptions[i].hot = exc_table[i].hot;
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);
623#endif
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
656#if (IVT_FIRST > 0)
657 if (excn < IVT_FIRST)
658 return ret;
659#endif
660
661#if (IVT_ITEMS + IVT_FIRST == 0)
662 return ret;
663#else
664 if (excn >= IVT_ITEMS + IVT_FIRST)
665 return ret;
666#endif
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);
692 stats_exception->hot = exc_table[excn].hot;
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
702/** Get physical memory statistics
703 *
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.
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 */
712static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
713 bool dry_run)
714{
715 *size = sizeof(stats_physmem_t);
716 if (dry_run)
717 return NULL;
718
719 stats_physmem_t *stats_physmem =
720 (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
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
732/** Get system load
733 *
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.
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 */
742static void *get_stats_load(struct sysinfo_item *item, size_t *size,
743 bool dry_run)
744{
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);
750 if (stats_load == NULL) {
751 *size = 0;
752 return NULL;
753 }
754
755 /* To always get consistent values acquire the mutex */
756 mutex_lock(&load_lock);
757
758 unsigned int i;
759 for (i = 0; i < LOAD_STEPS; i++)
760 stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
761
762 mutex_unlock(&load_lock);
763
764 return ((void *) stats_load);
765}
766
767/** Calculate load
768 *
769 */
770static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
771{
772 load *= exp;
773 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
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) {
790 atomic_count_t ready = atomic_get(&nrdy);
791
792 /* Mutually exclude with get_stats_load() */
793 mutex_lock(&load_lock);
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
799 mutex_unlock(&load_lock);
800
801 thread_sleep(LOAD_INTERVAL);
802 }
803}
804
805/** Register sysinfo statistical items
806 *
807 */
808void stats_init(void)
809{
810 mutex_initialize(&load_lock, MUTEX_PASSIVE);
811
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);
817 sysinfo_set_item_fn_data("system.threads", NULL, get_stats_threads);
818 sysinfo_set_item_fn_data("system.exceptions", NULL, get_stats_exceptions);
819 sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task);
820 sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread);
821 sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception);
822}
823
824/** @}
825 */
Note: See TracBrowser for help on using the repository browser.