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