-
Notifications
You must be signed in to change notification settings - Fork 4
mmap #29
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
base: master
Are you sure you want to change the base?
mmap #29
Changes from all 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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| #include <pthread.h> | ||
| #include <threads.h> | ||
| #include <execinfo.h> | ||
| #include <numaif.h> | ||
|
|
||
| #ifdef HAVE_STDATOMIC_H | ||
| #include <stdatomic.h> | ||
|
|
@@ -284,6 +285,7 @@ memtier_policy_static_ratio_get_kind(struct memtier_memory *memory, | |
| dest_tier = i; | ||
| } | ||
| } | ||
| //log_info("kind: %d", dest_tier); | ||
|
Collaborator
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. Maybe we could remove this line? If not, put it under some #def? |
||
| return cfg[dest_tier].kind; | ||
| } | ||
|
|
||
|
|
@@ -1189,14 +1191,85 @@ MEMKIND_EXPORT void *memtier_malloc(struct memtier_memory *memory, size_t size) | |
| void *ptr; | ||
| uint64_t data; | ||
|
|
||
| ptr = memtier_kind_malloc(memory->get_kind(memory, size, &data), size); | ||
| memkind_t kind = memory->get_kind(memory, size, &data); | ||
| ptr = memtier_kind_malloc(kind, size); | ||
| memory->post_alloc(data, ptr, size); | ||
| memory->update_cfg(memory); | ||
| print_memory_statistics(memory); | ||
|
|
||
| return ptr; | ||
| } | ||
|
|
||
| void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t off) | ||
| { | ||
| long ret = syscall(SYS_mmap, addr, length, prot, flags, fd, off); | ||
| if (ret == -EPERM && !off && (flags&MAP_ANON) && !(flags&MAP_FIXED)) | ||
| ret = -ENOMEM; | ||
| if (ret > -4096 && ret < 0) { | ||
|
Collaborator
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. Why > - 4096 ? Please at least add a comment about an origin of this magick number... |
||
| errno = -ret; | ||
| return MAP_FAILED; | ||
| } | ||
|
|
||
| return (void*)ret; | ||
| } | ||
|
|
||
| int munmap(void *addr, size_t length) | ||
|
Collaborator
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. How does this implementation differ from the original munmap? Is it necessary to add it here? |
||
| { | ||
| long ret = syscall(SYS_munmap, addr, length); | ||
| if (!ret) | ||
| return 0; | ||
| errno = -ret; | ||
| return -1; | ||
| } | ||
|
|
||
| #define SLICE (2*1048576) | ||
|
|
||
| static void split_into_slices(struct memtier_memory *memory, void *addr, size_t length) | ||
| { | ||
| size_t todo = length; | ||
| void *sladdr = addr; | ||
|
|
||
| while (todo > 0) { | ||
| size_t len = (todo > SLICE) ? SLICE : todo; | ||
|
|
||
| uint64_t data = 0; | ||
| memkind_t kind = memory->get_kind(memory, len, &data); | ||
| memory->post_alloc(data, sladdr, len); | ||
| if (kind == MEMKIND_DEFAULT) | ||
| mbind(sladdr, len, MPOL_DEFAULT, 0, 0 ,0); | ||
| else { | ||
| unsigned long nodemask; | ||
| if (kind->ops->get_mbind_nodemask | ||
| && !kind->ops->get_mbind_nodemask(kind, &nodemask, sizeof(nodemask)*8)) | ||
| { | ||
| mbind(sladdr, len, MPOL_PREFERRED, &nodemask, sizeof(nodemask)*8, 0); | ||
| } else | ||
| fprintf(stderr, "Kind without nodemask\n"); | ||
| } | ||
|
|
||
| todo -= len; | ||
| sladdr += len; | ||
| } | ||
| } | ||
|
|
||
| MEMKIND_EXPORT void* memtier_mmap(struct memtier_memory *memory, void *addr, size_t length, int prot, int flags, int fd, off_t offset) | ||
| { | ||
| if (memory == NULL) | ||
| return sys_mmap(addr, length, prot, flags, fd, offset); | ||
|
|
||
| void* ret = memtier_malloc(memory, length); | ||
|
Collaborator
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. Isn't malloc supposed to be thread safe? We only use variables specified as arguments here, do we use the mutex to protect them?
Collaborator
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 does not support splittings big mmaps into multiple smaller remaps on different regions... I thought this was supposed to be the key feature |
||
|
|
||
| if (ret) | ||
| split_into_slices(memory, ret, length); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| MEMKIND_EXPORT void memtier_munmap(void* ptr) | ||
| { | ||
| memtier_free(ptr); | ||
| } | ||
|
|
||
| MEMKIND_EXPORT void *memtier_kind_malloc(memkind_t kind, size_t size) | ||
| { | ||
| // static atomic_uint_fast16_t counter=0; | ||
|
|
@@ -1277,9 +1350,8 @@ MEMKIND_EXPORT void *memtier_realloc(struct memtier_memory *memory, void *ptr, | |
| return ptr; | ||
| } | ||
|
|
||
| if (size == 0) { | ||
| if (size == 0) | ||
| return NULL; | ||
| } | ||
|
|
||
| return memtier_malloc(memory, size); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,19 @@ | |
| #include <pthread.h> | ||
| #include <string.h> | ||
| #include <dlfcn.h> | ||
| #include <sys/mman.h> | ||
|
|
||
| #include <pthread.h> | ||
| #include <threads.h> | ||
| #include <execinfo.h> | ||
|
|
||
| #ifdef HAVE_STDATOMIC_H | ||
| #include <stdatomic.h> | ||
| #define MEMKIND_ATOMIC _Atomic | ||
| #else | ||
| #define MEMKIND_ATOMIC | ||
|
Collaborator
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. Is it really how we want to handle atomics in this case? |
||
| #endif | ||
|
|
||
|
|
||
| #define MEMTIER_EXPORT __attribute__((visibility("default"))) | ||
| #define MEMTIER_INIT __attribute__((constructor)) | ||
|
|
@@ -85,6 +98,9 @@ static int destructed; | |
|
|
||
| static struct memtier_memory *current_memory; | ||
|
|
||
| void *sys_mmap(void *, size_t, int, int, int, off_t); | ||
| int sys_munmap(void *, size_t); | ||
|
|
||
| MEMTIER_EXPORT void *malloc(size_t size) | ||
| { | ||
| if (MEMTIER_LIKELY(current_memory)) { | ||
|
|
@@ -143,6 +159,40 @@ MEMTIER_EXPORT size_t malloc_usable_size(void *ptr) | |
| return memtier_usable_size(ptr); | ||
| } | ||
|
|
||
| MEMTIER_EXPORT void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) | ||
| { | ||
| // TODO tweaked for MLC - find other valid flag combinations | ||
| if ((current_memory == 0) || ( | ||
| (addr == NULL) && | ||
| (prot == (PROT_READ | PROT_WRITE)) && | ||
| (flags == (MAP_ANONYMOUS | MAP_PRIVATE)))) | ||
| { | ||
| //log_err("mmap: start:%p, length:%lu, prot:%d, flags:%d, fd:%d, offset:%ld", | ||
| // addr, length, prot, flags, fd, offset); | ||
|
|
||
| return memtier_mmap(current_memory, addr, length, prot, flags, fd, offset); | ||
| } | ||
|
|
||
| return sys_mmap(addr, length, prot, flags, fd, offset); | ||
| } | ||
|
|
||
| /* | ||
| MEMTIER_EXPORT int munmap(void *addr, size_t length) | ||
| { | ||
| int i; | ||
| for(i = 0; i < num_mmaps; i++) | ||
| { | ||
| if (mmap_map[i] == addr) { | ||
| //log_err("munmap: start:%p, length:%lu", addr, length); | ||
| memtier_munmap(addr); | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| return sys_munmap(addr, length); | ||
| } | ||
| */ | ||
|
|
||
| static pthread_once_t init_once = PTHREAD_ONCE_INIT; | ||
|
|
||
| static MEMTIER_INIT void memtier_init(void) | ||
|
|
||
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.
What does this TODO relate to?