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

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

Make thread cycle statistics atomic

  • Property mode set to 100644
File size: 23.1 KB
RevLine 
[9dae191e]1/*
2 * Copyright (c) 2010 Stanislav Kozina
3 * Copyright (c) 2010 Martin Decky
[ef1eab7]4 * Copyright (c) 2018 Jiri Svoboda
[9dae191e]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
[174156fd]31/** @addtogroup kernel_generic
[9dae191e]32 * @{
33 */
34/** @file
35 */
36
[63e27ef]37#include <assert.h>
[9dae191e]38#include <typedefs.h>
[c0699467]39#include <abi/sysinfo.h>
[9dae191e]40#include <sysinfo/stats.h>
41#include <sysinfo/sysinfo.h>
[6e121b8]42#include <synch/spinlock.h>
43#include <synch/mutex.h>
[9dae191e]44#include <time/clock.h>
45#include <mm/frame.h>
[e1b6742]46#include <proc/task.h>
[9dae191e]47#include <proc/thread.h>
[8eec3c8]48#include <interrupt.h>
[525c5ac]49#include <stdbool.h>
[9dae191e]50#include <str.h>
51#include <errno.h>
52#include <cpu.h>
53#include <arch.h>
[aafed15]54#include <stdlib.h>
[9dae191e]55
56/** Bits of fixed-point precision for load */
57#define LOAD_FIXED_SHIFT 11
58
[fd3a631f]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
[9dae191e]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
[a6302ae]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
[80bfb601]79/** Fixed-point representation of
[9dae191e]80 *
81 * 1 / exp(5 sec / 1 min)
82 * 1 / exp(5 sec / 5 min)
83 * 1 / exp(5 sec / 15 min)
84 *
85 */
[3bacee1]86static load_t load_exp[LOAD_STEPS] = { 1884, 2014, 2037 };
[80bfb601]87
88/** Running average of the number of ready threads */
[3bacee1]89static load_t avenrdy[LOAD_STEPS] = { 0, 0, 0 };
[80bfb601]90
[6e121b8]91/** Load calculation lock */
92static mutex_t load_lock;
[9dae191e]93
[80bfb601]94/** Get statistics of all CPUs
95 *
[70e2b2d]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.
[196c253]99 * @param data Unused.
[80bfb601]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 */
[70e2b2d]105static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
[196c253]106 bool dry_run, void *data)
[9dae191e]107{
[70e2b2d]108 *size = sizeof(stats_cpu_t) * config.cpu_count;
109 if (dry_run)
110 return NULL;
[a35b458]111
[80bfb601]112 /* Assumption: config.cpu_count is constant */
[11b285d]113 stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size);
[9dae191e]114 if (stats_cpus == NULL) {
115 *size = 0;
116 return NULL;
117 }
[a35b458]118
[9dae191e]119 size_t i;
120 for (i = 0; i < config.cpu_count; i++) {
121 stats_cpus[i].id = cpus[i].id;
[bd01a4e]122 stats_cpus[i].active = cpus[i].active;
[9dae191e]123 stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
[b2ec5cf]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);
[9dae191e]127 }
[a35b458]128
[9dae191e]129 return ((void *) stats_cpus);
130}
131
[dec16a2]132/** Get the size of a virtual address space
133 *
134 * @param as Address space.
[80bfb601]135 *
[dec16a2]136 * @return Size of the mapped virtual address space (bytes).
137 *
138 */
139static size_t get_task_virtmem(as_t *as)
140{
[d69f959]141 /*
[fc47885]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.
[d69f959]146 */
[a35b458]147
[897fd8f1]148 if (mutex_trylock(&as->lock) != EOK)
[fc47885]149 return 0;
[a35b458]150
[fc47885]151 size_t pages = 0;
[a35b458]152
[88cc71c0]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);
[dec16a2]162 }
[a35b458]163
[dec16a2]164 mutex_unlock(&as->lock);
[a35b458]165
[fc47885]166 return (pages << PAGE_WIDTH);
[dec16a2]167}
168
[a0ce870]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 /*
[fc47885]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.
[a0ce870]183 */
[a35b458]184
[897fd8f1]185 if (mutex_trylock(&as->lock) != EOK)
[fc47885]186 return 0;
[a35b458]187
[fc47885]188 size_t pages = 0;
[a35b458]189
[88cc71c0]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;
[a35b458]195
[2fc3b2d]196 pages += area->used_space.pages;
[88cc71c0]197 mutex_unlock(&area->lock);
198 area = as_area_next(area);
[a0ce870]199 }
[a35b458]200
[a0ce870]201 mutex_unlock(&as->lock);
[a35b458]202
[fc47885]203 return (pages << PAGE_WIDTH);
[a0ce870]204}
205
[7c3fb9b]206/** Produce task statistics
[dec16a2]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{
[63e27ef]216 assert(interrupts_disabled());
217 assert(irq_spinlock_locked(&task->lock));
[a35b458]218
[dec16a2]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);
[a0ce870]222 stats_task->resmem = get_task_resmem(task->as);
[036e97c]223 stats_task->threads = atomic_load(&task->refcount);
[dec16a2]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
[80bfb601]230 *
[70e2b2d]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.
[196c253]234 * @param data Unused.
[80bfb601]235 *
[dec16a2]236 * @return Data containing several stats_task_t structures.
[80bfb601]237 * If the return value is not NULL, it should be freed
238 * in the context of the sysinfo request.
239 */
[70e2b2d]240static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
[196c253]241 bool dry_run, void *data)
[9dae191e]242{
243 /* Messing with task structures, avoid deadlock */
[da1bafb]244 irq_spinlock_lock(&tasks_lock, true);
[a35b458]245
[ef1eab7]246 /* Count the tasks */
[aab5e46]247 size_t count = task_count();
[a35b458]248
[9dae191e]249 if (count == 0) {
[80bfb601]250 /* No tasks found (strange) */
[da1bafb]251 irq_spinlock_unlock(&tasks_lock, true);
[9dae191e]252 *size = 0;
253 return NULL;
254 }
[a35b458]255
[dec16a2]256 *size = sizeof(stats_task_t) * count;
[70e2b2d]257 if (dry_run) {
[da1bafb]258 irq_spinlock_unlock(&tasks_lock, true);
[70e2b2d]259 return NULL;
260 }
[a35b458]261
[11b285d]262 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size);
[dec16a2]263 if (stats_tasks == NULL) {
[80bfb601]264 /* No free space for allocation */
[da1bafb]265 irq_spinlock_unlock(&tasks_lock, true);
[9dae191e]266 *size = 0;
267 return NULL;
268 }
[a35b458]269
[ef1eab7]270 /* Gather the statistics for each task */
271 size_t i = 0;
[aab5e46]272 task_t *task = task_first();
273 while (task != NULL) {
[ef1eab7]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);
[aab5e46]282 task = task_next(task);
[ef1eab7]283 }
[a35b458]284
[da1bafb]285 irq_spinlock_unlock(&tasks_lock, true);
[a35b458]286
[dec16a2]287 return ((void *) stats_tasks);
288}
289
[7c3fb9b]290/** Produce thread statistics
[dec16a2]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{
[63e27ef]300 assert(interrupts_disabled());
[a35b458]301
[dec16a2]302 stats_thread->thread_id = thread->tid;
303 stats_thread->task_id = thread->task->taskid;
[41bfc64]304 stats_thread->state = atomic_get_unordered(&thread->state);
[3d84734]305 stats_thread->priority = atomic_get_unordered(&thread->priority);
[11909ce3]306 stats_thread->ucycles = atomic_time_read(&thread->ucycles);
307 stats_thread->kcycles = atomic_time_read(&thread->kcycles);
[a35b458]308
[efed95a3]309 cpu_t *cpu = atomic_get_unordered(&thread->cpu);
310
311 if (cpu != NULL) {
[dec16a2]312 stats_thread->on_cpu = true;
[efed95a3]313 stats_thread->cpu = cpu->id;
[dec16a2]314 } else
315 stats_thread->on_cpu = false;
[9dae191e]316}
317
[dec16a2]318/** Get thread statistics
[e1b6742]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.
[196c253]323 * @param data Unused.
[e1b6742]324 *
[dec16a2]325 * @return Data containing several stats_task_t structures.
[e1b6742]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,
[196c253]330 bool dry_run, void *data)
[e1b6742]331{
[1871118]332 /* Messing with threads structures */
[da1bafb]333 irq_spinlock_lock(&threads_lock, true);
[a35b458]334
[ef1eab7]335 /* Count the threads */
[aab5e46]336 size_t count = thread_count();
[a35b458]337
[e1b6742]338 if (count == 0) {
339 /* No threads found (strange) */
[da1bafb]340 irq_spinlock_unlock(&threads_lock, true);
[e1b6742]341 *size = 0;
342 return NULL;
343 }
[a35b458]344
[dec16a2]345 *size = sizeof(stats_thread_t) * count;
[e1b6742]346 if (dry_run) {
[da1bafb]347 irq_spinlock_unlock(&threads_lock, true);
[e1b6742]348 return NULL;
349 }
[a35b458]350
[11b285d]351 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size);
[dec16a2]352 if (stats_threads == NULL) {
[e1b6742]353 /* No free space for allocation */
[da1bafb]354 irq_spinlock_unlock(&threads_lock, true);
[e1b6742]355 *size = 0;
356 return NULL;
357 }
[a35b458]358
[dec16a2]359 /* Walk tha thread tree again to gather the statistics */
[ef1eab7]360 size_t i = 0;
361
[aab5e46]362 thread_t *thread = thread_first();
363 while (thread != NULL) {
[ef1eab7]364 /* Interrupts are already disabled */
365 irq_spinlock_lock(&thread->lock, false);
366
367 /* Record the statistics and increment the index */
368 produce_stats_thread(thread, &stats_threads[i]);
369 i++;
370
371 irq_spinlock_unlock(&thread->lock, false);
372
[aab5e46]373 thread = thread_next(thread);
[ef1eab7]374 }
[a35b458]375
[da1bafb]376 irq_spinlock_unlock(&threads_lock, true);
[a35b458]377
[dec16a2]378 return ((void *) stats_threads);
[e1b6742]379}
380
[a6302ae]381/** Produce IPC connection statistics
382 *
383 * Summarize IPC connection information into IPC connection statistics.
384 *
385 * @param cap Phone capability.
386 * @param arg State variable.
387 *
388 */
389static bool produce_stats_ipcc_cb(cap_t *cap, void *arg)
390{
391 phone_t *phone = cap->kobject->phone;
392 ipccs_state_t *state = (ipccs_state_t *) arg;
393
394 if (state->counting) {
395 /*
396 * Simply update the number of entries
397 * in case we are in the counting mode.
398 */
399
400 state->count++;
401 return true;
402 }
403
404 /* We are in the gathering mode */
405
406 if ((state->data == NULL) || (state->i >= state->count)) {
407 /*
408 * Do nothing if we have no buffer
409 * to store the data to (meaning we are
410 * in a dry run) or the buffer is already
411 * full.
412 */
413
414 return true;
415 }
416
417 mutex_lock(&phone->lock);
418
419 if (phone->state == IPC_PHONE_CONNECTED) {
420 state->data[state->i].caller = phone->caller->taskid;
421 state->data[state->i].callee = phone->callee->task->taskid;
422 state->i++;
423 }
424
425 mutex_unlock(&phone->lock);
426
427 return true;
428}
429
430/** Get IPC connections statistics
431 *
432 * @param item Sysinfo item (unused).
433 * @param size Size of the returned data.
434 * @param dry_run Do not get the data, just calculate the size.
435 * @param data Unused.
436 *
437 * @return Data containing several stats_ipccs_t structures.
438 * If the return value is not NULL, it should be freed
439 * in the context of the sysinfo request.
440 *
441 */
442static void *get_stats_ipccs(struct sysinfo_item *item, size_t *size,
443 bool dry_run, void *data)
444{
445 /* Messing with tasks structures, avoid deadlock */
446 irq_spinlock_lock(&tasks_lock, true);
447
448 ipccs_state_t state = {
449 .counting = true,
450 .count = 0,
451 .i = 0,
452 .data = NULL
453 };
454
455 /* Compute the number of IPC connections */
456 task_t *task = task_first();
457 while (task != NULL) {
458 task_hold(task);
459 irq_spinlock_unlock(&tasks_lock, true);
460
461 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
462 produce_stats_ipcc_cb, &state);
463
464 irq_spinlock_lock(&tasks_lock, true);
465
466 task = task_next(task);
467 }
468
469 state.counting = false;
470 *size = sizeof(stats_ipcc_t) * state.count;
471
472 if (!dry_run)
473 state.data = (stats_ipcc_t *) malloc(*size);
474
475 /* Gather the statistics for each task */
476 task = task_first();
477 while (task != NULL) {
478 /* We already hold a reference to the task */
479 irq_spinlock_unlock(&tasks_lock, true);
480
481 caps_apply_to_kobject_type(task, KOBJECT_TYPE_PHONE,
482 produce_stats_ipcc_cb, &state);
483
484 irq_spinlock_lock(&tasks_lock, true);
485
[fe7bcf1]486 task_t *prev_task = task;
487 task = task_next(prev_task);
488 task_release(prev_task);
[a6302ae]489 }
490
491 irq_spinlock_unlock(&tasks_lock, true);
492
493 return ((void *) state.data);
494}
495
[dec16a2]496/** Get a single task statistics
[80bfb601]497 *
498 * Get statistics of a given task. The task ID is passed
499 * as a string (current limitation of the sysinfo interface,
500 * but it is still reasonable for the given purpose).
501 *
[e1b6742]502 * @param name Task ID (string-encoded number).
503 * @param dry_run Do not get the data, just calculate the size.
[5869ce0]504 * @param data Unused.
[80bfb601]505 *
506 * @return Sysinfo return holder. The type of the returned
507 * data is either SYSINFO_VAL_UNDEFINED (unknown
508 * task ID or memory allocation error) or
509 * SYSINFO_VAL_FUNCTION_DATA (in that case the
510 * generated data should be freed within the
511 * sysinfo request context).
512 *
513 */
[5869ce0]514static sysinfo_return_t get_stats_task(const char *name, bool dry_run,
515 void *data)
[9dae191e]516{
[80bfb601]517 /* Initially no return value */
[9dae191e]518 sysinfo_return_t ret;
519 ret.tag = SYSINFO_VAL_UNDEFINED;
[a35b458]520
[80bfb601]521 /* Parse the task ID */
[9dae191e]522 task_id_t task_id;
[059a8e4]523 if (str_uint64_t(name, NULL, 0, true, &task_id) != EOK)
[9dae191e]524 return ret;
[a35b458]525
[80bfb601]526 /* Messing with task structures, avoid deadlock */
[da1bafb]527 irq_spinlock_lock(&tasks_lock, true);
[a35b458]528
[9dae191e]529 task_t *task = task_find_by_id(task_id);
530 if (task == NULL) {
[80bfb601]531 /* No task with this ID */
[da1bafb]532 irq_spinlock_unlock(&tasks_lock, true);
[9dae191e]533 return ret;
534 }
[a35b458]535
[e1b6742]536 if (dry_run) {
537 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
538 ret.data.data = NULL;
539 ret.data.size = sizeof(stats_task_t);
[a35b458]540
[da1bafb]541 irq_spinlock_unlock(&tasks_lock, true);
[e1b6742]542 } else {
543 /* Allocate stats_task_t structure */
544 stats_task_t *stats_task =
[11b285d]545 (stats_task_t *) malloc(sizeof(stats_task_t));
[e1b6742]546 if (stats_task == NULL) {
[da1bafb]547 irq_spinlock_unlock(&tasks_lock, true);
[e1b6742]548 return ret;
549 }
[a35b458]550
[e1b6742]551 /* Correct return value */
552 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
553 ret.data.data = (void *) stats_task;
554 ret.data.size = sizeof(stats_task_t);
[a35b458]555
[e1b6742]556 /* Hand-over-hand locking */
[da1bafb]557 irq_spinlock_exchange(&tasks_lock, &task->lock);
[a35b458]558
[dec16a2]559 produce_stats_task(task, stats_task);
[a35b458]560
[da1bafb]561 irq_spinlock_unlock(&task->lock, true);
[e1b6742]562 }
[a35b458]563
[e1b6742]564 return ret;
565}
566
567/** Get thread statistics
568 *
569 * Get statistics of a given thread. The thread ID is passed
570 * as a string (current limitation of the sysinfo interface,
571 * but it is still reasonable for the given purpose).
572 *
573 * @param name Thread ID (string-encoded number).
574 * @param dry_run Do not get the data, just calculate the size.
[5869ce0]575 * @param data Unused.
[e1b6742]576 *
577 * @return Sysinfo return holder. The type of the returned
578 * data is either SYSINFO_VAL_UNDEFINED (unknown
579 * thread ID or memory allocation error) or
580 * SYSINFO_VAL_FUNCTION_DATA (in that case the
581 * generated data should be freed within the
582 * sysinfo request context).
583 *
584 */
[5869ce0]585static sysinfo_return_t get_stats_thread(const char *name, bool dry_run,
586 void *data)
[e1b6742]587{
588 /* Initially no return value */
589 sysinfo_return_t ret;
590 ret.tag = SYSINFO_VAL_UNDEFINED;
[a35b458]591
[e1b6742]592 /* Parse the thread ID */
593 thread_id_t thread_id;
[059a8e4]594 if (str_uint64_t(name, NULL, 0, true, &thread_id) != EOK)
[e1b6742]595 return ret;
[a35b458]596
[1871118]597 /* Messing with threads structures */
[da1bafb]598 irq_spinlock_lock(&threads_lock, true);
[a35b458]599
[e1b6742]600 thread_t *thread = thread_find_by_id(thread_id);
601 if (thread == NULL) {
602 /* No thread with this ID */
[da1bafb]603 irq_spinlock_unlock(&threads_lock, true);
[e1b6742]604 return ret;
605 }
[a35b458]606
[e1b6742]607 if (dry_run) {
608 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
609 ret.data.data = NULL;
610 ret.data.size = sizeof(stats_thread_t);
[a35b458]611
[da1bafb]612 irq_spinlock_unlock(&threads_lock, true);
[e1b6742]613 } else {
614 /* Allocate stats_thread_t structure */
615 stats_thread_t *stats_thread =
[11b285d]616 (stats_thread_t *) malloc(sizeof(stats_thread_t));
[e1b6742]617 if (stats_thread == NULL) {
[da1bafb]618 irq_spinlock_unlock(&threads_lock, true);
[e1b6742]619 return ret;
620 }
[a35b458]621
[e1b6742]622 /* Correct return value */
623 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
624 ret.data.data = (void *) stats_thread;
625 ret.data.size = sizeof(stats_thread_t);
[a35b458]626
[1871118]627 /*
628 * Replaced hand-over-hand locking with regular nested sections
629 * to avoid weak reference leak issues.
630 */
631 irq_spinlock_lock(&thread->lock, false);
[dec16a2]632 produce_stats_thread(thread, stats_thread);
[1871118]633 irq_spinlock_unlock(&thread->lock, false);
[a35b458]634
[1871118]635 irq_spinlock_unlock(&threads_lock, true);
[e1b6742]636 }
[a35b458]637
[9dae191e]638 return ret;
639}
640
[8eec3c8]641/** Get exceptions statistics
642 *
643 * @param item Sysinfo item (unused).
644 * @param size Size of the returned data.
645 * @param dry_run Do not get the data, just calculate the size.
[196c253]646 * @param data Unused.
[8eec3c8]647 *
648 * @return Data containing several stats_exc_t structures.
649 * If the return value is not NULL, it should be freed
650 * in the context of the sysinfo request.
651 */
652static void *get_stats_exceptions(struct sysinfo_item *item, size_t *size,
[196c253]653 bool dry_run, void *data)
[8eec3c8]654{
655 *size = sizeof(stats_exc_t) * IVT_ITEMS;
[a35b458]656
[8eec3c8]657 if ((dry_run) || (IVT_ITEMS == 0))
658 return NULL;
[a35b458]659
[8eec3c8]660 stats_exc_t *stats_exceptions =
[11b285d]661 (stats_exc_t *) malloc(*size);
[8eec3c8]662 if (stats_exceptions == NULL) {
663 /* No free space for allocation */
664 *size = 0;
665 return NULL;
666 }
[a35b458]667
[b3b7e14a]668#if (IVT_ITEMS > 0)
[8eec3c8]669 /* Messing with exception table, avoid deadlock */
670 irq_spinlock_lock(&exctbl_lock, true);
[a35b458]671
[8eec3c8]672 unsigned int i;
673 for (i = 0; i < IVT_ITEMS; i++) {
674 stats_exceptions[i].id = i + IVT_FIRST;
675 str_cpy(stats_exceptions[i].desc, EXC_NAME_BUFLEN, exc_table[i].name);
[b3b7e14a]676 stats_exceptions[i].hot = exc_table[i].hot;
[8eec3c8]677 stats_exceptions[i].cycles = exc_table[i].cycles;
678 stats_exceptions[i].count = exc_table[i].count;
679 }
[a35b458]680
[8eec3c8]681 irq_spinlock_unlock(&exctbl_lock, true);
[b3b7e14a]682#endif
[a35b458]683
[8eec3c8]684 return ((void *) stats_exceptions);
685}
686
687/** Get exception statistics
688 *
689 * Get statistics of a given exception. The exception number
690 * is passed as a string (current limitation of the sysinfo
691 * interface, but it is still reasonable for the given purpose).
692 *
693 * @param name Exception number (string-encoded number).
694 * @param dry_run Do not get the data, just calculate the size.
[5869ce0]695 * @param data Unused.
[8eec3c8]696 *
697 * @return Sysinfo return holder. The type of the returned
698 * data is either SYSINFO_VAL_UNDEFINED (unknown
699 * exception number or memory allocation error) or
700 * SYSINFO_VAL_FUNCTION_DATA (in that case the
701 * generated data should be freed within the
702 * sysinfo request context).
703 *
704 */
[5869ce0]705static sysinfo_return_t get_stats_exception(const char *name, bool dry_run,
706 void *data)
[8eec3c8]707{
708 /* Initially no return value */
709 sysinfo_return_t ret;
710 ret.tag = SYSINFO_VAL_UNDEFINED;
[a35b458]711
[8eec3c8]712 /* Parse the exception number */
713 uint64_t excn;
[059a8e4]714 if (str_uint64_t(name, NULL, 0, true, &excn) != EOK)
[8eec3c8]715 return ret;
[a35b458]716
[b3b7e14a]717#if (IVT_FIRST > 0)
[8eec3c8]718 if (excn < IVT_FIRST)
719 return ret;
720#endif
[a35b458]721
[b3b7e14a]722#if (IVT_ITEMS + IVT_FIRST == 0)
723 return ret;
724#else
[8eec3c8]725 if (excn >= IVT_ITEMS + IVT_FIRST)
726 return ret;
[b3b7e14a]727#endif
[a35b458]728
[8eec3c8]729 if (dry_run) {
730 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
731 ret.data.data = NULL;
732 ret.data.size = sizeof(stats_thread_t);
733 } else {
734 /* Update excn index for accessing exc_table */
735 excn -= IVT_FIRST;
[a35b458]736
[8eec3c8]737 /* Allocate stats_exc_t structure */
738 stats_exc_t *stats_exception =
[11b285d]739 (stats_exc_t *) malloc(sizeof(stats_exc_t));
[8eec3c8]740 if (stats_exception == NULL)
741 return ret;
[a35b458]742
[8eec3c8]743 /* Messing with exception table, avoid deadlock */
744 irq_spinlock_lock(&exctbl_lock, true);
[a35b458]745
[8eec3c8]746 /* Correct return value */
747 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
748 ret.data.data = (void *) stats_exception;
749 ret.data.size = sizeof(stats_exc_t);
[a35b458]750
[8eec3c8]751 stats_exception->id = excn;
752 str_cpy(stats_exception->desc, EXC_NAME_BUFLEN, exc_table[excn].name);
[b3b7e14a]753 stats_exception->hot = exc_table[excn].hot;
[8eec3c8]754 stats_exception->cycles = exc_table[excn].cycles;
755 stats_exception->count = exc_table[excn].count;
[a35b458]756
[8eec3c8]757 irq_spinlock_unlock(&exctbl_lock, true);
758 }
[a35b458]759
[8eec3c8]760 return ret;
761}
762
[80bfb601]763/** Get physical memory statistics
764 *
[70e2b2d]765 * @param item Sysinfo item (unused).
766 * @param size Size of the returned data.
767 * @param dry_run Do not get the data, just calculate the size.
[196c253]768 * @param data Unused.
[80bfb601]769 *
770 * @return Data containing stats_physmem_t.
771 * If the return value is not NULL, it should be freed
772 * in the context of the sysinfo request.
773 */
[70e2b2d]774static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
[196c253]775 bool dry_run, void *data)
[9dae191e]776{
[70e2b2d]777 *size = sizeof(stats_physmem_t);
778 if (dry_run)
779 return NULL;
[a35b458]780
[9dae191e]781 stats_physmem_t *stats_physmem =
[11b285d]782 (stats_physmem_t *) malloc(*size);
[9dae191e]783 if (stats_physmem == NULL) {
784 *size = 0;
785 return NULL;
786 }
[a35b458]787
[9dae191e]788 zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
789 &(stats_physmem->used), &(stats_physmem->free));
[a35b458]790
[9dae191e]791 return ((void *) stats_physmem);
792}
793
[80bfb601]794/** Get system load
795 *
[70e2b2d]796 * @param item Sysinfo item (unused).
797 * @param size Size of the returned data.
798 * @param dry_run Do not get the data, just calculate the size.
[196c253]799 * @param data Unused.
[80bfb601]800 *
801 * @return Data several load_t values.
802 * If the return value is not NULL, it should be freed
803 * in the context of the sysinfo request.
804 */
[70e2b2d]805static void *get_stats_load(struct sysinfo_item *item, size_t *size,
[196c253]806 bool dry_run, void *data)
[9dae191e]807{
[70e2b2d]808 *size = sizeof(load_t) * LOAD_STEPS;
809 if (dry_run)
810 return NULL;
[a35b458]811
[11b285d]812 load_t *stats_load = (load_t *) malloc(*size);
[9dae191e]813 if (stats_load == NULL) {
814 *size = 0;
815 return NULL;
816 }
[a35b458]817
[6e121b8]818 /* To always get consistent values acquire the mutex */
819 mutex_lock(&load_lock);
[a35b458]820
[9dae191e]821 unsigned int i;
822 for (i = 0; i < LOAD_STEPS; i++)
[fd3a631f]823 stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
[a35b458]824
[6e121b8]825 mutex_unlock(&load_lock);
[a35b458]826
[9dae191e]827 return ((void *) stats_load);
828}
829
830/** Calculate load
831 *
832 */
[3cfe2b8]833static inline load_t load_calc(load_t load, load_t exp, size_t ready)
[9dae191e]834{
835 load *= exp;
[9efff92]836 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
[a35b458]837
[9dae191e]838 return (load >> LOAD_FIXED_SHIFT);
839}
840
841/** Load computation thread.
842 *
843 * Compute system load every few seconds.
844 *
845 * @param arg Unused.
846 *
847 */
848void kload(void *arg)
849{
850 while (true) {
[3cfe2b8]851 size_t ready = atomic_load(&nrdy);
[a35b458]852
[80bfb601]853 /* Mutually exclude with get_stats_load() */
[6e121b8]854 mutex_lock(&load_lock);
[a35b458]855
[9dae191e]856 unsigned int i;
857 for (i = 0; i < LOAD_STEPS; i++)
858 avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
[a35b458]859
[6e121b8]860 mutex_unlock(&load_lock);
[a35b458]861
[9dae191e]862 thread_sleep(LOAD_INTERVAL);
863 }
864}
865
[80bfb601]866/** Register sysinfo statistical items
867 *
868 */
[9dae191e]869void stats_init(void)
870{
[6e121b8]871 mutex_initialize(&load_lock, MUTEX_PASSIVE);
[a35b458]872
[196c253]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);
[a6302ae]878 sysinfo_set_item_gen_data("system.ipccs", NULL, get_stats_ipccs, NULL);
[196c253]879 sysinfo_set_item_gen_data("system.exceptions", NULL, get_stats_exceptions, NULL);
[5869ce0]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);
[9dae191e]883}
884
885/** @}
886 */
Note: See TracBrowser for help on using the repository browser.