14#if DETWS_ENABLE_GUARDRAILS
18uint8_t detws_guardrail_eval(
const DetwsHealth *h, uint32_t heap_min, uint32_t frag_min_block, uint32_t stack_min)
20 uint8_t b = DetwsBreach::DETWS_BREACH_NONE;
23 if (h->free_heap < heap_min)
24 b |= DetwsBreach::DETWS_BREACH_HEAP;
25 if (h->largest_free_block < frag_min_block)
26 b |= DetwsBreach::DETWS_BREACH_FRAG;
27 if (h->stack_free < stack_min)
28 b |= DetwsBreach::DETWS_BREACH_STACK;
32int detws_health_json(
const DetwsHealth *h,
char *out,
size_t cap)
39 int w = snprintf(out, cap,
"{\"free_heap\":%u,\"min_free_heap\":%u,\"largest_free_block\":%u,\"stack_free\":%u}",
40 (
unsigned)h->free_heap, (
unsigned)h->min_free_heap, (
unsigned)h->largest_free_block,
41 (
unsigned)h->stack_free);
42 if (w < 0 || (
size_t)w >= cap)
52#include "esp_heap_caps.h"
53#include "esp_system.h"
54#include "freertos/FreeRTOS.h"
55#include "freertos/task.h"
63 detws_breach_fn cb =
nullptr;
68void detws_guardrails_sample(DetwsHealth *h)
72 h->free_heap = esp_get_free_heap_size();
73 h->min_free_heap = esp_get_minimum_free_heap_size();
74 h->largest_free_block = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT);
75 h->stack_free = (uint32_t)uxTaskGetStackHighWaterMark(
nullptr) *
sizeof(StackType_t);
78void detws_guardrails_begin(detws_breach_fn cb)
83uint8_t detws_guardrails_check(
void)
86 detws_guardrails_sample(&h);
89 if (b != DetwsBreach::DETWS_BREACH_NONE && s_gr.cb)
96void detws_guardrails_sample(DetwsHealth *h)
101void detws_guardrails_begin(detws_breach_fn)
104uint8_t detws_guardrails_check(
void)
106 return DetwsBreach::DETWS_BREACH_NONE;
#define DETWS_GUARDRAIL_STACK_MIN
Task remaining-stack floor (bytes); below this trips the stack guardrail.
#define DETWS_GUARDRAIL_HEAP_MIN
Free-heap floor (bytes); below this trips the heap guardrail.
#define DETWS_GUARDRAIL_FRAG_MIN_BLOCK
Largest-free-block floor (bytes); below this trips the fragmentation guardrail.
Runtime heap/stack guardrails (DETWS_ENABLE_GUARDRAILS).