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 <interrupt.h>
|
---|
47 | #include <str.h>
|
---|
48 | #include <errno.h>
|
---|
49 | #include <cpu.h>
|
---|
50 | #include <arch.h>
|
---|
51 |
|
---|
52 | /** Bits of fixed-point precision for load */
|
---|
53 | #define LOAD_FIXED_SHIFT 11
|
---|
54 |
|
---|
55 | /** 1.0 as fixed-point for load */
|
---|
56 | #define LOAD_FIXED_1 (1 << LOAD_FIXED_SHIFT)
|
---|
57 |
|
---|
58 | /** Compute load in 5 second intervals */
|
---|
59 | #define LOAD_INTERVAL 5
|
---|
60 |
|
---|
61 | /** Fixed-point representation of
|
---|
62 | *
|
---|
63 | * 1 / exp(5 sec / 1 min)
|
---|
64 | * 1 / exp(5 sec / 5 min)
|
---|
65 | * 1 / exp(5 sec / 15 min)
|
---|
66 | *
|
---|
67 | */
|
---|
68 | static load_t load_exp[LOAD_STEPS] = {1884, 2014, 2037};
|
---|
69 |
|
---|
70 | /** Running average of the number of ready threads */
|
---|
71 | static load_t avenrdy[LOAD_STEPS] = {0, 0, 0};
|
---|
72 |
|
---|
73 | /** Load calculation lock */
|
---|
74 | static mutex_t load_lock;
|
---|
75 |
|
---|
76 | /** Get system uptime
|
---|
77 | *
|
---|
78 | * @param item Sysinfo item (unused).
|
---|
79 | *
|
---|
80 | * @return System uptime (in secords).
|
---|
81 | *
|
---|
82 | */
|
---|
83 | static unative_t get_stats_uptime(struct sysinfo_item *item)
|
---|
84 | {
|
---|
85 | /* This doesn't have to be very accurate */
|
---|
86 | return uptime->seconds1;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Get statistics of all CPUs
|
---|
90 | *
|
---|
91 | * @param item Sysinfo item (unused).
|
---|
92 | * @param size Size of the returned data.
|
---|
93 | * @param dry_run Do not get the data, just calculate the size.
|
---|
94 | *
|
---|
95 | * @return Data containing several stats_cpu_t structures.
|
---|
96 | * If the return value is not NULL, it should be freed
|
---|
97 | * in the context of the sysinfo request.
|
---|
98 | */
|
---|
99 | static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
|
---|
100 | bool dry_run)
|
---|
101 | {
|
---|
102 | *size = sizeof(stats_cpu_t) * config.cpu_count;
|
---|
103 | if (dry_run)
|
---|
104 | return NULL;
|
---|
105 |
|
---|
106 | /* Assumption: config.cpu_count is constant */
|
---|
107 | stats_cpu_t *stats_cpus = (stats_cpu_t *) malloc(*size, FRAME_ATOMIC);
|
---|
108 | if (stats_cpus == NULL) {
|
---|
109 | *size = 0;
|
---|
110 | return NULL;
|
---|
111 | }
|
---|
112 |
|
---|
113 | size_t i;
|
---|
114 | for (i = 0; i < config.cpu_count; i++) {
|
---|
115 | irq_spinlock_lock(&cpus[i].lock, true);
|
---|
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 | irq_spinlock_unlock(&cpus[i].lock, true);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return ((void *) stats_cpus);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Count number of nodes in an AVL tree
|
---|
130 | *
|
---|
131 | * AVL tree walker for counting nodes.
|
---|
132 | *
|
---|
133 | * @param node AVL tree node (unused).
|
---|
134 | * @param arg Pointer to the counter (size_t).
|
---|
135 | *
|
---|
136 | * @param Always true (continue the walk).
|
---|
137 | *
|
---|
138 | */
|
---|
139 | static bool avl_count_walker(avltree_node_t *node, void *arg)
|
---|
140 | {
|
---|
141 | size_t *count = (size_t *) arg;
|
---|
142 | (*count)++;
|
---|
143 |
|
---|
144 | return true;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /** Get the size of a virtual address space
|
---|
148 | *
|
---|
149 | * @param as Address space.
|
---|
150 | *
|
---|
151 | * @return Size of the mapped virtual address space (bytes).
|
---|
152 | *
|
---|
153 | */
|
---|
154 | static size_t get_task_virtmem(as_t *as)
|
---|
155 | {
|
---|
156 | size_t result = 0;
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * We are holding some spinlocks here and therefore are not allowed to
|
---|
160 | * block. Only attempt to lock the address space and address space area
|
---|
161 | * mutexes conditionally. If it is not possible to lock either object,
|
---|
162 | * allow the statistics to be inexact by skipping the respective object.
|
---|
163 | *
|
---|
164 | * Note that it may be infinitely better to let the address space
|
---|
165 | * management code compute these statistics as it proceeds instead of
|
---|
166 | * having them calculated here over and over again here.
|
---|
167 | */
|
---|
168 |
|
---|
169 | if (SYNCH_FAILED(mutex_trylock(&as->lock)))
|
---|
170 | return result * PAGE_SIZE;
|
---|
171 |
|
---|
172 | /* Walk the B+ tree and count pages */
|
---|
173 | link_t *cur;
|
---|
174 | for (cur = as->as_area_btree.leaf_head.next;
|
---|
175 | cur != &as->as_area_btree.leaf_head; cur = cur->next) {
|
---|
176 | btree_node_t *node =
|
---|
177 | list_get_instance(cur, btree_node_t, leaf_link);
|
---|
178 |
|
---|
179 | unsigned int i;
|
---|
180 | for (i = 0; i < node->keys; i++) {
|
---|
181 | as_area_t *area = node->value[i];
|
---|
182 |
|
---|
183 | if (SYNCH_FAILED(mutex_trylock(&area->lock)))
|
---|
184 | continue;
|
---|
185 | result += area->pages;
|
---|
186 | mutex_unlock(&area->lock);
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | mutex_unlock(&as->lock);
|
---|
191 |
|
---|
192 | return result * PAGE_SIZE;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /* Produce task statistics
|
---|
196 | *
|
---|
197 | * Summarize task information into task statistics.
|
---|
198 | *
|
---|
199 | * @param task Task.
|
---|
200 | * @param stats_task Task statistics.
|
---|
201 | *
|
---|
202 | */
|
---|
203 | static void produce_stats_task(task_t *task, stats_task_t *stats_task)
|
---|
204 | {
|
---|
205 | ASSERT(interrupts_disabled());
|
---|
206 | ASSERT(irq_spinlock_locked(&task->lock));
|
---|
207 |
|
---|
208 | stats_task->task_id = task->taskid;
|
---|
209 | str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
|
---|
210 | stats_task->virtmem = get_task_virtmem(task->as);
|
---|
211 | stats_task->threads = atomic_get(&task->refcount);
|
---|
212 | task_get_accounting(task, &(stats_task->ucycles),
|
---|
213 | &(stats_task->kcycles));
|
---|
214 | stats_task->ipc_info = task->ipc_info;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Gather statistics of all tasks
|
---|
218 | *
|
---|
219 | * AVL task tree walker for gathering task statistics. Interrupts should
|
---|
220 | * be already disabled while walking the tree.
|
---|
221 | *
|
---|
222 | * @param node AVL task tree node.
|
---|
223 | * @param arg Pointer to the iterator into the array of stats_task_t.
|
---|
224 | *
|
---|
225 | * @param Always true (continue the walk).
|
---|
226 | *
|
---|
227 | */
|
---|
228 | static bool task_serialize_walker(avltree_node_t *node, void *arg)
|
---|
229 | {
|
---|
230 | stats_task_t **iterator = (stats_task_t **) arg;
|
---|
231 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
232 |
|
---|
233 | /* Interrupts are already disabled */
|
---|
234 | irq_spinlock_lock(&(task->lock), false);
|
---|
235 |
|
---|
236 | /* Record the statistics and increment the iterator */
|
---|
237 | produce_stats_task(task, *iterator);
|
---|
238 | (*iterator)++;
|
---|
239 |
|
---|
240 | irq_spinlock_unlock(&(task->lock), false);
|
---|
241 |
|
---|
242 | return true;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /** Get task statistics
|
---|
246 | *
|
---|
247 | * @param item Sysinfo item (unused).
|
---|
248 | * @param size Size of the returned data.
|
---|
249 | * @param dry_run Do not get the data, just calculate the size.
|
---|
250 | *
|
---|
251 | * @return Data containing several stats_task_t structures.
|
---|
252 | * If the return value is not NULL, it should be freed
|
---|
253 | * in the context of the sysinfo request.
|
---|
254 | */
|
---|
255 | static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
|
---|
256 | bool dry_run)
|
---|
257 | {
|
---|
258 | /* Messing with task structures, avoid deadlock */
|
---|
259 | irq_spinlock_lock(&tasks_lock, true);
|
---|
260 |
|
---|
261 | /* First walk the task tree to count the tasks */
|
---|
262 | size_t count = 0;
|
---|
263 | avltree_walk(&tasks_tree, avl_count_walker, (void *) &count);
|
---|
264 |
|
---|
265 | if (count == 0) {
|
---|
266 | /* No tasks found (strange) */
|
---|
267 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
268 | *size = 0;
|
---|
269 | return NULL;
|
---|
270 | }
|
---|
271 |
|
---|
272 | *size = sizeof(stats_task_t) * count;
|
---|
273 | if (dry_run) {
|
---|
274 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
275 | return NULL;
|
---|
276 | }
|
---|
277 |
|
---|
278 | stats_task_t *stats_tasks = (stats_task_t *) malloc(*size, FRAME_ATOMIC);
|
---|
279 | if (stats_tasks == NULL) {
|
---|
280 | /* No free space for allocation */
|
---|
281 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
282 | *size = 0;
|
---|
283 | return NULL;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Walk tha task tree again to gather the statistics */
|
---|
287 | stats_task_t *iterator = stats_tasks;
|
---|
288 | avltree_walk(&tasks_tree, task_serialize_walker, (void *) &iterator);
|
---|
289 |
|
---|
290 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
291 |
|
---|
292 | return ((void *) stats_tasks);
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* Produce thread statistics
|
---|
296 | *
|
---|
297 | * Summarize thread information into thread statistics.
|
---|
298 | *
|
---|
299 | * @param thread Thread.
|
---|
300 | * @param stats_thread Thread statistics.
|
---|
301 | *
|
---|
302 | */
|
---|
303 | static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
|
---|
304 | {
|
---|
305 | ASSERT(interrupts_disabled());
|
---|
306 | ASSERT(irq_spinlock_locked(&thread->lock));
|
---|
307 |
|
---|
308 | stats_thread->thread_id = thread->tid;
|
---|
309 | stats_thread->task_id = thread->task->taskid;
|
---|
310 | stats_thread->state = thread->state;
|
---|
311 | stats_thread->priority = thread->priority;
|
---|
312 | stats_thread->ucycles = thread->ucycles;
|
---|
313 | stats_thread->kcycles = thread->kcycles;
|
---|
314 |
|
---|
315 | if (thread->cpu != NULL) {
|
---|
316 | stats_thread->on_cpu = true;
|
---|
317 | stats_thread->cpu = thread->cpu->id;
|
---|
318 | } else
|
---|
319 | stats_thread->on_cpu = false;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /** Gather statistics of all threads
|
---|
323 | *
|
---|
324 | * AVL three tree walker for gathering thread statistics. Interrupts should
|
---|
325 | * be already disabled while walking the tree.
|
---|
326 | *
|
---|
327 | * @param node AVL thread tree node.
|
---|
328 | * @param arg Pointer to the iterator into the array of thread statistics.
|
---|
329 | *
|
---|
330 | * @param Always true (continue the walk).
|
---|
331 | *
|
---|
332 | */
|
---|
333 | static bool thread_serialize_walker(avltree_node_t *node, void *arg)
|
---|
334 | {
|
---|
335 | stats_thread_t **iterator = (stats_thread_t **) arg;
|
---|
336 | thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
|
---|
337 |
|
---|
338 | /* Interrupts are already disabled */
|
---|
339 | irq_spinlock_lock(&thread->lock, false);
|
---|
340 |
|
---|
341 | /* Record the statistics and increment the iterator */
|
---|
342 | produce_stats_thread(thread, *iterator);
|
---|
343 | (*iterator)++;
|
---|
344 |
|
---|
345 | irq_spinlock_unlock(&thread->lock, false);
|
---|
346 |
|
---|
347 | return true;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** Get thread statistics
|
---|
351 | *
|
---|
352 | * @param item Sysinfo item (unused).
|
---|
353 | * @param size Size of the returned data.
|
---|
354 | * @param dry_run Do not get the data, just calculate the size.
|
---|
355 | *
|
---|
356 | * @return Data containing several stats_task_t structures.
|
---|
357 | * If the return value is not NULL, it should be freed
|
---|
358 | * in the context of the sysinfo request.
|
---|
359 | */
|
---|
360 | static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
|
---|
361 | bool dry_run)
|
---|
362 | {
|
---|
363 | /* Messing with threads structures, avoid deadlock */
|
---|
364 | irq_spinlock_lock(&threads_lock, true);
|
---|
365 |
|
---|
366 | /* First walk the thread tree to count the threads */
|
---|
367 | size_t count = 0;
|
---|
368 | avltree_walk(&threads_tree, avl_count_walker, (void *) &count);
|
---|
369 |
|
---|
370 | if (count == 0) {
|
---|
371 | /* No threads found (strange) */
|
---|
372 | irq_spinlock_unlock(&threads_lock, true);
|
---|
373 | *size = 0;
|
---|
374 | return NULL;
|
---|
375 | }
|
---|
376 |
|
---|
377 | *size = sizeof(stats_thread_t) * count;
|
---|
378 | if (dry_run) {
|
---|
379 | irq_spinlock_unlock(&threads_lock, true);
|
---|
380 | return NULL;
|
---|
381 | }
|
---|
382 |
|
---|
383 | stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size, FRAME_ATOMIC);
|
---|
384 | if (stats_threads == NULL) {
|
---|
385 | /* No free space for allocation */
|
---|
386 | irq_spinlock_unlock(&threads_lock, true);
|
---|
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 | irq_spinlock_unlock(&threads_lock, true);
|
---|
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 | */
|
---|
417 | static 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 | irq_spinlock_lock(&tasks_lock, true);
|
---|
430 |
|
---|
431 | task_t *task = task_find_by_id(task_id);
|
---|
432 | if (task == NULL) {
|
---|
433 | /* No task with this ID */
|
---|
434 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
435 | return ret;
|
---|
436 | }
|
---|
437 |
|
---|
438 | if (dry_run) {
|
---|
439 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
440 | ret.data.data = NULL;
|
---|
441 | ret.data.size = sizeof(stats_task_t);
|
---|
442 |
|
---|
443 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
444 | } else {
|
---|
445 | /* Allocate stats_task_t structure */
|
---|
446 | stats_task_t *stats_task =
|
---|
447 | (stats_task_t *) malloc(sizeof(stats_task_t), FRAME_ATOMIC);
|
---|
448 | if (stats_task == NULL) {
|
---|
449 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
450 | return ret;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /* Correct return value */
|
---|
454 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
455 | ret.data.data = (void *) stats_task;
|
---|
456 | ret.data.size = sizeof(stats_task_t);
|
---|
457 |
|
---|
458 | /* Hand-over-hand locking */
|
---|
459 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
460 |
|
---|
461 | produce_stats_task(task, stats_task);
|
---|
462 |
|
---|
463 | irq_spinlock_unlock(&task->lock, true);
|
---|
464 | }
|
---|
465 |
|
---|
466 | return ret;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /** Get thread statistics
|
---|
470 | *
|
---|
471 | * Get statistics of a given thread. The thread ID is passed
|
---|
472 | * as a string (current limitation of the sysinfo interface,
|
---|
473 | * but it is still reasonable for the given purpose).
|
---|
474 | *
|
---|
475 | * @param name Thread ID (string-encoded number).
|
---|
476 | * @param dry_run Do not get the data, just calculate the size.
|
---|
477 | *
|
---|
478 | * @return Sysinfo return holder. The type of the returned
|
---|
479 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
---|
480 | * thread ID or memory allocation error) or
|
---|
481 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
---|
482 | * generated data should be freed within the
|
---|
483 | * sysinfo request context).
|
---|
484 | *
|
---|
485 | */
|
---|
486 | static sysinfo_return_t get_stats_thread(const char *name, bool dry_run)
|
---|
487 | {
|
---|
488 | /* Initially no return value */
|
---|
489 | sysinfo_return_t ret;
|
---|
490 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
---|
491 |
|
---|
492 | /* Parse the thread ID */
|
---|
493 | thread_id_t thread_id;
|
---|
494 | if (str_uint64(name, NULL, 0, true, &thread_id) != EOK)
|
---|
495 | return ret;
|
---|
496 |
|
---|
497 | /* Messing with threads structures, avoid deadlock */
|
---|
498 | irq_spinlock_lock(&threads_lock, true);
|
---|
499 |
|
---|
500 | thread_t *thread = thread_find_by_id(thread_id);
|
---|
501 | if (thread == NULL) {
|
---|
502 | /* No thread with this ID */
|
---|
503 | irq_spinlock_unlock(&threads_lock, true);
|
---|
504 | return ret;
|
---|
505 | }
|
---|
506 |
|
---|
507 | if (dry_run) {
|
---|
508 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
509 | ret.data.data = NULL;
|
---|
510 | ret.data.size = sizeof(stats_thread_t);
|
---|
511 |
|
---|
512 | irq_spinlock_unlock(&threads_lock, true);
|
---|
513 | } else {
|
---|
514 | /* Allocate stats_thread_t structure */
|
---|
515 | stats_thread_t *stats_thread =
|
---|
516 | (stats_thread_t *) malloc(sizeof(stats_thread_t), FRAME_ATOMIC);
|
---|
517 | if (stats_thread == NULL) {
|
---|
518 | irq_spinlock_unlock(&threads_lock, true);
|
---|
519 | return ret;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /* Correct return value */
|
---|
523 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
524 | ret.data.data = (void *) stats_thread;
|
---|
525 | ret.data.size = sizeof(stats_thread_t);
|
---|
526 |
|
---|
527 | /* Hand-over-hand locking */
|
---|
528 | irq_spinlock_exchange(&threads_lock, &thread->lock);
|
---|
529 |
|
---|
530 | produce_stats_thread(thread, stats_thread);
|
---|
531 |
|
---|
532 | irq_spinlock_unlock(&thread->lock, true);
|
---|
533 | }
|
---|
534 |
|
---|
535 | return ret;
|
---|
536 | }
|
---|
537 |
|
---|
538 | /** Get exceptions statistics
|
---|
539 | *
|
---|
540 | * @param item Sysinfo item (unused).
|
---|
541 | * @param size Size of the returned data.
|
---|
542 | * @param dry_run Do not get the data, just calculate the size.
|
---|
543 | *
|
---|
544 | * @return Data containing several stats_exc_t structures.
|
---|
545 | * If the return value is not NULL, it should be freed
|
---|
546 | * in the context of the sysinfo request.
|
---|
547 | */
|
---|
548 | static void *get_stats_exceptions(struct sysinfo_item *item, size_t *size,
|
---|
549 | bool dry_run)
|
---|
550 | {
|
---|
551 | *size = sizeof(stats_exc_t) * IVT_ITEMS;
|
---|
552 |
|
---|
553 | if ((dry_run) || (IVT_ITEMS == 0))
|
---|
554 | return NULL;
|
---|
555 |
|
---|
556 | stats_exc_t *stats_exceptions =
|
---|
557 | (stats_exc_t *) malloc(*size, FRAME_ATOMIC);
|
---|
558 | if (stats_exceptions == NULL) {
|
---|
559 | /* No free space for allocation */
|
---|
560 | *size = 0;
|
---|
561 | return NULL;
|
---|
562 | }
|
---|
563 |
|
---|
564 | /* Messing with exception table, avoid deadlock */
|
---|
565 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
566 |
|
---|
567 | unsigned int i;
|
---|
568 | for (i = 0; i < IVT_ITEMS; i++) {
|
---|
569 | stats_exceptions[i].id = i + IVT_FIRST;
|
---|
570 | str_cpy(stats_exceptions[i].desc, EXC_NAME_BUFLEN, exc_table[i].name);
|
---|
571 | stats_exceptions[i].cycles = exc_table[i].cycles;
|
---|
572 | stats_exceptions[i].count = exc_table[i].count;
|
---|
573 | }
|
---|
574 |
|
---|
575 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
576 |
|
---|
577 | return ((void *) stats_exceptions);
|
---|
578 | }
|
---|
579 |
|
---|
580 | /** Get exception statistics
|
---|
581 | *
|
---|
582 | * Get statistics of a given exception. The exception number
|
---|
583 | * is passed as a string (current limitation of the sysinfo
|
---|
584 | * interface, but it is still reasonable for the given purpose).
|
---|
585 | *
|
---|
586 | * @param name Exception number (string-encoded number).
|
---|
587 | * @param dry_run Do not get the data, just calculate the size.
|
---|
588 | *
|
---|
589 | * @return Sysinfo return holder. The type of the returned
|
---|
590 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
---|
591 | * exception number or memory allocation error) or
|
---|
592 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
---|
593 | * generated data should be freed within the
|
---|
594 | * sysinfo request context).
|
---|
595 | *
|
---|
596 | */
|
---|
597 | static sysinfo_return_t get_stats_exception(const char *name, bool dry_run)
|
---|
598 | {
|
---|
599 | /* Initially no return value */
|
---|
600 | sysinfo_return_t ret;
|
---|
601 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
---|
602 |
|
---|
603 | /* Parse the exception number */
|
---|
604 | uint64_t excn;
|
---|
605 | if (str_uint64(name, NULL, 0, true, &excn) != EOK)
|
---|
606 | return ret;
|
---|
607 |
|
---|
608 | #if IVT_FIRST > 0
|
---|
609 | if (excn < IVT_FIRST)
|
---|
610 | return ret;
|
---|
611 | #endif
|
---|
612 |
|
---|
613 | if (excn >= IVT_ITEMS + IVT_FIRST)
|
---|
614 | return ret;
|
---|
615 |
|
---|
616 | if (dry_run) {
|
---|
617 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
618 | ret.data.data = NULL;
|
---|
619 | ret.data.size = sizeof(stats_thread_t);
|
---|
620 | } else {
|
---|
621 | /* Update excn index for accessing exc_table */
|
---|
622 | excn -= IVT_FIRST;
|
---|
623 |
|
---|
624 | /* Allocate stats_exc_t structure */
|
---|
625 | stats_exc_t *stats_exception =
|
---|
626 | (stats_exc_t *) malloc(sizeof(stats_exc_t), FRAME_ATOMIC);
|
---|
627 | if (stats_exception == NULL)
|
---|
628 | return ret;
|
---|
629 |
|
---|
630 | /* Messing with exception table, avoid deadlock */
|
---|
631 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
632 |
|
---|
633 | /* Correct return value */
|
---|
634 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
635 | ret.data.data = (void *) stats_exception;
|
---|
636 | ret.data.size = sizeof(stats_exc_t);
|
---|
637 |
|
---|
638 | stats_exception->id = excn;
|
---|
639 | str_cpy(stats_exception->desc, EXC_NAME_BUFLEN, exc_table[excn].name);
|
---|
640 | stats_exception->cycles = exc_table[excn].cycles;
|
---|
641 | stats_exception->count = exc_table[excn].count;
|
---|
642 |
|
---|
643 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
644 | }
|
---|
645 |
|
---|
646 | return ret;
|
---|
647 | }
|
---|
648 |
|
---|
649 | /** Get physical memory statistics
|
---|
650 | *
|
---|
651 | * @param item Sysinfo item (unused).
|
---|
652 | * @param size Size of the returned data.
|
---|
653 | * @param dry_run Do not get the data, just calculate the size.
|
---|
654 | *
|
---|
655 | * @return Data containing stats_physmem_t.
|
---|
656 | * If the return value is not NULL, it should be freed
|
---|
657 | * in the context of the sysinfo request.
|
---|
658 | */
|
---|
659 | static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
|
---|
660 | bool dry_run)
|
---|
661 | {
|
---|
662 | *size = sizeof(stats_physmem_t);
|
---|
663 | if (dry_run)
|
---|
664 | return NULL;
|
---|
665 |
|
---|
666 | stats_physmem_t *stats_physmem =
|
---|
667 | (stats_physmem_t *) malloc(*size, FRAME_ATOMIC);
|
---|
668 | if (stats_physmem == NULL) {
|
---|
669 | *size = 0;
|
---|
670 | return NULL;
|
---|
671 | }
|
---|
672 |
|
---|
673 | zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
|
---|
674 | &(stats_physmem->used), &(stats_physmem->free));
|
---|
675 |
|
---|
676 | return ((void *) stats_physmem);
|
---|
677 | }
|
---|
678 |
|
---|
679 | /** Get system load
|
---|
680 | *
|
---|
681 | * @param item Sysinfo item (unused).
|
---|
682 | * @param size Size of the returned data.
|
---|
683 | * @param dry_run Do not get the data, just calculate the size.
|
---|
684 | *
|
---|
685 | * @return Data several load_t values.
|
---|
686 | * If the return value is not NULL, it should be freed
|
---|
687 | * in the context of the sysinfo request.
|
---|
688 | */
|
---|
689 | static void *get_stats_load(struct sysinfo_item *item, size_t *size,
|
---|
690 | bool dry_run)
|
---|
691 | {
|
---|
692 | *size = sizeof(load_t) * LOAD_STEPS;
|
---|
693 | if (dry_run)
|
---|
694 | return NULL;
|
---|
695 |
|
---|
696 | load_t *stats_load = (load_t *) malloc(*size, FRAME_ATOMIC);
|
---|
697 | if (stats_load == NULL) {
|
---|
698 | *size = 0;
|
---|
699 | return NULL;
|
---|
700 | }
|
---|
701 |
|
---|
702 | /* To always get consistent values acquire the mutex */
|
---|
703 | mutex_lock(&load_lock);
|
---|
704 |
|
---|
705 | unsigned int i;
|
---|
706 | for (i = 0; i < LOAD_STEPS; i++)
|
---|
707 | stats_load[i] = avenrdy[i] << LOAD_FIXED_SHIFT;
|
---|
708 |
|
---|
709 | mutex_unlock(&load_lock);
|
---|
710 |
|
---|
711 | return ((void *) stats_load);
|
---|
712 | }
|
---|
713 |
|
---|
714 | /** Calculate load
|
---|
715 | *
|
---|
716 | */
|
---|
717 | static inline load_t load_calc(load_t load, load_t exp, atomic_count_t ready)
|
---|
718 | {
|
---|
719 | load *= exp;
|
---|
720 | load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
|
---|
721 |
|
---|
722 | return (load >> LOAD_FIXED_SHIFT);
|
---|
723 | }
|
---|
724 |
|
---|
725 | /** Load computation thread.
|
---|
726 | *
|
---|
727 | * Compute system load every few seconds.
|
---|
728 | *
|
---|
729 | * @param arg Unused.
|
---|
730 | *
|
---|
731 | */
|
---|
732 | void kload(void *arg)
|
---|
733 | {
|
---|
734 | thread_detach(THREAD);
|
---|
735 |
|
---|
736 | while (true) {
|
---|
737 | atomic_count_t ready = atomic_get(&nrdy);
|
---|
738 |
|
---|
739 | /* Mutually exclude with get_stats_load() */
|
---|
740 | mutex_lock(&load_lock);
|
---|
741 |
|
---|
742 | unsigned int i;
|
---|
743 | for (i = 0; i < LOAD_STEPS; i++)
|
---|
744 | avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
|
---|
745 |
|
---|
746 | mutex_unlock(&load_lock);
|
---|
747 |
|
---|
748 | thread_sleep(LOAD_INTERVAL);
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | /** Register sysinfo statistical items
|
---|
753 | *
|
---|
754 | */
|
---|
755 | void stats_init(void)
|
---|
756 | {
|
---|
757 | mutex_initialize(&load_lock, MUTEX_PASSIVE);
|
---|
758 |
|
---|
759 | sysinfo_set_item_fn_val("system.uptime", NULL, get_stats_uptime);
|
---|
760 | sysinfo_set_item_fn_data("system.cpus", NULL, get_stats_cpus);
|
---|
761 | sysinfo_set_item_fn_data("system.physmem", NULL, get_stats_physmem);
|
---|
762 | sysinfo_set_item_fn_data("system.load", NULL, get_stats_load);
|
---|
763 | sysinfo_set_item_fn_data("system.tasks", NULL, get_stats_tasks);
|
---|
764 | sysinfo_set_item_fn_data("system.threads", NULL, get_stats_threads);
|
---|
765 | sysinfo_set_item_fn_data("system.exceptions", NULL, get_stats_exceptions);
|
---|
766 | sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task);
|
---|
767 | sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread);
|
---|
768 | sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception);
|
---|
769 | }
|
---|
770 |
|
---|
771 | /** @}
|
---|
772 | */
|
---|