Skip to content

Commit

Permalink
Merge pull request #48 from oyama/fix/multicore-flash
Browse files Browse the repository at this point in the history
Increased VFS read/write concurrency
  • Loading branch information
oyama authored Jun 18, 2024
2 parents 14acced + 7af8f6c commit af7b195
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/freertos_benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ target_link_libraries(freertos_benchmark PRIVATE
)
target_link_options(freertos_benchmark PRIVATE -Wl,--print-memory-usage)

#pico_set_binary_type(freertos_benchmark no_flash)
pico_set_binary_type(freertos_benchmark copy_to_ram)
pico_enable_stdio_usb(freertos_benchmark 1)
pico_add_extra_outputs(freertos_benchmark)
7 changes: 5 additions & 2 deletions examples/freertos_benchmark/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@
#define configTIMER_TASK_STACK_DEPTH 1024

/* SMP port only */
#define configNUMBER_OF_CORES 1
#define configNUMBER_OF_CORES 2
#define configNUM_CORES configNUMBER_OF_CORES // for pico-sdk 1.5.1
#if configNUMBER_OF_CORES > 1
#define configUSE_CORE_AFFINITY 1
#endif
#define configTICK_CORE 1
#define configRUN_MULTIPLE_PRIORITIES 1
#define configUSE_CORE_AFFINITY 0
#define configUSE_PASSIVE_IDLE_HOOK 0

/* RP2040 specific */
Expand Down
20 changes: 8 additions & 12 deletions examples/freertos_benchmark/fs_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,18 @@ bool fs_init(void) {
}
}

printf("mount /flash\n");
printf("format and mount /flash\n");
blockdevice_t *device = blockdevice_flash_create(PICO_FLASH_SIZE_BYTES - PICO_FS_DEFAULT_SIZE, 0);
filesystem_t *fs = filesystem_littlefs_create(500, 16);
err = fs_format(fs, device);
if (err == -1) {
printf("fs_format error: %s", strerror(errno));
return false;
}
err = fs_mount("/flash", fs, device);
if (err == -1) {
printf("format /flash\n");
err = fs_format(fs, device);
if (err == -1) {
printf("fs_format error: %s", strerror(errno));
return false;
}
err = fs_mount("/flash", fs, device);
if (err == -1) {
printf("fs_mount error: %s", strerror(errno));
return false;
}
printf("fs_mount error: %s", strerror(errno));
return false;
}
return true;
}
59 changes: 48 additions & 11 deletions examples/freertos_benchmark/main.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include <FreeRTOS.h>
#include <portable.h>

#include <task.h>
#include <errno.h>
#include <string.h>
#include <pico/stdlib.h>
#include <portable.h>
#include <stdio.h>
#include <string.h>
#include <task.h>
#include "filesystem/vfs.h"

#define BENCHMARK_SIZE (0.4 * 1024 * 1024)
#define BUFFER_SIZE (512 * 1)
#define BUFFER_SIZE (1024 * 2)
#define CORE_ACCESSING_FLASH 1 // core0

static uint32_t xor_rand(uint32_t *seed) {
*seed ^= *seed << 13;
Expand All @@ -25,7 +25,7 @@ static uint32_t xor_rand_32bit(uint32_t *seed) {
void benchmark_task(void *p)
{
const char *path = p;
printf("start benchmark %s\n", path);
printf("start benchmark %s on core%d\n", path, get_core_num());

uint64_t start_at = get_absolute_time();
int fd = open(path, O_WRONLY|O_CREAT);
Expand All @@ -47,7 +47,7 @@ void benchmark_task(void *p)

ssize_t write_size = write(fd, buffer, chunk);
if (write_size == -1) {
printf("write: error: %s\n", strerror(errno));
printf("write error: %s\n", strerror(errno));
return;
}
remind = remind - write_size;
Expand All @@ -58,9 +58,47 @@ void benchmark_task(void *p)
printf("close error: %s\n", strerror(errno));
return;
}

double duration = (double)absolute_time_diff_us(start_at, get_absolute_time()) / 1000 / 1000;
printf("Finish %s: %.1f KB/s\n", path, (double)(BENCHMARK_SIZE) / duration / 1024);
printf("Write %s: %.1f KB/s\n", path, (double)(BENCHMARK_SIZE) / duration / 1024);


start_at = get_absolute_time();
fd = open(path, O_RDONLY);
if (fd == -1) {
printf("open error: %s\n", strerror(errno));
return;
}

counter = 0;
xor_rand(&counter);
remind = BENCHMARK_SIZE;
while (remind > 0) {
size_t chunk = remind % sizeof(buffer) ? remind % sizeof(buffer) : sizeof(buffer);
ssize_t read_size = read(fd, buffer, chunk);
if (read_size == -1) {
printf("read error: %s\n", strerror(errno));
return;
}
uint32_t *b = (uint32_t *)buffer;
for (size_t j = 0; j < chunk / sizeof(uint32_t); j++) {
volatile uint32_t v = xor_rand_32bit(&counter);
if (b[j] != v) {
printf("data mismatch\n");
return;
}
}

remind = remind - read_size;
}

err = close(fd);
if (err == -1) {
printf("close error: %s\n", strerror(errno));
return;
}
duration = (double)absolute_time_diff_us(start_at, get_absolute_time()) / 1000 / 1000;
printf("Read %s: %.1f KB/s\n", path, (double)(BENCHMARK_SIZE) / duration / 1024);


while (true) {
;
Expand All @@ -77,11 +115,10 @@ int main(void)

TaskHandle_t task_handle;
xTaskCreate(benchmark_task, "flash", 1024*1, "/flash/benchmark", 1, &(task_handle));
//vTaskCoreAffinitySet(task_handle, 1);
vTaskCoreAffinitySet(task_handle, CORE_ACCESSING_FLASH);

vTaskStartScheduler();

while (true)
;
}

5 changes: 3 additions & 2 deletions src/filesystem/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,10 @@ ssize_t _write(int fildes, const void *buf, size_t nbyte) {
recursive_mutex_exit(&_mutex);
return _error_remap(-EBADF);
}
recursive_mutex_exit(&_mutex);

ssize_t size = fs->file_write(fs, file, buf, nbyte);

recursive_mutex_exit(&_mutex);
return _error_remap(size);
}

Expand All @@ -533,9 +534,9 @@ ssize_t _read(int fildes, void *buf, size_t nbyte) {
recursive_mutex_exit(&_mutex);
return _error_remap(-EBADF);
}
recursive_mutex_exit(&_mutex);

ssize_t size = fs->file_read(fs, file, buf, nbyte);
recursive_mutex_exit(&_mutex);

return _error_remap(size);
}
Expand Down

0 comments on commit af7b195

Please sign in to comment.