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