add malloc statistics infrastructure
cover add malloc statistics infrastructure
These patches add a --enable-malloc-stats configure options, which will make qemu trace its memory allocations. A new monitor command dump-malloc-stats lets the user dump them to a file.
Using statistics with KVM depends on the patch I posted recently to use qemu_free consistently. However, there is no regression without the configure flag.
Paolo Bonzini (2):
malloc: move memalign/vmalloc to qemu-malloc.c
malloc: add statistics infrastructure
configure | 9 ++ hmp-commands.hx | 16 ++++ monitor.c | 12 +++ osdep.h | 5 +- oslib-posix.c | 32 ++------ oslib-win32.c | 31 +------- qemu-common.h | 46 +++++++++-- qemu-malloc.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++---- trace-events | 1 - 9 files changed, 316 insertions(+), 80 deletions(-)
Signature · 1 line
1.7.4.4
PATCH 1/2 malloc: move memalign/vmalloc to qemu-malloc.c
This splits qemu_memalign and qemu_vfree in two: a part that does just the allocation in oslib, and a part that includes the tracing in qemu-malloc.c.
--- osdep.h | 5 ++--- oslib-posix.c | 32 +++++++------------------------- oslib-win32.c | 31 ++----------------------------- qemu-common.h | 7 +++++-- qemu-malloc.c | 33 +++++++++++++++++++++++++++++++++ trace-events | 1 - 6 files changed, 49 insertions(+), 60 deletions(-)
osdep.h +2 -3
@@ -88,9 +88,8 @@# define QEMU_GNUC_PREREQ (maj, min) 0#endif -void *qemu_memalign (size_t alignment,size_t size); -void *qemu_vmalloc (size_t size); -void qemu_vfree (void *ptr); +void *os_memalign (size_t alignment,size_t size); +void os_vfree (void *ptr);#define QEMU_MADV_INVALID -1
oslib-posix.c +7 -25
@@ -28,47 +28,29 @@#include "config-host.h" #include "sysemu.h" -#include "trace.h" #include "qemu_socket.h" -void *qemu_oom_check (void *ptr) -{ -if (ptr ==NULL ) { -fprintf (stderr,"Failed to allocate memory: %s\n" ,strerror (errno)); -abort (); - } -return ptr; -} - -void *qemu_memalign (size_t alignment,size_t size) +void *os_memalign (size_t alignment,size_t size) {void *ptr; +assert (alignment >sizeof (void *)); +assert ((alignment & (alignment -1 )) ==0 );#if defined(_POSIX_C_SOURCE) && !defined(__sun__)int ret; ret =posix_memalign (&ptr, alignment, size);if (ret !=0 ) { -fprintf (stderr,"Failed to allocate %zu B: %s\n" , - size,strerror (ret)); -abort (); + ptr =NULL ; }#elif defined(CONFIG_BSD ) - ptr =qemu_oom_check (valloc (size)); + ptr =valloc (size);#else - ptr =qemu_oom_check (memalign (alignment, size)); + ptr =memalign (alignment, size);#endif -trace_qemu_memalign (alignment, size, ptr);return ptr; } -/* alloc shared memory pages */ -void *qemu_vmalloc (size_t size) -{ -return qemu_memalign (getpagesize (), size); -} - -void qemu_vfree (void *ptr) +void os_vfree (void *ptr) { -trace_qemu_vfree (ptr);free (ptr); }
oslib-win32.c +2 -29
@@ -28,48 +28,21 @@#include <windows.h> #include "config-host.h" #include "sysemu.h" -#include "trace.h" #include "qemu_socket.h" -void *qemu_oom_check (void *ptr) +void *os_memalign (size_t alignment,size_t size) { -if (ptr ==NULL ) { -fprintf (stderr,"Failed to allocate memory: %lu\n" ,GetLastError ()); -abort (); - } -return ptr; -} - -void *qemu_memalign (size_t alignment,size_t size) -{ -void *ptr; - -if (!size) { -abort (); - } - ptr =qemu_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 (); } - ptr =qemu_oom_check (VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE )); -trace_qemu_vmalloc (size, ptr); -return ptr; +return VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE ); }void qemu_vfree (void *ptr) { -trace_qemu_vfree (ptr);VirtualFree (ptr,0 ,MEM_RELEASE ); }
qemu-common.h +5 -2
@@ -206,13 +206,16 @@ const char *path(const char *pathname);int ffs (int i);#endif -void *qemu_oom_check (void *ptr);void *qemu_malloc (size_t size);void *qemu_realloc (void *ptr,size_t size);void *qemu_mallocz (size_t size); -void qemu_free (void *ptr);char *qemu_strdup (const char *str);char *qemu_strndup (const char *str,size_t size); +void *qemu_memalign (size_t alignment,size_t size); +void *qemu_vmalloc (size_t size); + +void qemu_free (void *ptr); +void qemu_vfree (void *ptr);void qemu_mutex_lock_iothread (void );void qemu_mutex_unlock_iothread (void );
qemu-malloc.c +33 -0
@@ -31,6 +31,15 @@ void qemu_free(void *ptr)free (ptr); } +static void *qemu_oom_check (void *ptr) +{ +if (ptr ==NULL ) { +fprintf (stderr,"Failed to allocate memory: %s\n" ,strerror (errno)); +abort (); + } +return ptr; +} +static int allow_zero_malloc (void ) {#if defined(CONFIG_ZERO_MALLOC )
@@ -96,3 +105,27 @@ char *qemu_strndup(const char *str, size_t size)return memcpy (new, str, size); } + +void *qemu_memalign (size_t alignment,size_t size) +{ +char *ptr; +assert (size <=LONG_MAX ); +if (!size && !allow_zero_malloc ()) { +abort (); + } + ptr =qemu_oom_check (os_memalign (alignment, size ? size :1 )); +trace_qemu_memalign (alignment, size, (char *)ptr); +return ptr; +} + +/* alloc shared memory pages */ +void *qemu_vmalloc (size_t size) +{ +return qemu_memalign (getpagesize (), size); +} + +void qemu_vfree (void *ptr) +{ +trace_qemu_vfree (ptr); +os_vfree (ptr); +}
trace-events +0 -1
@@ -35,7 +35,6 @@ 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_vmalloc(size_t size, void *ptr) "size %zu ptr %p" disable qemu_vfree(void *ptr) "ptr %p" # hw/virtio.c
Signature · 1 line
1.7.4.4
PATCH 2/2 malloc: add statistics infrastructure
In reply to Paolo BonziniThe infrastructure tracks the following items for each allocation locus:
number of currently allocated blocks, total number of allocated blocks, number of currently allocated bytes, maximum number of allocated bytes, total number of allocated bytes. Accounting is thread safe, which is why I didn't use qemu-queue functions.
Tracing is done by sticking an extra header in front of the allocated block. The header is needed so that free can account bytes correctly and to the correct allocation locus. The header is variable-sized because of memalign, with the information stored at the tail so it is easily accessible from free's parameter. Hence it also has to include a pointer to the actual base of the memory block.
--- configure | 9 ++ hmp-commands.hx | 16 ++++ monitor.c | 12 +++ qemu-common.h | 45 ++++++++++-- qemu-malloc.c | 223 +++++++++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 276 insertions(+), 29 deletions(-)
configure +9 -0
@@ -171,6 +171,7 @@ pkgversion="" check_utests="no" user_pie="no" zero_malloc="" +malloc_stats="no" trace_backend="nop" trace_file="trace" spice=""
@@ -665,6 +666,10 @@ for opt do ;; --disable-guest-base) guest_base="no" ;; + --enable-malloc-stats) malloc_stats="yes" + ;; + --disable-malloc-stats) malloc_stats="no" + ;; --enable-user-pie) user_pie="yes" ;; --disable-user-pie) user_pie="no"
@@ -2602,6 +2607,7 @@ echo "Documentation $docs" echo "uname -r $uname_release" echo "NPTL support $nptl" echo "GUEST_BASE $guest_base" +echo "Malloc statistics $malloc_stats" echo "PIE user targets $user_pie" echo "vde support $vde" echo "IO thread $io_thread"
@@ -2924,6 +2930,9 @@ fi echo "CONFIG_UNAME_RELEASE=\"$uname_release\"" >> $config_host_mak +if test "$malloc_stats" = "yes" ; then + echo "CONFIG_MALLOC_STATS=y" >> $config_host_mak +fi if test "$zero_malloc" = "yes" ; then echo "CONFIG_ZERO_MALLOC=y" >> $config_host_mak fi
hmp-commands.hx +16 -0
@@ -180,6 +180,22 @@ STEXI Output logs to @var{filename}. ETEXI +#ifdef CONFIG_MALLOC_STATS + { + .name = "dump-malloc-stats", + .args_type = "name:F", + .params = "name", + .help = "dump memory allocation statistics to a file", + .mhandler.cmd = do_dump_malloc_stats, + }, + +STEXI +@item dump-malloc-stats @var{filename} +@findex dump-malloc-stats +Dump memory allocation statistics to @var{filename}. +ETEXI +#endif + #ifdef CONFIG_SIMPLE_TRACE { .name = "trace-event",
monitor.c +12 -0
@@ -592,6 +592,18 @@ static void do_help_cmd(Monitor *mon, const QDict *qdict)help_cmd (mon,qdict_get_try_str (qdict,"name" )); } +#ifdef CONFIG_MALLOC_STATS +static void do_dump_malloc_stats (Monitor *mon,const QDict *qdict) +{ +const char *file_name =qdict_get_str (qdict,"name" ); +int ret =dump_malloc_stats (file_name); + +if (ret <0 ) { +monitor_printf (mon,"error writing file: %s\n" ,strerror (-ret)); + } +} +#endif +#ifdef CONFIG_SIMPLE_TRACE static void do_change_trace_event_state (Monitor *mon,const QDict *qdict) {
qemu-common.h +38 -7
55 lines not on this page · raw message
qemu-malloc.c +201 -22
293 lines not on this page · raw message
Signature · 1 line
1.7.4.4