diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2019-08-19 18:23:40 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2019-08-22 16:07:58 +0200 |
| commit | 196fa608205bdbd4016200e47a55e9fb000f2b11 (patch) | |
| tree | 331ac7235f87e9b4ee1900613f5d302a0b9fd5be | |
| parent | 3358e8204627bfba994abe989ea4119fe033fca9 (diff) | |
| download | allocbench-196fa608205bdbd4016200e47a55e9fb000f2b11.tar.gz allocbench-196fa608205bdbd4016200e47a55e9fb000f2b11.zip | |
readd chattymalloc
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | src/Makefile | 29 | ||||
| -rw-r--r-- | src/benchmarks/lld.py | 4 | ||||
| -rw-r--r-- | src/chattymalloc.c | 244 |
4 files changed, 261 insertions, 17 deletions
@@ -12,3 +12,4 @@ lld-speed-test* *.hist status tags +**/chattymalloc.txt diff --git a/src/Makefile b/src/Makefile index 8d6d110..bd3b791 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,28 +13,29 @@ LDFLAGS ?= -pthread -static-libgcc MEMSIZE_KB=$(shell free -t | tail -1 | tr -s ' ' | cut -d ' ' -f 2) MEMSIZE=$(shell echo $(MEMSIZE_KB)"* 1024" | bc) +TOOLS = print_status_on_exit.so exec +ALLOCS = chattymalloc.so bumpptr_alloc.so +TARGETS = $(addprefix $(OBJDIR)/allocators/,$(ALLOCS)) $(addprefix $(OBJDIR)/,$(TOOLS)) + .PHONY: all clean -all: $(OBJDIR)/print_status_on_exit.so $(OBJDIR)/allocators/bumpptr_alloc.so $(OBJDIR)/exec +all: $(TARGETS) -$(OBJDIR)/allocators/bumpptr_alloc.so: bumpptr_alloc.c Makefile | $(OBJDIR)/allocators - @echo "Compiling $@..."; +$(OBJDIR)/allocators/bumpptr_alloc.so: bumpptr_alloc.c Makefile + @if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi $(CC) $(LDFLAGS) -shared -DMEMSIZE=$(MEMSIZE) $(CFLAGS) -o $@ $< - # $(CC) $(LDFLAGS) -shared $(CFLAGS) -o $@ $< -$(OBJDIR)/print_status_on_exit.so: print_status_on_exit.c Makefile | $(OBJDIR) - @echo "Compiling $@..."; +$(OBJDIR)/allocators/chattymalloc.so: chattymalloc.c Makefile + @if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi + $(CC) $(LDFLAGS) -shared $(CFLAGS) -o $@ $< -ldl + +$(OBJDIR)/print_status_on_exit.so: print_status_on_exit.c Makefile + @if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi $(CC) $(LDFLAGS) -shared $(CFLAGS) -o $@ $< -$(OBJDIR)/exec: exec.c Makefile | $(OBJDIR) - @echo "Compiling $@..."; +$(OBJDIR)/exec: exec.c Makefile + @if test \( ! \( -d $(@D) \) \) ;then mkdir -p $(@D);fi $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $< -$(OBJDIR)/allocators: - mkdir -p $@ - -$(OBJDIR): - mkdir $@ - clean: rm -rf $(OBJDIR) diff --git a/src/benchmarks/lld.py b/src/benchmarks/lld.py index 1a7b69e..5c525d5 100644 --- a/src/benchmarks/lld.py +++ b/src/benchmarks/lld.py @@ -68,7 +68,6 @@ class BenchmarkLld(Benchmark): args = self.results["args"] allocators = self.results["allocators"] - for perm in self.iterate_args(args=args): for i, allocator in enumerate(allocators): @@ -81,8 +80,6 @@ class BenchmarkLld(Benchmark): plt.savefig(".".join([self.name, perm.test, "runtime", "png"])) plt.clf() - - # TODO: get memusage # Memusage # self.barplot_single_arg("{VmHWM}", @@ -96,4 +93,5 @@ class BenchmarkLld(Benchmark): # self.export_stats_to_dataref("VmHWM") self.export_stats_to_dataref("task-clock") + lld = BenchmarkLld() diff --git a/src/chattymalloc.c b/src/chattymalloc.c new file mode 100644 index 0000000..b3ecd02 --- /dev/null +++ b/src/chattymalloc.c @@ -0,0 +1,244 @@ +#define _GNU_SOURCE +#include <dlfcn.h> +#include <errno.h> +#include <fcntl.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static char tmpbuff[4096]; +static unsigned long tmppos = 0; +static unsigned long tmpallocs = 0; + +static int out = -1; + +/*========================================================= + * intercepted functions + */ + +static void* (*myfn_malloc)(size_t size); +static void (*myfn_free)(void* ptr); +static void* (*myfn_calloc)(size_t nmemb, size_t size); +static void* (*myfn_realloc)(void* ptr, size_t size); +static void* (*myfn_memalign)(size_t alignment, size_t size); +static int (*myfn_posix_memalign)(void** memptr, size_t alignment, size_t size); +static void* (*myfn_valloc)(size_t size); +static void* (*myfn_pvalloc)(size_t size); +static void* (*myfn_aligned_alloc)(size_t alignment, size_t size); +static int (*myfn_malloc_stats)(); + +static void +write_output(const char* fmt, ...) +{ + static int prevent_recursion = 0; + + if (!prevent_recursion) { + prevent_recursion = 1; + + /* lockf(out, F_LOCK, 0); */ + + va_list args; + va_start(args, fmt); + vdprintf(out, fmt, args); + va_end(args); + + /* lockf(out, F_ULOCK, 0); */ + prevent_recursion = 0; + } +} + +static void __attribute__((constructor)) +init() +{ + out = open("chattymalloc.txt", + O_WRONLY | O_TRUNC | O_CREAT, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (out == -1) { + perror("opening output file"); + exit(1); + } + + myfn_malloc = dlsym(RTLD_NEXT, "malloc"); + myfn_free = dlsym(RTLD_NEXT, "free"); + myfn_calloc = dlsym(RTLD_NEXT, "calloc"); + myfn_realloc = dlsym(RTLD_NEXT, "realloc"); + myfn_memalign = dlsym(RTLD_NEXT, "memalign"); + myfn_posix_memalign = dlsym(RTLD_NEXT, "posix_memalign"); + myfn_valloc = dlsym(RTLD_NEXT, "valloc"); + myfn_pvalloc = dlsym(RTLD_NEXT, "pvalloc"); + myfn_aligned_alloc = dlsym(RTLD_NEXT, "aligned_alloc"); + myfn_malloc_stats = dlsym(RTLD_NEXT, "malloc_stats"); + + if (!myfn_malloc || !myfn_free || !myfn_calloc || !myfn_realloc || + !myfn_memalign) { + fprintf(stderr, "Can't load core functions with `dlsym`: %s\n", dlerror()); + exit(1); + } + if (!myfn_posix_memalign) + fprintf(stderr, "Can't load posix_memalign with `dlsym`: %s\n", dlerror()); + if (!myfn_valloc) + fprintf(stderr, "Can't load valloc with `dlsym`: %s\n", dlerror()); + if (!myfn_pvalloc) + fprintf(stderr, "Can't load pvalloc with `dlsym`: %s\n", dlerror()); + if (!myfn_aligned_alloc) + fprintf(stderr, "Can't load aligned_alloc with `dlsym`: %s\n", dlerror()); + if (!myfn_malloc_stats) + fprintf(stderr, "Can't load malloc_stats with `dlsym`: %s\n", dlerror()); +} + +void* +malloc(size_t size) +{ + static int initializing = 0; + + if (myfn_malloc == NULL) { + if (!initializing) { + initializing = 1; + init(); + initializing = 0; + + } else { + void* retptr = tmpbuff + tmppos; + tmppos += size; + ++tmpallocs; + + if (tmppos < sizeof(tmpbuff)) { + return retptr; + } else { + fprintf(stderr, "%d in %d allocs\n", tmppos, tmpallocs); + fprintf(stderr, + "jcheck: too much memory requested during initialisation - " + "increase tmpbuff size\n"); + exit(1); + } + } + } + + void* ptr = myfn_malloc(size); + write_output("m %zu %p\n", size, ptr); + return ptr; +} + +void +free(void* ptr) +{ + // something wrong if we call free before one of the allocators! + if (myfn_malloc == NULL) + init(); + if (!(ptr >= (void*)tmpbuff && ptr <= (void*)(tmpbuff + tmppos))) { + write_output("f %p\n", ptr); + myfn_free(ptr); + } +} + +void* +realloc(void* ptr, size_t size) +{ + if (myfn_realloc == NULL) { + void* nptr = malloc(size); + if (nptr && ptr) { + memmove(nptr, ptr, size); + free(ptr); + } + return nptr; + } + + void* nptr = myfn_realloc(ptr, size); + write_output("r %p %zu %p\n", ptr, size, nptr); + return nptr; +} +void* +calloc(size_t nmemb, size_t size) +{ + if (myfn_calloc == NULL) { + void* ptr = malloc(nmemb * size); + if (ptr) + memset(ptr, 0, nmemb * size); + return ptr; + } + + void* ptr = myfn_calloc(nmemb, size); + write_output("c %zu %zu %p\n", nmemb, size, ptr); + return ptr; +} + +void* +memalign(size_t alignment, size_t size) +{ + if (myfn_memalign == NULL) { + fprintf(stderr, "called memalign before or during init\n"); + exit(1); + } + + void* ptr = myfn_memalign(alignment, size); + write_output("ma %zu %zu %p\n", alignment, size, ptr); + return ptr; +} + +int +posix_memalign(void** memptr, size_t alignment, size_t size) +{ + if (myfn_posix_memalign == NULL) { + fprintf(stderr, "called posix_memalign before or during init\n"); + exit(1); + } + + int ret = myfn_posix_memalign(memptr, alignment, size); + write_output("p_ma %p %zu %zu %d\n", memptr, alignment, size, ret); + return ret; +} + +void* +valloc(size_t size) +{ + if (myfn_valloc == NULL) { + fprintf(stderr, "called valloc before or during init"); + exit(1); + } + + void* ptr = myfn_valloc(size); + write_output("v %zu %p\n", size, ptr); + return ptr; +} + +void* +pvalloc(size_t size) +{ + if (myfn_pvalloc == NULL) { + fprintf(stderr, "called pvalloc before or during init\n"); + exit(1); + } + + void* ptr = myfn_pvalloc(size); + write_output("pv %zu %p\n", size, ptr); + return ptr; +} + +void* +aligned_alloc(size_t alignment, size_t size) +{ + if (myfn_aligned_alloc == NULL) { + fprintf(stderr, "called aligned_alloc before or during init\n"); + exit(1); + } + + void* ptr = myfn_pvalloc(size); + write_output("a_m %zu %zu %p\n", alignment, size, ptr); + return ptr; +} + +int +malloc_stats() +{ + if (myfn_malloc_stats == NULL) { + fprintf(stderr, "called malloc_stats before or during init\n"); + exit(1); + } + + fprintf(stderr, "chattymalloc by muhq\n"); + write_output("m_s\n"); + return myfn_malloc_stats(); +} |
