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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 169815e was 169815e, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Split cpu_t::lock into fpu_lock and tlb_lock

For all other purposes, locking is unnecessary, since the fields
in question are only accessed locally from the CPU they belong to.

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