kfsw-services/src/resmon/resmon.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include <zephyr/kernel.h> | ||
| 8 | #include <zephyr/sys/byteorder.h> | ||
| 9 | #include <zephyr/sys/util.h> | ||
| 10 | |||
| 11 | #include <kfsw/services/resmon.h> | ||
| 12 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_RESMON | ||
| 13 | #include <kfsw/services/log.h> | ||
| 14 | #if CONFIG_KFSW_EVENT | ||
| 15 | #include <kfsw/services/event.h> | ||
| 16 | #endif | ||
| 17 | |||
| 18 | /* Allow low alert thresholds for native tests, which use host stacks. | ||
| 19 | */ | ||
| 20 | #define KFSW_RESMON_PERCENT_MIN 1U | ||
| 21 | |||
| 22 | /* What one walk of the thread list found. */ | ||
| 23 | struct sweep { | ||
| 24 | /* The busiest thread, by percentage used. */ | ||
| 25 | const struct k_thread *thread; | ||
| 26 | uint32_t used_percent; | ||
| 27 | uint32_t unused_bytes; | ||
| 28 | uint32_t stack_bytes; | ||
| 29 | /* The thread with the least room left, which is rarely the same one. */ | ||
| 30 | const struct k_thread *tightest; | ||
| 31 | uint32_t tightest_unused_bytes; | ||
| 32 | uint32_t tightest_stack_bytes; | ||
| 33 | uint16_t threads; | ||
| 34 | uint16_t unmeasured; | ||
| 35 | }; | ||
| 36 | |||
| 37 | static K_MUTEX_DEFINE(resmon_lock); | ||
| 38 | static struct kfsw_resmon_status resmon_state = { | ||
| 39 | .alert_percent = CONFIG_KFSW_RESMON_ALERT_PERCENT, | ||
| 40 | }; | ||
| 41 | /* Latch each threshold crossing until a sweep falls below it. | ||
| 42 | */ | ||
| 43 | static bool alerting; | ||
| 44 | |||
| 45 | static void resmon_work_handler(struct k_work *work); | ||
| 46 | K_WORK_DELAYABLE_DEFINE(resmon_work, resmon_work_handler); | ||
| 47 | |||
| 48 | 72 | static void visit_thread(const struct k_thread *thread, void *user_data) | |
| 49 | { | ||
| 50 | 72 | struct sweep *sweep = user_data; | |
| 51 | 72 | size_t stack_bytes = thread->stack_info.size; | |
| 52 | 72 | size_t unused = 0U; | |
| 53 | uint32_t used_percent; | ||
| 54 | |||
| 55 | /* A stack that cannot be read is counted as unmeasured rather than | ||
| 56 | * dropped, so a margin is never published for part of the system as if | ||
| 57 | * it were all of it. A stack too small to hold a call frame is the same | ||
| 58 | * case wearing a number: nothing runs in a few dozen bytes, so the | ||
| 59 | * figure describes whoever reported it and not the thread. | ||
| 60 | */ | ||
| 61 | 144 | if ((stack_bytes < (size_t)CONFIG_KFSW_RESMON_MIN_STACK_BYTES) || | |
| 62 | 72 | (k_thread_stack_space_get(thread, &unused) != 0)) { | |
| 63 | ✗ | sweep->unmeasured++; | |
| 64 | ✗ | return; | |
| 65 | } | ||
| 66 | |||
| 67 | 72 | used_percent = (uint32_t)(((stack_bytes - MIN(unused, stack_bytes)) * 100U) / stack_bytes); | |
| 68 | |||
| 69 | 72 | if ((sweep->threads == 0U) || (used_percent > sweep->used_percent)) { | |
| 70 | 36 | sweep->used_percent = used_percent; | |
| 71 | 36 | sweep->unused_bytes = (uint32_t)MIN(unused, (size_t)UINT32_MAX); | |
| 72 | 36 | sweep->stack_bytes = (uint32_t)MIN(stack_bytes, (size_t)UINT32_MAX); | |
| 73 | 36 | sweep->thread = thread; | |
| 74 | } | ||
| 75 | 72 | if ((sweep->threads == 0U) || (unused < sweep->tightest_unused_bytes)) { | |
| 76 | 36 | sweep->tightest_unused_bytes = (uint32_t)MIN(unused, (size_t)UINT32_MAX); | |
| 77 | 36 | sweep->tightest_stack_bytes = (uint32_t)MIN(stack_bytes, (size_t)UINT32_MAX); | |
| 78 | 36 | sweep->tightest = thread; | |
| 79 | } | ||
| 80 | 72 | sweep->threads++; | |
| 81 | } | ||
| 82 | |||
| 83 | 20 | static void copy_thread_name(char *destination, const struct k_thread *thread) | |
| 84 | { | ||
| 85 | 20 | const char *name = NULL; | |
| 86 | |||
| 87 | 20 | if (thread != NULL) { | |
| 88 | 20 | name = k_thread_name_get((k_tid_t)thread); | |
| 89 | } | ||
| 90 | 20 | if ((name == NULL) || (name[0] == '\0')) { | |
| 91 | ✗ | name = "unnamed"; | |
| 92 | } | ||
| 93 | |||
| 94 | 20 | (void)strncpy(destination, name, KFSW_RESMON_NAME_SIZE - 1U); | |
| 95 | 20 | destination[KFSW_RESMON_NAME_SIZE - 1U] = '\0'; | |
| 96 | 20 | } | |
| 97 | |||
| 98 | 18 | int kfsw_resmon_sample(void) | |
| 99 | { | ||
| 100 | 18 | struct sweep sweep = {0}; | |
| 101 | char name[KFSW_RESMON_NAME_SIZE]; | ||
| 102 | 18 | bool crossed = false; | |
| 103 | |||
| 104 | 18 | k_thread_foreach_unlocked(visit_thread, &sweep); | |
| 105 | |||
| 106 | 18 | if (sweep.threads == 0U) { | |
| 107 | ✗ | return -ENODATA; | |
| 108 | } | ||
| 109 | |||
| 110 | 18 | copy_thread_name(name, sweep.thread); | |
| 111 | |||
| 112 | 18 | k_mutex_lock(&resmon_lock, K_FOREVER); | |
| 113 | 18 | if (resmon_state.sweeps < UINT32_MAX) { | |
| 114 | 18 | resmon_state.sweeps++; | |
| 115 | } | ||
| 116 | 18 | resmon_state.threads = sweep.threads; | |
| 117 | 18 | resmon_state.unmeasured = sweep.unmeasured; | |
| 118 | 18 | resmon_state.last_used_percent = sweep.used_percent; | |
| 119 | |||
| 120 | 18 | if ((resmon_state.sweeps == 1U) || | |
| 121 | 16 | (sweep.tightest_unused_bytes < resmon_state.tightest_unused_bytes)) { | |
| 122 | 2 | resmon_state.tightest_unused_bytes = sweep.tightest_unused_bytes; | |
| 123 | 2 | resmon_state.tightest_stack_bytes = sweep.tightest_stack_bytes; | |
| 124 | 2 | copy_thread_name(resmon_state.tightest_thread, sweep.tightest); | |
| 125 | } | ||
| 126 | |||
| 127 | 18 | if (sweep.used_percent >= resmon_state.worst_used_percent) { | |
| 128 | 18 | resmon_state.worst_used_percent = sweep.used_percent; | |
| 129 | 18 | resmon_state.worst_unused_bytes = sweep.unused_bytes; | |
| 130 | 18 | resmon_state.worst_stack_bytes = sweep.stack_bytes; | |
| 131 | 18 | (void)strncpy(resmon_state.worst_thread, name, KFSW_RESMON_NAME_SIZE - 1U); | |
| 132 | 18 | resmon_state.worst_thread[KFSW_RESMON_NAME_SIZE - 1U] = '\0'; | |
| 133 | } | ||
| 134 | |||
| 135 | 18 | if (sweep.used_percent >= resmon_state.alert_percent) { | |
| 136 | 6 | if (!alerting) { | |
| 137 | 4 | alerting = true; | |
| 138 | 4 | crossed = true; | |
| 139 | 4 | if (resmon_state.alerts < UINT32_MAX) { | |
| 140 | 4 | resmon_state.alerts++; | |
| 141 | } | ||
| 142 | } | ||
| 143 | } else { | ||
| 144 | 12 | alerting = false; | |
| 145 | } | ||
| 146 | 18 | k_mutex_unlock(&resmon_lock); | |
| 147 | |||
| 148 | 18 | if (crossed) { | |
| 149 | 4 | kfsw_log_warning("Resource monitor: %s is at %u%% of its stack, %u bytes left", | |
| 150 | name, sweep.used_percent, sweep.unused_bytes); | ||
| 151 | #if CONFIG_KFSW_EVENT | ||
| 152 | { | ||
| 153 | uint8_t payload[5]; | ||
| 154 | |||
| 155 | payload[0] = (uint8_t)MIN(sweep.used_percent, 100U); | ||
| 156 | sys_put_be32(sweep.unused_bytes, &payload[1]); | ||
| 157 | kfsw_event_emit(KFSW_EVENT_SOURCE_RESMON, KFSW_EVENT_RESMON_LOW_STACK, | ||
| 158 | KFSW_EVENT_WARNING, payload, sizeof(payload)); | ||
| 159 | } | ||
| 160 | #endif | ||
| 161 | } | ||
| 162 | |||
| 163 | 18 | return (int)sweep.threads; | |
| 164 | } | ||
| 165 | |||
| 166 | ✗ | static void resmon_work_handler(struct k_work *work) | |
| 167 | { | ||
| 168 | ARG_UNUSED(work); | ||
| 169 | |||
| 170 | ✗ | (void)kfsw_resmon_sample(); | |
| 171 | |||
| 172 | ✗ | k_mutex_lock(&resmon_lock, K_FOREVER); | |
| 173 | ✗ | if (resmon_state.running) { | |
| 174 | ✗ | (void)k_work_reschedule(&resmon_work, K_MSEC(CONFIG_KFSW_RESMON_PERIOD_MS)); | |
| 175 | } | ||
| 176 | ✗ | k_mutex_unlock(&resmon_lock); | |
| 177 | ✗ | } | |
| 178 | |||
| 179 | 4 | int kfsw_resmon_start(void) | |
| 180 | { | ||
| 181 | 4 | k_mutex_lock(&resmon_lock, K_FOREVER); | |
| 182 | 4 | if (resmon_state.running) { | |
| 183 | 2 | k_mutex_unlock(&resmon_lock); | |
| 184 | 2 | return -EALREADY; | |
| 185 | } | ||
| 186 | 2 | resmon_state.running = true; | |
| 187 | 2 | k_mutex_unlock(&resmon_lock); | |
| 188 | |||
| 189 | /* Sweep once now, so the numbers are readable without waiting a period. */ | ||
| 190 | 2 | (void)kfsw_resmon_sample(); | |
| 191 | 2 | (void)k_work_reschedule(&resmon_work, K_MSEC(CONFIG_KFSW_RESMON_PERIOD_MS)); | |
| 192 | 2 | kfsw_log_info("Resource monitor started, every %u ms, alert at %u%%", | |
| 193 | CONFIG_KFSW_RESMON_PERIOD_MS, resmon_state.alert_percent); | ||
| 194 | 2 | return 0; | |
| 195 | } | ||
| 196 | |||
| 197 | 18 | int kfsw_resmon_stop(void) | |
| 198 | { | ||
| 199 | 18 | k_mutex_lock(&resmon_lock, K_FOREVER); | |
| 200 | 18 | if (!resmon_state.running) { | |
| 201 | 16 | k_mutex_unlock(&resmon_lock); | |
| 202 | 16 | return -EALREADY; | |
| 203 | } | ||
| 204 | 2 | resmon_state.running = false; | |
| 205 | 2 | k_mutex_unlock(&resmon_lock); | |
| 206 | |||
| 207 | 2 | (void)k_work_cancel_delayable(&resmon_work); | |
| 208 | 2 | return 0; | |
| 209 | } | ||
| 210 | |||
| 211 | 20 | void kfsw_resmon_get_status(struct kfsw_resmon_status *status) | |
| 212 | { | ||
| 213 | 20 | if (status == NULL) { | |
| 214 | 2 | return; | |
| 215 | } | ||
| 216 | |||
| 217 | 18 | k_mutex_lock(&resmon_lock, K_FOREVER); | |
| 218 | 18 | *status = resmon_state; | |
| 219 | 18 | k_mutex_unlock(&resmon_lock); | |
| 220 | } | ||
| 221 | |||
| 222 | 24 | int kfsw_resmon_set_alert_percent(uint32_t percent) | |
| 223 | { | ||
| 224 | 24 | if ((percent < KFSW_RESMON_PERCENT_MIN) || (percent > 100U)) { | |
| 225 | 4 | return -ERANGE; | |
| 226 | } | ||
| 227 | |||
| 228 | 20 | k_mutex_lock(&resmon_lock, K_FOREVER); | |
| 229 | 20 | resmon_state.alert_percent = percent; | |
| 230 | /* A new threshold decides its own crossing on the next sweep. */ | ||
| 231 | 20 | alerting = false; | |
| 232 | 20 | k_mutex_unlock(&resmon_lock); | |
| 233 | 20 | return 0; | |
| 234 | } | ||
| 235 |