From 21e40967669ae023a1e7d94f2a4fedbe9f3f0559 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Fri, 26 Jan 2024 20:18:25 +0100 Subject: [PATCH] Extract zero-copy reading into a function Will get used directly by the daemon. Change-Id: I13168087ef460e74c203ab0f0311b91f6f2421f1 --- src/libdlogutil/fdi_zero_copy.c | 40 ++++++++++++++++++++++++++++------------ src/libdlogutil/fdi_zero_copy.h | 1 + 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/libdlogutil/fdi_zero_copy.c b/src/libdlogutil/fdi_zero_copy.c index 72413b1..a1ed7e7 100644 --- a/src/libdlogutil/fdi_zero_copy.c +++ b/src/libdlogutil/fdi_zero_copy.c @@ -336,6 +336,33 @@ static void sort_items_list(struct zlog_entry_list **items) *items = item_group; } +int zero_copy_read_buffer(int fd, struct zlog_entry_list **items, char *bitmap, uint16_t *last_offsets) +{ + assert(fd >= 0); + assert(items); + assert(bitmap); + assert(last_offsets); + + if (*items) + return -EAGAIN; + + for (size_t i = 0; i < ZLOGGER_DEVICE_COUNT; ++i) { + volatile void *const addr = mmap(NULL, ZLOGGER_MAP_SIZE, PROT_READ, MAP_SHARED, fd, ZLOGGER_MAP_SIZE * i); + if (addr == MAP_FAILED) { + sort_items_list(items); // maybe worth salvaging + return -EIO; + } + load_single_device(addr, items + , bitmap + (ZLOGGER_BLOCK_MAP_COUNT / CHAR_BIT) * i + , last_offsets + ZLOGGER_BLOCK_MAP_COUNT * i + ); + munmap((void *) addr, ZLOGGER_MAP_SIZE); + } + + sort_items_list(items); + return 0; +} + static int zero_copy_read(struct fd_info *fdi) { struct zero_copy_priv_data *const zpd = (struct zero_copy_priv_data *)fdi->priv_data; @@ -368,18 +395,7 @@ static int zero_copy_read(struct fd_info *fdi) zpd->read_everything = false; } - for (size_t i = 0; i < ZLOGGER_DEVICE_COUNT; ++i) { - volatile void *const addr = mmap(NULL, ZLOGGER_MAP_SIZE, PROT_READ, MAP_SHARED, fdi->fd, ZLOGGER_MAP_SIZE * i); - if (addr == MAP_FAILED) - break; - load_single_device(addr, &zpd->items - , bitmap + (ZLOGGER_BLOCK_MAP_COUNT / CHAR_BIT) * i - , zpd->last_block_offsets + ZLOGGER_BLOCK_MAP_COUNT * i - ); - munmap((void *) addr, ZLOGGER_MAP_SIZE); - } - - sort_items_list(&zpd->items); + (void) zero_copy_read_buffer(fdi->fd, &zpd->items, bitmap, zpd->last_block_offsets); if (zpd->dump) zpd->eof = true; diff --git a/src/libdlogutil/fdi_zero_copy.h b/src/libdlogutil/fdi_zero_copy.h index e5a9a5d..1e54ae8 100644 --- a/src/libdlogutil/fdi_zero_copy.h +++ b/src/libdlogutil/fdi_zero_copy.h @@ -44,3 +44,4 @@ struct zero_copy_priv_data { uint16_t *last_block_offsets; }; +int zero_copy_read_buffer(int fd, struct zlog_entry_list **items, char *bitmap, uint16_t *last_offsets); -- 2.7.4