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

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

statistics: provide resident (actually used) memory of tasks

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