zlogger: use dynamic mappings for each client 56/281156/14
authorMarek Szyprowski <m.szyprowski@samsung.com>
Wed, 14 Sep 2022 08:16:43 +0000 (10:16 +0200)
committerMarek Szyprowski <m.szyprowski@samsung.com>
Wed, 19 Oct 2022 16:22:56 +0000 (18:22 +0200)
Change-Id: I7cdf81ab79445d9121728dbecd140af8042fbc84
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
include/zero_copy_backend.h
src/libdlog/log_zero_copy.c
src/libdlogutil/fdi_zero_copy.c

index 082a3ea..03fae4f 100644 (file)
@@ -11,6 +11,7 @@
 #include <time.h>
 
 #define ZERO_COPY_DEVICE_NAME "/dev/zlogger"
+#define ZERO_COPY_DUMP_DEVICE_NAME "/dev/zlogger_dump"
 
 static inline uint64_t to_zlog_clock(struct timespec *t)
 {
index 60f1ed9..080afbd 100644 (file)
 #include <unistd.h>
 #include <pthread.h>
 
+#include <libdlog.h>
 #include <logcommon.h>
 #include <zero_copy_backend.h>
 
-static int g_fd;
-static char *g_shm_ptr[ZLOGGER_DEVICE_COUNT];
-_Thread_local uint16_t blk = 0;
+static int g_fd = 0;
+_Thread_local volatile void *g_shm_ptr = 0;
 _Thread_local int g_tid = 0;
 
 extern int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono);
@@ -59,49 +59,33 @@ static inline int gettid_cached(void)
 static void clear_thread_locals(void)
 {
        g_tid = 0;
+       g_shm_ptr = 0;
 }
 
-static inline char *get_shared_memory(int dev_index)
+static volatile struct zlogger_block *get_valid_block(int tid, size_t len)
 {
-       if (dev_index < 0 || dev_index >= ZLOGGER_DEVICE_COUNT)
-               return NULL;
-
-       return g_shm_ptr[dev_index];
-}
+       if (g_shm_ptr == 0) {
+               void *ptr = mmap(NULL, ZLOGGER_BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, g_fd, 0);
 
-static inline struct zlogger_block *get_block(uint16_t block_index)
-{
-       uint16_t index = block_index - 1;
-       int offset = index & (ZLOGGER_BLOCK_MAP_COUNT - 1);
-       char *p = get_shared_memory(index / ZLOGGER_BLOCK_MAP_COUNT);
-
-       if (!p)
-               return NULL;
+               if (ptr == MAP_FAILED)
+                       return NULL;
 
-       return (struct zlogger_block *)(p + (offset * ZLOGGER_BLOCK_SIZE));
-}
-
-static inline int alloc_block(int tid)
-{
-       int r = ioctl(g_fd, ZLOGGER_IOCTL_COMMAND_ALLOC, 0);
-       return r;
-}
+               g_shm_ptr = ptr;
+       }
 
-static inline struct zlogger_block *get_valid_block(int tid, size_t len)
-{
-       if (blk != 0) {
-               struct zlogger_block *block = get_block(blk);
+       volatile struct zlogger_block *block = (struct zlogger_block *)g_shm_ptr;
 
-               if (block && block->head.tid == tid && block->head.offset + len < ZLOGGER_DATA_MAX)
-                       return block;
-       }
+       if (block->head.tid == tid && block->head.offset + len < ZLOGGER_DATA_MAX)
+               return block;
 
-       int r = alloc_block(tid);
-       if (r <= 0)
+       /* Ask kernel to remap mmapped g_shm_ptr to the new memory block */
+       if (ioctl(g_fd, ZLOGGER_IOCTL_COMMAND_ALLOC, 0) < 0)
                return NULL;
-       blk = r;
 
-       return get_block((uint16_t)r);
+       if (block->head.tid == tid && block->head.offset + len < ZLOGGER_DATA_MAX)
+               return block;
+
+       return NULL;
 }
 
 static int zero_copy_write(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono)
@@ -119,7 +103,7 @@ static int zero_copy_write(log_id_t log_id, log_priority prio, const char *tag,
        size_t tag_size = strnlen(tag, ZLOGGER_TAG_MAX) + 1;
        size_t msg_size = strnlen(msg, ZLOGGER_MSG_MAX) + 1;
        size_t entry_size = hd_size + prio_size + tag_size + msg_size;
-       struct zlogger_block *block = get_valid_block(tid, entry_size);
+       volatile struct zlogger_block *block = get_valid_block(tid, entry_size);
 
        if (block == NULL)
                return -1;
@@ -144,30 +128,19 @@ static int zero_copy_write(log_id_t log_id, log_priority prio, const char *tag,
        block->head.offset += (uint16_t)entry_size;
        block->head.ts = ts;
 
-       return 0;
+       return msg_size;
 }
 
 static void zero_copy_deinit(void)
 {
-       for (int i = 0; i < ZLOGGER_DEVICE_COUNT; i++) {
-               if (g_shm_ptr[i] == MAP_FAILED)
-                       continue;
-
-               munmap(g_shm_ptr[i], ZLOGGER_MAP_SIZE);
-               g_shm_ptr[i] = MAP_FAILED;
-       }
-
        if (g_fd > 0) {
                close(g_fd);
                g_fd = -1;
        }
 }
 
-struct log_config;
-int __dlog_init_zero_copy(struct log_config *config)
+int __dlog_init_zero_copy(const struct log_config *config)
 {
-       (void) config;
-
        g_fd = open(ZERO_COPY_DEVICE_NAME, O_RDWR | O_CLOEXEC);
        if (g_fd < 0)
                return -1;
@@ -178,17 +151,6 @@ int __dlog_init_zero_copy(struct log_config *config)
                return -1;
        }
 
-       for (int i = 0; i < ZLOGGER_DEVICE_COUNT; i++)
-               g_shm_ptr[i] = MAP_FAILED;
-
-       for (int i = 0; i < ZLOGGER_DEVICE_COUNT; i++) {
-               g_shm_ptr[i] = mmap(NULL, ZLOGGER_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, g_fd, (ZLOGGER_MAP_SIZE * i));
-               if (g_shm_ptr[i] == MAP_FAILED) {
-                       zero_copy_deinit();
-                       return -1;
-               }
-       }
-
        write_to_log = zero_copy_write;
        destroy_backend = zero_copy_deinit;
 
index c0a8020..e97b897 100644 (file)
@@ -70,7 +70,7 @@ static int zero_copy_clear(struct fd_info *fdi)
 static int zero_copy_create(struct fd_info *fdi, const struct log_config *conf, log_id_t id, list_head *used_paths, log_id_t *aliased)
 {
        // We can't clear, so the flag is R not RW
-       fdi->fd = open(ZERO_COPY_DEVICE_NAME, O_RDONLY | O_CLOEXEC);
+       fdi->fd = open(ZERO_COPY_DUMP_DEVICE_NAME, O_RDONLY | O_CLOEXEC);
        if (fdi->fd < 0)
                return -errno;