From d56025ec1a5167542dc543166d9f41f848f38f90 Mon Sep 17 00:00:00 2001 From: Jes Sorensen Date: Mon, 27 Dec 2010 16:47:56 -0200 Subject: [RHEL6 qemu-kvm PATCH 06/23] trace: Trace qemu_malloc() and qemu_vmalloc() RH-Author: Jes Sorensen Message-id: <1293468492-25473-5-git-send-email-Jes.Sorensen@redhat.com> Patchwork-id: 15287 O-Subject: [PATCH 04/20] trace: Trace qemu_malloc() and qemu_vmalloc() Bugzilla: 632722 RH-Acked-by: Markus Armbruster RH-Acked-by: Gleb Natapov RH-Acked-by: Marcelo Tosatti RH-Acked-by: Daniel P. Berrange From: Stefan Hajnoczi It is often useful to instrument memory management functions in order to find leaks or performance problems. This patch adds trace events for the memory allocation primitives. Signed-off-by: Stefan Hajnoczi (cherry picked from commit cd245a19329edfcd968b00d05ad92de7a0e2daa1) --- osdep.c | 24 ++++++++++++++++++------ qemu-malloc.c | 16 +++++++++++++--- trace-events | 10 ++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) Signed-off-by: Eduardo Habkost --- osdep.c | 24 ++++++++++++++++++------ qemu-malloc.c | 16 +++++++++++++--- trace-events | 10 ++++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/osdep.c b/osdep.c index b60d32b..a094db2 100644 --- a/osdep.c +++ b/osdep.c @@ -45,6 +45,7 @@ #endif #include "qemu-common.h" +#include "trace.h" #include "sysemu.h" #include "qemu_socket.h" @@ -66,25 +67,34 @@ static void *oom_check(void *ptr) #if defined(_WIN32) void *qemu_memalign(size_t alignment, size_t size) { + void *ptr; + if (!size) { abort(); } - return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); + ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); + trace_qemu_memalign(alignment, size, ptr); + return ptr; } void *qemu_vmalloc(size_t size) { + void *ptr; + /* FIXME: this is not exactly optimal solution since VirtualAlloc has 64Kb granularity, but at least it guarantees us that the memory is page aligned. */ if (!size) { abort(); } - return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); + ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); + trace_qemu_vmalloc(size, ptr); + return ptr; } void qemu_vfree(void *ptr) { + trace_qemu_vfree(ptr); VirtualFree(ptr, 0, MEM_RELEASE); } @@ -92,21 +102,22 @@ void qemu_vfree(void *ptr) void *qemu_memalign(size_t alignment, size_t size) { + void *ptr; #if defined(_POSIX_C_SOURCE) && !defined(__sun__) int ret; - void *ptr; ret = posix_memalign(&ptr, alignment, size); if (ret != 0) { fprintf(stderr, "Failed to allocate %zu B: %s\n", size, strerror(ret)); abort(); } - return ptr; #elif defined(CONFIG_BSD) - return oom_check(valloc(size)); + ptr = oom_check(valloc(size)); #else - return oom_check(memalign(alignment, size)); + ptr = oom_check(memalign(alignment, size)); #endif + trace_qemu_memalign(alignment, size, ptr); + return ptr; } /* alloc shared memory pages */ @@ -121,6 +132,7 @@ void *qemu_vmalloc(size_t size) void qemu_vfree(void *ptr) { + trace_qemu_vfree(ptr); free(ptr); } diff --git a/qemu-malloc.c b/qemu-malloc.c index 5d9e34d..bd82e6f 100644 --- a/qemu-malloc.c +++ b/qemu-malloc.c @@ -22,6 +22,7 @@ * THE SOFTWARE. */ #include "qemu-common.h" +#include "trace.h" #include static void *oom_check(void *ptr) @@ -39,6 +40,7 @@ void *get_mmap_addr(unsigned long size) void qemu_free(void *ptr) { + trace_qemu_free(ptr); free(ptr); } @@ -53,18 +55,26 @@ static int allow_zero_malloc(void) void *qemu_malloc(size_t size) { + void *ptr; if (!size && !allow_zero_malloc()) { abort(); } - return oom_check(malloc(size ? size : 1)); + ptr = oom_check(malloc(size ? size : 1)); + trace_qemu_malloc(size, ptr); + return ptr; } void *qemu_realloc(void *ptr, size_t size) { + void *newptr; if (size) { - return oom_check(realloc(ptr, size)); + newptr = oom_check(realloc(ptr, size)); + trace_qemu_realloc(ptr, size, newptr); + return newptr; } else if (allow_zero_malloc()) { - return oom_check(realloc(ptr, size ? size : 1)); + newptr = oom_check(realloc(ptr, size ? size : 1)); + trace_qemu_realloc(ptr, size, newptr); + return newptr; } abort(); } diff --git a/trace-events b/trace-events index 2a986ec..d2f2bbc 100644 --- a/trace-events +++ b/trace-events @@ -27,3 +27,13 @@ # system may not have the necessary headers included. # # The should be a sprintf()-compatible format string. + +# qemu-malloc.c +disable qemu_malloc(size_t size, void *ptr) "size %zu ptr %p" +disable qemu_realloc(void *ptr, size_t size, void *newptr) "ptr %p size %zu newptr %p" +disable qemu_free(void *ptr) "ptr %p" + +# osdep.c +disable qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu size %zu ptr %p" +disable qemu_valloc(size_t size, void *ptr) "size %zu ptr %p" +disable qemu_vfree(void *ptr) "ptr %p" -- 1.7.3.2