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

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