kfsw-services/src/event/event.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/services/event.h> | ||
| 11 | #if CONFIG_KFSW_JOURNAL | ||
| 12 | #include <kfsw/services/journal.h> | ||
| 13 | #endif | ||
| 14 | |||
| 15 | /* Fixed ring of records. A spinlock allows recording from any context. */ | ||
| 16 | |||
| 17 | static struct kfsw_event_record ring[CONFIG_KFSW_EVENT_RING_DEPTH]; | ||
| 18 | static struct k_spinlock ring_lock; | ||
| 19 | |||
| 20 | static uint32_t next_sequence; | ||
| 21 | static uint32_t recorded_count; | ||
| 22 | static uint32_t rejected_count; | ||
| 23 | static uint32_t overwritten_count; | ||
| 24 | static uint16_t held_count; | ||
| 25 | /* Index of the oldest held record. */ | ||
| 26 | static uint16_t oldest_index; | ||
| 27 | |||
| 28 | 484 | static bool source_is_known(enum kfsw_event_source source) | |
| 29 | { | ||
| 30 | 484 | switch (source) { | |
| 31 | 476 | case KFSW_EVENT_SOURCE_BOOT: | |
| 32 | case KFSW_EVENT_SOURCE_COMMAND: | ||
| 33 | case KFSW_EVENT_SOURCE_FTP: | ||
| 34 | case KFSW_EVENT_SOURCE_APP: | ||
| 35 | case KFSW_EVENT_SOURCE_FBO: | ||
| 36 | case KFSW_EVENT_SOURCE_GNDWDT: | ||
| 37 | case KFSW_EVENT_SOURCE_RESMON: | ||
| 38 | case KFSW_EVENT_SOURCE_TABLE: | ||
| 39 | case KFSW_EVENT_SOURCE_SCRUB: | ||
| 40 | case KFSW_EVENT_SOURCE_LIMITS: | ||
| 41 | 476 | return true; | |
| 42 | 8 | default: | |
| 43 | 8 | return false; | |
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | 484 | void kfsw_event_emit(enum kfsw_event_source source, uint16_t id, enum kfsw_event_severity severity, | |
| 48 | const void *payload, size_t payload_size) | ||
| 49 | { | ||
| 50 | struct kfsw_event_record *slot; | ||
| 51 | #if CONFIG_KFSW_JOURNAL | ||
| 52 | struct kfsw_event_record copy; | ||
| 53 | #endif | ||
| 54 | k_spinlock_key_t key; | ||
| 55 | uint16_t write_index; | ||
| 56 | |||
| 57 | 484 | if (!source_is_known(source) || (severity < KFSW_EVENT_INFO) || | |
| 58 | 474 | (severity > KFSW_EVENT_CRITICAL) || (payload_size > KFSW_EVENT_MAX_PAYLOAD_SIZE) || | |
| 59 | 418 | ((payload_size != 0U) && (payload == NULL))) { | |
| 60 | 14 | key = k_spin_lock(&ring_lock); | |
| 61 | 14 | rejected_count++; | |
| 62 | 14 | k_spin_unlock(&ring_lock, key); | |
| 63 | 14 | return; | |
| 64 | } | ||
| 65 | |||
| 66 | 470 | key = k_spin_lock(&ring_lock); | |
| 67 | |||
| 68 | 470 | write_index = (uint16_t)((oldest_index + held_count) % ARRAY_SIZE(ring)); | |
| 69 | 470 | if (held_count == ARRAY_SIZE(ring)) { | |
| 70 | /* Full: the oldest record is replaced and the loss is counted. */ | ||
| 71 | 120 | overwritten_count++; | |
| 72 | 120 | oldest_index = (uint16_t)((oldest_index + 1U) % ARRAY_SIZE(ring)); | |
| 73 | } else { | ||
| 74 | 350 | held_count++; | |
| 75 | } | ||
| 76 | |||
| 77 | 470 | slot = &ring[write_index]; | |
| 78 | 470 | slot->monotonic_us = (uint64_t)k_ticks_to_us_floor64(k_uptime_ticks()); | |
| 79 | 470 | slot->sequence = next_sequence; | |
| 80 | 470 | slot->source = (uint16_t)source; | |
| 81 | 470 | slot->id = id; | |
| 82 | 470 | slot->severity = (uint8_t)severity; | |
| 83 | 470 | slot->payload_size = (uint8_t)payload_size; | |
| 84 | 470 | if (payload_size != 0U) { | |
| 85 | 416 | memcpy(slot->payload, payload, payload_size); | |
| 86 | } | ||
| 87 | 470 | memset(&slot->payload[payload_size], 0, sizeof(slot->payload) - payload_size); | |
| 88 | |||
| 89 | 470 | next_sequence++; | |
| 90 | 470 | recorded_count++; | |
| 91 | #if CONFIG_KFSW_JOURNAL | ||
| 92 | 44 | copy = *slot; | |
| 93 | #endif | ||
| 94 | 470 | k_spin_unlock(&ring_lock, key); | |
| 95 | #if CONFIG_KFSW_JOURNAL | ||
| 96 | 44 | kfsw_journal_submit(©); | |
| 97 | #endif | ||
| 98 | } | ||
| 99 | |||
| 100 | 6 | void kfsw_event_visit(kfsw_event_visitor_t visitor, void *context) | |
| 101 | { | ||
| 102 | struct kfsw_event_record copy; | ||
| 103 | 6 | uint16_t index = 0U; | |
| 104 | |||
| 105 | 6 | if (visitor == NULL) { | |
| 106 | 2 | return; | |
| 107 | } | ||
| 108 | 6 | for (;;) { | |
| 109 | 10 | k_spinlock_key_t key = k_spin_lock(&ring_lock); | |
| 110 | |||
| 111 | 10 | if (index >= held_count) { | |
| 112 | 2 | k_spin_unlock(&ring_lock, key); | |
| 113 | 4 | return; | |
| 114 | } | ||
| 115 | /* Copy under the lock, call the visitor outside it. */ | ||
| 116 | 8 | copy = ring[(oldest_index + index) % ARRAY_SIZE(ring)]; | |
| 117 | 8 | k_spin_unlock(&ring_lock, key); | |
| 118 | |||
| 119 | 8 | if (!visitor(©, context)) { | |
| 120 | 2 | return; | |
| 121 | } | ||
| 122 | 6 | index++; | |
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | 44 | int kfsw_event_get(uint16_t age, struct kfsw_event_record *record) | |
| 127 | { | ||
| 128 | k_spinlock_key_t key; | ||
| 129 | 44 | int result = 0; | |
| 130 | |||
| 131 | 44 | if (record == NULL) { | |
| 132 | 2 | return -EINVAL; | |
| 133 | } | ||
| 134 | 42 | key = k_spin_lock(&ring_lock); | |
| 135 | 42 | if (age >= held_count) { | |
| 136 | 2 | result = -ENOENT; | |
| 137 | } else { | ||
| 138 | /* Age 0 is the newest, which sits at the end of the ring. */ | ||
| 139 | 40 | *record = ring[(oldest_index + held_count - 1U - age) % ARRAY_SIZE(ring)]; | |
| 140 | } | ||
| 141 | 42 | k_spin_unlock(&ring_lock, key); | |
| 142 | 42 | return result; | |
| 143 | } | ||
| 144 | |||
| 145 | 30 | void kfsw_event_get_stats(struct kfsw_event_stats *stats) | |
| 146 | { | ||
| 147 | k_spinlock_key_t key; | ||
| 148 | |||
| 149 | 30 | if (stats == NULL) { | |
| 150 | ✗ | return; | |
| 151 | } | ||
| 152 | 30 | key = k_spin_lock(&ring_lock); | |
| 153 | 30 | stats->recorded = recorded_count; | |
| 154 | 30 | stats->rejected = rejected_count; | |
| 155 | 30 | stats->overwritten = overwritten_count; | |
| 156 | 30 | stats->held = held_count; | |
| 157 | 30 | stats->capacity = (uint16_t)ARRAY_SIZE(ring); | |
| 158 | 30 | k_spin_unlock(&ring_lock, key); | |
| 159 | } | ||
| 160 | |||
| 161 | 18 | void kfsw_event_clear(void) | |
| 162 | { | ||
| 163 | 18 | k_spinlock_key_t key = k_spin_lock(&ring_lock); | |
| 164 | |||
| 165 | 18 | held_count = 0U; | |
| 166 | 18 | oldest_index = 0U; | |
| 167 | 18 | k_spin_unlock(&ring_lock, key); | |
| 168 | 18 | } | |
| 169 | |||
| 170 | 4 | const char *kfsw_event_severity_name(enum kfsw_event_severity severity) | |
| 171 | { | ||
| 172 | 4 | switch (severity) { | |
| 173 | ✗ | case KFSW_EVENT_INFO: | |
| 174 | ✗ | return "info"; | |
| 175 | ✗ | case KFSW_EVENT_WARNING: | |
| 176 | ✗ | return "warning"; | |
| 177 | 2 | case KFSW_EVENT_ERROR: | |
| 178 | 2 | return "error"; | |
| 179 | ✗ | case KFSW_EVENT_CRITICAL: | |
| 180 | ✗ | return "critical"; | |
| 181 | 2 | default: | |
| 182 | 2 | return "unknown"; | |
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | 16 | const char *kfsw_event_source_name(enum kfsw_event_source source) | |
| 187 | { | ||
| 188 | 16 | switch (source) { | |
| 189 | ✗ | case KFSW_EVENT_SOURCE_BOOT: | |
| 190 | ✗ | return "boot"; | |
| 191 | ✗ | case KFSW_EVENT_SOURCE_COMMAND: | |
| 192 | ✗ | return "command"; | |
| 193 | 2 | case KFSW_EVENT_SOURCE_FTP: | |
| 194 | 2 | return "ftp"; | |
| 195 | ✗ | case KFSW_EVENT_SOURCE_APP: | |
| 196 | ✗ | return "app"; | |
| 197 | 2 | case KFSW_EVENT_SOURCE_FBO: | |
| 198 | 2 | return "fbo"; | |
| 199 | 2 | case KFSW_EVENT_SOURCE_GNDWDT: | |
| 200 | 2 | return "gndwdt"; | |
| 201 | 2 | case KFSW_EVENT_SOURCE_RESMON: | |
| 202 | 2 | return "resmon"; | |
| 203 | 2 | case KFSW_EVENT_SOURCE_TABLE: | |
| 204 | 2 | return "table"; | |
| 205 | 2 | case KFSW_EVENT_SOURCE_SCRUB: | |
| 206 | 2 | return "scrub"; | |
| 207 | 2 | case KFSW_EVENT_SOURCE_LIMITS: | |
| 208 | 2 | return "limits"; | |
| 209 | 2 | default: | |
| 210 | 2 | return "unknown"; | |
| 211 | } | ||
| 212 | } | ||
| 213 |