| 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 | /** Fixed-point representation of
|
|---|
| 72 | *
|
|---|
| 73 | * 1 / exp(5 sec / 1 min)
|
|---|
| 74 | * 1 / exp(5 sec / 5 min)
|
|---|
| 75 | * 1 / exp(5 sec / 15 min)
|
|---|
| 76 | *
|
|---|
| 77 | */
|
|---|
| 78 | static load_t load_exp[LOAD_STEPS] = { 1884, 2014, 2037 };
|
|---|
| 79 |
|
|---|
| 80 | /** Running average of the number of ready threads */
|
|---|
| 81 | static load_t avenrdy[LOAD_STEPS] = { 0, 0, 0 };
|
|---|
| 82 |
|
|---|
| 83 | /** Load calculation lock */
|
|---|
| 84 | static mutex_t load_lock;
|
|---|
| 85 |
|
|---|
| 86 | /** Get statistics of all CPUs
|
|---|
| 87 | *
|
|---|
| 88 | * @param item Sysinfo item (unused).
|
|---|
| 89 | * @param size Size of the returned data.
|
|---|
| 90 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 91 | * @param data Unused.
|
|---|
| 92 | *
|
|---|
| 93 | * @return Data containing several stats_cpu_t structures.
|
|---|
| 94 | * If the return value is not NULL, it should be freed
|
|---|
| 95 | * in the context of the sysinfo request.
|
|---|
| 96 | */
|
|---|
| 97 | static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
|
|---|
| 98 | bool dry_run, void *data)
|
|---|
| 99 | {
|
|---|
| 100 | *size = sizeof(stats_cpu_t) * config.cpu_count;
|
|---|
| 101 | if (dry_run)
|
|---|
| 102 | return NULL;
|
|---|
| 103 |
|
|---|
| 104 | /* Assumption: config.cpu_count is constant */
|
|---|
| 105 | stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size);
|
|---|
| 106 | if (stats_cpus == NULL) {
|
|---|
| 107 | *size = 0;
|
|---|
| 108 | return NULL;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | size_t i;
|
|---|
| 112 | for (i = 0; i < config.cpu_count; i++) {
|
|---|
| 113 | irq_spinlock_lock(&cpus[i].lock, true);
|
|---|
| 114 |
|
|---|
| 115 | stats_cpus[i].id = cpus[i].id;
|
|---|
| 116 | stats_cpus[i].active = cpus[i].active;
|
|---|
| 117 | stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
|
|---|
| 118 | stats_cpus[i].busy_cycles = cpus[i].busy_cycles;
|
|---|
| 119 | stats_cpus[i].idle_cycles = cpus[i].idle_cycles;
|
|---|
| 120 |
|
|---|
| 121 | irq_spinlock_unlock(&cpus[i].lock, true);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | return ((void *) stats_cpus);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /** Get the size of a virtual address space
|
|---|
| 128 | *
|
|---|
| 129 | * @param as Address space.
|
|---|
| 130 | *
|
|---|
| 131 | * @return Size of the mapped virtual address space (bytes).
|
|---|
| 132 | *
|
|---|
| 133 | */
|
|---|
| 134 | static size_t get_task_virtmem(as_t *as)
|
|---|
| 135 | {
|
|---|
| 136 | /*
|
|---|
| 137 | * We are holding spinlocks here and therefore are not allowed to
|
|---|
| 138 | * block. Only attempt to lock the address space and address space
|
|---|
| 139 | * area mutexes conditionally. If it is not possible to lock either
|
|---|
| 140 | * object, return inexact statistics by skipping the respective object.
|
|---|
| 141 | */
|
|---|
| 142 |
|
|---|
| 143 | if (mutex_trylock(&as->lock) != EOK)
|
|---|
| 144 | return 0;
|
|---|
| 145 |
|
|---|
| 146 | size_t pages = 0;
|
|---|
| 147 |
|
|---|
| 148 | /* Walk areas in the address space and count pages */
|
|---|
| 149 | as_area_t *area = as_area_first(as);
|
|---|
| 150 | while (area != NULL) {
|
|---|
| 151 | if (mutex_trylock(&area->lock) != EOK)
|
|---|
| 152 | continue;
|
|---|
| 153 |
|
|---|
| 154 | pages += area->pages;
|
|---|
| 155 | mutex_unlock(&area->lock);
|
|---|
| 156 | area = as_area_next(area);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | mutex_unlock(&as->lock);
|
|---|
| 160 |
|
|---|
| 161 | return (pages << PAGE_WIDTH);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /** Get the resident (used) size of a virtual address space
|
|---|
| 165 | *
|
|---|
| 166 | * @param as Address space.
|
|---|
| 167 | *
|
|---|
| 168 | * @return Size of the resident (used) virtual address space (bytes).
|
|---|
| 169 | *
|
|---|
| 170 | */
|
|---|
| 171 | static size_t get_task_resmem(as_t *as)
|
|---|
| 172 | {
|
|---|
| 173 | /*
|
|---|
| 174 | * We are holding spinlocks here and therefore are not allowed to
|
|---|
| 175 | * block. Only attempt to lock the address space and address space
|
|---|
| 176 | * area mutexes conditionally. If it is not possible to lock either
|
|---|
| 177 | * object, return inexact statistics by skipping the respective object.
|
|---|
| 178 | */
|
|---|
| 179 |
|
|---|
| 180 | if (mutex_trylock(&as->lock) != EOK)
|
|---|
| 181 | return 0;
|
|---|
| 182 |
|
|---|
| 183 | size_t pages = 0;
|
|---|
| 184 |
|
|---|
| 185 | /* Walk areas in the address space and count pages */
|
|---|
| 186 | as_area_t *area = as_area_first(as);
|
|---|
| 187 | while (area != NULL) {
|
|---|
| 188 | if (mutex_trylock(&area->lock) != EOK)
|
|---|
| 189 | continue;
|
|---|
| 190 |
|
|---|
| 191 | pages += area->used_space.pages;
|
|---|
| 192 | mutex_unlock(&area->lock);
|
|---|
| 193 | area = as_area_next(area);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | mutex_unlock(&as->lock);
|
|---|
| 197 |
|
|---|
| 198 | return (pages << PAGE_WIDTH);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** Produce task statistics
|
|---|
| 202 | *
|
|---|
| 203 | * Summarize task information into task statistics.
|
|---|
| 204 | *
|
|---|
| 205 | * @param task Task.
|
|---|
| 206 | * @param stats_task Task statistics.
|
|---|
| 207 | *
|
|---|
| 208 | */
|
|---|
| 209 | static void produce_stats_task(task_t *task, stats_task_t *stats_task)
|
|---|
| 210 | {
|
|---|
| 211 | assert(interrupts_disabled());
|
|---|
| 212 | assert(irq_spinlock_locked(&task->lock));
|
|---|
| 213 |
|
|---|
| 214 | stats_task->task_id = task->taskid;
|
|---|
| 215 | str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
|
|---|
| 216 | stats_task->virtmem = get_task_virtmem(task->as);
|
|---|
| 217 | stats_task->resmem = get_task_resmem(task->as);
|
|---|
| 218 | stats_task->threads = atomic_load(&task->refcount);
|
|---|
| 219 | task_get_accounting(task, &(stats_task->ucycles),
|
|---|
| 220 | &(stats_task->kcycles));
|
|---|
| 221 | stats_task->ipc_info = task->ipc_info;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /** Get task statistics
|
|---|
| 225 | *
|
|---|
| 226 | * @param item Sysinfo item (unused).
|
|---|
| 227 | * @param size Size of the returned data.
|
|---|
| 228 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 229 | * @param data Unused.
|
|---|
| 230 | *
|
|---|
| 231 | * @return Data containing several stats_task_t structures.
|
|---|
| 232 | * If the return value is not NULL, it should be freed
|
|---|
| 233 | * in the context of the sysinfo request.
|
|---|
| 234 | */
|
|---|
| 235 | static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
|
|---|
| 236 | bool dry_run, void *data)
|
|---|
| 237 | {
|
|---|
| 238 | /* Messing with task structures, avoid deadlock */
|
|---|
| 239 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 240 |
|
|---|
| 241 | /* Count the tasks */
|
|---|
| 242 | size_t count = task_count();
|
|---|
| 243 |
|
|---|
| 244 | if (count == 0) {
|
|---|
| 245 | /* No tasks found (strange) */
|
|---|
| 246 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 247 | *size = 0;
|
|---|
| 248 | return NULL;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | *size = sizeof(stats_task_t) * count;
|
|---|
| 252 | if (dry_run) {
|
|---|
| 253 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 254 | return NULL;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | stats_task_t *stats_tasks = (stats_task_t *) malloc(*size);
|
|---|
| 258 | if (stats_tasks == NULL) {
|
|---|
| 259 | /* No free space for allocation */
|
|---|
| 260 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 261 | *size = 0;
|
|---|
| 262 | return NULL;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | /* Gather the statistics for each task */
|
|---|
| 266 | size_t i = 0;
|
|---|
| 267 | task_t *task = task_first();
|
|---|
| 268 | while (task != NULL) {
|
|---|
| 269 | /* Interrupts are already disabled */
|
|---|
| 270 | irq_spinlock_lock(&(task->lock), false);
|
|---|
| 271 |
|
|---|
| 272 | /* Record the statistics and increment the index */
|
|---|
| 273 | produce_stats_task(task, &stats_tasks[i]);
|
|---|
| 274 | i++;
|
|---|
| 275 |
|
|---|
| 276 | irq_spinlock_unlock(&(task->lock), false);
|
|---|
| 277 | task = task_next(task);
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 281 |
|
|---|
| 282 | return ((void *) stats_tasks);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /** Produce thread statistics
|
|---|
| 286 | *
|
|---|
| 287 | * Summarize thread information into thread statistics.
|
|---|
| 288 | *
|
|---|
| 289 | * @param thread Thread.
|
|---|
| 290 | * @param stats_thread Thread statistics.
|
|---|
| 291 | *
|
|---|
| 292 | */
|
|---|
| 293 | static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
|
|---|
| 294 | {
|
|---|
| 295 | assert(interrupts_disabled());
|
|---|
| 296 | assert(irq_spinlock_locked(&thread->lock));
|
|---|
| 297 |
|
|---|
| 298 | stats_thread->thread_id = thread->tid;
|
|---|
| 299 | stats_thread->task_id = thread->task->taskid;
|
|---|
| 300 | stats_thread->state = thread->state;
|
|---|
| 301 | stats_thread->priority = thread->priority;
|
|---|
| 302 | stats_thread->ucycles = thread->ucycles;
|
|---|
| 303 | stats_thread->kcycles = thread->kcycles;
|
|---|
| 304 |
|
|---|
| 305 | if (thread->cpu != NULL) {
|
|---|
| 306 | stats_thread->on_cpu = true;
|
|---|
| 307 | stats_thread->cpu = thread->cpu->id;
|
|---|
| 308 | } else
|
|---|
| 309 | stats_thread->on_cpu = false;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | /** Get thread statistics
|
|---|
| 313 | *
|
|---|
| 314 | * @param item Sysinfo item (unused).
|
|---|
| 315 | * @param size Size of the returned data.
|
|---|
| 316 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 317 | * @param data Unused.
|
|---|
| 318 | *
|
|---|
| 319 | * @return Data containing several stats_task_t structures.
|
|---|
| 320 | * If the return value is not NULL, it should be freed
|
|---|
| 321 | * in the context of the sysinfo request.
|
|---|
| 322 | */
|
|---|
| 323 | static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
|
|---|
| 324 | bool dry_run, void *data)
|
|---|
| 325 | {
|
|---|
| 326 | /* Messing with threads structures, avoid deadlock */
|
|---|
| 327 | irq_spinlock_lock(&threads_lock, true);
|
|---|
| 328 |
|
|---|
| 329 | /* Count the threads */
|
|---|
| 330 | size_t count = thread_count();
|
|---|
| 331 |
|
|---|
| 332 | if (count == 0) {
|
|---|
| 333 | /* No threads found (strange) */
|
|---|
| 334 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 335 | *size = 0;
|
|---|
| 336 | return NULL;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | *size = sizeof(stats_thread_t) * count;
|
|---|
| 340 | if (dry_run) {
|
|---|
| 341 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 342 | return NULL;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size);
|
|---|
| 346 | if (stats_threads == NULL) {
|
|---|
| 347 | /* No free space for allocation */
|
|---|
| 348 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 349 | *size = 0;
|
|---|
| 350 | return NULL;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /* Walk tha thread tree again to gather the statistics */
|
|---|
| 354 | size_t i = 0;
|
|---|
| 355 |
|
|---|
| 356 | thread_t *thread = thread_first();
|
|---|
| 357 | while (thread != NULL) {
|
|---|
| 358 | /* Interrupts are already disabled */
|
|---|
| 359 | irq_spinlock_lock(&thread->lock, false);
|
|---|
| 360 |
|
|---|
| 361 | /* Record the statistics and increment the index */
|
|---|
| 362 | produce_stats_thread(thread, &stats_threads[i]);
|
|---|
| 363 | i++;
|
|---|
| 364 |
|
|---|
| 365 | irq_spinlock_unlock(&thread->lock, false);
|
|---|
| 366 |
|
|---|
| 367 | thread = thread_next(thread);
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 371 |
|
|---|
| 372 | return ((void *) stats_threads);
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | /** Get a single task statistics
|
|---|
| 376 | *
|
|---|
| 377 | * Get statistics of a given task. The task ID is passed
|
|---|
| 378 | * as a string (current limitation of the sysinfo interface,
|
|---|
| 379 | * but it is still reasonable for the given purpose).
|
|---|
| 380 | *
|
|---|
| 381 | * @param name Task ID (string-encoded number).
|
|---|
| 382 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 383 | * @param data Unused.
|
|---|
| 384 | *
|
|---|
| 385 | * @return Sysinfo return holder. The type of the returned
|
|---|
| 386 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
|---|
| 387 | * task ID or memory allocation error) or
|
|---|
| 388 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
|---|
| 389 | * generated data should be freed within the
|
|---|
| 390 | * sysinfo request context).
|
|---|
| 391 | *
|
|---|
| 392 | */
|
|---|
| 393 | static sysinfo_return_t get_stats_task(const char *name, bool dry_run,
|
|---|
| 394 | void *data)
|
|---|
| 395 | {
|
|---|
| 396 | /* Initially no return value */
|
|---|
| 397 | sysinfo_return_t ret;
|
|---|
| 398 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 399 |
|
|---|
| 400 | /* Parse the task ID */
|
|---|
| 401 | task_id_t task_id;
|
|---|
| 402 | if (str_uint64_t(name, NULL, 0, true, &task_id) != EOK)
|
|---|
| 403 | return ret;
|
|---|
| 404 |
|
|---|
| 405 | /* Messing with task structures, avoid deadlock */
|
|---|
| 406 | irq_spinlock_lock(&tasks_lock, true);
|
|---|
| 407 |
|
|---|
| 408 | task_t *task = task_find_by_id(task_id);
|
|---|
| 409 | if (task == NULL) {
|
|---|
| 410 | /* No task with this ID */
|
|---|
| 411 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 412 | return ret;
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | if (dry_run) {
|
|---|
| 416 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 417 | ret.data.data = NULL;
|
|---|
| 418 | ret.data.size = sizeof(stats_task_t);
|
|---|
| 419 |
|
|---|
| 420 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 421 | } else {
|
|---|
| 422 | /* Allocate stats_task_t structure */
|
|---|
| 423 | stats_task_t *stats_task =
|
|---|
| 424 | (stats_task_t *) malloc(sizeof(stats_task_t));
|
|---|
| 425 | if (stats_task == NULL) {
|
|---|
| 426 | irq_spinlock_unlock(&tasks_lock, true);
|
|---|
| 427 | return ret;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | /* Correct return value */
|
|---|
| 431 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 432 | ret.data.data = (void *) stats_task;
|
|---|
| 433 | ret.data.size = sizeof(stats_task_t);
|
|---|
| 434 |
|
|---|
| 435 | /* Hand-over-hand locking */
|
|---|
| 436 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
|---|
| 437 |
|
|---|
| 438 | produce_stats_task(task, stats_task);
|
|---|
| 439 |
|
|---|
| 440 | irq_spinlock_unlock(&task->lock, true);
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | return ret;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | /** Get thread statistics
|
|---|
| 447 | *
|
|---|
| 448 | * Get statistics of a given thread. The thread ID is passed
|
|---|
| 449 | * as a string (current limitation of the sysinfo interface,
|
|---|
| 450 | * but it is still reasonable for the given purpose).
|
|---|
| 451 | *
|
|---|
| 452 | * @param name Thread ID (string-encoded number).
|
|---|
| 453 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 454 | * @param data Unused.
|
|---|
| 455 | *
|
|---|
| 456 | * @return Sysinfo return holder. The type of the returned
|
|---|
| 457 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
|---|
| 458 | * thread ID or memory allocation error) or
|
|---|
| 459 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
|---|
| 460 | * generated data should be freed within the
|
|---|
| 461 | * sysinfo request context).
|
|---|
| 462 | *
|
|---|
| 463 | */
|
|---|
| 464 | static sysinfo_return_t get_stats_thread(const char *name, bool dry_run,
|
|---|
| 465 | void *data)
|
|---|
| 466 | {
|
|---|
| 467 | /* Initially no return value */
|
|---|
| 468 | sysinfo_return_t ret;
|
|---|
| 469 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 470 |
|
|---|
| 471 | /* Parse the thread ID */
|
|---|
| 472 | thread_id_t thread_id;
|
|---|
| 473 | if (str_uint64_t(name, NULL, 0, true, &thread_id) != EOK)
|
|---|
| 474 | return ret;
|
|---|
| 475 |
|
|---|
| 476 | /* Messing with threads structures, avoid deadlock */
|
|---|
| 477 | irq_spinlock_lock(&threads_lock, true);
|
|---|
| 478 |
|
|---|
| 479 | thread_t *thread = thread_find_by_id(thread_id);
|
|---|
| 480 | if (thread == NULL) {
|
|---|
| 481 | /* No thread with this ID */
|
|---|
| 482 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 483 | return ret;
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | if (dry_run) {
|
|---|
| 487 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 488 | ret.data.data = NULL;
|
|---|
| 489 | ret.data.size = sizeof(stats_thread_t);
|
|---|
| 490 |
|
|---|
| 491 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 492 | } else {
|
|---|
| 493 | /* Allocate stats_thread_t structure */
|
|---|
| 494 | stats_thread_t *stats_thread =
|
|---|
| 495 | (stats_thread_t *) malloc(sizeof(stats_thread_t));
|
|---|
| 496 | if (stats_thread == NULL) {
|
|---|
| 497 | irq_spinlock_unlock(&threads_lock, true);
|
|---|
| 498 | return ret;
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | /* Correct return value */
|
|---|
| 502 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 503 | ret.data.data = (void *) stats_thread;
|
|---|
| 504 | ret.data.size = sizeof(stats_thread_t);
|
|---|
| 505 |
|
|---|
| 506 | /* Hand-over-hand locking */
|
|---|
| 507 | irq_spinlock_exchange(&threads_lock, &thread->lock);
|
|---|
| 508 |
|
|---|
| 509 | produce_stats_thread(thread, stats_thread);
|
|---|
| 510 |
|
|---|
| 511 | irq_spinlock_unlock(&thread->lock, true);
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | return ret;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | /** Get exceptions statistics
|
|---|
| 518 | *
|
|---|
| 519 | * @param item Sysinfo item (unused).
|
|---|
| 520 | * @param size Size of the returned data.
|
|---|
| 521 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 522 | * @param data Unused.
|
|---|
| 523 | *
|
|---|
| 524 | * @return Data containing several stats_exc_t structures.
|
|---|
| 525 | * If the return value is not NULL, it should be freed
|
|---|
| 526 | * in the context of the sysinfo request.
|
|---|
| 527 | */
|
|---|
| 528 | static void *get_stats_exceptions(struct sysinfo_item *item, size_t *size,
|
|---|
| 529 | bool dry_run, void *data)
|
|---|
| 530 | {
|
|---|
| 531 | *size = sizeof(stats_exc_t) * IVT_ITEMS;
|
|---|
| 532 |
|
|---|
| 533 | if ((dry_run) || (IVT_ITEMS == 0))
|
|---|
| 534 | return NULL;
|
|---|
| 535 |
|
|---|
| 536 | stats_exc_t *stats_exceptions =
|
|---|
| 537 | (stats_exc_t *) malloc(*size);
|
|---|
| 538 | if (stats_exceptions == NULL) {
|
|---|
| 539 | /* No free space for allocation */
|
|---|
| 540 | *size = 0;
|
|---|
| 541 | return NULL;
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | #if (IVT_ITEMS > 0)
|
|---|
| 545 | /* Messing with exception table, avoid deadlock */
|
|---|
| 546 | irq_spinlock_lock(&exctbl_lock, true);
|
|---|
| 547 |
|
|---|
| 548 | unsigned int i;
|
|---|
| 549 | for (i = 0; i < IVT_ITEMS; i++) {
|
|---|
| 550 | stats_exceptions[i].id = i + IVT_FIRST;
|
|---|
| 551 | str_cpy(stats_exceptions[i].desc, EXC_NAME_BUFLEN, exc_table[i].name);
|
|---|
| 552 | stats_exceptions[i].hot = exc_table[i].hot;
|
|---|
| 553 | stats_exceptions[i].cycles = exc_table[i].cycles;
|
|---|
| 554 | stats_exceptions[i].count = exc_table[i].count;
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | irq_spinlock_unlock(&exctbl_lock, true);
|
|---|
| 558 | #endif
|
|---|
| 559 |
|
|---|
| 560 | return ((void *) stats_exceptions);
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | /** Get exception statistics
|
|---|
| 564 | *
|
|---|
| 565 | * Get statistics of a given exception. The exception number
|
|---|
| 566 | * is passed as a string (current limitation of the sysinfo
|
|---|
| 567 | * interface, but it is still reasonable for the given purpose).
|
|---|
| 568 | *
|
|---|
| 569 | * @param name Exception number (string-encoded number).
|
|---|
| 570 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 571 | * @param data Unused.
|
|---|
| 572 | *
|
|---|
| 573 | * @return Sysinfo return holder. The type of the returned
|
|---|
| 574 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
|---|
| 575 | * exception number or memory allocation error) or
|
|---|
| 576 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
|---|
| 577 | * generated data should be freed within the
|
|---|
| 578 | * sysinfo request context).
|
|---|
| 579 | *
|
|---|
| 580 | */
|
|---|
| 581 | static sysinfo_return_t get_stats_exception(const char *name, bool dry_run,
|
|---|
| 582 | void *data)
|
|---|
| 583 | {
|
|---|
| 584 | /* Initially no return value */
|
|---|
| 585 | sysinfo_return_t ret;
|
|---|
| 586 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 587 |
|
|---|
| 588 | /* Parse the exception number */
|
|---|
| 589 | uint64_t excn;
|
|---|
| 590 | if (str_uint64_t(name, NULL, 0, true, &excn) != EOK)
|
|---|
| 591 | return ret;
|
|---|
| 592 |
|
|---|
| 593 | #if (IVT_FIRST > 0)
|
|---|
| 594 | if (excn < IVT_FIRST)
|
|---|
| 595 | return ret;
|
|---|
| 596 | #endif
|
|---|
| 597 |
|
|---|
| 598 | #if (IVT_ITEMS + IVT_FIRST == 0)
|
|---|
| 599 | return ret;
|
|---|
| 600 | #else
|
|---|
| 601 | if (excn >= IVT_ITEMS + IVT_FIRST)
|
|---|
| 602 | return ret;
|
|---|
| 603 | #endif
|
|---|
| 604 |
|
|---|
| 605 | if (dry_run) {
|
|---|
| 606 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 607 | ret.data.data = NULL;
|
|---|
| 608 | ret.data.size = sizeof(stats_thread_t);
|
|---|
| 609 | } else {
|
|---|
| 610 | /* Update excn index for accessing exc_table */
|
|---|
| 611 | excn -= IVT_FIRST;
|
|---|
| 612 |
|
|---|
| 613 | /* Allocate stats_exc_t structure */
|
|---|
| 614 | stats_exc_t *stats_exception =
|
|---|
| 615 | (stats_exc_t *) malloc(sizeof(stats_exc_t));
|
|---|
| 616 | if (stats_exception == NULL)
|
|---|
| 617 | return ret;
|
|---|
| 618 |
|
|---|
| 619 | /* Messing with exception table, avoid deadlock */
|
|---|
| 620 | irq_spinlock_lock(&exctbl_lock, true);
|
|---|
| 621 |
|
|---|
| 622 | /* Correct return value */
|
|---|
| 623 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| 624 | ret.data.data = (void *) stats_exception;
|
|---|
| 625 | ret.data.size = sizeof(stats_exc_t);
|
|---|
| 626 |
|
|---|
| 627 | stats_exception->id = excn;
|
|---|
| 628 | str_cpy(stats_exception->desc, EXC_NAME_BUFLEN, exc_table[excn].name);
|
|---|
| 629 | stats_exception->hot = exc_table[excn].hot;
|
|---|
| 630 | stats_exception->cycles = exc_table[excn].cycles;
|
|---|
| 631 | stats_exception->count = exc_table[excn].count;
|
|---|
| 632 |
|
|---|
| 633 | irq_spinlock_unlock(&exctbl_lock, true);
|
|---|
| 634 | }
|
|---|
| 635 |
|
|---|
| 636 | return ret;
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | /** Get physical memory statistics
|
|---|
| 640 | *
|
|---|
| 641 | * @param item Sysinfo item (unused).
|
|---|
| 642 | * @param size Size of the returned data.
|
|---|
| 643 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 644 | * @param data Unused.
|
|---|
| 645 | *
|
|---|
| 646 | * @return Data containing stats_physmem_t.
|
|---|
| 647 | * If the return value is not NULL, it should be freed
|
|---|
| 648 | * in the context of the sysinfo request.
|
|---|
| 649 | */
|
|---|
| 650 | static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
|
|---|
| 651 | bool dry_run, void *data)
|
|---|
| 652 | {
|
|---|
| 653 | *size = sizeof(stats_physmem_t);
|
|---|
| 654 | if (dry_run)
|
|---|
| 655 | return NULL;
|
|---|
| 656 |
|
|---|
| 657 | stats_physmem_t *stats_physmem =
|
|---|
| 658 | (stats_physmem_t *) malloc(*size);
|
|---|
| 659 | if (stats_physmem == NULL) {
|
|---|
| 660 | *size = 0;
|
|---|
| 661 | return NULL;
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
|
|---|
| 665 | &(stats_physmem->used), &(stats_physmem->free));
|
|---|
| 666 |
|
|---|
| 667 | return ((void *) stats_physmem);
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | /** Get system load
|
|---|
| 671 | *
|
|---|
| 672 | * @param item Sysinfo item (unused).
|
|---|
| 673 | * @param size Size of the returned data.
|
|---|
| 674 | * @param dry_run Do not get the data, just calculate the size.
|
|---|
| 675 | * @param data Unused.
|
|---|
| 676 | *
|
|---|
| 677 | * @return Data several load_t values.
|
|---|
| 678 | * If the return value is not NULL, it should be freed
|
|---|
| 679 | * in the context of the sysinfo request.
|
|---|
| 680 | */
|
|---|
| 681 | static void *get_stats_load(struct sysinfo_item *item, size_t *size,
|
|---|
| 682 | bool dry_run, void *data)
|
|---|
| 683 | {
|
|---|
| 684 | *size = sizeof(load_t) * LOAD_STEPS;
|
|---|
| 685 | if (dry_run)
|
|---|
| 686 | return NULL;
|
|---|
| 687 |
|
|---|
| 688 | load_t *stats_load = (load_t *) malloc(*size);
|
|---|
| 689 | if (stats_load == NULL) {
|
|---|
| 690 | *size = 0;
|
|---|
| 691 | return NULL;
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | /* To always get consistent values acquire the mutex */
|
|---|
| 695 | mutex_lock(&load_lock);
|
|---|
| 696 |
|
|---|
| 697 | unsigned int i;
|
|---|
| 698 | for (i = 0; i < LOAD_STEPS; i++)
|
|---|
| 699 | stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
|
|---|
| 700 |
|
|---|
| 701 | mutex_unlock(&load_lock);
|
|---|
| 702 |
|
|---|
| 703 | return ((void *) stats_load);
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | /** Calculate load
|
|---|
| 707 | *
|
|---|
| 708 | */
|
|---|
| 709 | static inline load_t load_calc(load_t load, load_t exp, size_t ready)
|
|---|
| 710 | {
|
|---|
| 711 | load *= exp;
|
|---|
| 712 | load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
|
|---|
| 713 |
|
|---|
| 714 | return (load >> LOAD_FIXED_SHIFT);
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | /** Load computation thread.
|
|---|
| 718 | *
|
|---|
| 719 | * Compute system load every few seconds.
|
|---|
| 720 | *
|
|---|
| 721 | * @param arg Unused.
|
|---|
| 722 | *
|
|---|
| 723 | */
|
|---|
| 724 | void kload(void *arg)
|
|---|
| 725 | {
|
|---|
| 726 | thread_detach(THREAD);
|
|---|
| 727 |
|
|---|
| 728 | while (true) {
|
|---|
| 729 | size_t ready = atomic_load(&nrdy);
|
|---|
| 730 |
|
|---|
| 731 | /* Mutually exclude with get_stats_load() */
|
|---|
| 732 | mutex_lock(&load_lock);
|
|---|
| 733 |
|
|---|
| 734 | unsigned int i;
|
|---|
| 735 | for (i = 0; i < LOAD_STEPS; i++)
|
|---|
| 736 | avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
|
|---|
| 737 |
|
|---|
| 738 | mutex_unlock(&load_lock);
|
|---|
| 739 |
|
|---|
| 740 | thread_sleep(LOAD_INTERVAL);
|
|---|
| 741 | }
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | /** Register sysinfo statistical items
|
|---|
| 745 | *
|
|---|
| 746 | */
|
|---|
| 747 | void stats_init(void)
|
|---|
| 748 | {
|
|---|
| 749 | mutex_initialize(&load_lock, MUTEX_PASSIVE);
|
|---|
| 750 |
|
|---|
| 751 | sysinfo_set_item_gen_data("system.cpus", NULL, get_stats_cpus, NULL);
|
|---|
| 752 | sysinfo_set_item_gen_data("system.physmem", NULL, get_stats_physmem, NULL);
|
|---|
| 753 | sysinfo_set_item_gen_data("system.load", NULL, get_stats_load, NULL);
|
|---|
| 754 | sysinfo_set_item_gen_data("system.tasks", NULL, get_stats_tasks, NULL);
|
|---|
| 755 | sysinfo_set_item_gen_data("system.threads", NULL, get_stats_threads, NULL);
|
|---|
| 756 | sysinfo_set_item_gen_data("system.exceptions", NULL, get_stats_exceptions, NULL);
|
|---|
| 757 | sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task, NULL);
|
|---|
| 758 | sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread, NULL);
|
|---|
| 759 | sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception, NULL);
|
|---|
| 760 | }
|
|---|
| 761 |
|
|---|
| 762 | /** @}
|
|---|
| 763 | */
|
|---|