Skip to content

Commit 12d0c1a

Browse files
tomaszkob89rlubos
authored andcommitted
logging: rpc: command to get current history usage
Add log_rpc_history_usage_current() and log_rpc_get_history_usage_max() API and the underlying RPC commands to get current history usage. Signed-off-by: Tomasz Kobylarz <[email protected]>
1 parent 1846d4d commit 12d0c1a

File tree

8 files changed

+131
-0
lines changed

8 files changed

+131
-0
lines changed

include/logging/log_rpc.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ void log_rpc_set_history_level(enum log_rpc_level level);
9090
*/
9191
uint8_t log_rpc_get_history_usage_threshold(void);
9292

93+
/**
94+
* @brief Gets the current history usage size in bytes.
95+
*
96+
* This function fetches the current history usage size in bytes.
97+
*
98+
* @returns The current history usage size in bytes.
99+
*/
100+
size_t log_rpc_get_history_usage_current(void);
101+
102+
/**
103+
* @brief Gets the maximum history size in bytes.
104+
*
105+
* This function fetches the maximum history size in bytes.
106+
*
107+
* @returns The maximum history size in bytes.
108+
*/
109+
size_t log_rpc_get_history_usage_max(void);
110+
93111
/**
94112
* @brief Sets the current history usage threshold.
95113
*

samples/nrf_rpc/protocols_serialization/client/src/log_rpc_shell.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ static int cmd_log_rpc_history_threshold(const struct shell *sh, size_t argc, ch
145145
return 0;
146146
}
147147

148+
static int cmd_log_rpc_history_usage_current(const struct shell *sh, size_t argc, char *argv[])
149+
{
150+
size_t usage_size;
151+
size_t max_size;
152+
size_t usage;
153+
154+
usage_size = log_rpc_get_history_usage_current();
155+
max_size = log_rpc_get_history_usage_max();
156+
usage = (usage_size * 100 + max_size / 2) / max_size;
157+
shell_print(sh,
158+
"History usage size: %zu bytes, max size: %zu bytes, usage: %zu%%",
159+
usage_size, max_size, usage);
160+
161+
return 0;
162+
}
163+
148164
static int cmd_log_rpc_crash(const struct shell *sh, size_t argc, char *argv[])
149165
{
150166
int rc;
@@ -246,6 +262,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
246262
cmd_log_rpc_history_stop_fetch, 2, 0),
247263
SHELL_CMD_ARG(history_threshold, NULL, "Get or set history usage threshold [0-100]",
248264
cmd_log_rpc_history_threshold, 1, 1),
265+
SHELL_CMD_ARG(history_usage, NULL, "Get current history usage",
266+
cmd_log_rpc_history_usage_current, 1, 0),
249267
SHELL_CMD_ARG(crash, &crash_cmds, "Retrieve crash dump from remote", cmd_log_rpc_crash, 1,
250268
0),
251269
SHELL_CMD_ARG(echo, NULL, "Generate log message on remote <0-4> <msg>", cmd_log_rpc_echo, 3,

subsys/logging/log_backend_rpc.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,29 @@ NRF_RPC_CBOR_CMD_DECODER(log_rpc_group, log_rpc_get_history_usage_threshold_hand
557557
LOG_RPC_CMD_GET_HISTORY_USAGE_THRESHOLD,
558558
log_rpc_get_history_usage_threshold_handler, NULL);
559559

560+
static void log_rpc_get_history_usage_current_handler(const struct nrf_rpc_group *group,
561+
struct nrf_rpc_cbor_ctx *ctx,
562+
void *handler_data)
563+
{
564+
nrf_rpc_cbor_decoding_done(group, ctx);
565+
nrf_rpc_rsp_send_uint(group, (uint32_t)log_rpc_history_get_usage_size());
566+
}
567+
568+
NRF_RPC_CBOR_CMD_DECODER(log_rpc_group, log_rpc_get_history_usage_current_handler,
569+
LOG_RPC_CMD_GET_HISTORY_USAGE_SIZE,
570+
log_rpc_get_history_usage_current_handler, NULL);
571+
572+
static void log_rpc_get_history_usage_max_handler(const struct nrf_rpc_group *group,
573+
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
574+
{
575+
nrf_rpc_cbor_decoding_done(group, ctx);
576+
nrf_rpc_rsp_send_uint(group, (uint32_t)log_rpc_history_get_max_size());
577+
}
578+
579+
NRF_RPC_CBOR_CMD_DECODER(log_rpc_group, log_rpc_get_history_usage_max_handler,
580+
LOG_RPC_CMD_GET_HISTORY_MAX_SIZE,
581+
log_rpc_get_history_usage_max_handler, NULL);
582+
560583
static void log_rpc_set_history_usage_threshold_handler(const struct nrf_rpc_group *group,
561584
struct nrf_rpc_cbor_ctx *ctx,
562585
void *handler_data)

subsys/logging/log_backend_rpc_history.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ void log_rpc_history_free(const union log_msg_generic *msg);
1919

2020
uint8_t log_rpc_history_get_usage(void);
2121

22+
size_t log_rpc_history_get_usage_size(void);
23+
24+
size_t log_rpc_history_get_max_size(void);
25+
2226
#endif /* LOG_RPC_HISTORY_H_ */

subsys/logging/log_backend_rpc_history_fcb.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,28 @@ uint8_t log_rpc_history_get_usage(void)
171171
*/
172172
return (fcb.f_sector_cnt - num_free_sectors) * 100 / fcb.f_sector_cnt;
173173
}
174+
175+
size_t log_rpc_history_get_usage_size(void)
176+
{
177+
int num_free_sectors;
178+
size_t used_size;
179+
180+
k_mutex_lock(&fcb_lock, K_FOREVER);
181+
182+
num_free_sectors = fcb_free_sector_cnt(&fcb);
183+
184+
k_mutex_unlock(&fcb_lock);
185+
186+
/*
187+
* Calculate the used size in bytes based on the number of used sectors.
188+
* This provides sector-level granularity for the size calculation.
189+
*/
190+
used_size = (fcb.f_sector_cnt - num_free_sectors) * fcb_sectors[0].fs_size;
191+
192+
return used_size;
193+
}
194+
195+
size_t log_rpc_history_get_max_size(void)
196+
{
197+
return CONFIG_LOG_BACKEND_RPC_HISTORY_SIZE;
198+
}

subsys/logging/log_backend_rpc_history_ram.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,18 @@ uint8_t log_rpc_history_get_usage(void)
9191

9292
return current_size * 100 / total_size;
9393
}
94+
95+
size_t log_rpc_history_get_usage_size(void)
96+
{
97+
size_t total_size;
98+
size_t current_size;
99+
100+
mpsc_pbuf_get_utilization(&log_history_pbuf, &total_size, &current_size);
101+
102+
return current_size;
103+
}
104+
105+
size_t log_rpc_history_get_max_size(void)
106+
{
107+
return CONFIG_LOG_BACKEND_RPC_HISTORY_SIZE;
108+
}

subsys/logging/log_forwarder_rpc.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,32 @@ uint8_t log_rpc_get_history_usage_threshold(void)
256256
return threshold;
257257
}
258258

259+
size_t log_rpc_get_history_usage_current(void)
260+
{
261+
struct nrf_rpc_cbor_ctx ctx;
262+
uint32_t usage_size;
263+
264+
NRF_RPC_CBOR_ALLOC(&log_rpc_group, ctx, 0);
265+
266+
nrf_rpc_cbor_cmd_no_err(&log_rpc_group, LOG_RPC_CMD_GET_HISTORY_USAGE_SIZE, &ctx,
267+
nrf_rpc_rsp_decode_u32, &usage_size);
268+
269+
return (size_t)usage_size;
270+
}
271+
272+
size_t log_rpc_get_history_usage_max(void)
273+
{
274+
struct nrf_rpc_cbor_ctx ctx;
275+
uint32_t max_size;
276+
277+
NRF_RPC_CBOR_ALLOC(&log_rpc_group, ctx, 0);
278+
279+
nrf_rpc_cbor_cmd_no_err(&log_rpc_group, LOG_RPC_CMD_GET_HISTORY_MAX_SIZE, &ctx,
280+
nrf_rpc_rsp_decode_u32, &max_size);
281+
282+
return (size_t)max_size;
283+
}
284+
259285
void log_rpc_set_history_usage_threshold(log_rpc_history_threshold_reached_handler_t handler,
260286
uint8_t threshold)
261287
{

subsys/logging/log_rpc_group.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ enum log_rpc_cmd_backend {
4141
LOG_RPC_CMD_SET_HISTORY_LEVEL,
4242
LOG_RPC_CMD_GET_HISTORY_USAGE_THRESHOLD,
4343
LOG_RPC_CMD_SET_HISTORY_USAGE_THRESHOLD,
44+
LOG_RPC_CMD_GET_HISTORY_USAGE_SIZE,
45+
LOG_RPC_CMD_GET_HISTORY_MAX_SIZE,
4446
LOG_RPC_CMD_FETCH_HISTORY,
4547
LOG_RPC_CMD_STOP_FETCH_HISTORY,
4648
LOG_RPC_CMD_GET_CRASH_DUMP,

0 commit comments

Comments
 (0)