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

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

simplify load calculation, the number of ready threads is available from the scheduler as an atomic variable

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