1 | /*
|
---|
2 | * Copyright (c) 2010 Stanislav Kozina
|
---|
3 | * Copyright (c) 2010 Martin Decky
|
---|
4 | * Copyright (c) 2018 Jiri Svoboda
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @addtogroup kernel_generic
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <typedefs.h>
|
---|
39 | #include <abi/sysinfo.h>
|
---|
40 | #include <sysinfo/stats.h>
|
---|
41 | #include <sysinfo/sysinfo.h>
|
---|
42 | #include <synch/spinlock.h>
|
---|
43 | #include <synch/mutex.h>
|
---|
44 | #include <time/clock.h>
|
---|
45 | #include <mm/frame.h>
|
---|
46 | #include <proc/task.h>
|
---|
47 | #include <proc/thread.h>
|
---|
48 | #include <interrupt.h>
|
---|
49 | #include <stdbool.h>
|
---|
50 | #include <str.h>
|
---|
51 | #include <errno.h>
|
---|
52 | #include <cpu.h>
|
---|
53 | #include <arch.h>
|
---|
54 |
|
---|
55 | /** Bits of fixed-point precision for load */
|
---|
56 | #define LOAD_FIXED_SHIFT 11
|
---|
57 |
|
---|
58 | /** Uspace load fixed-point precision */
|
---|
59 | #define LOAD_USPACE_SHIFT 6
|
---|
60 |
|
---|
61 | /** Kernel load shift */
|
---|
62 | #define LOAD_KERNEL_SHIFT (LOAD_FIXED_SHIFT - LOAD_USPACE_SHIFT)
|
---|
63 |
|
---|
64 | /** 1.0 as fixed-point for load */
|
---|
65 | #define LOAD_FIXED_1 (1 << LOAD_FIXED_SHIFT)
|
---|
66 |
|
---|
67 | /** Compute load in 5 second intervals */
|
---|
68 | #define LOAD_INTERVAL 5
|
---|
69 |
|
---|
70 | /** Fixed-point representation of
|
---|
71 | *
|
---|
72 | * 1 / exp(5 sec / 1 min)
|
---|
73 | * 1 / exp(5 sec / 5 min)
|
---|
74 | * 1 / exp(5 sec / 15 min)
|
---|
75 | *
|
---|
76 | */
|
---|
77 | static load_t load_exp[LOAD_STEPS] = { 1884, 2014, 2037 };
|
---|
78 |
|
---|
79 | /** Running average of the number of ready threads */
|
---|
80 | static load_t avenrdy[LOAD_STEPS] = { 0, 0, 0 };
|
---|
81 |
|
---|
82 | /** Load calculation lock */
|
---|
83 | static mutex_t load_lock;
|
---|
84 |
|
---|
85 | /** Get statistics of all CPUs
|
---|
86 | *
|
---|
87 | * @param item Sysinfo item (unused).
|
---|
88 | * @param size Size of the returned data.
|
---|
89 | * @param dry_run Do not get the data, just calculate the size.
|
---|
90 | * @param data Unused.
|
---|
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 | */
|
---|
96 | static void *get_stats_cpus(struct sysinfo_item *item, size_t *size,
|
---|
97 | bool dry_run, void *data)
|
---|
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);
|
---|
105 | if (stats_cpus == NULL) {
|
---|
106 | *size = 0;
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 | size_t i;
|
---|
111 | for (i = 0; i < config.cpu_count; i++) {
|
---|
112 | irq_spinlock_lock(&cpus[i].lock, true);
|
---|
113 |
|
---|
114 | stats_cpus[i].id = cpus[i].id;
|
---|
115 | stats_cpus[i].active = cpus[i].active;
|
---|
116 | stats_cpus[i].frequency_mhz = cpus[i].frequency_mhz;
|
---|
117 | stats_cpus[i].busy_cycles = cpus[i].busy_cycles;
|
---|
118 | stats_cpus[i].idle_cycles = cpus[i].idle_cycles;
|
---|
119 |
|
---|
120 | irq_spinlock_unlock(&cpus[i].lock, true);
|
---|
121 | }
|
---|
122 |
|
---|
123 | return ((void *) stats_cpus);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Get the size of a virtual address space
|
---|
127 | *
|
---|
128 | * @param as Address space.
|
---|
129 | *
|
---|
130 | * @return Size of the mapped virtual address space (bytes).
|
---|
131 | *
|
---|
132 | */
|
---|
133 | static size_t get_task_virtmem(as_t *as)
|
---|
134 | {
|
---|
135 | /*
|
---|
136 | * We are holding spinlocks here and therefore are not allowed to
|
---|
137 | * block. Only attempt to lock the address space and address space
|
---|
138 | * area mutexes conditionally. If it is not possible to lock either
|
---|
139 | * object, return inexact statistics by skipping the respective object.
|
---|
140 | */
|
---|
141 |
|
---|
142 | if (mutex_trylock(&as->lock) != EOK)
|
---|
143 | return 0;
|
---|
144 |
|
---|
145 | size_t pages = 0;
|
---|
146 |
|
---|
147 | /* Walk areas in the address space and count pages */
|
---|
148 | as_area_t *area = as_area_first(as);
|
---|
149 | while (area != NULL) {
|
---|
150 | if (mutex_trylock(&area->lock) != EOK)
|
---|
151 | continue;
|
---|
152 |
|
---|
153 | pages += area->pages;
|
---|
154 | mutex_unlock(&area->lock);
|
---|
155 | area = as_area_next(area);
|
---|
156 | }
|
---|
157 |
|
---|
158 | mutex_unlock(&as->lock);
|
---|
159 |
|
---|
160 | return (pages << PAGE_WIDTH);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /** Get the resident (used) size of a virtual address space
|
---|
164 | *
|
---|
165 | * @param as Address space.
|
---|
166 | *
|
---|
167 | * @return Size of the resident (used) virtual address space (bytes).
|
---|
168 | *
|
---|
169 | */
|
---|
170 | static size_t get_task_resmem(as_t *as)
|
---|
171 | {
|
---|
172 | /*
|
---|
173 | * We are holding spinlocks here and therefore are not allowed to
|
---|
174 | * block. Only attempt to lock the address space and address space
|
---|
175 | * area mutexes conditionally. If it is not possible to lock either
|
---|
176 | * object, return inexact statistics by skipping the respective object.
|
---|
177 | */
|
---|
178 |
|
---|
179 | if (mutex_trylock(&as->lock) != EOK)
|
---|
180 | return 0;
|
---|
181 |
|
---|
182 | size_t pages = 0;
|
---|
183 |
|
---|
184 | /* Walk areas in the address space and count pages */
|
---|
185 | as_area_t *area = as_area_first(as);
|
---|
186 | while (area != NULL) {
|
---|
187 | if (mutex_trylock(&area->lock) != EOK)
|
---|
188 | continue;
|
---|
189 |
|
---|
190 | pages += area->resident;
|
---|
191 | mutex_unlock(&area->lock);
|
---|
192 | area = as_area_next(area);
|
---|
193 | }
|
---|
194 |
|
---|
195 | mutex_unlock(&as->lock);
|
---|
196 |
|
---|
197 | return (pages << PAGE_WIDTH);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** Produce task statistics
|
---|
201 | *
|
---|
202 | * Summarize task information into task statistics.
|
---|
203 | *
|
---|
204 | * @param task Task.
|
---|
205 | * @param stats_task Task statistics.
|
---|
206 | *
|
---|
207 | */
|
---|
208 | static void produce_stats_task(task_t *task, stats_task_t *stats_task)
|
---|
209 | {
|
---|
210 | assert(interrupts_disabled());
|
---|
211 | assert(irq_spinlock_locked(&task->lock));
|
---|
212 |
|
---|
213 | stats_task->task_id = task->taskid;
|
---|
214 | str_cpy(stats_task->name, TASK_NAME_BUFLEN, task->name);
|
---|
215 | stats_task->virtmem = get_task_virtmem(task->as);
|
---|
216 | stats_task->resmem = get_task_resmem(task->as);
|
---|
217 | stats_task->threads = atomic_load(&task->refcount);
|
---|
218 | task_get_accounting(task, &(stats_task->ucycles),
|
---|
219 | &(stats_task->kcycles));
|
---|
220 | stats_task->ipc_info = task->ipc_info;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Get task statistics
|
---|
224 | *
|
---|
225 | * @param item Sysinfo item (unused).
|
---|
226 | * @param size Size of the returned data.
|
---|
227 | * @param dry_run Do not get the data, just calculate the size.
|
---|
228 | * @param data Unused.
|
---|
229 | *
|
---|
230 | * @return Data containing several stats_task_t structures.
|
---|
231 | * If the return value is not NULL, it should be freed
|
---|
232 | * in the context of the sysinfo request.
|
---|
233 | */
|
---|
234 | static void *get_stats_tasks(struct sysinfo_item *item, size_t *size,
|
---|
235 | bool dry_run, void *data)
|
---|
236 | {
|
---|
237 | /* Messing with task structures, avoid deadlock */
|
---|
238 | irq_spinlock_lock(&tasks_lock, true);
|
---|
239 |
|
---|
240 | /* Count the tasks */
|
---|
241 | size_t count = task_count();
|
---|
242 |
|
---|
243 | if (count == 0) {
|
---|
244 | /* No tasks found (strange) */
|
---|
245 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
246 | *size = 0;
|
---|
247 | return NULL;
|
---|
248 | }
|
---|
249 |
|
---|
250 | *size = sizeof(stats_task_t) * count;
|
---|
251 | if (dry_run) {
|
---|
252 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
253 | return NULL;
|
---|
254 | }
|
---|
255 |
|
---|
256 | stats_task_t *stats_tasks = (stats_task_t *) malloc(*size);
|
---|
257 | if (stats_tasks == NULL) {
|
---|
258 | /* No free space for allocation */
|
---|
259 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
260 | *size = 0;
|
---|
261 | return NULL;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* Gather the statistics for each task */
|
---|
265 | size_t i = 0;
|
---|
266 | task_t *task = task_first();
|
---|
267 | while (task != NULL) {
|
---|
268 | /* Interrupts are already disabled */
|
---|
269 | irq_spinlock_lock(&(task->lock), false);
|
---|
270 |
|
---|
271 | /* Record the statistics and increment the index */
|
---|
272 | produce_stats_task(task, &stats_tasks[i]);
|
---|
273 | i++;
|
---|
274 |
|
---|
275 | irq_spinlock_unlock(&(task->lock), false);
|
---|
276 | task = task_next(task);
|
---|
277 | }
|
---|
278 |
|
---|
279 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
280 |
|
---|
281 | return ((void *) stats_tasks);
|
---|
282 | }
|
---|
283 |
|
---|
284 | /** Produce thread statistics
|
---|
285 | *
|
---|
286 | * Summarize thread information into thread statistics.
|
---|
287 | *
|
---|
288 | * @param thread Thread.
|
---|
289 | * @param stats_thread Thread statistics.
|
---|
290 | *
|
---|
291 | */
|
---|
292 | static void produce_stats_thread(thread_t *thread, stats_thread_t *stats_thread)
|
---|
293 | {
|
---|
294 | assert(interrupts_disabled());
|
---|
295 | assert(irq_spinlock_locked(&thread->lock));
|
---|
296 |
|
---|
297 | stats_thread->thread_id = thread->tid;
|
---|
298 | stats_thread->task_id = thread->task->taskid;
|
---|
299 | stats_thread->state = thread->state;
|
---|
300 | stats_thread->priority = thread->priority;
|
---|
301 | stats_thread->ucycles = thread->ucycles;
|
---|
302 | stats_thread->kcycles = thread->kcycles;
|
---|
303 |
|
---|
304 | if (thread->cpu != NULL) {
|
---|
305 | stats_thread->on_cpu = true;
|
---|
306 | stats_thread->cpu = thread->cpu->id;
|
---|
307 | } else
|
---|
308 | stats_thread->on_cpu = false;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /** Get thread statistics
|
---|
312 | *
|
---|
313 | * @param item Sysinfo item (unused).
|
---|
314 | * @param size Size of the returned data.
|
---|
315 | * @param dry_run Do not get the data, just calculate the size.
|
---|
316 | * @param data Unused.
|
---|
317 | *
|
---|
318 | * @return Data containing several stats_task_t structures.
|
---|
319 | * If the return value is not NULL, it should be freed
|
---|
320 | * in the context of the sysinfo request.
|
---|
321 | */
|
---|
322 | static void *get_stats_threads(struct sysinfo_item *item, size_t *size,
|
---|
323 | bool dry_run, void *data)
|
---|
324 | {
|
---|
325 | /* Messing with threads structures, avoid deadlock */
|
---|
326 | irq_spinlock_lock(&threads_lock, true);
|
---|
327 |
|
---|
328 | /* Count the threads */
|
---|
329 | size_t count = thread_count();
|
---|
330 |
|
---|
331 | if (count == 0) {
|
---|
332 | /* No threads found (strange) */
|
---|
333 | irq_spinlock_unlock(&threads_lock, true);
|
---|
334 | *size = 0;
|
---|
335 | return NULL;
|
---|
336 | }
|
---|
337 |
|
---|
338 | *size = sizeof(stats_thread_t) * count;
|
---|
339 | if (dry_run) {
|
---|
340 | irq_spinlock_unlock(&threads_lock, true);
|
---|
341 | return NULL;
|
---|
342 | }
|
---|
343 |
|
---|
344 | stats_thread_t *stats_threads = (stats_thread_t *) malloc(*size);
|
---|
345 | if (stats_threads == NULL) {
|
---|
346 | /* No free space for allocation */
|
---|
347 | irq_spinlock_unlock(&threads_lock, true);
|
---|
348 | *size = 0;
|
---|
349 | return NULL;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /* Walk tha thread tree again to gather the statistics */
|
---|
353 | size_t i = 0;
|
---|
354 |
|
---|
355 | thread_t *thread = thread_first();
|
---|
356 | while (thread != NULL) {
|
---|
357 | /* Interrupts are already disabled */
|
---|
358 | irq_spinlock_lock(&thread->lock, false);
|
---|
359 |
|
---|
360 | /* Record the statistics and increment the index */
|
---|
361 | produce_stats_thread(thread, &stats_threads[i]);
|
---|
362 | i++;
|
---|
363 |
|
---|
364 | irq_spinlock_unlock(&thread->lock, false);
|
---|
365 |
|
---|
366 | thread = thread_next(thread);
|
---|
367 | }
|
---|
368 |
|
---|
369 | irq_spinlock_unlock(&threads_lock, true);
|
---|
370 |
|
---|
371 | return ((void *) stats_threads);
|
---|
372 | }
|
---|
373 |
|
---|
374 | /** Get a single task statistics
|
---|
375 | *
|
---|
376 | * Get statistics of a given task. The task ID is passed
|
---|
377 | * as a string (current limitation of the sysinfo interface,
|
---|
378 | * but it is still reasonable for the given purpose).
|
---|
379 | *
|
---|
380 | * @param name Task ID (string-encoded number).
|
---|
381 | * @param dry_run Do not get the data, just calculate the size.
|
---|
382 | * @param data Unused.
|
---|
383 | *
|
---|
384 | * @return Sysinfo return holder. The type of the returned
|
---|
385 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
---|
386 | * task ID or memory allocation error) or
|
---|
387 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
---|
388 | * generated data should be freed within the
|
---|
389 | * sysinfo request context).
|
---|
390 | *
|
---|
391 | */
|
---|
392 | static sysinfo_return_t get_stats_task(const char *name, bool dry_run,
|
---|
393 | void *data)
|
---|
394 | {
|
---|
395 | /* Initially no return value */
|
---|
396 | sysinfo_return_t ret;
|
---|
397 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
---|
398 |
|
---|
399 | /* Parse the task ID */
|
---|
400 | task_id_t task_id;
|
---|
401 | if (str_uint64_t(name, NULL, 0, true, &task_id) != EOK)
|
---|
402 | return ret;
|
---|
403 |
|
---|
404 | /* Messing with task structures, avoid deadlock */
|
---|
405 | irq_spinlock_lock(&tasks_lock, true);
|
---|
406 |
|
---|
407 | task_t *task = task_find_by_id(task_id);
|
---|
408 | if (task == NULL) {
|
---|
409 | /* No task with this ID */
|
---|
410 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
411 | return ret;
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (dry_run) {
|
---|
415 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
416 | ret.data.data = NULL;
|
---|
417 | ret.data.size = sizeof(stats_task_t);
|
---|
418 |
|
---|
419 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
420 | } else {
|
---|
421 | /* Allocate stats_task_t structure */
|
---|
422 | stats_task_t *stats_task =
|
---|
423 | (stats_task_t *) malloc(sizeof(stats_task_t));
|
---|
424 | if (stats_task == NULL) {
|
---|
425 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
426 | return ret;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /* Correct return value */
|
---|
430 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
431 | ret.data.data = (void *) stats_task;
|
---|
432 | ret.data.size = sizeof(stats_task_t);
|
---|
433 |
|
---|
434 | /* Hand-over-hand locking */
|
---|
435 | irq_spinlock_exchange(&tasks_lock, &task->lock);
|
---|
436 |
|
---|
437 | produce_stats_task(task, stats_task);
|
---|
438 |
|
---|
439 | irq_spinlock_unlock(&task->lock, true);
|
---|
440 | }
|
---|
441 |
|
---|
442 | return ret;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /** Get thread statistics
|
---|
446 | *
|
---|
447 | * Get statistics of a given thread. The thread ID is passed
|
---|
448 | * as a string (current limitation of the sysinfo interface,
|
---|
449 | * but it is still reasonable for the given purpose).
|
---|
450 | *
|
---|
451 | * @param name Thread ID (string-encoded number).
|
---|
452 | * @param dry_run Do not get the data, just calculate the size.
|
---|
453 | * @param data Unused.
|
---|
454 | *
|
---|
455 | * @return Sysinfo return holder. The type of the returned
|
---|
456 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
---|
457 | * thread ID or memory allocation error) or
|
---|
458 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
---|
459 | * generated data should be freed within the
|
---|
460 | * sysinfo request context).
|
---|
461 | *
|
---|
462 | */
|
---|
463 | static sysinfo_return_t get_stats_thread(const char *name, bool dry_run,
|
---|
464 | void *data)
|
---|
465 | {
|
---|
466 | /* Initially no return value */
|
---|
467 | sysinfo_return_t ret;
|
---|
468 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
---|
469 |
|
---|
470 | /* Parse the thread ID */
|
---|
471 | thread_id_t thread_id;
|
---|
472 | if (str_uint64_t(name, NULL, 0, true, &thread_id) != EOK)
|
---|
473 | return ret;
|
---|
474 |
|
---|
475 | /* Messing with threads structures, avoid deadlock */
|
---|
476 | irq_spinlock_lock(&threads_lock, true);
|
---|
477 |
|
---|
478 | thread_t *thread = thread_find_by_id(thread_id);
|
---|
479 | if (thread == NULL) {
|
---|
480 | /* No thread with this ID */
|
---|
481 | irq_spinlock_unlock(&threads_lock, true);
|
---|
482 | return ret;
|
---|
483 | }
|
---|
484 |
|
---|
485 | if (dry_run) {
|
---|
486 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
487 | ret.data.data = NULL;
|
---|
488 | ret.data.size = sizeof(stats_thread_t);
|
---|
489 |
|
---|
490 | irq_spinlock_unlock(&threads_lock, true);
|
---|
491 | } else {
|
---|
492 | /* Allocate stats_thread_t structure */
|
---|
493 | stats_thread_t *stats_thread =
|
---|
494 | (stats_thread_t *) malloc(sizeof(stats_thread_t));
|
---|
495 | if (stats_thread == NULL) {
|
---|
496 | irq_spinlock_unlock(&threads_lock, true);
|
---|
497 | return ret;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /* Correct return value */
|
---|
501 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
502 | ret.data.data = (void *) stats_thread;
|
---|
503 | ret.data.size = sizeof(stats_thread_t);
|
---|
504 |
|
---|
505 | /* Hand-over-hand locking */
|
---|
506 | irq_spinlock_exchange(&threads_lock, &thread->lock);
|
---|
507 |
|
---|
508 | produce_stats_thread(thread, stats_thread);
|
---|
509 |
|
---|
510 | irq_spinlock_unlock(&thread->lock, true);
|
---|
511 | }
|
---|
512 |
|
---|
513 | return ret;
|
---|
514 | }
|
---|
515 |
|
---|
516 | /** Get exceptions statistics
|
---|
517 | *
|
---|
518 | * @param item Sysinfo item (unused).
|
---|
519 | * @param size Size of the returned data.
|
---|
520 | * @param dry_run Do not get the data, just calculate the size.
|
---|
521 | * @param data Unused.
|
---|
522 | *
|
---|
523 | * @return Data containing several stats_exc_t structures.
|
---|
524 | * If the return value is not NULL, it should be freed
|
---|
525 | * in the context of the sysinfo request.
|
---|
526 | */
|
---|
527 | static void *get_stats_exceptions(struct sysinfo_item *item, size_t *size,
|
---|
528 | bool dry_run, void *data)
|
---|
529 | {
|
---|
530 | *size = sizeof(stats_exc_t) * IVT_ITEMS;
|
---|
531 |
|
---|
532 | if ((dry_run) || (IVT_ITEMS == 0))
|
---|
533 | return NULL;
|
---|
534 |
|
---|
535 | stats_exc_t *stats_exceptions =
|
---|
536 | (stats_exc_t *) malloc(*size);
|
---|
537 | if (stats_exceptions == NULL) {
|
---|
538 | /* No free space for allocation */
|
---|
539 | *size = 0;
|
---|
540 | return NULL;
|
---|
541 | }
|
---|
542 |
|
---|
543 | #if (IVT_ITEMS > 0)
|
---|
544 | /* Messing with exception table, avoid deadlock */
|
---|
545 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
546 |
|
---|
547 | unsigned int i;
|
---|
548 | for (i = 0; i < IVT_ITEMS; i++) {
|
---|
549 | stats_exceptions[i].id = i + IVT_FIRST;
|
---|
550 | str_cpy(stats_exceptions[i].desc, EXC_NAME_BUFLEN, exc_table[i].name);
|
---|
551 | stats_exceptions[i].hot = exc_table[i].hot;
|
---|
552 | stats_exceptions[i].cycles = exc_table[i].cycles;
|
---|
553 | stats_exceptions[i].count = exc_table[i].count;
|
---|
554 | }
|
---|
555 |
|
---|
556 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
557 | #endif
|
---|
558 |
|
---|
559 | return ((void *) stats_exceptions);
|
---|
560 | }
|
---|
561 |
|
---|
562 | /** Get exception statistics
|
---|
563 | *
|
---|
564 | * Get statistics of a given exception. The exception number
|
---|
565 | * is passed as a string (current limitation of the sysinfo
|
---|
566 | * interface, but it is still reasonable for the given purpose).
|
---|
567 | *
|
---|
568 | * @param name Exception number (string-encoded number).
|
---|
569 | * @param dry_run Do not get the data, just calculate the size.
|
---|
570 | * @param data Unused.
|
---|
571 | *
|
---|
572 | * @return Sysinfo return holder. The type of the returned
|
---|
573 | * data is either SYSINFO_VAL_UNDEFINED (unknown
|
---|
574 | * exception number or memory allocation error) or
|
---|
575 | * SYSINFO_VAL_FUNCTION_DATA (in that case the
|
---|
576 | * generated data should be freed within the
|
---|
577 | * sysinfo request context).
|
---|
578 | *
|
---|
579 | */
|
---|
580 | static sysinfo_return_t get_stats_exception(const char *name, bool dry_run,
|
---|
581 | void *data)
|
---|
582 | {
|
---|
583 | /* Initially no return value */
|
---|
584 | sysinfo_return_t ret;
|
---|
585 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
---|
586 |
|
---|
587 | /* Parse the exception number */
|
---|
588 | uint64_t excn;
|
---|
589 | if (str_uint64_t(name, NULL, 0, true, &excn) != EOK)
|
---|
590 | return ret;
|
---|
591 |
|
---|
592 | #if (IVT_FIRST > 0)
|
---|
593 | if (excn < IVT_FIRST)
|
---|
594 | return ret;
|
---|
595 | #endif
|
---|
596 |
|
---|
597 | #if (IVT_ITEMS + IVT_FIRST == 0)
|
---|
598 | return ret;
|
---|
599 | #else
|
---|
600 | if (excn >= IVT_ITEMS + IVT_FIRST)
|
---|
601 | return ret;
|
---|
602 | #endif
|
---|
603 |
|
---|
604 | if (dry_run) {
|
---|
605 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
606 | ret.data.data = NULL;
|
---|
607 | ret.data.size = sizeof(stats_thread_t);
|
---|
608 | } else {
|
---|
609 | /* Update excn index for accessing exc_table */
|
---|
610 | excn -= IVT_FIRST;
|
---|
611 |
|
---|
612 | /* Allocate stats_exc_t structure */
|
---|
613 | stats_exc_t *stats_exception =
|
---|
614 | (stats_exc_t *) malloc(sizeof(stats_exc_t));
|
---|
615 | if (stats_exception == NULL)
|
---|
616 | return ret;
|
---|
617 |
|
---|
618 | /* Messing with exception table, avoid deadlock */
|
---|
619 | irq_spinlock_lock(&exctbl_lock, true);
|
---|
620 |
|
---|
621 | /* Correct return value */
|
---|
622 | ret.tag = SYSINFO_VAL_FUNCTION_DATA;
|
---|
623 | ret.data.data = (void *) stats_exception;
|
---|
624 | ret.data.size = sizeof(stats_exc_t);
|
---|
625 |
|
---|
626 | stats_exception->id = excn;
|
---|
627 | str_cpy(stats_exception->desc, EXC_NAME_BUFLEN, exc_table[excn].name);
|
---|
628 | stats_exception->hot = exc_table[excn].hot;
|
---|
629 | stats_exception->cycles = exc_table[excn].cycles;
|
---|
630 | stats_exception->count = exc_table[excn].count;
|
---|
631 |
|
---|
632 | irq_spinlock_unlock(&exctbl_lock, true);
|
---|
633 | }
|
---|
634 |
|
---|
635 | return ret;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /** Get physical memory statistics
|
---|
639 | *
|
---|
640 | * @param item Sysinfo item (unused).
|
---|
641 | * @param size Size of the returned data.
|
---|
642 | * @param dry_run Do not get the data, just calculate the size.
|
---|
643 | * @param data Unused.
|
---|
644 | *
|
---|
645 | * @return Data containing stats_physmem_t.
|
---|
646 | * If the return value is not NULL, it should be freed
|
---|
647 | * in the context of the sysinfo request.
|
---|
648 | */
|
---|
649 | static void *get_stats_physmem(struct sysinfo_item *item, size_t *size,
|
---|
650 | bool dry_run, void *data)
|
---|
651 | {
|
---|
652 | *size = sizeof(stats_physmem_t);
|
---|
653 | if (dry_run)
|
---|
654 | return NULL;
|
---|
655 |
|
---|
656 | stats_physmem_t *stats_physmem =
|
---|
657 | (stats_physmem_t *) malloc(*size);
|
---|
658 | if (stats_physmem == NULL) {
|
---|
659 | *size = 0;
|
---|
660 | return NULL;
|
---|
661 | }
|
---|
662 |
|
---|
663 | zones_stats(&(stats_physmem->total), &(stats_physmem->unavail),
|
---|
664 | &(stats_physmem->used), &(stats_physmem->free));
|
---|
665 |
|
---|
666 | return ((void *) stats_physmem);
|
---|
667 | }
|
---|
668 |
|
---|
669 | /** Get system load
|
---|
670 | *
|
---|
671 | * @param item Sysinfo item (unused).
|
---|
672 | * @param size Size of the returned data.
|
---|
673 | * @param dry_run Do not get the data, just calculate the size.
|
---|
674 | * @param data Unused.
|
---|
675 | *
|
---|
676 | * @return Data several load_t values.
|
---|
677 | * If the return value is not NULL, it should be freed
|
---|
678 | * in the context of the sysinfo request.
|
---|
679 | */
|
---|
680 | static void *get_stats_load(struct sysinfo_item *item, size_t *size,
|
---|
681 | bool dry_run, void *data)
|
---|
682 | {
|
---|
683 | *size = sizeof(load_t) * LOAD_STEPS;
|
---|
684 | if (dry_run)
|
---|
685 | return NULL;
|
---|
686 |
|
---|
687 | load_t *stats_load = (load_t *) malloc(*size);
|
---|
688 | if (stats_load == NULL) {
|
---|
689 | *size = 0;
|
---|
690 | return NULL;
|
---|
691 | }
|
---|
692 |
|
---|
693 | /* To always get consistent values acquire the mutex */
|
---|
694 | mutex_lock(&load_lock);
|
---|
695 |
|
---|
696 | unsigned int i;
|
---|
697 | for (i = 0; i < LOAD_STEPS; i++)
|
---|
698 | stats_load[i] = avenrdy[i] << LOAD_KERNEL_SHIFT;
|
---|
699 |
|
---|
700 | mutex_unlock(&load_lock);
|
---|
701 |
|
---|
702 | return ((void *) stats_load);
|
---|
703 | }
|
---|
704 |
|
---|
705 | /** Calculate load
|
---|
706 | *
|
---|
707 | */
|
---|
708 | static inline load_t load_calc(load_t load, load_t exp, size_t ready)
|
---|
709 | {
|
---|
710 | load *= exp;
|
---|
711 | load += (ready << LOAD_FIXED_SHIFT) * (LOAD_FIXED_1 - exp);
|
---|
712 |
|
---|
713 | return (load >> LOAD_FIXED_SHIFT);
|
---|
714 | }
|
---|
715 |
|
---|
716 | /** Load computation thread.
|
---|
717 | *
|
---|
718 | * Compute system load every few seconds.
|
---|
719 | *
|
---|
720 | * @param arg Unused.
|
---|
721 | *
|
---|
722 | */
|
---|
723 | void kload(void *arg)
|
---|
724 | {
|
---|
725 | thread_detach(THREAD);
|
---|
726 |
|
---|
727 | while (true) {
|
---|
728 | size_t ready = atomic_load(&nrdy);
|
---|
729 |
|
---|
730 | /* Mutually exclude with get_stats_load() */
|
---|
731 | mutex_lock(&load_lock);
|
---|
732 |
|
---|
733 | unsigned int i;
|
---|
734 | for (i = 0; i < LOAD_STEPS; i++)
|
---|
735 | avenrdy[i] = load_calc(avenrdy[i], load_exp[i], ready);
|
---|
736 |
|
---|
737 | mutex_unlock(&load_lock);
|
---|
738 |
|
---|
739 | thread_sleep(LOAD_INTERVAL);
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | /** Register sysinfo statistical items
|
---|
744 | *
|
---|
745 | */
|
---|
746 | void stats_init(void)
|
---|
747 | {
|
---|
748 | mutex_initialize(&load_lock, MUTEX_PASSIVE);
|
---|
749 |
|
---|
750 | sysinfo_set_item_gen_data("system.cpus", NULL, get_stats_cpus, NULL);
|
---|
751 | sysinfo_set_item_gen_data("system.physmem", NULL, get_stats_physmem, NULL);
|
---|
752 | sysinfo_set_item_gen_data("system.load", NULL, get_stats_load, NULL);
|
---|
753 | sysinfo_set_item_gen_data("system.tasks", NULL, get_stats_tasks, NULL);
|
---|
754 | sysinfo_set_item_gen_data("system.threads", NULL, get_stats_threads, NULL);
|
---|
755 | sysinfo_set_item_gen_data("system.exceptions", NULL, get_stats_exceptions, NULL);
|
---|
756 | sysinfo_set_subtree_fn("system.tasks", NULL, get_stats_task, NULL);
|
---|
757 | sysinfo_set_subtree_fn("system.threads", NULL, get_stats_thread, NULL);
|
---|
758 | sysinfo_set_subtree_fn("system.exceptions", NULL, get_stats_exception, NULL);
|
---|
759 | }
|
---|
760 |
|
---|
761 | /** @}
|
---|
762 | */
|
---|