From a776a0a4f4b201be142924f40138d8a176b2fd53 Mon Sep 17 00:00:00 2001 From: Hariharan Devarajan Date: Sun, 3 Dec 2023 21:07:15 +0000 Subject: [PATCH 1/3] changes to support mmap reads --- src/modules/dyad.c | 10 ++-------- src/utils/read_all.c | 20 +++++++++++++------- src/utils/read_all.h | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/modules/dyad.c b/src/modules/dyad.c index af7ac2bf..c72c7d31 100644 --- a/src/modules/dyad.c +++ b/src/modules/dyad.c @@ -159,16 +159,10 @@ dyad_fetch_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, #endif // DYAD_SPIN_WAIT FLUX_LOG_INFO (h, "Reading file %s for transfer", fullpath); - fd = open (fullpath, O_RDONLY); - if (fd < 0) { - FLUX_LOG_ERR (h, "DYAD_MOD: Failed to open file \"%s\".\n", fullpath); - goto fetch_error; - } - if ((inlen = read_all (fd, &inbuf)) < 0) { + if ((inlen = read_all (fullpath, &inbuf)) < 0) { FLUX_LOG_ERR (h, "DYAD_MOD: Failed to load file \"%s\".\n", fullpath); goto fetch_error; } - close (fd); FLUX_LOG_INFO (h, "Is inbuf NULL? -> %i\n", (int)(inbuf == NULL)); FLUX_LOG_INFO (h, "Establish DTL connection with consumer"); @@ -188,7 +182,7 @@ dyad_fetch_request_cb (flux_t *h, flux_msg_handler_t *w, const flux_msg_t *msg, errno = ECOMM; goto fetch_error; } - + munmap(inbuf, inlen); FLUX_LOG_INFO (h, "Close RPC message stream with an ENODATA (%d) message", ENODATA); if (flux_respond_error (h, msg, ENODATA, NULL) < 0) { FLUX_LOG_ERR (h, diff --git a/src/utils/read_all.c b/src/utils/read_all.c index a279ad73..459bdb8d 100644 --- a/src/utils/read_all.c +++ b/src/utils/read_all.c @@ -11,11 +11,14 @@ #if HAVE_CONFIG_H #include "config.h" #endif // HAVE_CONFIG_H - +#define _GNU_SOURCE #include #include +#include +#include #include - +#include +#include #include "read_all.h" ssize_t write_all (int fd, const void *buf, size_t len) @@ -37,8 +40,13 @@ ssize_t write_all (int fd, const void *buf, size_t len) return count; } -ssize_t read_all (int fd, void **bufp) +ssize_t read_all (const char* filename, void **bufp) { + int fd = open (filename, O_RDONLY); + if (fd < 0) { + errno = EINVAL; + return -1; + } const ssize_t file_size = lseek (fd, 0, SEEK_END); if (file_size == 0) { errno = EINVAL; @@ -54,11 +62,9 @@ ssize_t read_all (int fd, void **bufp) errno = EINVAL; return -1; } - ssize_t bytes_read = read (fd, *bufp, file_size); - if (bytes_read < file_size) { - // could not read all data + if ((*bufp = mmap (*bufp, file_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 0)) == (caddr_t) -1) { errno = EINVAL; - return bytes_read; + return -1; } return file_size; } diff --git a/src/utils/read_all.h b/src/utils/read_all.h index 23a0ed78..0106d444 100644 --- a/src/utils/read_all.h +++ b/src/utils/read_all.h @@ -23,7 +23,7 @@ ssize_t write_all (int fd, const void *buf, size_t len); __attribute__ ((annotate ("@critical_path()"))) #endif ssize_t -read_all (int fd, void **bufp); +read_all (const char* filename, void **bufp); #if defined(__cplusplus) }; From 0b70ef82837f826f22899a7421596c523bd75c5f Mon Sep 17 00:00:00 2001 From: Hariharan Devarajan Date: Sun, 3 Dec 2023 21:25:54 +0000 Subject: [PATCH 2/3] allign malloc for mmap. --- src/utils/read_all.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/utils/read_all.c b/src/utils/read_all.c index 459bdb8d..37b8ec6a 100644 --- a/src/utils/read_all.c +++ b/src/utils/read_all.c @@ -21,6 +21,12 @@ #include #include "read_all.h" +void * allocate_aligned(size_t size) { + size_t alignment = getpagesize(); + int malloc_offset = (size + alignment) % alignment; + void* mem = malloc(size + alignment); + return (char*) mem + malloc_offset; +} ssize_t write_all (int fd, const void *buf, size_t len) { ssize_t n = 0; @@ -57,7 +63,7 @@ ssize_t read_all (const char* filename, void **bufp) errno = EINVAL; return -1; } - *bufp = malloc (file_size); + *bufp = allocate_aligned (file_size); if (*bufp == NULL) { errno = EINVAL; return -1; From 3e9a46c66a1dba80ee7ea81167a0389bac9de29f Mon Sep 17 00:00:00 2001 From: Hariharan Devarajan Date: Mon, 4 Dec 2023 15:41:51 +0000 Subject: [PATCH 3/3] use stdlib alligned alloc --- src/utils/read_all.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/utils/read_all.c b/src/utils/read_all.c index 37b8ec6a..5df68dfd 100644 --- a/src/utils/read_all.c +++ b/src/utils/read_all.c @@ -21,12 +21,6 @@ #include #include "read_all.h" -void * allocate_aligned(size_t size) { - size_t alignment = getpagesize(); - int malloc_offset = (size + alignment) % alignment; - void* mem = malloc(size + alignment); - return (char*) mem + malloc_offset; -} ssize_t write_all (int fd, const void *buf, size_t len) { ssize_t n = 0; @@ -63,7 +57,8 @@ ssize_t read_all (const char* filename, void **bufp) errno = EINVAL; return -1; } - *bufp = allocate_aligned (file_size); + size_t alignment = getpagesize(); + *bufp = aligned_alloc(alignment, file_size); if (*bufp == NULL) { errno = EINVAL; return -1;