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

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

Make thread→priority weakly atomic to avoid need for locking

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