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

Last change on this file since 07d4271 was 07d4271, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 17 months ago

Fix some unsound task reference manipulation and locking

In some operations that take task ID as an argument,
there's a possibility of the task being destroyed mid-operation
and a subsequent use-after-free situation.
As a general solution, task_find_by_id() is reimplemented to
check for this situation and always return a valid strong reference.
The callers then only need to handle the reference itself, and
don't need to concern themselves with tasks_lock.

  • Property mode set to 100644
File size: 22.4 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->lifecount);
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
302 stats_thread->thread_id = thread->tid;
303 stats_thread->task_id = thread->task->taskid;
304 stats_thread->state = atomic_get_unordered(&thread->state);
305 stats_thread->priority = atomic_get_unordered(&thread->priority);
306 stats_thread->ucycles = atomic_time_read(&thread->ucycles);
307 stats_thread->kcycles = atomic_time_read(&thread->kcycles);
308
309 cpu_t *cpu = atomic_get_unordered(&thread->cpu);
310
311 if (cpu != NULL) {
312 stats_thread->on_cpu = true;
313 stats_thread->cpu = cpu->id;
314 } else
315 stats_thread->on_cpu = false;
316}
317
318/** Get thread statistics
319 *
320 * @param item Sysinfo item (unused).
321 * @param size Size of the returned data.
322 * @param dry_run Do not get the data, just calculate the size.
323 * @param data Unused.
324 *
325 * @return Data containing several stats_task_t structures.
326 * If the return value is not NULL, it should be freed
327 * in the context of the sysinfo request.
328 */
329static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
330 bool dry_run, void *data)
331{
332 /* Messing with threads structures */
333 irq_spinlock_lock(&threads_lock, true);
334
335 /* Count the threads */
336 size_t count = thread_count();
337
338 if (count == 0) {
339 /* No threads found (strange) */
340 irq_spinlock_unlock(&threads_lock, true);
341 *size = 0;
342 return NULL;
343 }
344
345 *size = sizeof(stats_thread_t) * count;
346 if (dry_run) {
347 irq_spinlock_unlock(&threads_lock, true);
348 return NULL;
349 }
350
351 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size);
352 if (stats_threads == NULL) {
353 /* No free space for allocation */
354 irq_spinlock_unlock(&threads_lock, true);
355 *size = 0;
356 return NULL;
357 }
358
359 /* Walk tha thread tree again to gather the statistics */
360 size_t i = 0;
361
362 thread_t *thread = thread_first();
363 while (thread != NULL) {
364 /* Record the statistics and increment the index */
365 produce_stats_thread(thread, &stats_threads[i]);
366 i++;
367
368 thread = thread_next(thread);
369 }
370
371 irq_spinlock_unlock(&threads_lock, true);
372
373 return ((void *) stats_threads);
374}
375
376/** Produce IPC connection statistics
377 *
378 * Summarize IPC connection information into IPC connection statistics.
379 *
380 * @param cap Phone capability.
381 * @param arg State variable.
382 *
383 */
384static bool produce_stats_ipcc_cb(cap_t *cap, void *arg)
385{
386 phone_t *phone = cap->kobject->phone;
387 ipccs_state_t *state = (ipccs_state_t *) arg;
388
389 if (state->counting) {
390 /*
391 * Simply update the number of entries
392 * in case we are in the counting mode.
393 */
394
395 state->count++;
396 return true;
397 }
398
399 /* We are in the gathering mode */
400
401 if ((state->data == NULL) || (state->i >= state->count)) {
402 /*
403 * Do nothing if we have no buffer
404 * to store the data to (meaning we are
405 * in a dry run) or the buffer is already
406 * full.
407 */
408
409 return true;
410 }
411
412 mutex_lock(&phone->lock);
413
414 if (phone->state == IPC_PHONE_CONNECTED) {
415 state->data[state->i].caller = phone->caller->taskid;
416 state->data[state->i].callee = phone->callee->task->taskid;
417 state->i++;
418 }
419
420 mutex_unlock(&phone->lock);
421
422 return true;
423}
424
425/** Get IPC connections statistics
426 *
427 * @param item Sysinfo item (unused).
428 * @param size Size of the returned data.
429 * @param dry_run Do not get the data, just calculate the size.
430 * @param data Unused.
431 *
432 * @return Data containing several stats_ipccs_t structures.
433 * If the return value is not NULL, it should be freed
434 * in the context of the sysinfo request.
435 *
436 */
437static void *get_stats_ipccs(struct sysinfo_item *item, size_t *size,
438 bool dry_run, void *data)
439{
440 /* Messing with tasks structures, avoid deadlock */
441 irq_spinlock_lock(&tasks_lock, true);
442
443 ipccs_state_t state = {
444 .counting = true,
445 .count = 0,
446 .i = 0,
447 .data = NULL
448 };
449
450 /* Compute the number of IPC connections */
451 task_t *task = task_first();
452 while (task != NULL) {
453 task_hold(task);
454 irq_spinlock_unlock(&tasks_lock, true);
455
456 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
457 produce_stats_ipcc_cb, &state);
458
459 irq_spinlock_lock(&tasks_lock, true);
460
461 task = task_next(task);
462 }
463
464 state.counting = false;
465 *size = sizeof(stats_ipcc_t) * state.count;
466
467 if (!dry_run)
468 state.data = (stats_ipcc_t *) malloc(*size);
469
470 /* Gather the statistics for each task */
471 task = task_first();
472 while (task != NULL) {
473 /* We already hold a reference to the task */
474 irq_spinlock_unlock(&tasks_lock, true);
475
476 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
477 produce_stats_ipcc_cb, &state);
478
479 irq_spinlock_lock(&tasks_lock, true);
480
481 task_t *prev_task = task;
482 task = task_next(prev_task);
483 task_release(prev_task);
484 }
485
486 irq_spinlock_unlock(&tasks_lock, true);
487
488 return ((void *) state.data);
489}
490
491/** Get a single task statistics
492 *
493 * Get statistics of a given task. The task ID is passed
494 * as a string (current limitation of the sysinfo interface,
495 * but it is still reasonable for the given purpose).
496 *
497 * @param name Task ID (string-encoded number).
498 * @param dry_run Do not get the data, just calculate the size.
499 * @param data Unused.
500 *
501 * @return Sysinfo return holder. The type of the returned
502 * data is either SYSINFO_VAL_UNDEFINED (unknown
503 * task ID or memory allocation error) or
504 * SYSINFO_VAL_FUNCTION_DATA (in that case the
505 * generated data should be freed within the
506 * sysinfo request context).
507 *
508 */
509static sysinfo_return_t get_stats_task(const char *name, bool dry_run,
510 void *data)
511{
512 /* Initially no return value */
513 sysinfo_return_t ret = {
514 .tag = SYSINFO_VAL_UNDEFINED,
515 };
516
517 /* Parse the task ID */
518 task_id_t task_id;
519 if (str_uint64_t(name, NULL, 0, true, &task_id) != EOK)
520 return ret;
521
522 task_t *task = task_find_by_id(task_id);
523 if (!task)
524 return ret;
525
526 if (dry_run) {
527 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
528 ret.data.data = NULL;
529 ret.data.size = sizeof(stats_task_t);
530 } else {
531 /* Allocate stats_task_t structure */
532 stats_task_t *stats_task = malloc(sizeof(stats_task_t));
533
534 if (stats_task != NULL) {
535 /* Correct return value */
536 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
537 ret.data.data = stats_task;
538 ret.data.size = sizeof(stats_task_t);
539
540 irq_spinlock_lock(&task->lock, true);
541 produce_stats_task(task, stats_task);
542 irq_spinlock_unlock(&task->lock, true);
543 }
544 }
545
546 task_release(task);
547 return ret;
548}
549
550/** Get thread statistics
551 *
552 * Get statistics of a given thread. The thread ID is passed
553 * as a string (current limitation of the sysinfo interface,
554 * but it is still reasonable for the given purpose).
555 *
556 * @param name Thread ID (string-encoded number).
557 * @param dry_run Do not get the data, just calculate the size.
558 * @param data Unused.
559 *
560 * @return Sysinfo return holder. The type of the returned
561 * data is either SYSINFO_VAL_UNDEFINED (unknown
562 * thread ID or memory allocation error) or
563 * SYSINFO_VAL_FUNCTION_DATA (in that case the
564 * generated data should be freed within the
565 * sysinfo request context).
566 *
567 */
568static sysinfo_return_t get_stats_thread(const char *name, bool dry_run,
569 void *data)
570{
571 /* Initially no return value */
572 sysinfo_return_t ret;
573 ret.tag = SYSINFO_VAL_UNDEFINED;
574
575 /* Parse the thread ID */
576 thread_id_t thread_id;
577 if (str_uint64_t(name, NULL, 0, true, &thread_id) != EOK)
578 return ret;
579
580 /* Messing with threads structures */
581 irq_spinlock_lock(&threads_lock, true);
582
583 thread_t *thread = thread_find_by_id(thread_id);
584 if (thread == NULL) {
585 /* No thread with this ID */
586 irq_spinlock_unlock(&threads_lock, true);
587 return ret;
588 }
589
590 if (dry_run) {
591 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
592 ret.data.data = NULL;
593 ret.data.size = sizeof(stats_thread_t);
594
595 irq_spinlock_unlock(&threads_lock, true);
596 } else {
597 /* Allocate stats_thread_t structure */
598 stats_thread_t *stats_thread =
599 (stats_thread_t *) malloc(sizeof(stats_thread_t));
600 if (stats_thread == NULL) {
601 irq_spinlock_unlock(&threads_lock, true);
602 return ret;
603 }
604
605 /* Correct return value */
606 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
607 ret.data.data = (void *) stats_thread;
608 ret.data.size = sizeof(stats_thread_t);
609
610 produce_stats_thread(thread, stats_thread);
611
612 irq_spinlock_unlock(&threads_lock, true);
613 }
614
615 return ret;
616}
617
618/** Get exceptions statistics
619 *
620 * @param item Sysinfo item (unused).
621 * @param size Size of the returned data.
622 * @param dry_run Do not get the data, just calculate the size.
623 * @param data Unused.
624 *
625 * @return Data containing several stats_exc_t structures.
626 * If the return value is not NULL, it should be freed
627 * in the context of the sysinfo request.
628 */
629static void *get_stats_exceptions(struct sysinfo_item *item, size_t *size,
630 bool dry_run, void *data)
631{
632 *size = sizeof(stats_exc_t) * IVT_ITEMS;
633
634 if ((dry_run) || (IVT_ITEMS == 0))
635 return NULL;
636
637 stats_exc_t *stats_exceptions =
638 (stats_exc_t *) malloc(*size);
639 if (stats_exceptions == NULL) {
640 /* No free space for allocation */
641 *size = 0;
642 return NULL;
643 }
644
645#if (IVT_ITEMS > 0)
646 /* Messing with exception table, avoid deadlock */
647 irq_spinlock_lock(&exctbl_lock, true);
648
649 unsigned int i;
650 for (i = 0; i < IVT_ITEMS; i++) {
651 stats_exceptions[i].id = i + IVT_FIRST;
652 str_cpy(stats_exceptions[i].desc, EXC_NAME_BUFLEN, exc_table[i].name);
653 stats_exceptions[i].hot = exc_table[i].hot;
654 stats_exceptions[i].cycles = exc_table[i].cycles;
655 stats_exceptions[i].count = exc_table[i].count;
656 }
657
658 irq_spinlock_unlock(&exctbl_lock, true);
659#endif
660
661 return ((void *) stats_exceptions);
662}
663
664/** Get exception statistics
665 *
666 * Get statistics of a given exception. The exception number
667 * is passed as a string (current limitation of the sysinfo
668 * interface, but it is still reasonable for the given purpose).
669 *
670 * @param name Exception number (string-encoded number).
671 * @param dry_run Do not get the data, just calculate the size.
672 * @param data Unused.
673 *
674 * @return Sysinfo return holder. The type of the returned
675 * data is either SYSINFO_VAL_UNDEFINED (unknown
676 * exception number or memory allocation error) or
677 * SYSINFO_VAL_FUNCTION_DATA (in that case the
678 * generated data should be freed within the
679 * sysinfo request context).
680 *
681 */
682static sysinfo_return_t get_stats_exception(const char *name, bool dry_run,
683 void *data)
684{
685 /* Initially no return value */
686 sysinfo_return_t ret;
687 ret.tag = SYSINFO_VAL_UNDEFINED;
688
689 /* Parse the exception number */
690 uint64_t excn;
691 if (str_uint64_t(name, NULL, 0, true, &excn) != EOK)
692 return ret;
693
694#if (IVT_FIRST > 0)
695 if (excn < IVT_FIRST)
696 return ret;
697#endif
698
699#if (IVT_ITEMS + IVT_FIRST == 0)
700 return ret;
701#else
702 if (excn >= IVT_ITEMS + IVT_FIRST)
703 return ret;
704#endif
705
706 if (dry_run) {
707 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
708 ret.data.data = NULL;
709 ret.data.size = sizeof(stats_thread_t);
710 } else {
711 /* Update excn index for accessing exc_table */
712 excn -= IVT_FIRST;
713
714 /* Allocate stats_exc_t structure */
715 stats_exc_t *stats_exception =
716 (stats_exc_t *) malloc(sizeof(stats_exc_t));
717 if (stats_exception == NULL)
718 return ret;
719
720 /* Messing with exception table, avoid deadlock */
721 irq_spinlock_lock(&exctbl_lock, true);
722
723 /* Correct return value */
724 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
725 ret.data.data = (void *) stats_exception;
726 ret.data.size = sizeof(stats_exc_t);
727
728 stats_exception->id = excn;
729 str_cpy(stats_exception->desc, EXC_NAME_BUFLEN, exc_table[excn].name);
730 stats_exception->hot = exc_table[excn].hot;
731 stats_exception->cycles = exc_table[excn].cycles;
732 stats_exception->count = exc_table[excn].count;
733
734 irq_spinlock_unlock(&exctbl_lock, true);
735 }
736
737 return ret;
738}
739
740/** Get physical memory statistics
741 *
742 * @param item Sysinfo item (unused).
743 * @param size Size of the returned data.
744 * @param dry_run Do not get the data, just calculate the size.
745 * @param data Unused.
746 *
747 * @return Data containing stats_physmem_t.
748 * If the return value is not NULL, it should be freed
749 * in the context of the sysinfo request.
750 */
751static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
752 bool dry_run, void *data)
753{
754 *size = sizeof(stats_physmem_t);
755 if (dry_run)
756 return NULL;
757
758 stats_physmem_t *stats_physmem =
759 (stats_physmem_t *) malloc(*size);
760 if (stats_physmem == NULL) {
761 *size = 0;
762 return NULL;
763 }
764
765 zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
766 &(stats_physmem->used), &(stats_physmem->free));
767
768 return ((void *) stats_physmem);
769}
770
771/** Get system load
772 *
773 * @param item Sysinfo item (unused).
774 * @param size Size of the returned data.
775 * @param dry_run Do not get the data, just calculate the size.
776 * @param data Unused.
777 *
778 * @return Data several load_t values.
779 * If the return value is not NULL, it should be freed
780 * in the context of the sysinfo request.
781 */
782static void *get_stats_load(struct sysinfo_item *item, size_t *size,
783 bool dry_run, void *data)
784{
785 *size = sizeof(load_t) * LOAD_STEPS;
786 if (dry_run)
787 return NULL;
788
789 load_t *stats_load = (load_t *) malloc(*size);
790 if (stats_load == NULL) {
791 *size = 0;
792 return NULL;
793 }
794
795 /* To always get consistent values acquire the mutex */
796 mutex_lock(&load_lock);
797
798 unsigned int i;
799 for (i = 0; i < LOAD_STEPS; i++)
800 stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
801
802 mutex_unlock(&load_lock);
803
804 return ((void *) stats_load);
805}
806
807/** Calculate load
808 *
809 */
810static inline load_t load_calc(load_t load, load_t exp, size_t ready)
811{
812 load *= exp;
813 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
814
815 return (load >> LOAD_FIXED_SHIFT);
816}
817
818/** Load computation thread.
819 *
820 * Compute system load every few seconds.
821 *
822 * @param arg Unused.
823 *
824 */
825void kload(void *arg)
826{
827 while (true) {
828 size_t ready = atomic_load(&nrdy);
829
830 /* Mutually exclude with get_stats_load() */
831 mutex_lock(&load_lock);
832
833 unsigned int i;
834 for (i = 0; i < LOAD_STEPS; i++)
835 avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
836
837 mutex_unlock(&load_lock);
838
839 thread_sleep(LOAD_INTERVAL);
840 }
841}
842
843/** Register sysinfo statistical items
844 *
845 */
846void stats_init(void)
847{
848 mutex_initialize(&load_lock, MUTEX_PASSIVE);
849
850 sysinfo_set_item_gen_data("system.cpus", NULL, get_stats_cpus, NULL);
851 sysinfo_set_item_gen_data("system.physmem", NULL, get_stats_physmem, NULL);
852 sysinfo_set_item_gen_data("system.load", NULL, get_stats_load, NULL);
853 sysinfo_set_item_gen_data("system.tasks", NULL, get_stats_tasks, NULL);
854 sysinfo_set_item_gen_data("system.threads", NULL, get_stats_threads, NULL);
855 sysinfo_set_item_gen_data("system.ipccs", NULL, get_stats_ipccs, NULL);
856 sysinfo_set_item_gen_data("system.exceptions", NULL, get_stats_exceptions, NULL);
857 sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task, NULL);
858 sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread, NULL);
859 sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception, NULL);
860}
861
862/** @}
863 */
Note: See TracBrowser for help on using the repository browser.