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

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

remove system.uptime sysinfo entry since it is redundant
cleanup the time handling routines

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