Extract zero-copy reading into a function 13/305113/5
authorMichal Bloch <m.bloch@samsung.com>
Fri, 26 Jan 2024 19:18:25 +0000 (20:18 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Fri, 2 Feb 2024 14:05:47 +0000 (15:05 +0100)
Will get used directly by the daemon.

Change-Id: I13168087ef460e74c203ab0f0311b91f6f2421f1

src/libdlogutil/fdi_zero_copy.c
src/libdlogutil/fdi_zero_copy.h

index 72413b1..a1ed7e7 100644 (file)
@@ -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;
index e5a9a5d..1e54ae8 100644 (file)
@@ -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);