kfsw-services/src/health/health.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/util.h> | ||
| 9 | |||
| 10 | #include <kfsw/platform/time.h> | ||
| 11 | #include <kfsw/platform/watchdog.h> | ||
| 12 | #if CONFIG_KFSW_LASTWORDS | ||
| 13 | #include <kfsw/platform/lastwords.h> | ||
| 14 | #endif | ||
| 15 | #include <kfsw/services/health.h> | ||
| 16 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_HEALTH | ||
| 17 | #include <kfsw/services/log.h> | ||
| 18 | #if CONFIG_KFSW_EVENT | ||
| 19 | #include <kfsw/services/event.h> | ||
| 20 | #endif | ||
| 21 | |||
| 22 | struct health_entry { | ||
| 23 | char name[KFSW_HEALTH_NAME_SIZE]; | ||
| 24 | uint32_t deadline_ms; | ||
| 25 | uint64_t last_report_ms; | ||
| 26 | uint32_t reports; | ||
| 27 | bool used; | ||
| 28 | /* Warned already, so the warning is logged once per crossing. */ | ||
| 29 | bool warned; | ||
| 30 | }; | ||
| 31 | |||
| 32 | /* Set at runtime because it depends on the watchdog timeout. */ | ||
| 33 | static uint32_t health_interval_ms = CONFIG_KFSW_HEALTH_INTERVAL_MS; | ||
| 34 | |||
| 35 | static K_MUTEX_DEFINE(health_lock); | ||
| 36 | static struct health_entry health_entries[KFSW_HEALTH_MAX_COMPONENTS]; | ||
| 37 | static struct kfsw_health_status health_state = { | ||
| 38 | .state = KFSW_HEALTH_STOPPED, | ||
| 39 | }; | ||
| 40 | |||
| 41 | static void health_work_handler(struct k_work *work); | ||
| 42 | K_WORK_DELAYABLE_DEFINE(health_work, health_work_handler); | ||
| 43 | |||
| 44 | /* Caller holds health_lock. */ | ||
| 45 | 108 | static struct health_entry *find_by_name(const char *name) | |
| 46 | { | ||
| 47 | 556 | for (uint8_t index = 0U; index < KFSW_HEALTH_MAX_COMPONENTS; index++) { | |
| 48 | 452 | if (health_entries[index].used && | |
| 49 | 162 | (strncmp(health_entries[index].name, name, KFSW_HEALTH_NAME_SIZE) == 0)) { | |
| 50 | 4 | return &health_entries[index]; | |
| 51 | } | ||
| 52 | } | ||
| 53 | 104 | return NULL; | |
| 54 | } | ||
| 55 | |||
| 56 | 124 | int kfsw_health_register(const char *name, uint32_t deadline_ms, uint8_t *handle) | |
| 57 | { | ||
| 58 | 124 | if ((name == NULL) || (handle == NULL) || (name[0] == '\0') || (deadline_ms == 0U)) { | |
| 59 | 16 | return -EINVAL; | |
| 60 | } | ||
| 61 | |||
| 62 | 108 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 63 | |||
| 64 | 108 | if (find_by_name(name) != NULL) { | |
| 65 | 4 | k_mutex_unlock(&health_lock); | |
| 66 | 4 | return -EEXIST; | |
| 67 | } | ||
| 68 | |||
| 69 | 250 | for (uint8_t index = 0U; index < KFSW_HEALTH_MAX_COMPONENTS; index++) { | |
| 70 | 226 | struct health_entry *entry = &health_entries[index]; | |
| 71 | |||
| 72 | 226 | if (entry->used) { | |
| 73 | 146 | continue; | |
| 74 | } | ||
| 75 | |||
| 76 | 80 | (void)strncpy(entry->name, name, KFSW_HEALTH_NAME_SIZE - 1U); | |
| 77 | 80 | entry->name[KFSW_HEALTH_NAME_SIZE - 1U] = '\0'; | |
| 78 | 80 | entry->deadline_ms = deadline_ms; | |
| 79 | 80 | entry->last_report_ms = kfsw_time_monotonic_ms(); | |
| 80 | 80 | entry->reports = 0U; | |
| 81 | 80 | entry->used = true; | |
| 82 | 80 | entry->warned = false; | |
| 83 | |||
| 84 | 80 | health_state.count++; | |
| 85 | 80 | *handle = index; | |
| 86 | |||
| 87 | 80 | k_mutex_unlock(&health_lock); | |
| 88 | 80 | kfsw_log_info("Health watching %s, deadline %u ms", entry->name, deadline_ms); | |
| 89 | 80 | return 0; | |
| 90 | } | ||
| 91 | |||
| 92 | 24 | k_mutex_unlock(&health_lock); | |
| 93 | 24 | return -ENOSPC; | |
| 94 | } | ||
| 95 | |||
| 96 | 302 | int kfsw_health_unregister(uint8_t handle) | |
| 97 | { | ||
| 98 | 302 | if (handle >= KFSW_HEALTH_MAX_COMPONENTS) { | |
| 99 | ✗ | return -EINVAL; | |
| 100 | } | ||
| 101 | |||
| 102 | 302 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 103 | |||
| 104 | 302 | if (!health_entries[handle].used) { | |
| 105 | 240 | k_mutex_unlock(&health_lock); | |
| 106 | 240 | return -EINVAL; | |
| 107 | } | ||
| 108 | |||
| 109 | /* If this component caused the fault, clear it. */ | ||
| 110 | 62 | if (strncmp(health_state.faulted_by, health_entries[handle].name, KFSW_HEALTH_NAME_SIZE) == | |
| 111 | 0) { | ||
| 112 | 4 | health_state.faulted_by[0] = '\0'; | |
| 113 | } | ||
| 114 | |||
| 115 | 62 | health_entries[handle].used = false; | |
| 116 | 62 | health_entries[handle].name[0] = '\0'; | |
| 117 | 62 | health_state.count--; | |
| 118 | |||
| 119 | 62 | k_mutex_unlock(&health_lock); | |
| 120 | 62 | kfsw_log_info("Health stopped watching handle %u", handle); | |
| 121 | 62 | return 0; | |
| 122 | } | ||
| 123 | |||
| 124 | 38 | int kfsw_health_report(uint8_t handle) | |
| 125 | { | ||
| 126 | 38 | if (handle >= KFSW_HEALTH_MAX_COMPONENTS) { | |
| 127 | 8 | return -EINVAL; | |
| 128 | } | ||
| 129 | |||
| 130 | 30 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 131 | |||
| 132 | 30 | if (!health_entries[handle].used) { | |
| 133 | 4 | k_mutex_unlock(&health_lock); | |
| 134 | 4 | return -EINVAL; | |
| 135 | } | ||
| 136 | |||
| 137 | 26 | health_entries[handle].last_report_ms = kfsw_time_monotonic_ms(); | |
| 138 | 26 | if (health_entries[handle].reports < UINT32_MAX) { | |
| 139 | 26 | health_entries[handle].reports++; | |
| 140 | } | ||
| 141 | |||
| 142 | 26 | k_mutex_unlock(&health_lock); | |
| 143 | 26 | return 0; | |
| 144 | } | ||
| 145 | |||
| 146 | 18 | int kfsw_health_evaluate(void) | |
| 147 | { | ||
| 148 | 18 | struct health_entry *overdue = NULL; | |
| 149 | char faulted_by[KFSW_HEALTH_NAME_SIZE]; | ||
| 150 | uint64_t now; | ||
| 151 | uint32_t elapsed; | ||
| 152 | bool was_ok; | ||
| 153 | |||
| 154 | 18 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 155 | |||
| 156 | 18 | if (health_state.state == KFSW_HEALTH_STOPPED) { | |
| 157 | 6 | k_mutex_unlock(&health_lock); | |
| 158 | 6 | return -EINVAL; | |
| 159 | } | ||
| 160 | |||
| 161 | 12 | now = kfsw_time_monotonic_ms(); | |
| 162 | 12 | was_ok = (health_state.state == KFSW_HEALTH_OK); | |
| 163 | |||
| 164 | 76 | for (uint8_t index = 0U; index < KFSW_HEALTH_MAX_COMPONENTS; index++) { | |
| 165 | 68 | struct health_entry *entry = &health_entries[index]; | |
| 166 | |||
| 167 | 68 | if (!entry->used) { | |
| 168 | 56 | continue; | |
| 169 | } | ||
| 170 | 12 | elapsed = (uint32_t)(now - entry->last_report_ms); | |
| 171 | 12 | if (elapsed > entry->deadline_ms) { | |
| 172 | 4 | overdue = entry; | |
| 173 | 4 | break; | |
| 174 | } | ||
| 175 | |||
| 176 | /* Warn once when a component is well into its deadline. */ | ||
| 177 | 8 | if ((elapsed > ((entry->deadline_ms * 3U) / 4U)) && !entry->warned) { | |
| 178 | ✗ | entry->warned = true; | |
| 179 | ✗ | kfsw_log_warning("Health: %s is %u ms into a %u ms deadline", entry->name, | |
| 180 | elapsed, entry->deadline_ms); | ||
| 181 | 8 | } else if (elapsed <= (entry->deadline_ms / 2U)) { | |
| 182 | 8 | entry->warned = false; | |
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | 12 | if (overdue == NULL) { | |
| 187 | 8 | if (!was_ok) { | |
| 188 | 4 | kfsw_log_info("Health: every component is reporting again"); | |
| 189 | #if CONFIG_KFSW_LASTWORDS | ||
| 190 | /* Fault cleared: withdraw our note. */ | ||
| 191 | 4 | (void)kfsw_lastwords_withdraw(KFSW_LASTWORDS_STARVED); | |
| 192 | #endif | ||
| 193 | } | ||
| 194 | 8 | health_state.state = KFSW_HEALTH_OK; | |
| 195 | 8 | health_state.faulted_by[0] = '\0'; | |
| 196 | 8 | k_mutex_unlock(&health_lock); | |
| 197 | |||
| 198 | 8 | if (kfsw_platform_watchdog_feed() == 0) { | |
| 199 | 8 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 200 | 8 | if (health_state.feeds < UINT32_MAX) { | |
| 201 | 8 | health_state.feeds++; | |
| 202 | } | ||
| 203 | 8 | health_state.feeding = true; | |
| 204 | 8 | k_mutex_unlock(&health_lock); | |
| 205 | } | ||
| 206 | 8 | return 0; | |
| 207 | } | ||
| 208 | |||
| 209 | 4 | health_state.state = KFSW_HEALTH_FAULTED; | |
| 210 | 4 | health_state.feeding = false; | |
| 211 | 4 | (void)strncpy(health_state.faulted_by, overdue->name, KFSW_HEALTH_NAME_SIZE - 1U); | |
| 212 | 4 | health_state.faulted_by[KFSW_HEALTH_NAME_SIZE - 1U] = '\0'; | |
| 213 | 4 | memcpy(faulted_by, health_state.faulted_by, sizeof(faulted_by)); | |
| 214 | 4 | if (was_ok) { | |
| 215 | 4 | health_state.faults++; | |
| 216 | } | ||
| 217 | 4 | k_mutex_unlock(&health_lock); | |
| 218 | |||
| 219 | /* Logged once per fault. */ | ||
| 220 | 4 | if (was_ok) { | |
| 221 | 4 | kfsw_log_error("Health: %s is overdue; the watchdog will no longer be fed", | |
| 222 | faulted_by); | ||
| 223 | #if CONFIG_KFSW_LASTWORDS | ||
| 224 | /* Write the note now; the detail is how long the component was silent. */ | ||
| 225 | 4 | kfsw_lastwords_write(KFSW_LASTWORDS_STARVED, elapsed, (uint32_t)now, 0U); | |
| 226 | #endif | ||
| 227 | #if CONFIG_KFSW_EVENT | ||
| 228 | 4 | kfsw_event_emit(KFSW_EVENT_SOURCE_APP, KFSW_EVENT_HEALTH_FAULT, KFSW_EVENT_CRITICAL, | |
| 229 | 4 | (const uint8_t *)faulted_by, (uint8_t)strlen(faulted_by)); | |
| 230 | #endif | ||
| 231 | } | ||
| 232 | |||
| 233 | /* Stop feeding but don't starve, so a component that recovers before the | ||
| 234 | * timeout avoids the reset. | ||
| 235 | */ | ||
| 236 | 4 | return -ETIMEDOUT; | |
| 237 | } | ||
| 238 | |||
| 239 | ✗ | static void health_work_handler(struct k_work *work) | |
| 240 | { | ||
| 241 | ARG_UNUSED(work); | ||
| 242 | |||
| 243 | ✗ | (void)kfsw_health_evaluate(); | |
| 244 | ✗ | k_mutex_lock(&health_lock, K_FOREVER); | |
| 245 | ✗ | (void)k_work_reschedule(&health_work, K_MSEC(health_interval_ms)); | |
| 246 | ✗ | k_mutex_unlock(&health_lock); | |
| 247 | ✗ | } | |
| 248 | |||
| 249 | 8 | int kfsw_health_start(void) | |
| 250 | { | ||
| 251 | uint64_t now; | ||
| 252 | int result; | ||
| 253 | |||
| 254 | 8 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 255 | 8 | if (health_state.state != KFSW_HEALTH_STOPPED) { | |
| 256 | ✗ | k_mutex_unlock(&health_lock); | |
| 257 | ✗ | return -EALREADY; | |
| 258 | } | ||
| 259 | 8 | result = kfsw_health_check_interval_ms(health_interval_ms); | |
| 260 | 8 | if (result != 0) { | |
| 261 | ✗ | k_mutex_unlock(&health_lock); | |
| 262 | ✗ | return result; | |
| 263 | } | ||
| 264 | 8 | result = kfsw_platform_watchdog_release(); | |
| 265 | 8 | if (result != 0) { | |
| 266 | 4 | k_mutex_unlock(&health_lock); | |
| 267 | 4 | kfsw_log_error("Health could not take over the watchdog: %d", result); | |
| 268 | 4 | return result; | |
| 269 | } | ||
| 270 | |||
| 271 | 4 | now = kfsw_time_monotonic_ms(); | |
| 272 | 36 | for (uint8_t index = 0U; index < KFSW_HEALTH_MAX_COMPONENTS; index++) { | |
| 273 | 32 | if (health_entries[index].used) { | |
| 274 | /* Treat every component as just reported. */ | ||
| 275 | 4 | health_entries[index].last_report_ms = now; | |
| 276 | } | ||
| 277 | } | ||
| 278 | 4 | health_state.state = KFSW_HEALTH_OK; | |
| 279 | 4 | (void)k_work_reschedule(&health_work, K_MSEC(health_interval_ms)); | |
| 280 | 4 | kfsw_log_info("Health supervising %u component(s) every %u ms", health_state.count, | |
| 281 | health_interval_ms); | ||
| 282 | 4 | k_mutex_unlock(&health_lock); | |
| 283 | kfsw_log_debug("Health took the watchdog over from the platform keep-alive"); | ||
| 284 | 4 | return 0; | |
| 285 | } | ||
| 286 | |||
| 287 | 30 | int kfsw_health_get_status(struct kfsw_health_status *status) | |
| 288 | { | ||
| 289 | 30 | if (status == NULL) { | |
| 290 | 4 | return -EINVAL; | |
| 291 | } | ||
| 292 | |||
| 293 | 26 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 294 | 26 | *status = health_state; | |
| 295 | 26 | k_mutex_unlock(&health_lock); | |
| 296 | |||
| 297 | 26 | return 0; | |
| 298 | } | ||
| 299 | |||
| 300 | 142 | int kfsw_health_get_component(uint8_t index, struct kfsw_health_component *component) | |
| 301 | { | ||
| 302 | uint64_t now; | ||
| 303 | |||
| 304 | 142 | if (component == NULL) { | |
| 305 | 4 | return -EINVAL; | |
| 306 | } | ||
| 307 | 138 | if (index >= KFSW_HEALTH_MAX_COMPONENTS) { | |
| 308 | 4 | return -ENOENT; | |
| 309 | } | ||
| 310 | |||
| 311 | 134 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 312 | |||
| 313 | 134 | if (!health_entries[index].used) { | |
| 314 | 68 | k_mutex_unlock(&health_lock); | |
| 315 | 68 | return -ENOENT; | |
| 316 | } | ||
| 317 | |||
| 318 | 66 | now = kfsw_time_monotonic_ms(); | |
| 319 | 66 | (void)strncpy(component->name, health_entries[index].name, KFSW_HEALTH_NAME_SIZE); | |
| 320 | 66 | component->name[KFSW_HEALTH_NAME_SIZE - 1U] = '\0'; | |
| 321 | 66 | component->deadline_ms = health_entries[index].deadline_ms; | |
| 322 | 66 | component->since_report_ms = (uint32_t)(now - health_entries[index].last_report_ms); | |
| 323 | 66 | component->reports = health_entries[index].reports; | |
| 324 | 66 | component->overdue = component->since_report_ms > component->deadline_ms; | |
| 325 | |||
| 326 | 66 | k_mutex_unlock(&health_lock); | |
| 327 | 66 | return 0; | |
| 328 | } | ||
| 329 | |||
| 330 | 10 | uint32_t kfsw_health_get_interval_ms(void) | |
| 331 | { | ||
| 332 | uint32_t interval; | ||
| 333 | |||
| 334 | 10 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 335 | 10 | interval = health_interval_ms; | |
| 336 | 10 | k_mutex_unlock(&health_lock); | |
| 337 | 10 | return interval; | |
| 338 | } | ||
| 339 | |||
| 340 | 108 | int kfsw_health_check_interval_ms(uint32_t interval_ms) | |
| 341 | { | ||
| 342 | struct kfsw_platform_watchdog_info watchdog; | ||
| 343 | 108 | uint32_t timeout = kfsw_platform_watchdog_configured_timeout_ms(); | |
| 344 | uint32_t feed_interval; | ||
| 345 | |||
| 346 | 108 | if (interval_ms == 0U) { | |
| 347 | 8 | return -EINVAL; | |
| 348 | } | ||
| 349 | |||
| 350 | 100 | if ((kfsw_platform_watchdog_get_info(&watchdog) == 0) && (watchdog.timeout_ms != 0U)) { | |
| 351 | ✗ | timeout = watchdog.timeout_ms; | |
| 352 | } | ||
| 353 | 100 | if (timeout == 0U) { | |
| 354 | 92 | return 0; | |
| 355 | } | ||
| 356 | |||
| 357 | 8 | feed_interval = kfsw_platform_watchdog_feed_interval_ms(timeout); | |
| 358 | 8 | if (interval_ms > feed_interval) { | |
| 359 | 4 | kfsw_log_warning("Health: a %u ms check is slower than the %u ms feed interval", | |
| 360 | interval_ms, feed_interval); | ||
| 361 | 4 | return -ERANGE; | |
| 362 | } | ||
| 363 | 4 | return 0; | |
| 364 | } | ||
| 365 | |||
| 366 | 84 | int kfsw_health_set_interval_ms(uint32_t interval_ms) | |
| 367 | { | ||
| 368 | int result; | ||
| 369 | |||
| 370 | 84 | k_mutex_lock(&health_lock, K_FOREVER); | |
| 371 | 84 | result = kfsw_health_check_interval_ms(interval_ms); | |
| 372 | 84 | if (result != 0) { | |
| 373 | 4 | k_mutex_unlock(&health_lock); | |
| 374 | 4 | return result; | |
| 375 | } | ||
| 376 | |||
| 377 | 80 | health_interval_ms = interval_ms; | |
| 378 | 80 | if (health_state.state != KFSW_HEALTH_STOPPED) { | |
| 379 | ✗ | (void)k_work_reschedule(&health_work, K_MSEC(health_interval_ms)); | |
| 380 | } | ||
| 381 | 80 | k_mutex_unlock(&health_lock); | |
| 382 | |||
| 383 | 80 | kfsw_log_info("Health: checking every %u ms", interval_ms); | |
| 384 | 80 | return 0; | |
| 385 | } | ||
| 386 | |||
| 387 | 16 | const char *kfsw_health_state_name(enum kfsw_health_state state) | |
| 388 | { | ||
| 389 | 16 | switch (state) { | |
| 390 | 4 | case KFSW_HEALTH_OK: | |
| 391 | 4 | return "ok"; | |
| 392 | 4 | case KFSW_HEALTH_FAULTED: | |
| 393 | 4 | return "faulted"; | |
| 394 | 4 | case KFSW_HEALTH_STOPPED: | |
| 395 | 4 | return "stopped"; | |
| 396 | 4 | default: | |
| 397 | 4 | return "unknown"; | |
| 398 | } | ||
| 399 | } | ||
| 400 |