Skip to content
Closed
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
13 changes: 0 additions & 13 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ config OCRE
# Enable the WASM Micro Runtime
select WAMR

config OCRE_WAMR_HEAP_BUFFER_SIZE
int "WAMR heap buffer size in bytes"
default 32768
help
A static memory allocation for WAMR to use as a heap.

config OCRE_STORAGE_HEAP_BUFFER_SIZE
int "Storage heap buffer size in bytes"
default 500000
depends on MEMC
help
A static memory allocation for container storage to use as a heap.

config OCRE_CONTAINER_DEFAULT_HEAP_SIZE
int "Default value for the container heap size"
default 4096
Expand Down
2 changes: 0 additions & 2 deletions boards/b_u585i_iot02a.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ CONFIG_MEMC=y

# Container defaults
CONFIG_MAX_CONTAINERS=5
CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=6388608
CONFIG_OCRE_STORAGE_HEAP_BUFFER_SIZE=2000000

# Bus interfaces
CONFIG_GPIO=y
Expand Down
1 change: 0 additions & 1 deletion prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1


# Ocre configuration
CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=512000
CONFIG_OCRE_CONTAINER_DEFAULT_HEAP_SIZE=4096
CONFIG_OCRE_CONTAINER_DEFAULT_STACK_SIZE=4096
CONFIG_LOG_TRACE_SHORT_TIMESTAMP=y
Expand Down
35 changes: 15 additions & 20 deletions src/ocre/components/container_supervisor/cs_sm_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ LOG_MODULE_DECLARE(ocre_cs_component, OCRE_LOG_LEVEL);
#include "cs_sm.h"
#include "cs_sm_impl.h"

#include "ocre_psram.h"

// WAMR heap buffer - uses PSRAM when available
#if defined(CONFIG_MEMC)
PSRAM_SECTION_ATTR
#endif
static char wamr_heap_buf[CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE] = {0};


// Thread pool for container execution
#define CONTAINER_THREAD_POOL_SIZE 4
static core_thread_t container_threads[CONTAINER_THREAD_POOL_SIZE];
Expand Down Expand Up @@ -189,7 +180,7 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument
}

container_arguments->size = file_size;
container_arguments->buffer = storage_heap_alloc(file_size);
container_arguments->buffer = user_malloc(file_size);
if (!container_arguments->buffer) {
LOG_ERR("Failed to allocate memory for container binary from PSRAM.");
return -ENOMEM;
Expand All @@ -200,22 +191,22 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument
ret = core_fileopen(filepath, &file_handle);
if (ret < 0) {
LOG_ERR("Failed to open file %s: %d", filepath, ret);
storage_heap_free(container_arguments->buffer);
user_free(container_arguments->buffer);
return ret;
}

ret = core_fileread(file_handle, container_arguments->buffer, file_size);
if (ret < 0) {
LOG_ERR("Failed to read file %s: %d", filepath, ret);
core_fileclose(file_handle);
storage_heap_free(container_arguments->buffer);
user_free(container_arguments->buffer);
return ret;
}

ret = core_fileclose(file_handle);
if (ret < 0) {
LOG_ERR("Failed to close file %s: %d", filepath, ret);
storage_heap_free(container_arguments->buffer);
user_free(container_arguments->buffer);
return ret;
}
return 0;
Expand All @@ -239,12 +230,17 @@ int CS_ctx_init(ocre_cs_ctx *ctx) {
return 0;
}

#if WASM_MEM_ALLOC_WITH_USER_DATA || WASM_MEM_ALLOC_WITH_USAGE
#error user allocator not compatible with WASM_MEM_ALLOC_WITH_USER_DATA or WASM_MEM_ALLOC_WITH_USAGE
#endif

ocre_container_runtime_status_t CS_runtime_init(ocre_cs_ctx *ctx, ocre_container_init_arguments_t *args) {
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = wamr_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(wamr_heap_buf);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use system heap in systems where we don't have PSRAM for wasm, then we can't limit it anymore. I don't think it's good - we need a way to limit max amount of warm's linear memory, don't we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should enable WASM_MEM_ALLOC_WITH_USAGE and limit the amount of memory per-container instead. This is just the allocation. Every use of user_malloc should properly handle the ENOMEM error and recover gracefully if possible.

init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = user_malloc;
init_args.mem_alloc_option.allocator.free_func = user_free;
init_args.mem_alloc_option.allocator.realloc_func = user_realloc;
init_args.native_module_name = "env";
init_args.n_native_symbols = ocre_api_table_size;
init_args.native_symbols = ocre_api_table;
Expand All @@ -266,7 +262,6 @@ ocre_container_runtime_status_t CS_runtime_init(ocre_cs_ctx *ctx, ocre_container
#ifdef CONFIG_OCRE_CONTAINER_MESSAGING
ocre_messaging_init();
#endif
storage_heap_init();
return RUNTIME_STATUS_INITIALIZED;
}

Expand Down Expand Up @@ -321,7 +316,7 @@ ocre_container_status_t CS_create_container(ocre_container_t *container) {
curr_container_arguments->error_buf, sizeof(curr_container_arguments->error_buf));
if (!curr_container_arguments->module) {
LOG_ERR("Failed to load WASM module: %s", curr_container_arguments->error_buf);
storage_heap_free(curr_container_arguments->buffer);
user_free(curr_container_arguments->buffer);
return CONTAINER_STATUS_ERROR;
}

Expand Down Expand Up @@ -380,7 +375,7 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) {
LOG_ERR("Failed to instantiate WASM module: %s, for containerID= %d", curr_container_arguments->error_buf,
curr_container_ID);
wasm_runtime_unload(curr_container_arguments->module);
storage_heap_free(curr_container_arguments->buffer);
user_free(curr_container_arguments->buffer);
return CONTAINER_STATUS_ERROR;
}
#if defined(CONFIG_OCRE_TIMER) || defined(CONFIG_OCRE_GPIO) || defined(CONFIG_OCRE_SENSORS) || \
Expand Down Expand Up @@ -513,7 +508,7 @@ ocre_container_status_t CS_destroy_container(ocre_container_t *container, ocre_c
}

if (container->ocre_runtime_arguments.buffer) {
storage_heap_free(container->ocre_runtime_arguments.buffer);
user_free(container->ocre_runtime_arguments.buffer);
container->ocre_runtime_arguments.buffer = NULL;
}

Expand Down
24 changes: 24 additions & 0 deletions src/shared/platform/ocre_core_external.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ void *core_malloc(size_t size);
*/
void core_free(void *ptr);

/**
* @brief Allocate memory in application heap.
*
* @param size Number of bytes to allocate.
* @return Pointer to allocated memory, or NULL on failure.
*/
void *user_malloc(size_t size);

/**
* @brief Free previously allocated memory in application heap.
*
* @param ptr Pointer to memory to free.
*/
void user_free(void *ptr);

/**
* @brief Reallocate memory in application heap.
*
* @param ptr pointer to already allocated memory
* @param size Number of bytes to allocate.
* @return Pointer to reallocated memory, or NULL on failure.
*/
void *user_realloc(void *ptr, size_t size);

/**
* @brief Yield the current thread's execution.
*/
Expand Down
31 changes: 0 additions & 31 deletions src/shared/platform/ocre_psram.h

This file was deleted.

6 changes: 3 additions & 3 deletions src/shared/platform/posix/core_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ int core_filestat(const char *path, size_t *size) {
}

int core_fileopen(const char *path, void **handle) {
int *fd = core_malloc(sizeof(int));
int *fd = user_malloc(sizeof(int));
if (!fd) return -ENOMEM;
*fd = open(path, O_RDONLY);
if (*fd < 0) {
core_free(fd);
user_free(fd);
return -errno;
}
*handle = fd;
Expand All @@ -95,7 +95,7 @@ int core_fileread(void *handle, void *buffer, size_t size) {
int core_fileclose(void *handle) {
int fd = *(int *)handle;
int ret = close(fd);
core_free(handle);
user_free(handle);
return ret;
}

Expand Down
12 changes: 12 additions & 0 deletions src/shared/platform/posix/core_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ void *core_malloc(size_t size) {
void core_free(void *ptr) {
free(ptr);
}

void *user_malloc(size_t size) {
return malloc(size);
}

void user_free(void *ptr) {
free(ptr);
}

void *user_realloc(void *ptr, size_t size) {
return realloc(ptr, size);
}
6 changes: 3 additions & 3 deletions src/shared/platform/zephyr/core_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ int core_filestat(const char *path, size_t *size) {
}

int core_fileopen(const char *path, void **handle) {
struct fs_file_t *file = core_malloc(sizeof(struct fs_file_t));
struct fs_file_t *file = user_malloc(sizeof(struct fs_file_t));
if (!file) return -ENOMEM;
fs_file_t_init(file);
int ret = fs_open(file, path, FS_O_READ);
if (ret < 0) {
core_free(file);
user_free(file);
return ret;
}
*handle = file;
Expand All @@ -67,7 +67,7 @@ int core_fileread(void *handle, void *buffer, size_t size) {
int core_fileclose(void *handle) {
struct fs_file_t *file = (struct fs_file_t *)handle;
int ret = fs_close(file);
core_free(file);
user_free(file);
return ret;
}

Expand Down
31 changes: 31 additions & 0 deletions src/shared/platform/zephyr/core_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ocre_core_external.h"

#include <zephyr/kernel.h>
#include <zephyr/multi_heap/shared_multi_heap.h>

void *core_malloc(size_t size) {
return k_malloc(size);
Expand All @@ -16,3 +17,33 @@ void *core_malloc(size_t size) {
void core_free(void *ptr) {
k_free(ptr);
}

#ifdef CONFIG_SHARED_MULTI_HEAP
void *user_malloc(size_t size) {
return shared_multi_heap_aligned_alloc(SMH_REG_ATTR_EXTERNAL, 32, size);
}

void user_free(void *ptr) {
shared_multi_heap_free(ptr);
}

void *user_realloc(void *ptr, size_t size) {
// TODO
return NULL;
}

#else
#warning CONFIG_SHARED_MULTI_HEAP is not defined. Using internal RAM
void *user_malloc(size_t size) {
return k_malloc(size);
}

void user_free(void *ptr) {
k_free(ptr);
}

void *user_realloc(void *ptr, size_t size) {
return k_realloc(ptr, size);
}

#endif
Loading