source: mainline/kernel/generic/src/sysinfo/stats.c@ 2d3ddad

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2d3ddad was 1d432f9, checked in by Jakub Jermar <jakub@…>, 16 years ago

Reflect assumptions about lock and interrupt state in functions themselves.

  • Property mode set to 100644
File size: 17.9 KB
Line 
1/*
2 * Copyright (c) 2010 Stanislav Kozina
3 * Copyright (c) 2010 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup generic
31 * @{
32 */
33/** @file
34 */
35
36#include <typedefs.h>
37#include <sysinfo/abi.h>
38#include <sysinfo/stats.h>
39#include <sysinfo/sysinfo.h>
40#include <synch/spinlock.h>
41#include <synch/mutex.h>
42#include <time/clock.h>
43#include <mm/frame.h>
44#include <proc/task.h>
45#include <proc/thread.h>
46#include <str.h>
47#include <errno.h>
48#include <cpu.h>
49#include <arch.h>
50
51/** Bits of fixed-point precision for load */
52#define LOAD_FIXED_SHIFT 11
53
54/** 1.0 as fixed-point for load */
55#define LOAD_FIXED_1 (1 << LOAD_FIXED_SHIFT)
56
57/** Compute load in 5 second intervals */
58#define LOAD_INTERVAL 5
59
60/** Fixed-point representation of
61 *
62 * 1 / exp(5 sec / 1 min)
63 * 1 / exp(5 sec / 5 min)
64 * 1 / exp(5 sec / 15 min)
65 *
66 */
67static load_t load_exp[LOAD_STEPS] = {1884, 2014, 2037};
68
69/** Running average of the number of ready threads */
70static load_t avenrdy[LOAD_STEPS] = {0, 0, 0};
71
72/** Load calculation lock */
73static mutex_t load_lock;
74
75/** Get system uptime
76 *
77 * @param item Sysinfo item (unused).
78 *
79 * @return System uptime (in secords).
80 *
81 */
82static unative_t get_stats_uptime(struct sysinfo_item *item)
83{
84 /* This doesn't have to be very accurate */
85 return uptime->seconds1;
86}
87
88/** Get statistics of all CPUs
89 *
90 * @param item Sysinfo item (unused).
91 * @param size Size of the returned data.
92 * @param dry_run Do not get the data, just calculate the size.
93 *
94 * @return Data containing several stats_cpu_t structures.
95 * If the return value is not NULL, it should be freed
96 * in the context of the sysinfo request.
97 */
98static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
99 bool dry_run)
100{
101 *size = sizeof(stats_cpu_t) * config.cpu_count;
102 if (dry_run)
103 return NULL;
104
105 /* Assumption: config.cpu_count is constant */
106 stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size, FRAME_ATOMIC);
107 if (stats_cpus == NULL) {
108 *size = 0;
109 return NULL;
110 }
111
112 size_t i;
113 for (i = 0; i < config.cpu_count; i++) {
114 irq_spinlock_lock(&cpus[i].lock, true);
115
116 stats_cpus[i].id = cpus[i].id;
117 stats_cpus[i].active = cpus[i].active;
118 stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
119 stats_cpus[i].busy_ticks = cpus[i].busy_ticks;
120 stats_cpus[i].idle_ticks = cpus[i].idle_ticks;
121
122 irq_spinlock_unlock(&cpus[i].lock, true);
123 }
124
125 return ((void *) stats_cpus);
126}
127
128/** Count number of nodes in an AVL tree
129 *
130 * AVL tree walker for counting nodes.
131 *
132 * @param node AVL tree node (unused).
133 * @param arg Pointer to the counter (size_t).
134 *
135 * @param Always true (continue the walk).
136 *
137 */
138static bool avl_count_walker(avltree_node_t *node, void *arg)
139{
140 size_t *count = (size_t *) arg;
141 (*count)++;
142
143 return true;
144}
145
146/** Get the size of a virtual address space
147 *
148 * @param as Address space.
149 *
150 * @return Size of the mapped virtual address space (bytes).
151 *
152 */
153static size_t get_task_virtmem(as_t *as)
154{
155 size_t result = 0;
156
157 /*
158 * We are holding some spinlocks here and therefore are not allowed to
159 * block. Only attempt to lock the address space and address space area
160 * mutexes conditionally. If it is not possible to lock either object,
161 * allow the statistics to be inexact by skipping the respective object.
162 *
163 * Note that it may be infinitely better to let the address space
164 * management code compute these statistics as it proceeds instead of
165 * having them calculated here over and over again here.
166 */
167
168 if (SYNCH_FAILED(mutex_trylock(&as->lock)))
169 return result * PAGE_SIZE;
170
171 /* Walk the B+ tree and count pages */
172 link_t *cur;
173 for (cur = as->as_area_btree.leaf_head.next;
174 cur != &as->as_area_btree.leaf_head; cur = cur->next) {
175 btree_node_t *node =
176 list_get_instance(cur, btree_node_t, leaf_link);
177
178 unsigned int i;
179 for (i = 0; i < node->keys; i++) {
180 as_area_t *area = node->value[i];
181
182 if (SYNCH_FAILED(mutex_trylock(&area->lock)))
183 continue;
184 result += area->pages;
185 mutex_unlock(&area->lock);
186 }
187 }
188
189 mutex_unlock(&as->lock);
190
191 return result * PAGE_SIZE;
192}
193
194/* Produce task statistics
195 *
196 * Summarize task information into task statistics.
197 * Task lock should be held and interrupts disabled
198 * before executing this function.
199 *
200 * @param task Task.
201 * @param stats_task Task statistics.
202 *
203 */
204static void produce_stats_task(task_t *task, stats_task_t *stats_task)
205{
206 ASSERT(interrupts_disabled());
207 ASSERT(irq_spinlock_locked(&task->lock));
208
209 stats_task->task_id = task->taskid;
210 str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
211 stats_task->virtmem = get_task_virtmem(task->as);
212 stats_task->threads = atomic_get(&task->refcount);
213 task_get_accounting(task, &(stats_task->ucycles),
214 &(stats_task->kcycles));
215 stats_task->ipc_info = task->ipc_info;
216}
217
218/** Gather statistics of all tasks
219 *
220 * AVL task tree walker for gathering task statistics. Interrupts should
221 * be already disabled while walking the tree.
222 *
223 * @param node AVL task tree node.
224 * @param arg Pointer to the iterator into the array of stats_task_t.
225 *
226 * @param Always true (continue the walk).
227 *
228 */
229static bool task_serialize_walker(avltree_node_t *node, void *arg)
230{
231 stats_task_t **iterator = (stats_task_t **) arg;
232 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
233
234 /* Interrupts are already disabled */
235 irq_spinlock_lock(&(task->lock), false);
236
237 /* Record the statistics and increment the iterator */
238 produce_stats_task(task, *iterator);
239 (*iterator)++;
240
241 irq_spinlock_unlock(&(task->lock), false);
242
243 return true;
244}
245
246/** Get task statistics
247 *
248 * @param item Sysinfo item (unused).
249 * @param size Size of the returned data.
250 * @param dry_run Do not get the data, just calculate the size.
251 *
252 * @return Data containing several stats_task_t structures.
253 * If the return value is not NULL, it should be freed
254 * in the context of the sysinfo request.
255 */
256static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
257 bool dry_run)
258{
259 /* Messing with task structures, avoid deadlock */
260 irq_spinlock_lock(&tasks_lock, true);
261
262 /* First walk the task tree to count the tasks */
263 size_t count = 0;
264 avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
265
266 if (count == 0) {
267 /* No tasks found (strange) */
268 irq_spinlock_unlock(&tasks_lock, true);
269 *size = 0;
270 return NULL;
271 }
272
273 *size = sizeof(stats_task_t) * count;
274 if (dry_run) {
275 irq_spinlock_unlock(&tasks_lock, true);
276 return NULL;
277 }
278
279 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
280 if (stats_tasks == NULL) {
281 /* No free space for allocation */
282 irq_spinlock_unlock(&tasks_lock, true);
283 *size = 0;
284 return NULL;
285 }
286
287 /* Walk tha task tree again to gather the statistics */
288 stats_task_t *iterator = stats_tasks;
289 avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
290
291 irq_spinlock_unlock(&tasks_lock, true);
292
293 return ((void *) stats_tasks);
294}
295
296/* Produce thread statistics
297 *
298 * Summarize thread information into thread statistics.
299 * Thread lock should be held and interrupts disabled
300 * before executing this function.
301 *
302 * @param thread Thread.
303 * @param stats_thread Thread statistics.
304 *
305 */
306static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
307{
308 ASSERT(interrupts_disabled());
309 ASSERT(irq_spinlock_locked(&thread->lock));
310
311 stats_thread->thread_id = thread->tid;
312 stats_thread->task_id = thread->task->taskid;
313 stats_thread->state = thread->state;
314 stats_thread->priority = thread->priority;
315 stats_thread->ucycles = thread->ucycles;
316 stats_thread->kcycles = thread->kcycles;
317
318 if (thread->cpu != NULL) {
319 stats_thread->on_cpu = true;
320 stats_thread->cpu = thread->cpu->id;
321 } else
322 stats_thread->on_cpu = false;
323}
324
325/** Gather statistics of all threads
326 *
327 * AVL three tree walker for gathering thread statistics. Interrupts should
328 * be already disabled while walking the tree.
329 *
330 * @param node AVL thread tree node.
331 * @param arg Pointer to the iterator into the array of thread statistics.
332 *
333 * @param Always true (continue the walk).
334 *
335 */
336static bool thread_serialize_walker(avltree_node_t *node, void *arg)
337{
338 stats_thread_t **iterator = (stats_thread_t **) arg;
339 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
340
341 /* Interrupts are already disabled */
342 irq_spinlock_lock(&thread->lock, false);
343
344 /* Record the statistics and increment the iterator */
345 produce_stats_thread(thread, *iterator);
346 (*iterator)++;
347
348 irq_spinlock_unlock(&thread->lock, false);
349
350 return true;
351}
352
353/** Get thread statistics
354 *
355 * @param item Sysinfo item (unused).
356 * @param size Size of the returned data.
357 * @param dry_run Do not get the data, just calculate the size.
358 *
359 * @return Data containing several stats_task_t structures.
360 * If the return value is not NULL, it should be freed
361 * in the context of the sysinfo request.
362 */
363static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
364 bool dry_run)
365{
366 /* Messing with threads structures, avoid deadlock */
367 irq_spinlock_lock(&threads_lock, true);
368
369 /* First walk the thread tree to count the threads */
370 size_t count = 0;
371 avltree_walk(&threads_tree, avl_count_walker, (void *) &count);
372
373 if (count == 0) {
374 /* No threads found (strange) */
375 irq_spinlock_unlock(&threads_lock, true);
376 *size = 0;
377 return NULL;
378 }
379
380 *size = sizeof(stats_thread_t) * count;
381 if (dry_run) {
382 irq_spinlock_unlock(&threads_lock, true);
383 return NULL;
384 }
385
386 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
387 if (stats_threads == NULL) {
388 /* No free space for allocation */
389 irq_spinlock_unlock(&threads_lock, true);
390 *size = 0;
391 return NULL;
392 }
393
394 /* Walk tha thread tree again to gather the statistics */
395 stats_thread_t *iterator = stats_threads;
396 avltree_walk(&threads_tree, thread_serialize_walker, (void *) &iterator);
397
398 irq_spinlock_unlock(&threads_lock, true);
399
400 return ((void *) stats_threads);
401}
402
403/** Get a single task statistics
404 *
405 * Get statistics of a given task. The task ID is passed
406 * as a string (current limitation of the sysinfo interface,
407 * but it is still reasonable for the given purpose).
408 *
409 * @param name Task ID (string-encoded number).
410 * @param dry_run Do not get the data, just calculate the size.
411 *
412 * @return Sysinfo return holder. The type of the returned
413 * data is either SYSINFO_VAL_UNDEFINED (unknown
414 * task ID or memory allocation error) or
415 * SYSINFO_VAL_FUNCTION_DATA (in that case the
416 * generated data should be freed within the
417 * sysinfo request context).
418 *
419 */
420static sysinfo_return_t get_stats_task(const char *name, bool dry_run)
421{
422 /* Initially no return value */
423 sysinfo_return_t ret;
424 ret.tag = SYSINFO_VAL_UNDEFINED;
425
426 /* Parse the task ID */
427 task_id_t task_id;
428 if (str_uint64(name, NULL, 0, true, &task_id) != EOK)
429 return ret;
430
431 /* Messing with task structures, avoid deadlock */
432 irq_spinlock_lock(&tasks_lock, true);
433
434 task_t *task = task_find_by_id(task_id);
435 if (task == NULL) {
436 /* No task with this ID */
437 irq_spinlock_unlock(&tasks_lock, true);
438 return ret;
439 }
440
441 if (dry_run) {
442 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
443 ret.data.data = NULL;
444 ret.data.size = sizeof(stats_task_t);
445
446 irq_spinlock_unlock(&tasks_lock, true);
447 } else {
448 /* Allocate stats_task_t structure */
449 stats_task_t *stats_task =
450 (stats_task_t *) malloc(sizeof(stats_task_t), FRAME_ATOMIC);
451 if (stats_task == NULL) {
452 irq_spinlock_unlock(&tasks_lock, true);
453 return ret;
454 }
455
456 /* Correct return value */
457 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
458 ret.data.data = (void *) stats_task;
459 ret.data.size = sizeof(stats_task_t);
460
461 /* Hand-over-hand locking */
462 irq_spinlock_exchange(&tasks_lock, &task->lock);
463
464 produce_stats_task(task, stats_task);
465
466 irq_spinlock_unlock(&task->lock, true);
467 }
468
469 return ret;
470}
471
472/** Get thread statistics
473 *
474 * Get statistics of a given thread. The thread ID is passed
475 * as a string (current limitation of the sysinfo interface,
476 * but it is still reasonable for the given purpose).
477 *
478 * @param name Thread ID (string-encoded number).
479 * @param dry_run Do not get the data, just calculate the size.
480 *
481 * @return Sysinfo return holder. The type of the returned
482 * data is either SYSINFO_VAL_UNDEFINED (unknown
483 * thread ID or memory allocation error) or
484 * SYSINFO_VAL_FUNCTION_DATA (in that case the
485 * generated data should be freed within the
486 * sysinfo request context).
487 *
488 */
489static sysinfo_return_t get_stats_thread(const char *name, bool dry_run)
490{
491 /* Initially no return value */
492 sysinfo_return_t ret;
493 ret.tag = SYSINFO_VAL_UNDEFINED;
494
495 /* Parse the thread ID */
496 thread_id_t thread_id;
497 if (str_uint64(name, NULL, 0, true, &thread_id) != EOK)
498 return ret;
499
500 /* Messing with threads structures, avoid deadlock */
501 irq_spinlock_lock(&threads_lock, true);
502
503 thread_t *thread = thread_find_by_id(thread_id);
504 if (thread == NULL) {
505 /* No thread with this ID */
506 irq_spinlock_unlock(&threads_lock, true);
507 return ret;
508 }
509
510 if (dry_run) {
511 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
512 ret.data.data = NULL;
513 ret.data.size = sizeof(stats_thread_t);
514
515 irq_spinlock_unlock(&threads_lock, true);
516 } else {
517 /* Allocate stats_thread_t structure */
518 stats_thread_t *stats_thread =
519 (stats_thread_t *) malloc(sizeof(stats_thread_t), FRAME_ATOMIC);
520 if (stats_thread == NULL) {
521 irq_spinlock_unlock(&threads_lock, true);
522 return ret;
523 }
524
525 /* Correct return value */
526 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
527 ret.data.data = (void *) stats_thread;
528 ret.data.size = sizeof(stats_thread_t);
529
530 /* Hand-over-hand locking */
531 irq_spinlock_exchange(&threads_lock, &thread->lock);
532
533 produce_stats_thread(thread, stats_thread);
534
535 irq_spinlock_unlock(&thread->lock, true);
536 }
537
538 return ret;
539}
540
541/** Get physical memory statistics
542 *
543 * @param item Sysinfo item (unused).
544 * @param size Size of the returned data.
545 * @param dry_run Do not get the data, just calculate the size.
546 *
547 * @return Data containing stats_physmem_t.
548 * If the return value is not NULL, it should be freed
549 * in the context of the sysinfo request.
550 */
551static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
552 bool dry_run)
553{
554 *size = sizeof(stats_physmem_t);
555 if (dry_run)
556 return NULL;
557
558 stats_physmem_t *stats_physmem =
559 (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
560 if (stats_physmem == NULL) {
561 *size = 0;
562 return NULL;
563 }
564
565 zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
566 &(stats_physmem->used), &(stats_physmem->free));
567
568 return ((void *) stats_physmem);
569}
570
571/** Get system load
572 *
573 * @param item Sysinfo item (unused).
574 * @param size Size of the returned data.
575 * @param dry_run Do not get the data, just calculate the size.
576 *
577 * @return Data several load_t values.
578 * If the return value is not NULL, it should be freed
579 * in the context of the sysinfo request.
580 */
581static void *get_stats_load(struct sysinfo_item *item, size_t *size,
582 bool dry_run)
583{
584 *size = sizeof(load_t) * LOAD_STEPS;
585 if (dry_run)
586 return NULL;
587
588 load_t *stats_load = (load_t *) malloc(*size, FRAME_ATOMIC);
589 if (stats_load == NULL) {
590 *size = 0;
591 return NULL;
592 }
593
594 /* To always get consistent values acquire the mutex */
595 mutex_lock(&load_lock);
596
597 unsigned int i;
598 for (i = 0; i < LOAD_STEPS; i++)
599 stats_load[i] = avenrdy[i] << LOAD_FIXED_SHIFT;
600
601 mutex_unlock(&load_lock);
602
603 return ((void *) stats_load);
604}
605
606/** Calculate load
607 *
608 */
609static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
610{
611 load *= exp;
612 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
613
614 return (load >> LOAD_FIXED_SHIFT);
615}
616
617/** Load computation thread.
618 *
619 * Compute system load every few seconds.
620 *
621 * @param arg Unused.
622 *
623 */
624void kload(void *arg)
625{
626 thread_detach(THREAD);
627
628 while (true) {
629 atomic_count_t ready = atomic_get(&nrdy);
630
631 /* Mutually exclude with get_stats_load() */
632 mutex_lock(&load_lock);
633
634 unsigned int i;
635 for (i = 0; i < LOAD_STEPS; i++)
636 avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
637
638 mutex_unlock(&load_lock);
639
640 thread_sleep(LOAD_INTERVAL);
641 }
642}
643
644/** Register sysinfo statistical items
645 *
646 */
647void stats_init(void)
648{
649 mutex_initialize(&load_lock, MUTEX_PASSIVE);
650
651 sysinfo_set_item_fn_val("system.uptime", NULL, get_stats_uptime);
652 sysinfo_set_item_fn_data("system.cpus", NULL, get_stats_cpus);
653 sysinfo_set_item_fn_data("system.physmem", NULL, get_stats_physmem);
654 sysinfo_set_item_fn_data("system.load", NULL, get_stats_load);
655 sysinfo_set_item_fn_data("system.tasks", NULL, get_stats_tasks);
656 sysinfo_set_item_fn_data("system.threads", NULL, get_stats_threads);
657 sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task);
658 sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread);
659}
660
661/** @}
662 */
Note: See TracBrowser for help on using the repository browser.