./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
78 / 92
84.8%
Branches:
50 / 64
78.1%
Functions:
7 / 8
87.5%

kfsw-services/src/journal/journal.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3
4 #include <zephyr/kernel.h>
5 #include <zephyr/sys/atomic.h>
6 #include <kfsw/platform/storage.h>
7 #include <kfsw/platform/wallclock.h>
8 #include <kfsw/services/boot.h>
9 #if CONFIG_KFSW_CSP
10 #include <kfsw/comms/csp.h>
11 #endif
12 #include "journal_store.h"
13
14 K_MSGQ_DEFINE(events, sizeof(struct kfsw_event_record), CONFIG_KFSW_JOURNAL_QUEUE_DEPTH, 4);
15 K_MUTEX_DEFINE(journal_lock);
16 static struct kfsw_journal_store store;
17 static struct kfsw_journal_record pending;
18 static bool have_pending;
19 static bool started;
20 static uint64_t boot;
21 static atomic_t dropped;
22 static atomic_t boot_seen;
23 static uint32_t errors;
24 static int last_error;
25
26 42 static int open_store(void)
27 {
28 int result;
29
30
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 2 times.
42 if (store.ready && kfsw_storage_is_ready()) {
31 30 return 0;
32 }
33 12 result = kfsw_journal_store_open(&store);
34
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
12 if ((result == 0) && (boot == 0U)) {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (store.highest_boot >= UINT64_MAX - 1U) {
36 ✗ store.ready = false;
37 ✗ return -EOVERFLOW;
38 }
39 2 boot = store.highest_boot + 1U;
40 }
41 12 return result;
42 }
43
44 60 void kfsw_journal_submit(const struct kfsw_event_record *event)
45 {
46 60 int boot_bit = -1;
47
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (event == NULL) {
49 ✗ return;
50 }
51
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 2 times.
60 if ((event->payload_size > KFSW_EVENT_MAX_PAYLOAD_SIZE) ||
52
4/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 2 times.
58 (event->severity > KFSW_EVENT_CRITICAL) || (event->source < KFSW_EVENT_SOURCE_BOOT) ||
53
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 50 times.
54 (event->source > KFSW_EVENT_SOURCE_LIMITS)) {
54 10 atomic_inc(&dropped);
55 10 return;
56 }
57
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 46 times.
50 if (event->source == KFSW_EVENT_SOURCE_BOOT) {
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if ((event->id == KFSW_EVENT_BOOT_READY) ||
59
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (event->id == KFSW_EVENT_BOOT_LASTWORDS)) {
60 4 boot_bit = (int)event->id - 1;
61
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
4 if (atomic_test_and_set_bit(&boot_seen, boot_bit)) {
62 2 return;
63 }
64 }
65
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 44 times.
46 } else if (event->severity < CONFIG_KFSW_JOURNAL_MIN_SEVERITY) {
66 2 return;
67 }
68
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 34 times.
46 if (k_msgq_put(&events, event, K_NO_WAIT) != 0) {
69 12 atomic_inc(&dropped);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (boot_bit >= 0) {
71 ✗ atomic_clear_bit(&boot_seen, boot_bit);
72 }
73 }
74 }
75
76 34 static void timestamp(struct kfsw_journal_record *record)
77 {
78 #if CONFIG_KFSW_CSP
79 struct kfsw_csp_clock clock;
80
81 kfsw_csp_clock_get(&clock);
82 record->utc_valid = kfsw_csp_clock_is_set(&clock);
83 record->utc_seconds = record->utc_valid ? clock.seconds : 0;
84 #else
85 int64_t seconds;
86
87
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
34 record->utc_valid = (kfsw_wallclock_get(&seconds) == 0) && (seconds > 0);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 record->utc_seconds = record->utc_valid ? seconds : 0;
89 #endif
90 34 }
91
92 40 int kfsw_journal_flush(void)
93 {
94 int result;
95
96 40 k_mutex_lock(&journal_lock, K_FOREVER);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!started) {
98 ✗ k_mutex_unlock(&journal_lock);
99 ✗ return -EACCES;
100 }
101 40 result = open_store();
102
4/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 2 times.
80 for (size_t i = 0; (result == 0) && (i < CONFIG_KFSW_JOURNAL_QUEUE_DEPTH); i++) {
103
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 6 times.
70 if (!have_pending) {
104 64 memset(&pending, 0, sizeof(pending));
105
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 34 times.
64 if (k_msgq_get(&events, &pending.event, K_NO_WAIT) != 0) {
106 30 break;
107 }
108 34 pending.boot = boot;
109 34 timestamp(&pending);
110 34 have_pending = true;
111 }
112 40 result = kfsw_journal_store_append(&store, &pending);
113
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 6 times.
40 if (result == 0) {
114 34 have_pending = false;
115 }
116 }
117 40 last_error = result;
118
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 32 times.
40 if (result != 0) {
119 8 errors++;
120 }
121 40 k_mutex_unlock(&journal_lock);
122 40 return result;
123 }
124
125 ✗ static void writer(void *a, void *b, void *c)
126 {
127 ARG_UNUSED(a);
128 ARG_UNUSED(b);
129 ARG_UNUSED(c);
130 ✗ for (;;) {
131 ✗ k_sleep(K_MSEC(CONFIG_KFSW_JOURNAL_FLUSH_MS));
132 ✗ (void)kfsw_journal_flush();
133 }
134 }
135
136 K_THREAD_DEFINE(kfsw_journal_thread, CONFIG_KFSW_JOURNAL_STACK_SIZE, writer, NULL, NULL, NULL,
137 CONFIG_KFSW_JOURNAL_PRIORITY, 0, SYS_FOREVER_MS);
138
139 4 int kfsw_journal_start(void)
140 {
141 int result;
142
143 4 k_mutex_lock(&journal_lock, K_FOREVER);
144
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (started) {
145 2 result = last_error;
146 } else {
147 2 started = true;
148 2 result = open_store();
149 2 last_error = result;
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
151 ✗ errors++;
152 }
153 2 k_thread_start(kfsw_journal_thread);
154 }
155 4 k_mutex_unlock(&journal_lock);
156 4 return result;
157 }
158
159 26 int kfsw_journal_get(uint16_t age, struct kfsw_journal_record *record)
160 {
161 int result;
162
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (record == NULL) {
164 ✗ return -EINVAL;
165 }
166 26 k_mutex_lock(&journal_lock, K_FOREVER);
167 26 result = kfsw_journal_store_get(&store, age, record);
168 26 k_mutex_unlock(&journal_lock);
169 26 return result;
170 }
171
172 28 void kfsw_journal_get_stats(struct kfsw_journal_stats *stats)
173 {
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (stats == NULL) {
175 ✗ return;
176 }
177 28 k_mutex_lock(&journal_lock, K_FOREVER);
178 28 stats->held = store.held;
179
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 26 times.
28 stats->queued = k_msgq_num_used_get(&events) + (have_pending ? 1U : 0U);
180 28 stats->dropped = (uint32_t)atomic_get(&dropped);
181 28 stats->errors = errors;
182 28 stats->corrupt = store.corrupt;
183 28 stats->last_error = last_error;
184 28 stats->ready = store.ready;
185 28 k_mutex_unlock(&journal_lock);
186 }
187