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

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

Make thread_t reference counted

This simplifies interaction between various locks and thread
lifespan, which simplifies things. For example, threads_lock can
now simply be a mutex protecting the global it was made for, and
nothing more.

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