|
| 1 | +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. |
| 2 | +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md |
| 3 | + |
| 4 | +#include "prof.h" |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +struct prof_pool |
| 9 | +{ |
| 10 | + struct task_prof_data *pool; |
| 11 | + unsigned int _overflow_counter; |
| 12 | + unsigned int _pos; |
| 13 | + |
| 14 | + struct prof_pool_init_data data; |
| 15 | + void (*clean)(); |
| 16 | + void (*handle_overflow)(uint32_t id); |
| 17 | + struct task_prof_data* (*get)(uint32_t id); |
| 18 | +}; |
| 19 | + |
| 20 | + |
| 21 | +static struct prof_pool pool; |
| 22 | + |
| 23 | +static void _pool_clean() |
| 24 | +{ |
| 25 | + memset(pool.pool, 0, sizeof(*pool.pool)*pool.data.size); |
| 26 | + pool._overflow_counter =0; |
| 27 | + pool._pos = 0; |
| 28 | +} |
| 29 | + |
| 30 | +static void _pool_overflow(uint32_t id) |
| 31 | +{ |
| 32 | + pool._overflow_counter = id; |
| 33 | +} |
| 34 | + |
| 35 | +/// just meant to be fast get of element |
| 36 | +static struct task_prof_data* _pool_get(uint32_t id) |
| 37 | +{ |
| 38 | + if ( pool._pos == pool.data.size ) { |
| 39 | + pool.handle_overflow(id); |
| 40 | + } |
| 41 | + for ( size_t i =0; i < pool.data.size && i != pool._pos; ++i ) { |
| 42 | + if (id == pool.pool[i].task_TCB_id) { |
| 43 | + return &pool.pool[i]; |
| 44 | + } |
| 45 | + } |
| 46 | + struct task_prof_data* p = &pool.pool[pool._pos]; |
| 47 | + pool._pos++; |
| 48 | + return p; |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +void prof_pool_init(struct prof_pool_init_data init) |
| 54 | +{ |
| 55 | + pool.data = init; |
| 56 | + pool.clean = _pool_clean; |
| 57 | + pool.handle_overflow = _pool_overflow; |
| 58 | + pool.get = _pool_get; |
| 59 | + |
| 60 | + pool.pool = (struct task_prof_data *)(malloc(sizeof(struct task_prof_data)*pool.data.size)); |
| 61 | + pool.clean(); |
| 62 | +} |
| 63 | + |
| 64 | +void prof_pool_deinit() |
| 65 | +{ |
| 66 | + free(pool.pool); |
| 67 | + pool.pool = NULL; |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +struct prof_pool_init_data prof_pool_get_data() |
| 72 | +{ |
| 73 | + return pool.data; |
| 74 | +} |
| 75 | + |
| 76 | +void prof_pool_data_set(uint8_t ts, uint32_t id) |
| 77 | +{ |
| 78 | + struct task_prof_data *what = pool.get(id); |
| 79 | + if ( what == NULL) { |
| 80 | + return; |
| 81 | + } |
| 82 | + what->task_TCB_id=id; |
| 83 | + what->exec_time += ts; |
| 84 | + ++(what->switches); |
| 85 | +} |
| 86 | + |
| 87 | +unsigned int prof_pool_overflow() |
| 88 | +{ |
| 89 | + return pool._overflow_counter; |
| 90 | +} |
| 91 | + |
| 92 | +unsigned int prof_pool_flush(struct task_prof_data *mem, size_t cap) |
| 93 | +{ |
| 94 | + unsigned int to_ret = pool._overflow_counter; |
| 95 | + memcpy(mem, pool.pool, cap * (sizeof(struct task_prof_data))); |
| 96 | + pool.clean(); |
| 97 | + return to_ret; |
| 98 | +} |
0 commit comments