8000 feat(memory): add print memory info · esp-arduino-libs/esp-lib-utils@3ea3afd · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ea3afd

Browse files
committed
feat(memory): add print memory info
1 parent 61837ac commit 3ea3afd

10 files changed

+131
-29
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# ChangeLog
22

3+
## v0.1.2
4+
5+
### Enhancements:
6+
7+
* feat(memory): add print memory info
8+
39
## v0.1.1
410

511
### Enhancements:
612

7-
* feat(repo): update config header and Kconfig file s
13+
* feat(repo): update config header and Kconfig files
814

915
## v0.1.0
1016

idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.1.1"
1+
version: "0.1.2"
22
description: esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory.
33
url: https://github.com/esp-arduino-libs/esp-lib-utils
44
repository: https://github.com/esp-arduino-libs/esp-lib-utils.git

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=esp-lib-utils
2-
version=0.1.1
2+
version=0.1.2
33
author=espressif
44
maintainer=espressif
55
sentence=esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory.

micropython.cmake

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
add_library(usermod_esp_lib_utils INTERFACE)
44

5-
# Set the source directorya and find all source files.
5+
# Find all source files in the `src` directory.
66
set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
7-
file(GLOB_RECURSE SRCS ${SRC_DIR}/*.c)
7+
file(GLOB_RECURSE SRCS_C ${SRC_DIR}/*.c)
8+
file(GLOB_RECURSE SRCS_CXX ${SRC_DIR}/*.cpp)
9+
10+
# Find all source files in the `micropython` directory.
11+
set(MPY_DIR ${CMAKE_CURRENT_LIST_DIR}/mpy_support)
12+
file(GLOB_RECURSE MPY_C ${MPY_DIR}/*.c)
13+
file(GLOB_RECURSE MPY_CXX ${MPY_DIR}/*.cpp)
814

915
# Add our source files to the library.
10-
target_sources(usermod_esp_lib_utils INTERFACE ${SRCS})
16+
target_sources(usermod_esp_lib_utils INTERFACE ${SRCS_C} ${SRCS_CXX} ${MPY_C} ${MPY_CXX})
1117

1218
# Add the current directory as an include directory.
13-
target_include_directories(usermod_esp_lib_utils INTERFACE ${SRC_DIR})
19+
target_include_directories(usermod_esp_lib_utils INTERFACE ${SRC_DIR} ${MPY_DIR})
1420

1521
# Link our INTERFACE library to the usermod target.
1622
target_link_libraries(usermod INTERFACE usermod_esp_lib_utils)

mpy_support/esp_utils_mp_module.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "py/runtime.h"
7+
#include "memory/esp_utils_mem.h"
8+
9+
static mp_obj_t mem_info(void)
10+
{
11+
return mp_obj_new_bool(esp_utils_mem_print_info());
12+
}
13+
MP_DEFINE_CONST_FUN_OBJ_0(mem_info_func_obj, mem_info);
14+
15+
// Define all attributes of the module.
16+
// Table entries are key/value pairs of the attribute name (a string)
17+
// and the MicroPython object reference.
18+
// All identifiers and strings are written as MP_QSTR_xxx and will be
19+
// optimized to word-sized integers by the build system (interned strings).
20+
static const mp_rom_map_elem_t module_globals_table[] = {
21+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp_utils) },
22+
{ MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mem_info_func_obj) },
23+
};
24+
static MP_DEFINE_CONST_DICT(module_globals, module_globals_table);
25+
26+
// Define module object.
27+
const mp_obj_module_t esp_utils_mp_module = {
28+
.base = { &mp_type_module },
29+
.globals = (mp_obj_dict_t *) &module_globals,
30+
};
31+
32+
// Register the module to make it available in Python.
33+
MP_REGISTER_MODULE(MP_QSTR_esp_utils, esp_utils_mp_module);

src/esp_utils_versions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/* Library Version */
1111
#define ESP_UTILS_VERSION_MAJOR 0
1212
#define ESP_UTILS_VERSION_MINOR 1
13-
#define ESP_UTILS_VERSION_PATCH 1
13+
#define ESP_UTILS_VERSION_PATCH 2
1414

1515
/* File `esp_utils_conf.h` */
1616
#define ESP_UTILS_CONF_VERSION_MAJOR 1

src/memory/esp_utils_mem.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6+
#include <stdbool.h>
67
#include <stddef.h>
78
#include <stdlib.h>
9+
#include "esp_heap_caps.h"
810
#include "esp_utils_conf_internal.h"
911
#include "check/esp_utils_check.h"
1012
#include "log/esp_utils_log.h"
@@ -18,8 +20,12 @@
1820
#include <py/gc.h>
1921
#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE
2022

23+
#define PRINT_INFO_BUFFER_SIZE 256
24+
2125
void *esp_utils_mem_gen_malloc(size_t size)
2226
{
27+
ESP_UTILS_LOG_TRACE_ENTER();
28+
2329
void *p = NULL;
2430
#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB
2531
p = malloc(size);
@@ -29,17 +35,22 @@ void *esp_utils_mem_gen_malloc(size_t size)
2935
p = ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC(size);
3036
#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON
3137
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
32-
return gc_alloc(size, true);
38+
p = gc_alloc(size, true);
3339
#else
34-
return m_malloc(size);
40+
p = m_malloc(size);
3541
#endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE
3642
#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE
3743
ESP_UTILS_LOGD("Malloc @%p: %d", p, (int)size);
44+
45+
ESP_UTILS_LOG_TRACE_EXIT();
46+
3847
return p;
3948
}
4049

4150
void esp_utils_mem_gen_free(void *p)
4251
{
52+
ESP_UTILS_LOG_TRACE_ENTER();
53+
4354
ESP_UTILS_LOGD("Free @%p", p);
4455
#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB
4556
free(p);
@@ -54,4 +65,48 @@ void esp_utils_mem_gen_free(void *p)
5465
m_free(p);
5566
#endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE
5667
#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE
68+
69+
ESP_UTILS_LOG_TRACE_EXIT();
70+
}
71+
72+
void *esp_utils_mem_gen_calloc(size_t n, size_t size)
73+
{
74+
ESP_UTILS_LOG_TRACE_ENTER();
75+
76+
size_t total_size = (size_t)n * size;
77+
void *p = esp_utils_mem_gen_malloc(total_size);
78+
if (p != NULL) {
79+
memset(p, 0, total_size);
80+
}
81+
82+
ESP_UTILS_LOG_TRACE_EXIT();
83+
84+
return p;
85+
}
86+
87+
bool esp_utils_mem_print_info(void)
88+
{
89+
ESP_UTILS_LOG_TRACE_ENTER();
90+
91+
char *buffer = esp_utils_mem_gen_calloc(1, PRINT_INFO_BUFFER_SIZE);
92+
ESP_UTILS_CHECK_NULL_RETURN(buffer, false, "Allocate buffer failed");
93+
94+
snprintf(
95+
buffer, PRINT_INFO_BUFFER_SIZE,
96+
"ESP Memory Info:\n"
97+
" Biggest / Free / Total\n"
98+
" SRAM : [%8d / %8d / %8d]\n"
99+
"PSRAM : [%8d / %8d / %8d]",
100+
(int)heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), (int)heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
101+
(int)heap_caps_get_total_size(MALLOC_CAP_INTERNAL),
102+
(int)heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM), (int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
103+
(int)heap_caps_get_total_size(MALLOC_CAP_SPIRAM)
104+
);
105+
printf("%s\n", buffer);
106+
107+
esp_utils_mem_gen_free(buffer);
108+
109+
ESP_UTILS_LOG_TRACE_EXIT();
110+
111+
return true;
57112
}

src/memory/esp_utils_mem.h

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@
55
*/
66
#pragma once
77

8+
#include <stdbool.h>
9+
#include <stdlib.h>
810
#include "sdkconfig.h"
911
#include "esp_utils_conf_internal.h"
1012

1113
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
1216

13-
#include <memory>
14-
#include <cstdlib>
17+
void *esp_utils_mem_gen_malloc(size_t size);
18+
void esp_utils_mem_gen_free(void *p);
19+
void *esp_utils_mem_gen_calloc(size_t n, size_t size);
20+
bool esp_utils_mem_print_info(void);
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif
25+
26+
#ifdef __cplusplus
1527

16-
extern "C" void *esp_utils_mem_gen_malloc(size_t size);
17-
extern "C" void esp_utils_mem_gen_free(void *p);
28+
#include <memory>
1829

1930
namespace esp_utils {
2031

@@ -73,11 +84,6 @@ std::shared_ptr<T> make_shared(Args &&... args)
7384

7485
} // namespace esp_utils
7586

76-
#else
77-
78-
void *esp_utils_mem_gen_malloc(size_t size);
79-
void esp_utils_mem_gen_free(void *p);
80-
8187
#endif // __cplusplus
8288

8389
/**
@@ -88,13 +94,5 @@ void esp_utils_mem_gen_free(void *p);
8894
#undef free
8995
#undef calloc
9096
#define malloc(size) esp_utils_mem_gen_malloc(size)
91-
#define free(ptr) esp_utils_mem_gen_free(ptr)
92-
#define calloc(n, size) \
93-
({ \
94-
size_t _size = (size_t)n * size; \
95-
void *p = malloc(_size); \
96-
if (p != NULL) { \
97-
memset(p, 0, _size); \
98-
} \
99-
p; \
100-
})
97+
#define free(p) esp_utils_mem_gen_free(p)
98+
#define calloc(n, size) esp_utils_mem_gen_calloc(n, size)

test_apps/main/test_on_c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ TEST_CASE("Test log functions on C", "[utils][log][C]")
2424

2525
TEST_CASE("Test memory functions on C", "[utils][memory][C]")
2626
{
27+
TEST_ASSERT_TRUE_MESSAGE(esp_utils_mem_print_info(), "Print memory info failed");
28+
2729
char *good_ptr = (char *)calloc(1, MALLOC_GOOD_SIZE);
2830
ESP_UTILS_CHECK_NULL_GOTO(good_ptr, err, "Failed to allocate memory size: %d", MALLOC_GOOD_SIZE);
2931
ESP_UTILS_LOGI("Malloced value: %d", good_ptr[0]);

test_apps/main/test_on_cpp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ using TestBadClass = TestClass<MALLOC_BAD_SIZE>;
3434

3535
TEST_CASE("Test memory functions on cpp", "[utils][memory][CPP]")
3636
{
37+
TEST_ASSERT_TRUE_MESSAGE(esp_utils_mem_print_info(), "Print memory info failed");
38+
3739
std::shared_ptr<TestGoodClass> good_ptr = nullptr;
3840
ESP_UTILS_CHECK_EXCEPTION_GOTO(
3941
(good_ptr = esp_utils::make_shared<TestGoodClass>()), err, "Failed to allocate memory size: %d",

0 commit comments

Comments
 (0)
0