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

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

add basic support for IPC statistics

Dumping of phone connections is supported right now.

  • 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 * Copyright (c) 2018 Jiri Svoboda
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup kernel_generic
32 * @{
33 */
34/** @file
35 */
36
37#include <assert.h>
38#include <typedefs.h>
39#include <abi/sysinfo.h>
40#include <sysinfo/stats.h>
41#include <sysinfo/sysinfo.h>
42#include <synch/spinlock.h>
43#include <synch/mutex.h>
44#include <time/clock.h>
45#include <mm/frame.h>
46#include <proc/task.h>
47#include <proc/thread.h>
48#include <interrupt.h>
49#include <stdbool.h>
50#include <str.h>
51#include <errno.h>
52#include <cpu.h>
53#include <arch.h>
54#include <stdlib.h>
55
56/** Bits of fixed-point precision for load */
57#define LOAD_FIXED_SHIFT 11
58
59/** Uspace load fixed-point precision */
60#define LOAD_USPACE_SHIFT 6
61
62/** Kernel load shift */
63#define LOAD_KERNEL_SHIFT (LOAD_FIXED_SHIFT - LOAD_USPACE_SHIFT)
64
65/** 1.0 as fixed-point for load */
66#define LOAD_FIXED_1 (1 << LOAD_FIXED_SHIFT)
67
68/** Compute load in 5 second intervals */
69#define LOAD_INTERVAL 5
70
71/** IPC connections statistics state */
72typedef struct {
73 bool counting;
74 size_t count;
75 size_t i;
76 stats_ipcc_t *data;
77} ipccs_state_t;
78
79/** Fixed-point representation of
80 *
81 * 1 / exp(5 sec / 1 min)
82 * 1 / exp(5 sec / 5 min)
83 * 1 / exp(5 sec / 15 min)
84 *
85 */
86static load_t load_exp[LOAD_STEPS] = { 1884, 2014, 2037 };
87
88/** Running average of the number of ready threads */
89static load_t avenrdy[LOAD_STEPS] = { 0, 0, 0 };
90
91/** Load calculation lock */
92static mutex_t load_lock;
93
94/** Get statistics of all CPUs
95 *
96 * @param item Sysinfo item (unused).
97 * @param size Size of the returned data.
98 * @param dry_run Do not get the data, just calculate the size.
99 * @param data Unused.
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, void *data)
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);
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/** Get the size of a virtual address space
136 *
137 * @param as Address space.
138 *
139 * @return Size of the mapped virtual address space (bytes).
140 *
141 */
142static size_t get_task_virtmem(as_t *as)
143{
144 /*
145 * We are holding spinlocks here and therefore are not allowed to
146 * block. Only attempt to lock the address space and address space
147 * area mutexes conditionally. If it is not possible to lock either
148 * object, return inexact statistics by skipping the respective object.
149 */
150
151 if (mutex_trylock(&as->lock) != EOK)
152 return 0;
153
154 size_t pages = 0;
155
156 /* Walk areas in the address space and count pages */
157 as_area_t *area = as_area_first(as);
158 while (area != NULL) {
159 if (mutex_trylock(&area->lock) != EOK)
160 continue;
161
162 pages += area->pages;
163 mutex_unlock(&area->lock);
164 area = as_area_next(area);
165 }
166
167 mutex_unlock(&as->lock);
168
169 return (pages << PAGE_WIDTH);
170}
171
172/** Get the resident (used) size of a virtual address space
173 *
174 * @param as Address space.
175 *
176 * @return Size of the resident (used) virtual address space (bytes).
177 *
178 */
179static size_t get_task_resmem(as_t *as)
180{
181 /*
182 * We are holding spinlocks here and therefore are not allowed to
183 * block. Only attempt to lock the address space and address space
184 * area mutexes conditionally. If it is not possible to lock either
185 * object, return inexact statistics by skipping the respective object.
186 */
187
188 if (mutex_trylock(&as->lock) != EOK)
189 return 0;
190
191 size_t pages = 0;
192
193 /* Walk areas in the address space and count pages */
194 as_area_t *area = as_area_first(as);
195 while (area != NULL) {
196 if (mutex_trylock(&area->lock) != EOK)
197 continue;
198
199 pages += area->used_space.pages;
200 mutex_unlock(&area->lock);
201 area = as_area_next(area);
202 }
203
204 mutex_unlock(&as->lock);
205
206 return (pages << PAGE_WIDTH);
207}
208
209/** Produce task statistics
210 *
211 * Summarize task information into task statistics.
212 *
213 * @param task Task.
214 * @param stats_task Task statistics.
215 *
216 */
217static void produce_stats_task(task_t *task, stats_task_t *stats_task)
218{
219 assert(interrupts_disabled());
220 assert(irq_spinlock_locked(&task->lock));
221
222 stats_task->task_id = task->taskid;
223 str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
224 stats_task->virtmem = get_task_virtmem(task->as);
225 stats_task->resmem = get_task_resmem(task->as);
226 stats_task->threads = atomic_load(&task->refcount);
227 task_get_accounting(task, &(stats_task->ucycles),
228 &(stats_task->kcycles));
229 stats_task->ipc_info = task->ipc_info;
230}
231
232/** Get task statistics
233 *
234 * @param item Sysinfo item (unused).
235 * @param size Size of the returned data.
236 * @param dry_run Do not get the data, just calculate the size.
237 * @param data Unused.
238 *
239 * @return Data containing several stats_task_t structures.
240 * If the return value is not NULL, it should be freed
241 * in the context of the sysinfo request.
242 */
243static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
244 bool dry_run, void *data)
245{
246 /* Messing with task structures, avoid deadlock */
247 irq_spinlock_lock(&tasks_lock, true);
248
249 /* Count the tasks */
250 size_t count = task_count();
251
252 if (count == 0) {
253 /* No tasks found (strange) */
254 irq_spinlock_unlock(&tasks_lock, true);
255 *size = 0;
256 return NULL;
257 }
258
259 *size = sizeof(stats_task_t) * count;
260 if (dry_run) {
261 irq_spinlock_unlock(&tasks_lock, true);
262 return NULL;
263 }
264
265 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size);
266 if (stats_tasks == NULL) {
267 /* No free space for allocation */
268 irq_spinlock_unlock(&tasks_lock, true);
269 *size = 0;
270 return NULL;
271 }
272
273 /* Gather the statistics for each task */
274 size_t i = 0;
275 task_t *task = task_first();
276 while (task != NULL) {
277 /* Interrupts are already disabled */
278 irq_spinlock_lock(&(task->lock), false);
279
280 /* Record the statistics and increment the index */
281 produce_stats_task(task, &stats_tasks[i]);
282 i++;
283
284 irq_spinlock_unlock(&(task->lock), false);
285 task = task_next(task);
286 }
287
288 irq_spinlock_unlock(&tasks_lock, true);
289
290 return ((void *) stats_tasks);
291}
292
293/** Produce thread statistics
294 *
295 * Summarize thread information into thread statistics.
296 *
297 * @param thread Thread.
298 * @param stats_thread Thread statistics.
299 *
300 */
301static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
302{
303 assert(interrupts_disabled());
304 assert(irq_spinlock_locked(&thread->lock));
305
306 stats_thread->thread_id = thread->tid;
307 stats_thread->task_id = thread->task->taskid;
308 stats_thread->state = thread->state;
309 stats_thread->priority = thread->priority;
310 stats_thread->ucycles = thread->ucycles;
311 stats_thread->kcycles = thread->kcycles;
312
313 if (thread->cpu != NULL) {
314 stats_thread->on_cpu = true;
315 stats_thread->cpu = thread->cpu->id;
316 } else
317 stats_thread->on_cpu = false;
318}
319
320/** Get thread statistics
321 *
322 * @param item Sysinfo item (unused).
323 * @param size Size of the returned data.
324 * @param dry_run Do not get the data, just calculate the size.
325 * @param data Unused.
326 *
327 * @return Data containing several stats_task_t structures.
328 * If the return value is not NULL, it should be freed
329 * in the context of the sysinfo request.
330 */
331static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
332 bool dry_run, void *data)
333{
334 /* Messing with threads structures, avoid deadlock */
335 irq_spinlock_lock(&threads_lock, true);
336
337 /* Count the threads */
338 size_t count = thread_count();
339
340 if (count == 0) {
341 /* No threads found (strange) */
342 irq_spinlock_unlock(&threads_lock, true);
343 *size = 0;
344 return NULL;
345 }
346
347 *size = sizeof(stats_thread_t) * count;
348 if (dry_run) {
349 irq_spinlock_unlock(&threads_lock, true);
350 return NULL;
351 }
352
353 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size);
354 if (stats_threads == NULL) {
355 /* No free space for allocation */
356 irq_spinlock_unlock(&threads_lock, true);
357 *size = 0;
358 return NULL;
359 }
360
361 /* Walk tha thread tree again to gather the statistics */
362 size_t i = 0;
363
364 thread_t *thread = thread_first();
365 while (thread != NULL) {
366 /* Interrupts are already disabled */
367 irq_spinlock_lock(&thread->lock, false);
368
369 /* Record the statistics and increment the index */
370 produce_stats_thread(thread, &stats_threads[i]);
371 i++;
372
373 irq_spinlock_unlock(&thread->lock, false);
374
375 thread = thread_next(thread);
376 }
377
378 irq_spinlock_unlock(&threads_lock, true);
379
380 return ((void *) stats_threads);
381}
382
383/** Produce IPC connection statistics
384 *
385 * Summarize IPC connection information into IPC connection statistics.
386 *
387 * @param cap Phone capability.
388 * @param arg State variable.
389 *
390 */
391static bool produce_stats_ipcc_cb(cap_t *cap, void *arg)
392{
393 phone_t *phone = cap->kobject->phone;
394 ipccs_state_t *state = (ipccs_state_t *) arg;
395
396 if (state->counting) {
397 /*
398 * Simply update the number of entries
399 * in case we are in the counting mode.
400 */
401
402 state->count++;
403 return true;
404 }
405
406 /* We are in the gathering mode */
407
408 if ((state->data == NULL) || (state->i >= state->count)) {
409 /*
410 * Do nothing if we have no buffer
411 * to store the data to (meaning we are
412 * in a dry run) or the buffer is already
413 * full.
414 */
415
416 return true;
417 }
418
419 mutex_lock(&phone->lock);
420
421 if (phone->state == IPC_PHONE_CONNECTED) {
422 state->data[state->i].caller = phone->caller->taskid;
423 state->data[state->i].callee = phone->callee->task->taskid;
424 state->i++;
425 }
426
427 mutex_unlock(&phone->lock);
428
429 return true;
430}
431
432/** Get IPC connections statistics
433 *
434 * @param item Sysinfo item (unused).
435 * @param size Size of the returned data.
436 * @param dry_run Do not get the data, just calculate the size.
437 * @param data Unused.
438 *
439 * @return Data containing several stats_ipccs_t structures.
440 * If the return value is not NULL, it should be freed
441 * in the context of the sysinfo request.
442 *
443 */
444static void *get_stats_ipccs(struct sysinfo_item *item, size_t *size,
445 bool dry_run, void *data)
446{
447 /* Messing with tasks structures, avoid deadlock */
448 irq_spinlock_lock(&tasks_lock, true);
449
450 ipccs_state_t state = {
451 .counting = true,
452 .count = 0,
453 .i = 0,
454 .data = NULL
455 };
456
457 /* Compute the number of IPC connections */
458 task_t *task = task_first();
459 while (task != NULL) {
460 task_hold(task);
461 irq_spinlock_unlock(&tasks_lock, true);
462
463 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
464 produce_stats_ipcc_cb, &state);
465
466 irq_spinlock_lock(&tasks_lock, true);
467
468 task = task_next(task);
469 }
470
471 state.counting = false;
472 *size = sizeof(stats_ipcc_t) * state.count;
473
474 if (!dry_run)
475 state.data = (stats_ipcc_t *) malloc(*size);
476
477 /* Gather the statistics for each task */
478 task = task_first();
479 while (task != NULL) {
480 /* We already hold a reference to the task */
481 irq_spinlock_unlock(&tasks_lock, true);
482
483 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
484 produce_stats_ipcc_cb, &state);
485
486 irq_spinlock_lock(&tasks_lock, true);
487 task_release(task);
488
489 task = task_next(task);
490 }
491
492 irq_spinlock_unlock(&tasks_lock, true);
493
494 return ((void *) state.data);
495}
496
497/** Get a single task statistics
498 *
499 * Get statistics of a given task. The task ID is passed
500 * as a string (current limitation of the sysinfo interface,
501 * but it is still reasonable for the given purpose).
502 *
503 * @param name Task ID (string-encoded number).
504 * @param dry_run Do not get the data, just calculate the size.
505 * @param data Unused.
506 *
507 * @return Sysinfo return holder. The type of the returned
508 * data is either SYSINFO_VAL_UNDEFINED (unknown
509 * task ID or memory allocation error) or
510 * SYSINFO_VAL_FUNCTION_DATA (in that case the
511 * generated data should be freed within the
512 * sysinfo request context).
513 *
514 */
515static sysinfo_return_t get_stats_task(const char *name, bool dry_run,
516 void *data)
517{
518 /* Initially no return value */
519 sysinfo_return_t ret;
520 ret.tag = SYSINFO_VAL_UNDEFINED;
521
522 /* Parse the task ID */
523 task_id_t task_id;
524 if (str_uint64_t(name, NULL, 0, true, &task_id) != EOK)
525 return ret;
526
527 /* Messing with task structures, avoid deadlock */
528 irq_spinlock_lock(&tasks_lock, true);
529
530 task_t *task = task_find_by_id(task_id);
531 if (task == NULL) {
532 /* No task with this ID */
533 irq_spinlock_unlock(&tasks_lock, true);
534 return ret;
535 }
536
537 if (dry_run) {
538 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
539 ret.data.data = NULL;
540 ret.data.size = sizeof(stats_task_t);
541
542 irq_spinlock_unlock(&tasks_lock, true);
543 } else {
544 /* Allocate stats_task_t structure */
545 stats_task_t *stats_task =
546 (stats_task_t *) malloc(sizeof(stats_task_t));
547 if (stats_task == NULL) {
548 irq_spinlock_unlock(&tasks_lock, true);
549 return ret;
550 }
551
552 /* Correct return value */
553 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
554 ret.data.data = (void *) stats_task;
555 ret.data.size = sizeof(stats_task_t);
556
557 /* Hand-over-hand locking */
558 irq_spinlock_exchange(&tasks_lock, &task->lock);
559
560 produce_stats_task(task, stats_task);
561
562 irq_spinlock_unlock(&task->lock, true);
563 }
564
565 return ret;
566}
567
568/** Get thread statistics
569 *
570 * Get statistics of a given thread. The thread ID is passed
571 * as a string (current limitation of the sysinfo interface,
572 * but it is still reasonable for the given purpose).
573 *
574 * @param name Thread ID (string-encoded number).
575 * @param dry_run Do not get the data, just calculate the size.
576 * @param data Unused.
577 *
578 * @return Sysinfo return holder. The type of the returned
579 * data is either SYSINFO_VAL_UNDEFINED (unknown
580 * thread ID or memory allocation error) or
581 * SYSINFO_VAL_FUNCTION_DATA (in that case the
582 * generated data should be freed within the
583 * sysinfo request context).
584 *
585 */
586static sysinfo_return_t get_stats_thread(const char *name, bool dry_run,
587 void *data)
588{
589 /* Initially no return value */
590 sysinfo_return_t ret;
591 ret.tag = SYSINFO_VAL_UNDEFINED;
592
593 /* Parse the thread ID */
594 thread_id_t thread_id;
595 if (str_uint64_t(name, NULL, 0, true, &thread_id) != EOK)
596 return ret;
597
598 /* Messing with threads structures, avoid deadlock */
599 irq_spinlock_lock(&threads_lock, true);
600
601 thread_t *thread = thread_find_by_id(thread_id);
602 if (thread == NULL) {
603 /* No thread with this ID */
604 irq_spinlock_unlock(&threads_lock, true);
605 return ret;
606 }
607
608 if (dry_run) {
609 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
610 ret.data.data = NULL;
611 ret.data.size = sizeof(stats_thread_t);
612
613 irq_spinlock_unlock(&threads_lock, true);
614 } else {
615 /* Allocate stats_thread_t structure */
616 stats_thread_t *stats_thread =
617 (stats_thread_t *) malloc(sizeof(stats_thread_t));
618 if (stats_thread == NULL) {
619 irq_spinlock_unlock(&threads_lock, true);
620 return ret;
621 }
622
623 /* Correct return value */
624 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
625 ret.data.data = (void *) stats_thread;
626 ret.data.size = sizeof(stats_thread_t);
627
628 /* Hand-over-hand locking */
629 irq_spinlock_exchange(&threads_lock, &thread->lock);
630
631 produce_stats_thread(thread, stats_thread);
632
633 irq_spinlock_unlock(&thread->lock, true);
634 }
635
636 return ret;
637}
638
639/** Get exceptions statistics
640 *
641 * @param item Sysinfo item (unused).
642 * @param size Size of the returned data.
643 * @param dry_run Do not get the data, just calculate the size.
644 * @param data Unused.
645 *
646 * @return Data containing several stats_exc_t structures.
647 * If the return value is not NULL, it should be freed
648 * in the context of the sysinfo request.
649 */
650static void *get_stats_exceptions(struct sysinfo_item *item, size_t *size,
651 bool dry_run, void *data)
652{
653 *size = sizeof(stats_exc_t) * IVT_ITEMS;
654
655 if ((dry_run) || (IVT_ITEMS == 0))
656 return NULL;
657
658 stats_exc_t *stats_exceptions =
659 (stats_exc_t *) malloc(*size);
660 if (stats_exceptions == NULL) {
661 /* No free space for allocation */
662 *size = 0;
663 return NULL;
664 }
665
666#if (IVT_ITEMS > 0)
667 /* Messing with exception table, avoid deadlock */
668 irq_spinlock_lock(&exctbl_lock, true);
669
670 unsigned int i;
671 for (i = 0; i < IVT_ITEMS; i++) {
672 stats_exceptions[i].id = i + IVT_FIRST;
673 str_cpy(stats_exceptions[i].desc, EXC_NAME_BUFLEN, exc_table[i].name);
674 stats_exceptions[i].hot = exc_table[i].hot;
675 stats_exceptions[i].cycles = exc_table[i].cycles;
676 stats_exceptions[i].count = exc_table[i].count;
677 }
678
679 irq_spinlock_unlock(&exctbl_lock, true);
680#endif
681
682 return ((void *) stats_exceptions);
683}
684
685/** Get exception statistics
686 *
687 * Get statistics of a given exception. The exception number
688 * is passed as a string (current limitation of the sysinfo
689 * interface, but it is still reasonable for the given purpose).
690 *
691 * @param name Exception number (string-encoded number).
692 * @param dry_run Do not get the data, just calculate the size.
693 * @param data Unused.
694 *
695 * @return Sysinfo return holder. The type of the returned
696 * data is either SYSINFO_VAL_UNDEFINED (unknown
697 * exception number or memory allocation error) or
698 * SYSINFO_VAL_FUNCTION_DATA (in that case the
699 * generated data should be freed within the
700 * sysinfo request context).
701 *
702 */
703static sysinfo_return_t get_stats_exception(const char *name, bool dry_run,
704 void *data)
705{
706 /* Initially no return value */
707 sysinfo_return_t ret;
708 ret.tag = SYSINFO_VAL_UNDEFINED;
709
710 /* Parse the exception number */
711 uint64_t excn;
712 if (str_uint64_t(name, NULL, 0, true, &excn) != EOK)
713 return ret;
714
715#if (IVT_FIRST > 0)
716 if (excn < IVT_FIRST)
717 return ret;
718#endif
719
720#if (IVT_ITEMS + IVT_FIRST == 0)
721 return ret;
722#else
723 if (excn >= IVT_ITEMS + IVT_FIRST)
724 return ret;
725#endif
726
727 if (dry_run) {
728 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
729 ret.data.data = NULL;
730 ret.data.size = sizeof(stats_thread_t);
731 } else {
732 /* Update excn index for accessing exc_table */
733 excn -= IVT_FIRST;
734
735 /* Allocate stats_exc_t structure */
736 stats_exc_t *stats_exception =
737 (stats_exc_t *) malloc(sizeof(stats_exc_t));
738 if (stats_exception == NULL)
739 return ret;
740
741 /* Messing with exception table, avoid deadlock */
742 irq_spinlock_lock(&exctbl_lock, true);
743
744 /* Correct return value */
745 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
746 ret.data.data = (void *) stats_exception;
747 ret.data.size = sizeof(stats_exc_t);
748
749 stats_exception->id = excn;
750 str_cpy(stats_exception->desc, EXC_NAME_BUFLEN, exc_table[excn].name);
751 stats_exception->hot = exc_table[excn].hot;
752 stats_exception->cycles = exc_table[excn].cycles;
753 stats_exception->count = exc_table[excn].count;
754
755 irq_spinlock_unlock(&exctbl_lock, true);
756 }
757
758 return ret;
759}
760
761/** Get physical memory statistics
762 *
763 * @param item Sysinfo item (unused).
764 * @param size Size of the returned data.
765 * @param dry_run Do not get the data, just calculate the size.
766 * @param data Unused.
767 *
768 * @return Data containing stats_physmem_t.
769 * If the return value is not NULL, it should be freed
770 * in the context of the sysinfo request.
771 */
772static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
773 bool dry_run, void *data)
774{
775 *size = sizeof(stats_physmem_t);
776 if (dry_run)
777 return NULL;
778
779 stats_physmem_t *stats_physmem =
780 (stats_physmem_t *) malloc(*size);
781 if (stats_physmem == NULL) {
782 *size = 0;
783 return NULL;
784 }
785
786 zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
787 &(stats_physmem->used), &(stats_physmem->free));
788
789 return ((void *) stats_physmem);
790}
791
792/** Get system load
793 *
794 * @param item Sysinfo item (unused).
795 * @param size Size of the returned data.
796 * @param dry_run Do not get the data, just calculate the size.
797 * @param data Unused.
798 *
799 * @return Data several load_t values.
800 * If the return value is not NULL, it should be freed
801 * in the context of the sysinfo request.
802 */
803static void *get_stats_load(struct sysinfo_item *item, size_t *size,
804 bool dry_run, void *data)
805{
806 *size = sizeof(load_t) * LOAD_STEPS;
807 if (dry_run)
808 return NULL;
809
810 load_t *stats_load = (load_t *) malloc(*size);
811 if (stats_load == NULL) {
812 *size = 0;
813 return NULL;
814 }
815
816 /* To always get consistent values acquire the mutex */
817 mutex_lock(&load_lock);
818
819 unsigned int i;
820 for (i = 0; i < LOAD_STEPS; i++)
821 stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
822
823 mutex_unlock(&load_lock);
824
825 return ((void *) stats_load);
826}
827
828/** Calculate load
829 *
830 */
831static inline load_t load_calc(load_t load, load_t exp, size_t ready)
832{
833 load *= exp;
834 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
835
836 return (load >> LOAD_FIXED_SHIFT);
837}
838
839/** Load computation thread.
840 *
841 * Compute system load every few seconds.
842 *
843 * @param arg Unused.
844 *
845 */
846void kload(void *arg)
847{
848 thread_detach(THREAD);
849
850 while (true) {
851 size_t ready = atomic_load(&nrdy);
852
853 /* Mutually exclude with get_stats_load() */
854 mutex_lock(&load_lock);
855
856 unsigned int i;
857 for (i = 0; i < LOAD_STEPS; i++)
858 avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
859
860 mutex_unlock(&load_lock);
861
862 thread_sleep(LOAD_INTERVAL);
863 }
864}
865
866/** Register sysinfo statistical items
867 *
868 */
869void stats_init(void)
870{
871 mutex_initialize(&load_lock, MUTEX_PASSIVE);
872
873 sysinfo_set_item_gen_data("system.cpus", NULL, get_stats_cpus, NULL);
874 sysinfo_set_item_gen_data("system.physmem", NULL, get_stats_physmem, NULL);
875 sysinfo_set_item_gen_data("system.load", NULL, get_stats_load, NULL);
876 sysinfo_set_item_gen_data("system.tasks", NULL, get_stats_tasks, NULL);
877 sysinfo_set_item_gen_data("system.threads", NULL, get_stats_threads, NULL);
878 sysinfo_set_item_gen_data("system.ipccs", NULL, get_stats_ipccs, NULL);
879 sysinfo_set_item_gen_data("system.exceptions", NULL, get_stats_exceptions, NULL);
880 sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task, NULL);
881 sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread, NULL);
882 sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception, NULL);
883}
884
885/** @}
886 */
Note: See TracBrowser for help on using the repository browser.