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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ada559c was da1bafb, checked in by Martin Decky <martin@…>, 15 years ago

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 17.7 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 stats_task->task_id = task->taskid;
207 str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
208 stats_task->virtmem = get_task_virtmem(task->as);
209 stats_task->threads = atomic_get(&task->refcount);
210 task_get_accounting(task, &(stats_task->ucycles),
211 &(stats_task->kcycles));
212 stats_task->ipc_info = task->ipc_info;
213}
214
215/** Gather statistics of all tasks
216 *
217 * AVL task tree walker for gathering task statistics. Interrupts should
218 * be already disabled while walking the tree.
219 *
220 * @param node AVL task tree node.
221 * @param arg Pointer to the iterator into the array of stats_task_t.
222 *
223 * @param Always true (continue the walk).
224 *
225 */
226static bool task_serialize_walker(avltree_node_t *node, void *arg)
227{
228 stats_task_t **iterator = (stats_task_t **) arg;
229 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
230
231 /* Interrupts are already disabled */
232 irq_spinlock_lock(&(task->lock), false);
233
234 /* Record the statistics and increment the iterator */
235 produce_stats_task(task, *iterator);
236 (*iterator)++;
237
238 irq_spinlock_unlock(&(task->lock), false);
239
240 return true;
241}
242
243/** Get task statistics
244 *
245 * @param item Sysinfo item (unused).
246 * @param size Size of the returned data.
247 * @param dry_run Do not get the data, just calculate the size.
248 *
249 * @return Data containing several stats_task_t structures.
250 * If the return value is not NULL, it should be freed
251 * in the context of the sysinfo request.
252 */
253static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
254 bool dry_run)
255{
256 /* Messing with task structures, avoid deadlock */
257 irq_spinlock_lock(&tasks_lock, true);
258
259 /* First walk the task tree to count the tasks */
260 size_t count = 0;
261 avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
262
263 if (count == 0) {
264 /* No tasks found (strange) */
265 irq_spinlock_unlock(&tasks_lock, true);
266 *size = 0;
267 return NULL;
268 }
269
270 *size = sizeof(stats_task_t) * count;
271 if (dry_run) {
272 irq_spinlock_unlock(&tasks_lock, true);
273 return NULL;
274 }
275
276 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
277 if (stats_tasks == NULL) {
278 /* No free space for allocation */
279 irq_spinlock_unlock(&tasks_lock, true);
280 *size = 0;
281 return NULL;
282 }
283
284 /* Walk tha task tree again to gather the statistics */
285 stats_task_t *iterator = stats_tasks;
286 avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
287
288 irq_spinlock_unlock(&tasks_lock, true);
289
290 return ((void *) stats_tasks);
291}
292
293/* Produce thread statistics
294 *
295 * Summarize thread information into thread statistics.
296 * Thread lock should be held and interrupts disabled
297 * before executing this function.
298 *
299 * @param thread Thread.
300 * @param stats_thread Thread statistics.
301 *
302 */
303static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
304{
305 stats_thread->thread_id = thread->tid;
306 stats_thread->task_id = thread->task->taskid;
307 stats_thread->state = thread->state;
308 stats_thread->priority = thread->priority;
309 stats_thread->ucycles = thread->ucycles;
310 stats_thread->kcycles = thread->kcycles;
311
312 if (thread->cpu != NULL) {
313 stats_thread->on_cpu = true;
314 stats_thread->cpu = thread->cpu->id;
315 } else
316 stats_thread->on_cpu = false;
317}
318
319/** Gather statistics of all threads
320 *
321 * AVL three tree walker for gathering thread statistics. Interrupts should
322 * be already disabled while walking the tree.
323 *
324 * @param node AVL thread tree node.
325 * @param arg Pointer to the iterator into the array of thread statistics.
326 *
327 * @param Always true (continue the walk).
328 *
329 */
330static bool thread_serialize_walker(avltree_node_t *node, void *arg)
331{
332 stats_thread_t **iterator = (stats_thread_t **) arg;
333 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
334
335 /* Interrupts are already disabled */
336 irq_spinlock_lock(&thread->lock, false);
337
338 /* Record the statistics and increment the iterator */
339 produce_stats_thread(thread, *iterator);
340 (*iterator)++;
341
342 irq_spinlock_unlock(&thread->lock, false);
343
344 return true;
345}
346
347/** Get thread statistics
348 *
349 * @param item Sysinfo item (unused).
350 * @param size Size of the returned data.
351 * @param dry_run Do not get the data, just calculate the size.
352 *
353 * @return Data containing several stats_task_t structures.
354 * If the return value is not NULL, it should be freed
355 * in the context of the sysinfo request.
356 */
357static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
358 bool dry_run)
359{
360 /* Messing with threads structures, avoid deadlock */
361 irq_spinlock_lock(&threads_lock, true);
362
363 /* First walk the thread tree to count the threads */
364 size_t count = 0;
365 avltree_walk(&threads_tree, avl_count_walker, (void *) &count);
366
367 if (count == 0) {
368 /* No threads found (strange) */
369 irq_spinlock_unlock(&threads_lock, true);
370 *size = 0;
371 return NULL;
372 }
373
374 *size = sizeof(stats_thread_t) * count;
375 if (dry_run) {
376 irq_spinlock_unlock(&threads_lock, true);
377 return NULL;
378 }
379
380 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
381 if (stats_threads == NULL) {
382 /* No free space for allocation */
383 irq_spinlock_unlock(&threads_lock, true);
384 *size = 0;
385 return NULL;
386 }
387
388 /* Walk tha thread tree again to gather the statistics */
389 stats_thread_t *iterator = stats_threads;
390 avltree_walk(&threads_tree, thread_serialize_walker, (void *) &iterator);
391
392 irq_spinlock_unlock(&threads_lock, true);
393
394 return ((void *) stats_threads);
395}
396
397/** Get a single task statistics
398 *
399 * Get statistics of a given task. The task ID is passed
400 * as a string (current limitation of the sysinfo interface,
401 * but it is still reasonable for the given purpose).
402 *
403 * @param name Task ID (string-encoded number).
404 * @param dry_run Do not get the data, just calculate the size.
405 *
406 * @return Sysinfo return holder. The type of the returned
407 * data is either SYSINFO_VAL_UNDEFINED (unknown
408 * task ID or memory allocation error) or
409 * SYSINFO_VAL_FUNCTION_DATA (in that case the
410 * generated data should be freed within the
411 * sysinfo request context).
412 *
413 */
414static sysinfo_return_t get_stats_task(const char *name, bool dry_run)
415{
416 /* Initially no return value */
417 sysinfo_return_t ret;
418 ret.tag = SYSINFO_VAL_UNDEFINED;
419
420 /* Parse the task ID */
421 task_id_t task_id;
422 if (str_uint64(name, NULL, 0, true, &task_id) != EOK)
423 return ret;
424
425 /* Messing with task structures, avoid deadlock */
426 irq_spinlock_lock(&tasks_lock, true);
427
428 task_t *task = task_find_by_id(task_id);
429 if (task == NULL) {
430 /* No task with this ID */
431 irq_spinlock_unlock(&tasks_lock, true);
432 return ret;
433 }
434
435 if (dry_run) {
436 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
437 ret.data.data = NULL;
438 ret.data.size = sizeof(stats_task_t);
439
440 irq_spinlock_unlock(&tasks_lock, true);
441 } else {
442 /* Allocate stats_task_t structure */
443 stats_task_t *stats_task =
444 (stats_task_t *) malloc(sizeof(stats_task_t), FRAME_ATOMIC);
445 if (stats_task == NULL) {
446 irq_spinlock_unlock(&tasks_lock, true);
447 return ret;
448 }
449
450 /* Correct return value */
451 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
452 ret.data.data = (void *) stats_task;
453 ret.data.size = sizeof(stats_task_t);
454
455 /* Hand-over-hand locking */
456 irq_spinlock_exchange(&tasks_lock, &task->lock);
457
458 produce_stats_task(task, stats_task);
459
460 irq_spinlock_unlock(&task->lock, true);
461 }
462
463 return ret;
464}
465
466/** Get thread statistics
467 *
468 * Get statistics of a given thread. The thread ID is passed
469 * as a string (current limitation of the sysinfo interface,
470 * but it is still reasonable for the given purpose).
471 *
472 * @param name Thread ID (string-encoded number).
473 * @param dry_run Do not get the data, just calculate the size.
474 *
475 * @return Sysinfo return holder. The type of the returned
476 * data is either SYSINFO_VAL_UNDEFINED (unknown
477 * thread ID or memory allocation error) or
478 * SYSINFO_VAL_FUNCTION_DATA (in that case the
479 * generated data should be freed within the
480 * sysinfo request context).
481 *
482 */
483static sysinfo_return_t get_stats_thread(const char *name, bool dry_run)
484{
485 /* Initially no return value */
486 sysinfo_return_t ret;
487 ret.tag = SYSINFO_VAL_UNDEFINED;
488
489 /* Parse the thread ID */
490 thread_id_t thread_id;
491 if (str_uint64(name, NULL, 0, true, &thread_id) != EOK)
492 return ret;
493
494 /* Messing with threads structures, avoid deadlock */
495 irq_spinlock_lock(&threads_lock, true);
496
497 thread_t *thread = thread_find_by_id(thread_id);
498 if (thread == NULL) {
499 /* No thread with this ID */
500 irq_spinlock_unlock(&threads_lock, true);
501 return ret;
502 }
503
504 if (dry_run) {
505 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
506 ret.data.data = NULL;
507 ret.data.size = sizeof(stats_thread_t);
508
509 irq_spinlock_unlock(&threads_lock, true);
510 } else {
511 /* Allocate stats_thread_t structure */
512 stats_thread_t *stats_thread =
513 (stats_thread_t *) malloc(sizeof(stats_thread_t), FRAME_ATOMIC);
514 if (stats_thread == NULL) {
515 irq_spinlock_unlock(&threads_lock, true);
516 return ret;
517 }
518
519 /* Correct return value */
520 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
521 ret.data.data = (void *) stats_thread;
522 ret.data.size = sizeof(stats_thread_t);
523
524 /* Hand-over-hand locking */
525 irq_spinlock_exchange(&threads_lock, &thread->lock);
526
527 produce_stats_thread(thread, stats_thread);
528
529 irq_spinlock_unlock(&thread->lock, true);
530 }
531
532 return ret;
533}
534
535/** Get physical memory statistics
536 *
537 * @param item Sysinfo item (unused).
538 * @param size Size of the returned data.
539 * @param dry_run Do not get the data, just calculate the size.
540 *
541 * @return Data containing stats_physmem_t.
542 * If the return value is not NULL, it should be freed
543 * in the context of the sysinfo request.
544 */
545static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
546 bool dry_run)
547{
548 *size = sizeof(stats_physmem_t);
549 if (dry_run)
550 return NULL;
551
552 stats_physmem_t *stats_physmem =
553 (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
554 if (stats_physmem == NULL) {
555 *size = 0;
556 return NULL;
557 }
558
559 zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
560 &(stats_physmem->used), &(stats_physmem->free));
561
562 return ((void *) stats_physmem);
563}
564
565/** Get system load
566 *
567 * @param item Sysinfo item (unused).
568 * @param size Size of the returned data.
569 * @param dry_run Do not get the data, just calculate the size.
570 *
571 * @return Data several load_t values.
572 * If the return value is not NULL, it should be freed
573 * in the context of the sysinfo request.
574 */
575static void *get_stats_load(struct sysinfo_item *item, size_t *size,
576 bool dry_run)
577{
578 *size = sizeof(load_t) * LOAD_STEPS;
579 if (dry_run)
580 return NULL;
581
582 load_t *stats_load = (load_t *) malloc(*size, FRAME_ATOMIC);
583 if (stats_load == NULL) {
584 *size = 0;
585 return NULL;
586 }
587
588 /* To always get consistent values acquire the mutex */
589 mutex_lock(&load_lock);
590
591 unsigned int i;
592 for (i = 0; i < LOAD_STEPS; i++)
593 stats_load[i] = avenrdy[i] << LOAD_FIXED_SHIFT;
594
595 mutex_unlock(&load_lock);
596
597 return ((void *) stats_load);
598}
599
600/** Calculate load
601 *
602 */
603static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
604{
605 load *= exp;
606 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
607
608 return (load >> LOAD_FIXED_SHIFT);
609}
610
611/** Load computation thread.
612 *
613 * Compute system load every few seconds.
614 *
615 * @param arg Unused.
616 *
617 */
618void kload(void *arg)
619{
620 thread_detach(THREAD);
621
622 while (true) {
623 atomic_count_t ready = atomic_get(&nrdy);
624
625 /* Mutually exclude with get_stats_load() */
626 mutex_lock(&load_lock);
627
628 unsigned int i;
629 for (i = 0; i < LOAD_STEPS; i++)
630 avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
631
632 mutex_unlock(&load_lock);
633
634 thread_sleep(LOAD_INTERVAL);
635 }
636}
637
638/** Register sysinfo statistical items
639 *
640 */
641void stats_init(void)
642{
643 mutex_initialize(&load_lock, MUTEX_PASSIVE);
644
645 sysinfo_set_item_fn_val("system.uptime", NULL, get_stats_uptime);
646 sysinfo_set_item_fn_data("system.cpus", NULL, get_stats_cpus);
647 sysinfo_set_item_fn_data("system.physmem", NULL, get_stats_physmem);
648 sysinfo_set_item_fn_data("system.load", NULL, get_stats_load);
649 sysinfo_set_item_fn_data("system.tasks", NULL, get_stats_tasks);
650 sysinfo_set_item_fn_data("system.threads", NULL, get_stats_threads);
651 sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task);
652 sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread);
653}
654
655/** @}
656 */
Note: See TracBrowser for help on using the repository browser.