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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c5cb943d was 6e121b8, checked in by Jakub Jermar <jakub@…>, 15 years ago

Load lock can be mutex a interrupts are not required to be disabled when taking it.

  • Property mode set to 100644
File size: 18.1 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 /* Each CPU structure is locked separatelly */
113 ipl_t ipl = interrupts_disable();
114
115 size_t i;
116 for (i = 0; i < config.cpu_count; i++) {
117 spinlock_lock(&cpus[i].lock);
118
119 stats_cpus[i].id = cpus[i].id;
120 stats_cpus[i].active = cpus[i].active;
121 stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
122 stats_cpus[i].busy_ticks = cpus[i].busy_ticks;
123 stats_cpus[i].idle_ticks = cpus[i].idle_ticks;
124
125 spinlock_unlock(&cpus[i].lock);
126 }
127
128 interrupts_restore(ipl);
129
130 return ((void *) stats_cpus);
131}
132
133/** Count number of nodes in an AVL tree
134 *
135 * AVL tree walker for counting nodes.
136 *
137 * @param node AVL tree node (unused).
138 * @param arg Pointer to the counter (size_t).
139 *
140 * @param Always true (continue the walk).
141 *
142 */
143static bool avl_count_walker(avltree_node_t *node, void *arg)
144{
145 size_t *count = (size_t *) arg;
146 (*count)++;
147
148 return true;
149}
150
151/** Get the size of a virtual address space
152 *
153 * @param as Address space.
154 *
155 * @return Size of the mapped virtual address space (bytes).
156 *
157 */
158static size_t get_task_virtmem(as_t *as)
159{
160 size_t result = 0;
161
162 /*
163 * We are holding some spinlocks here and therefore are not allowed to
164 * block. Only attempt to lock the address space and address space area
165 * mutexes conditionally. If it is not possible to lock either object,
166 * allow the statistics to be inexact by skipping the respective object.
167 *
168 * Note that it may be infinitely better to let the address space
169 * management code compute these statistics as it proceeds instead of
170 * having them calculated here over and over again here.
171 */
172
173 if (SYNCH_FAILED(mutex_trylock(&as->lock)))
174 return result * PAGE_SIZE;
175
176 /* Walk the B+ tree and count pages */
177 link_t *cur;
178 for (cur = as->as_area_btree.leaf_head.next;
179 cur != &as->as_area_btree.leaf_head; cur = cur->next) {
180 btree_node_t *node =
181 list_get_instance(cur, btree_node_t, leaf_link);
182
183 unsigned int i;
184 for (i = 0; i < node->keys; i++) {
185 as_area_t *area = node->value[i];
186
187 if (SYNCH_FAILED(mutex_trylock(&area->lock)))
188 continue;
189 result += area->pages;
190 mutex_unlock(&area->lock);
191 }
192 }
193
194 mutex_unlock(&as->lock);
195
196 return result * PAGE_SIZE;
197}
198
199/* Produce task statistics
200 *
201 * Summarize task information into task statistics.
202 * Task lock should be held and interrupts disabled
203 * before executing this function.
204 *
205 * @param task Task.
206 * @param stats_task Task statistics.
207 *
208 */
209static void produce_stats_task(task_t *task, stats_task_t *stats_task)
210{
211 stats_task->task_id = task->taskid;
212 str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
213 stats_task->virtmem = get_task_virtmem(task->as);
214 stats_task->threads = atomic_get(&task->refcount);
215 task_get_accounting(task, &(stats_task->ucycles),
216 &(stats_task->kcycles));
217 stats_task->ipc_info = task->ipc_info;
218}
219
220/** Gather statistics of all tasks
221 *
222 * AVL task tree walker for gathering task statistics. Interrupts should
223 * be already disabled while walking the tree.
224 *
225 * @param node AVL task tree node.
226 * @param arg Pointer to the iterator into the array of stats_task_t.
227 *
228 * @param Always true (continue the walk).
229 *
230 */
231static bool task_serialize_walker(avltree_node_t *node, void *arg)
232{
233 stats_task_t **iterator = (stats_task_t **) arg;
234 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
235
236 /* Interrupts are already disabled */
237 spinlock_lock(&(task->lock));
238
239 /* Record the statistics and increment the iterator */
240 produce_stats_task(task, *iterator);
241 (*iterator)++;
242
243 spinlock_unlock(&(task->lock));
244
245 return true;
246}
247
248/** Get task statistics
249 *
250 * @param item Sysinfo item (unused).
251 * @param size Size of the returned data.
252 * @param dry_run Do not get the data, just calculate the size.
253 *
254 * @return Data containing several stats_task_t structures.
255 * If the return value is not NULL, it should be freed
256 * in the context of the sysinfo request.
257 */
258static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
259 bool dry_run)
260{
261 /* Messing with task structures, avoid deadlock */
262 ipl_t ipl = interrupts_disable();
263 spinlock_lock(&tasks_lock);
264
265 /* First walk the task tree to count the tasks */
266 size_t count = 0;
267 avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
268
269 if (count == 0) {
270 /* No tasks found (strange) */
271 spinlock_unlock(&tasks_lock);
272 interrupts_restore(ipl);
273
274 *size = 0;
275 return NULL;
276 }
277
278 *size = sizeof(stats_task_t) * count;
279 if (dry_run) {
280 spinlock_unlock(&tasks_lock);
281 interrupts_restore(ipl);
282 return NULL;
283 }
284
285 stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
286 if (stats_tasks == NULL) {
287 /* No free space for allocation */
288 spinlock_unlock(&tasks_lock);
289 interrupts_restore(ipl);
290
291 *size = 0;
292 return NULL;
293 }
294
295 /* Walk tha task tree again to gather the statistics */
296 stats_task_t *iterator = stats_tasks;
297 avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
298
299 spinlock_unlock(&tasks_lock);
300 interrupts_restore(ipl);
301
302 return ((void *) stats_tasks);
303}
304
305/* Produce thread statistics
306 *
307 * Summarize thread information into thread statistics.
308 * Thread lock should be held and interrupts disabled
309 * before executing this function.
310 *
311 * @param thread Thread.
312 * @param stats_thread Thread statistics.
313 *
314 */
315static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
316{
317 stats_thread->thread_id = thread->tid;
318 stats_thread->task_id = thread->task->taskid;
319 stats_thread->state = thread->state;
320 stats_thread->priority = thread->priority;
321 stats_thread->ucycles = thread->ucycles;
322 stats_thread->kcycles = thread->kcycles;
323
324 if (thread->cpu != NULL) {
325 stats_thread->on_cpu = true;
326 stats_thread->cpu = thread->cpu->id;
327 } else
328 stats_thread->on_cpu = false;
329}
330
331/** Gather statistics of all threads
332 *
333 * AVL three tree walker for gathering thread statistics. Interrupts should
334 * be already disabled while walking the tree.
335 *
336 * @param node AVL thread tree node.
337 * @param arg Pointer to the iterator into the array of thread statistics.
338 *
339 * @param Always true (continue the walk).
340 *
341 */
342static bool thread_serialize_walker(avltree_node_t *node, void *arg)
343{
344 stats_thread_t **iterator = (stats_thread_t **) arg;
345 thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
346
347 /* Interrupts are already disabled */
348 spinlock_lock(&thread->lock);
349
350 /* Record the statistics and increment the iterator */
351 produce_stats_thread(thread, *iterator);
352 (*iterator)++;
353
354 spinlock_unlock(&thread->lock);
355
356 return true;
357}
358
359/** Get thread statistics
360 *
361 * @param item Sysinfo item (unused).
362 * @param size Size of the returned data.
363 * @param dry_run Do not get the data, just calculate the size.
364 *
365 * @return Data containing several stats_task_t structures.
366 * If the return value is not NULL, it should be freed
367 * in the context of the sysinfo request.
368 */
369static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
370 bool dry_run)
371{
372 /* Messing with threads structures, avoid deadlock */
373 ipl_t ipl = interrupts_disable();
374 spinlock_lock(&threads_lock);
375
376 /* First walk the thread tree to count the threads */
377 size_t count = 0;
378 avltree_walk(&threads_tree, avl_count_walker, (void *) &count);
379
380 if (count == 0) {
381 /* No threads found (strange) */
382 spinlock_unlock(&threads_lock);
383 interrupts_restore(ipl);
384
385 *size = 0;
386 return NULL;
387 }
388
389 *size = sizeof(stats_thread_t) * count;
390 if (dry_run) {
391 spinlock_unlock(&threads_lock);
392 interrupts_restore(ipl);
393 return NULL;
394 }
395
396 stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
397 if (stats_threads == NULL) {
398 /* No free space for allocation */
399 spinlock_unlock(&threads_lock);
400 interrupts_restore(ipl);
401
402 *size = 0;
403 return NULL;
404 }
405
406 /* Walk tha thread tree again to gather the statistics */
407 stats_thread_t *iterator = stats_threads;
408 avltree_walk(&threads_tree, thread_serialize_walker, (void *) &iterator);
409
410 spinlock_unlock(&threads_lock);
411 interrupts_restore(ipl);
412
413 return ((void *) stats_threads);
414}
415
416/** Get a single task statistics
417 *
418 * Get statistics of a given task. The task ID is passed
419 * as a string (current limitation of the sysinfo interface,
420 * but it is still reasonable for the given purpose).
421 *
422 * @param name Task ID (string-encoded number).
423 * @param dry_run Do not get the data, just calculate the size.
424 *
425 * @return Sysinfo return holder. The type of the returned
426 * data is either SYSINFO_VAL_UNDEFINED (unknown
427 * task ID or memory allocation error) or
428 * SYSINFO_VAL_FUNCTION_DATA (in that case the
429 * generated data should be freed within the
430 * sysinfo request context).
431 *
432 */
433static sysinfo_return_t get_stats_task(const char *name, bool dry_run)
434{
435 /* Initially no return value */
436 sysinfo_return_t ret;
437 ret.tag = SYSINFO_VAL_UNDEFINED;
438
439 /* Parse the task ID */
440 task_id_t task_id;
441 if (str_uint64(name, NULL, 0, true, &task_id) != EOK)
442 return ret;
443
444 /* Messing with task structures, avoid deadlock */
445 ipl_t ipl = interrupts_disable();
446 spinlock_lock(&tasks_lock);
447
448 task_t *task = task_find_by_id(task_id);
449 if (task == NULL) {
450 /* No task with this ID */
451 spinlock_unlock(&tasks_lock);
452 interrupts_restore(ipl);
453 return ret;
454 }
455
456 if (dry_run) {
457 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
458 ret.data.data = NULL;
459 ret.data.size = sizeof(stats_task_t);
460
461 spinlock_unlock(&tasks_lock);
462 } else {
463 /* Allocate stats_task_t structure */
464 stats_task_t *stats_task =
465 (stats_task_t *) malloc(sizeof(stats_task_t), FRAME_ATOMIC);
466 if (stats_task == NULL) {
467 spinlock_unlock(&tasks_lock);
468 interrupts_restore(ipl);
469 return ret;
470 }
471
472 /* Correct return value */
473 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
474 ret.data.data = (void *) stats_task;
475 ret.data.size = sizeof(stats_task_t);
476
477 /* Hand-over-hand locking */
478 spinlock_lock(&task->lock);
479 spinlock_unlock(&tasks_lock);
480
481 produce_stats_task(task, stats_task);
482
483 spinlock_unlock(&task->lock);
484 }
485
486 interrupts_restore(ipl);
487
488 return ret;
489}
490
491/** Get thread statistics
492 *
493 * Get statistics of a given thread. The thread ID is passed
494 * as a string (current limitation of the sysinfo interface,
495 * but it is still reasonable for the given purpose).
496 *
497 * @param name Thread ID (string-encoded number).
498 * @param dry_run Do not get the data, just calculate the size.
499 *
500 * @return Sysinfo return holder. The type of the returned
501 * data is either SYSINFO_VAL_UNDEFINED (unknown
502 * thread ID or memory allocation error) or
503 * SYSINFO_VAL_FUNCTION_DATA (in that case the
504 * generated data should be freed within the
505 * sysinfo request context).
506 *
507 */
508static sysinfo_return_t get_stats_thread(const char *name, bool dry_run)
509{
510 /* Initially no return value */
511 sysinfo_return_t ret;
512 ret.tag = SYSINFO_VAL_UNDEFINED;
513
514 /* Parse the thread ID */
515 thread_id_t thread_id;
516 if (str_uint64(name, NULL, 0, true, &thread_id) != EOK)
517 return ret;
518
519 /* Messing with threads structures, avoid deadlock */
520 ipl_t ipl = interrupts_disable();
521 spinlock_lock(&threads_lock);
522
523 thread_t *thread = thread_find_by_id(thread_id);
524 if (thread == NULL) {
525 /* No thread with this ID */
526 spinlock_unlock(&threads_lock);
527 interrupts_restore(ipl);
528 return ret;
529 }
530
531 if (dry_run) {
532 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
533 ret.data.data = NULL;
534 ret.data.size = sizeof(stats_thread_t);
535
536 spinlock_unlock(&threads_lock);
537 } else {
538 /* Allocate stats_thread_t structure */
539 stats_thread_t *stats_thread =
540 (stats_thread_t *) malloc(sizeof(stats_thread_t), FRAME_ATOMIC);
541 if (stats_thread == NULL) {
542 spinlock_unlock(&threads_lock);
543 interrupts_restore(ipl);
544 return ret;
545 }
546
547 /* Correct return value */
548 ret.tag = SYSINFO_VAL_FUNCTION_DATA;
549 ret.data.data = (void *) stats_thread;
550 ret.data.size = sizeof(stats_thread_t);
551
552 /* Hand-over-hand locking */
553 spinlock_lock(&thread->lock);
554 spinlock_unlock(&threads_lock);
555
556 produce_stats_thread(thread, stats_thread);
557
558 spinlock_unlock(&thread->lock);
559 }
560
561 interrupts_restore(ipl);
562
563 return ret;
564}
565
566/** Get physical memory statistics
567 *
568 * @param item Sysinfo item (unused).
569 * @param size Size of the returned data.
570 * @param dry_run Do not get the data, just calculate the size.
571 *
572 * @return Data containing stats_physmem_t.
573 * If the return value is not NULL, it should be freed
574 * in the context of the sysinfo request.
575 */
576static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
577 bool dry_run)
578{
579 *size = sizeof(stats_physmem_t);
580 if (dry_run)
581 return NULL;
582
583 stats_physmem_t *stats_physmem =
584 (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
585 if (stats_physmem == NULL) {
586 *size = 0;
587 return NULL;
588 }
589
590 zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
591 &(stats_physmem->used), &(stats_physmem->free));
592
593 return ((void *) stats_physmem);
594}
595
596/** Get system load
597 *
598 * @param item Sysinfo item (unused).
599 * @param size Size of the returned data.
600 * @param dry_run Do not get the data, just calculate the size.
601 *
602 * @return Data several load_t values.
603 * If the return value is not NULL, it should be freed
604 * in the context of the sysinfo request.
605 */
606static void *get_stats_load(struct sysinfo_item *item, size_t *size,
607 bool dry_run)
608{
609 *size = sizeof(load_t) * LOAD_STEPS;
610 if (dry_run)
611 return NULL;
612
613 load_t *stats_load = (load_t *) malloc(*size, FRAME_ATOMIC);
614 if (stats_load == NULL) {
615 *size = 0;
616 return NULL;
617 }
618
619 /* To always get consistent values acquire the mutex */
620 mutex_lock(&load_lock);
621
622 unsigned int i;
623 for (i = 0; i < LOAD_STEPS; i++)
624 stats_load[i] = avenrdy[i] << LOAD_FIXED_SHIFT;
625
626 mutex_unlock(&load_lock);
627
628 return ((void *) stats_load);
629}
630
631/** Calculate load
632 *
633 */
634static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
635{
636 load *= exp;
637 load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
638
639 return (load >> LOAD_FIXED_SHIFT);
640}
641
642/** Load computation thread.
643 *
644 * Compute system load every few seconds.
645 *
646 * @param arg Unused.
647 *
648 */
649void kload(void *arg)
650{
651 thread_detach(THREAD);
652
653 while (true) {
654 atomic_count_t ready = atomic_get(&nrdy);
655
656 /* Mutually exclude with get_stats_load() */
657 mutex_lock(&load_lock);
658
659 unsigned int i;
660 for (i = 0; i < LOAD_STEPS; i++)
661 avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
662
663 mutex_unlock(&load_lock);
664
665 thread_sleep(LOAD_INTERVAL);
666 }
667}
668
669/** Register sysinfo statistical items
670 *
671 */
672void stats_init(void)
673{
674 mutex_initialize(&load_lock, MUTEX_PASSIVE);
675
676 sysinfo_set_item_fn_val("system.uptime", NULL, get_stats_uptime);
677 sysinfo_set_item_fn_data("system.cpus", NULL, get_stats_cpus);
678 sysinfo_set_item_fn_data("system.physmem", NULL, get_stats_physmem);
679 sysinfo_set_item_fn_data("system.load", NULL, get_stats_load);
680 sysinfo_set_item_fn_data("system.tasks", NULL, get_stats_tasks);
681 sysinfo_set_item_fn_data("system.threads", NULL, get_stats_threads);
682 sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task);
683 sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread);
684}
685
686/** @}
687 */
Note: See TracBrowser for help on using the repository browser.