|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <nrf_rpc/nrf_rpc_serialize.h> |
| 8 | +#include <rpc_utils_group.h> |
| 9 | + |
| 10 | +#include <nrf_rpc_cbor.h> |
| 11 | + |
| 12 | +#include <zephyr/kernel.h> |
| 13 | +#include <zephyr/sys/atomic.h> |
| 14 | +#include <zephyr/sys/util.h> |
| 15 | + |
| 16 | +#ifdef CONFIG_OPENTHREAD |
| 17 | +#include <zephyr/net/openthread.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +/* |
| 21 | + * Pointer to a function that "kicks" a watched thread. |
| 22 | + * |
| 23 | + * Currently, only work queue threads can be watched. |
| 24 | + * "Kicking" means submitting a task to the thread, which marks the thread as alive (not hung). |
| 25 | + */ |
| 26 | +typedef void (*watched_thread_kick_fn)(struct k_work *work); |
| 27 | + |
| 28 | +static void kick_system_workq(struct k_work *work) |
| 29 | +{ |
| 30 | + (void)k_work_submit(work); |
| 31 | +} |
| 32 | + |
| 33 | +#ifdef CONFIG_OPENTHREAD |
| 34 | +static void kick_openthread(struct k_work *work) |
| 35 | +{ |
| 36 | + k_tid_t tid = openthread_thread_id_get(); |
| 37 | + struct k_work_q *queue = CONTAINER_OF(tid, struct k_work_q, thread); |
| 38 | + |
| 39 | + (void)k_work_submit_to_queue(queue, work); |
| 40 | +} |
| 41 | +#endif |
| 42 | + |
| 43 | +#ifdef CONFIG_BT_LONG_WQ |
| 44 | +extern int bt_long_wq_submit(struct k_work *work); |
| 45 | + |
| 46 | +static void kick_bluetooth_long(struct k_work *work) |
| 47 | +{ |
| 48 | + (void)bt_long_wq_submit(work); |
| 49 | +} |
| 50 | +#endif |
| 51 | + |
| 52 | +const static watched_thread_kick_fn watched_thread_kick[] = { |
| 53 | + kick_system_workq, |
| 54 | +#ifdef CONFIG_OPENTHREAD |
| 55 | + kick_openthread, |
| 56 | +#endif |
| 57 | +#ifdef CONFIG_BT_LONG_WQ |
| 58 | + kick_bluetooth_long, |
| 59 | +#endif |
| 60 | +}; |
| 61 | + |
| 62 | +enum { |
| 63 | + NUM_WATCHED_THREADS = ARRAY_SIZE(watched_thread_kick), |
| 64 | +}; |
| 65 | + |
| 66 | +static atomic_t hung_threads; |
| 67 | +static atomic_t hung_thread_candidates; |
| 68 | +static void watchdog_timer_handler(struct k_timer *timer); |
| 69 | +static K_TIMER_DEFINE(watchdog_timer, watchdog_timer_handler, NULL); |
| 70 | +static struct k_work watched_thread_work[NUM_WATCHED_THREADS]; |
| 71 | + |
| 72 | +/* |
| 73 | + * Function called by a watched thread to prove it's not hung. |
| 74 | + */ |
| 75 | +static void watchdog_feed(struct k_work *work) |
| 76 | +{ |
| 77 | + const size_t index = ARRAY_INDEX(watched_thread_work, work); |
| 78 | + |
| 79 | + atomic_clear_bit(&hung_thread_candidates, index); |
| 80 | +} |
| 81 | + |
| 82 | +static void watchdog_timer_handler(struct k_timer *timer) |
| 83 | +{ |
| 84 | + const atomic_val_t ALL_THREADS = GENMASK(NUM_WATCHED_THREADS - 1, 0); |
| 85 | + |
| 86 | + atomic_set(&hung_threads, atomic_set(&hung_thread_candidates, ALL_THREADS)); |
| 87 | + |
| 88 | + for (size_t i = 0; i < NUM_WATCHED_THREADS; i++) { |
| 89 | + watched_thread_kick[i](&watched_thread_work[i]); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +static int watchdog_init(void) |
| 94 | +{ |
| 95 | + for (size_t i = 0; i < NUM_WATCHED_THREADS; i++) { |
| 96 | + k_work_init(&watched_thread_work[i], watchdog_feed); |
| 97 | + } |
| 98 | + |
| 99 | + k_timer_start(&watchdog_timer, K_NO_WAIT, K_SECONDS(CONFIG_NRF_RPC_UTILS_WATCHDOG_PERIOD)); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +SYS_INIT(watchdog_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); |
| 105 | + |
| 106 | +static void system_health_get_handler(const struct nrf_rpc_group *group, |
| 107 | + struct nrf_rpc_cbor_ctx *ctx, void *handler_data) |
| 108 | +{ |
| 109 | + nrf_rpc_cbor_decoding_done(group, ctx); |
| 110 | + nrf_rpc_rsp_send_uint(group, atomic_get(&hung_threads)); |
| 111 | +} |
| 112 | + |
| 113 | +NRF_RPC_CBOR_CMD_DECODER(rpc_utils_group, system_health_get, RPC_UTIL_SYSTEM_HEALTH_GET, |
| 114 | + system_health_get_handler, NULL); |
0 commit comments