Skip to content

feat(memory): add print memory info #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# ChangeLog

## v0.1.2

### Enhancements:

* feat(memory): add print memory info

## v0.1.1

### Enhancements:

* feat(repo): update config header and Kconfig file s
* feat(repo): update config header and Kconfig files

## v0.1.0

Expand Down
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "0.1.1"
version: "0.1.2"
description: esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory.
url: https://github.com/esp-arduino-libs/esp-lib-utils
repository: https://github.com/esp-arduino-libs/esp-lib-utils.git
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=esp-lib-utils
version=0.1.1
version=0.1.2
author=espressif
maintainer=espressif
sentence=esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory.
Expand Down
14 changes: 10 additions & 4 deletions micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

add_library(usermod_esp_lib_utils INTERFACE)

# Set the source directorya and find all source files.
# Find all source files in the `src` directory.
set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
file(GLOB_RECURSE SRCS ${SRC_DIR}/*.c)
file(GLOB_RECURSE SRCS_C ${SRC_DIR}/*.c)
file(GLOB_RECURSE SRCS_CXX ${SRC_DIR}/*.cpp)

# Find all source files in the `micropython` directory.
set(MPY_DIR ${CMAKE_CURRENT_LIST_DIR}/mpy_support)
file(GLOB_RECURSE MPY_C ${MPY_DIR}/*.c)
file(GLOB_RECURSE MPY_CXX ${MPY_DIR}/*.cpp)

# Add our source files to the library.
target_sources(usermod_esp_lib_utils INTERFACE ${SRCS})
target_sources(usermod_esp_lib_utils INTERFACE ${SRCS_C} ${SRCS_CXX} ${MPY_C} ${MPY_CXX})

# Add the current directory as an include directory.
target_include_directories(usermod_esp_lib_utils INTERFACE ${SRC_DIR})
target_include_directories(usermod_esp_lib_utils INTERFACE ${SRC_DIR} ${MPY_DIR})

# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_esp_lib_utils)
33 changes: 33 additions & 0 deletions mpy_support/esp_utils_mp_module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "py/runtime.h"
#include "memory/esp_utils_mem.h"

static mp_obj_t mem_info(void)
{
return mp_obj_new_bool(esp_utils_mem_print_info());
}
MP_DEFINE_CONST_FUN_OBJ_0(mem_info_func_obj, mem_info);

// Define all attributes of the module.
// Table entries are key/value pairs of the attribute name (a string)
// and the MicroPython object reference.
// All identifiers and strings are written as MP_QSTR_xxx and will be
// optimized to word-sized integers by the build system (interned strings).
static const mp_rom_map_elem_t module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp_utils) },
{ MP_ROM_QSTR(MP_QSTR_mem_info), MP_ROM_PTR(&mem_info_func_obj) },
};
static MP_DEFINE_CONST_DICT(module_globals, module_globals_table);

// Define module object.
const mp_obj_module_t esp_utils_mp_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *) &module_globals,
};

// Register the module to make it available in Python.
MP_REGISTER_MODULE(MP_QSTR_esp_utils, esp_utils_mp_module);
2 changes: 1 addition & 1 deletion src/esp_utils_versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/* Library Version */
#define ESP_UTILS_VERSION_MAJOR 0
#define ESP_UTILS_VERSION_MINOR 1
#define ESP_UTILS_VERSION_PATCH 1
#define ESP_UTILS_VERSION_PATCH 2

/* File `esp_utils_conf.h` */
#define ESP_UTILS_CONF_VERSION_MAJOR 1
Expand Down
59 changes: 57 additions & 2 deletions src/memory/esp_utils_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "esp_heap_caps.h"
#include "esp_utils_conf_internal.h"
#include "check/esp_utils_check.h"
#include "log/esp_utils_log.h"
Expand All @@ -18,8 +20,12 @@
#include <py/gc.h>
#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE

#define PRINT_INFO_BUFFER_SIZE 256

void *esp_utils_mem_gen_malloc(size_t size)
{
ESP_UTILS_LOG_TRACE_ENTER();

void *p = NULL;
#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB
p = malloc(size);
Expand All @@ -29,17 +35,22 @@ void *esp_utils_mem_gen_malloc(size_t size)
p = ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC(size);
#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
return gc_alloc(size, true);
p = gc_alloc(size, true);
#else
return m_malloc(size);
p = m_malloc(size);
#endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE
#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE
ESP_UTILS_LOGD("Malloc @%p: %d", p, (int)size);

ESP_UTILS_LOG_TRACE_EXIT();

return p;
}

void esp_utils_mem_gen_free(void *p)
{
ESP_UTILS_LOG_TRACE_ENTER();

ESP_UTILS_LOGD("Free @%p", p);
#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB
free(p);
Expand All @@ -54,4 +65,48 @@ void esp_utils_mem_gen_free(void *p)
m_free(p);
#endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE
#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE

ESP_UTILS_LOG_TRACE_EXIT();
}

void *esp_utils_mem_gen_calloc(size_t n, size_t size)
{
ESP_UTILS_LOG_TRACE_ENTER();

size_t total_size = (size_t)n * size;
void *p = esp_utils_mem_gen_malloc(total_size);
if (p != NULL) {
memset(p, 0, total_size);
}

ESP_UTILS_LOG_TRACE_EXIT();

return p;
}

bool esp_utils_mem_print_info(void)
{
ESP_UTILS_LOG_TRACE_ENTER();

char *buffer = esp_utils_mem_gen_calloc(1, PRINT_INFO_BUFFER_SIZE);
ESP_UTILS_CHECK_NULL_RETURN(buffer, false, "Allocate buffer failed");

snprintf(
buffer, PRINT_INFO_BUFFER_SIZE,
"ESP Memory Info:\n"
" Biggest / Free / Total\n"
" SRAM : [%8d / %8d / %8d]\n"
"PSRAM : [%8d / %8d / %8d]",
(int)heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), (int)heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
(int)heap_caps_get_total_size(MALLOC_CAP_INTERNAL),
(int)heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM), (int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
(int)heap_caps_get_total_size(MALLOC_CAP_SPIRAM)
);
printf("%s\n", buffer);

esp_utils_mem_gen_free(buffer);

ESP_UTILS_LOG_TRACE_EXIT();

return true;
}
36 changes: 17 additions & 19 deletions src/memory/esp_utils_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@
*/
#pragma once

#include <stdbool.h>
#include <stdlib.h>
#include "sdkconfig.h"
#include "esp_utils_conf_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

#include <memory>
#include <cstdlib>
void *esp_utils_mem_gen_malloc(size_t size);
void esp_utils_mem_gen_free(void *p);
void *esp_utils_mem_gen_calloc(size_t n, size_t size);
bool esp_utils_mem_print_info(void);

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus

extern "C" void *esp_utils_mem_gen_malloc(size_t size);
extern "C" void esp_utils_mem_gen_free(void *p);
#include <memory>

namespace esp_utils {

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

} // namespace esp_utils

#else

void *esp_utils_mem_gen_malloc(size_t size);
void esp_utils_mem_gen_free(void *p);

#endif // __cplusplus

/**
Expand All @@ -88,13 +94,5 @@ void esp_utils_mem_gen_free(void *p);
#undef free
#undef calloc
#define malloc(size) esp_utils_mem_gen_malloc(size)
#define free(ptr) esp_utils_mem_gen_free(ptr)
#define calloc(n, size) \
({ \
size_t _size = (size_t)n * size; \
void *p = malloc(_size); \
if (p != NULL) { \
memset(p, 0, _size); \
} \
p; \
})
#define free(p) esp_utils_mem_gen_free(p)
#define calloc(n, size) esp_utils_mem_gen_calloc(n, size)
2 changes: 2 additions & 0 deletions test_apps/main/test_on_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ TEST_CASE("Test log functions on C", "[utils][log][C]")

TEST_CASE("Test memory functions on C", "[utils][memory][C]")
{
TEST_ASSERT_TRUE_MESSAGE(esp_utils_mem_print_info(), "Print memory info failed");

char *good_ptr = (char *)calloc(1, MALLOC_GOOD_SIZE);
ESP_UTILS_CHECK_NULL_GOTO(good_ptr, err, "Failed to allocate memory size: %d", MALLOC_GOOD_SIZE);
ESP_UTILS_LOGI("Malloced value: %d", good_ptr[0]);
Expand Down
2 changes: 2 additions & 0 deletions test_apps/main/test_on_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ using TestBadClass = TestClass<MALLOC_BAD_SIZE>;

TEST_CASE("Test memory functions on cpp", "[utils][memory][CPP]")
{
TEST_ASSERT_TRUE_MESSAGE(esp_utils_mem_print_info(), "Print memory info failed");

std::shared_ptr<TestGoodClass> good_ptr = nullptr;
ESP_UTILS_CHECK_EXCEPTION_GOTO(
(good_ptr = esp_utils::make_shared<TestGoodClass>()), err, "Failed to allocate memory size: %d",
Expand Down
Loading