1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  * Copyright (c) 2016 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #if K_HEAP_MEM_POOL_SIZE > 0
9 #include "kernel_shell.h"
10 
11 #include <zephyr/sys/sys_heap.h>
12 
13 extern struct sys_heap _system_heap;
14 
cmd_kernel_heap(const struct shell * sh,size_t argc,char ** argv)15 static int cmd_kernel_heap(const struct shell *sh, size_t argc, char **argv)
16 {
17 	ARG_UNUSED(argc);
18 	ARG_UNUSED(argv);
19 
20 	int err;
21 	struct sys_memory_stats stats;
22 
23 	err = sys_heap_runtime_stats_get(&_system_heap, &stats);
24 	if (err) {
25 		shell_error(sh, "Failed to read kernel system heap statistics (err %d)", err);
26 		return -ENOEXEC;
27 	}
28 
29 	shell_print(sh, "free:           %zu", stats.free_bytes);
30 	shell_print(sh, "allocated:      %zu", stats.allocated_bytes);
31 	shell_print(sh, "max. allocated: %zu", stats.max_allocated_bytes);
32 
33 	return 0;
34 }
35 
36 KERNEL_CMD_ADD(heap, NULL, "System heap usage statistics.", cmd_kernel_heap);
37 
38 #endif /* K_HEAP_MEM_POOL_SIZE > 0 */
39