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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b23e9cc 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
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 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
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 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
233 pages += area->resident;
234 mutex_unlock(&area->lock);
235 }
236 }
237
238 mutex_unlock(&as->lock);
239
240 return (pages << PAGE_WIDTH);
241}
242
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{
253 ASSERT(interrupts_disabled());
254 ASSERT(irq_spinlock_locked(&task->lock));
255
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);
259 stats_task->resmem = get_task_resmem(task->as);
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
269 * be already disabled while walking the tree.
270 *
271 * @param node AVL task tree node.
272 * @param arg Pointer to the iterator into the array of stats_task_t.
273 *
274 * @param Always true (continue the walk).
275 *
276 */
277static bool task_serialize_walker(avltree_node_t *node, void *arg)
278{
279 stats_task_t **iterator = (stats_task_t **) arg;
280 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
281
282 /* Interrupts are already disabled */
283 irq_spinlock_lock(&(task->lock), false);
284
285 /* Record the statistics and increment the iterator */
286 produce_stats_task(task, *iterator);
287 (*iterator)++;
288
289 irq_spinlock_unlock(&(task->lock), false);
290
291 return true;
292}
293
294/** Get task statistics
295 *
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.
299 *
300 * @return Data containing several stats_task_t structures.
301 * If the return value is not NULL, it should be freed
302 * in the context of the sysinfo request.
303 */
304static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
305 bool dry_run)
306{
307 /* Messing with task structures, avoid deadlock */
308 irq_spinlock_lock(&tasks_lock, true);
309
310 /* First walk the task tree to count the tasks */
311 size_t count = 0;
312 avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
313
314 if (count == 0) {
315 /* No tasks found (strange) */
316 irq_spinlock_unlock(&tasks_lock, true);
317 *size = 0;
318 return NULL;
319 }
320
321 *size = sizeof(stats_task_t) * count;
322 if (dry_run) {
323 irq_spinlock_unlock(&tasks_lock, true);
324 return NULL;
325 }
326
327 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
328 if (stats_tasks == NULL) {
329 /* No free space for allocation */
330 irq_spinlock_unlock(&tasks_lock, true);
331 *size = 0;
332 return NULL;
333 }
334
335 /* Walk tha task tree again to gather the statistics */
336 stats_task_t *iterator = stats_tasks;
337 avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
338
339 irq_spinlock_unlock(&tasks_lock, true);
340
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{
354 ASSERT(interrupts_disabled());
355 ASSERT(irq_spinlock_locked(&thread->lock));
356
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;
369}
370
371/** Gather statistics of all threads
372 *
373 * AVL three tree walker for gathering thread statistics. Interrupts should
374 * be already disabled while walking the tree.
375 *
376 * @param node AVL thread tree node.
377 * @param arg Pointer to the iterator into the array of thread statistics.
378 *
379 * @param Always true (continue the walk).
380 *
381 */
382static bool thread_serialize_walker(avltree_node_t *node, void *arg)
383{
384 stats_thread_t **iterator = (stats_thread_t **) arg;
385 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
386
387 /* Interrupts are already disabled */
388 irq_spinlock_lock(&thread->lock, false);
389
390 /* Record the statistics and increment the iterator */
391 produce_stats_thread(thread, *iterator);
392 (*iterator)++;
393
394 irq_spinlock_unlock(&thread->lock, false);
395
396 return true;
397}
398
399/** Get thread statistics
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 *
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)
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 *
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 */
466static sysinfo_return_t get_stats_task(const char *name, bool dry_run)
467{
468 /* Initially no return value */
469 sysinfo_return_t ret;
470 ret.tag = SYSINFO_VAL_UNDEFINED;
471
472 /* Parse the task ID */
473 task_id_t task_id;
474 if (str_uint64(name, NULL, 0, true, &task_id) != EOK)
475 return ret;
476
477 /* Messing with task structures, avoid deadlock */
478 irq_spinlock_lock(&tasks_lock, true);
479
480 task_t *task = task_find_by_id(task_id);
481 if (task == NULL) {
482 /* No task with this ID */
483 irq_spinlock_unlock(&tasks_lock, true);
484 return ret;
485 }
486
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
492 irq_spinlock_unlock(&tasks_lock, true);
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) {
498 irq_spinlock_unlock(&tasks_lock, true);
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);
506
507 /* Hand-over-hand locking */
508 irq_spinlock_exchange(&tasks_lock, &task->lock);
509
510 produce_stats_task(task, stats_task);
511
512 irq_spinlock_unlock(&task->lock, true);
513 }
514
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 */
547 irq_spinlock_lock(&threads_lock, true);
548
549 thread_t *thread = thread_find_by_id(thread_id);
550 if (thread == NULL) {
551 /* No thread with this ID */
552 irq_spinlock_unlock(&threads_lock, true);
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
561 irq_spinlock_unlock(&threads_lock, true);
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) {
567 irq_spinlock_unlock(&threads_lock, true);
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);
575
576 /* Hand-over-hand locking */
577 irq_spinlock_exchange(&threads_lock, &thread->lock);
578
579 produce_stats_thread(thread, stats_thread);
580
581 irq_spinlock_unlock(&thread->lock, true);
582 }
583
584 return ret;
585}
586
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
613#if (IVT_ITEMS > 0)
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);
621 stats_exceptions[i].hot = exc_table[i].hot;
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);
627#endif
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
660#if (IVT_FIRST > 0)
661 if (excn < IVT_FIRST)
662 return ret;
663#endif
664
665#if (IVT_ITEMS + IVT_FIRST == 0)
666 return ret;
667#else
668 if (excn >= IVT_ITEMS + IVT_FIRST)
669 return ret;
670#endif
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);
696 stats_exception->hot = exc_table[excn].hot;
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
706/** Get physical memory statistics
707 *
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.
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 */
716static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
717 bool dry_run)
718{
719 *size = sizeof(stats_physmem_t);
720 if (dry_run)
721 return NULL;
722
723 stats_physmem_t *stats_physmem =
724 (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
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
736/** Get system load
737 *
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.
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 */
746static void *get_stats_load(struct sysinfo_item *item, size_t *size,
747 bool dry_run)
748{
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);
754 if (stats_load == NULL) {
755 *size = 0;
756 return NULL;
757 }
758
759 /* To always get consistent values acquire the mutex */
760 mutex_lock(&load_lock);
761
762 unsigned int i;
763 for (i = 0; i < LOAD_STEPS; i++)
764 stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
765
766 mutex_unlock(&load_lock);
767
768 return ((void *) stats_load);
769}
770
771/** Calculate load
772 *
773 */
774static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
775{
776 load *= exp;
777 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
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) {
794 atomic_count_t ready = atomic_get(&nrdy);
795
796 /* Mutually exclude with get_stats_load() */
797 mutex_lock(&load_lock);
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
803 mutex_unlock(&load_lock);
804
805 thread_sleep(LOAD_INTERVAL);
806 }
807}
808
809/** Register sysinfo statistical items
810 *
811 */
812void stats_init(void)
813{
814 mutex_initialize(&load_lock, MUTEX_PASSIVE);
815
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);
821 sysinfo_set_item_fn_data("system.threads", NULL, get_stats_threads);
822 sysinfo_set_item_fn_data("system.exceptions", NULL, get_stats_exceptions);
823 sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task);
824 sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread);
825 sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception);
826}
827
828/** @}
829 */
Note: See TracBrowser for help on using the repository browser.