-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dlmalloc to manage shared memory (#15)
* Use dlmalloc to manage shared memory * add stresstest
- Loading branch information
1 parent
04737f3
commit d52bf7d
Showing
12 changed files
with
6,496 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
build/* | ||
*~ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/mman.h> | ||
#include <unistd.h> | ||
|
||
#include "plasma.h" | ||
#include "uthash.h" | ||
|
||
void *fake_mmap(size_t); | ||
int fake_munmap(void *, size_t); | ||
|
||
#define MMAP(s) fake_mmap(s) | ||
#define MUNMAP(a, s) fake_munmap(a, s) | ||
#define DIRECT_MMAP(s) fake_mmap(s) | ||
#define DIRECT_MUNMAP(a, s) fake_munmap(a, s) | ||
#define USE_DL_PREFIX | ||
#define HAVE_MORECORE 0 | ||
|
||
#include "third_party/dlmalloc.c" | ||
|
||
#undef MMAP | ||
#undef MUNMAP | ||
#undef DIRECT_MMAP | ||
#undef DIRECT_MUNMAP | ||
#undef USE_DL_PREFIX | ||
#undef HAVE_MORECORE | ||
|
||
struct mmap_record { | ||
int fd; | ||
void *pointer; | ||
int64_t size; | ||
UT_hash_handle hh_fd; | ||
UT_hash_handle hh_pointer; | ||
}; | ||
|
||
struct mmap_record *records_by_fd = NULL; | ||
struct mmap_record *records_by_pointer = NULL; | ||
|
||
/* Create a buffer. This is creating a temporary file and then | ||
* immediately unlinking it so we do not leave traces in the system. */ | ||
int create_buffer(int64_t size) { | ||
static char template[] = "/tmp/plasmaXXXXXX"; | ||
char file_name[32]; | ||
strncpy(file_name, template, 32); | ||
int fd = mkstemp(file_name); | ||
if (fd < 0) | ||
return -1; | ||
FILE *file = fdopen(fd, "a+"); | ||
if (!file) { | ||
close(fd); | ||
return -1; | ||
} | ||
if (unlink(file_name) != 0) { | ||
LOG_ERR("unlink error"); | ||
return -1; | ||
} | ||
if (ftruncate(fd, (off_t) size) != 0) { | ||
LOG_ERR("ftruncate error"); | ||
return -1; | ||
} | ||
return fd; | ||
} | ||
|
||
void *fake_mmap(size_t size) { | ||
// Add sizeof(size_t) so that the returned pointer is deliberately not | ||
// page-aligned. This ensures that the segments of memory returned by | ||
// fake_mmap are never contiguous. | ||
int fd = create_buffer(size + sizeof(size_t)); | ||
void *pointer = mmap(NULL, size + sizeof(size_t), PROT_READ | PROT_WRITE, | ||
MAP_SHARED, fd, 0); | ||
if (pointer == MAP_FAILED) { | ||
return pointer; | ||
} | ||
pointer += sizeof(size_t); | ||
|
||
struct mmap_record *record = malloc(sizeof(struct mmap_record)); | ||
record->fd = fd; | ||
record->pointer = pointer; | ||
record->size = size; | ||
HASH_ADD(hh_fd, records_by_fd, fd, sizeof(fd), record); | ||
HASH_ADD(hh_pointer, records_by_pointer, pointer, sizeof(pointer), record); | ||
|
||
LOG_DEBUG("%p = fake_mmap(%lu)", pointer, size); | ||
return pointer; | ||
} | ||
|
||
int fake_munmap(void *addr, size_t size) { | ||
LOG_DEBUG("fake_munmap(%p, %lu)", addr, size); | ||
|
||
struct mmap_record *record; | ||
|
||
addr -= sizeof(size_t); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
HASH_FIND(hh_pointer, records_by_pointer, &addr, sizeof(addr), record); | ||
assert(record != NULL); | ||
close(record->fd); | ||
|
||
HASH_DELETE(hh_fd, records_by_fd, record); | ||
HASH_DELETE(hh_pointer, records_by_pointer, record); | ||
|
||
return munmap(addr, size + sizeof(size_t)); | ||
} | ||
|
||
void get_malloc_mapinfo(void *addr, | ||
int *fd, | ||
int64_t *map_size, | ||
ptrdiff_t *offset) { | ||
struct mmap_record *record; | ||
// TODO(rshin): Implement a more efficient search through records_by_fd. | ||
for (record = records_by_fd; record != NULL; record = record->hh_fd.next) { | ||
if (addr >= record->pointer && addr < record->pointer + record->size) { | ||
*fd = record->fd; | ||
*map_size = record->size; | ||
*offset = addr - record->pointer; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return; | ||
} | ||
} | ||
*fd = -1; | ||
*map_size = 0; | ||
*offset = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef MALLOC_H | ||
#define MALLOC_H | ||
|
||
void get_malloc_mapinfo(void *addr, | ||
int *fd, | ||
int64_t *map_length, | ||
ptrdiff_t *offset); | ||
|
||
#endif // MALLOC_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
This line is illegal (can't subtract from
void *
)