-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes to support mmap reads on Broker #57
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,22 @@ | |
#if HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif // HAVE_CONFIG_H | ||
|
||
#define _GNU_SOURCE | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
|
||
#include <fcntl.h> | ||
#include <sys/mman.h> | ||
#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; | ||
|
@@ -37,8 +46,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This interface probably should take a page aligned memory address as an input for the UCX managed memory. So, the allocation would happen outside of this function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is the buffer bufp. Once we do the UCX, we can move allocation there. |
||
{ | ||
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; | ||
|
@@ -49,16 +63,14 @@ ssize_t read_all (int fd, void **bufp) | |
errno = EINVAL; | ||
return -1; | ||
} | ||
*bufp = malloc (file_size); | ||
*bufp = allocate_aligned (file_size); | ||
if (*bufp == NULL) { | ||
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; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think that this will guarantee that the address is a multiple of alignment, while the size of the usable block is. In addition, i am wondering if it would be safe to use such an address with
munmap()
.We can instead rely on
posix_memalign()