qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Julien Desfossez <ju@klipix.org>,
	Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>,
	Prerna Saxena <prerna@linux.vnet.ibm.com>
Subject: [Qemu-devel] [PATCH 02/14] trace: Trace qemu_malloc() and qemu_vmalloc()
Date: Thu, 12 Aug 2010 11:36:23 +0100	[thread overview]
Message-ID: <1281609395-17621-3-git-send-email-stefanha@linux.vnet.ibm.com> (raw)
In-Reply-To: <1281609395-17621-1-git-send-email-stefanha@linux.vnet.ibm.com>

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 <stefanha@linux.vnet.ibm.com>
---
 osdep.c       |   24 ++++++++++++++++++------
 qemu-malloc.c |   12 ++++++++++--
 trace-events  |   10 ++++++++++
 3 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/osdep.c b/osdep.c
index dbf872a..30426ff 100644
--- a/osdep.c
+++ b/osdep.c
@@ -50,6 +50,7 @@
 #endif
 
 #include "qemu-common.h"
+#include "trace.h"
 #include "sysemu.h"
 #include "qemu_socket.h"
 
@@ -71,25 +72,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);
 }
 
@@ -97,21 +107,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 */
@@ -122,6 +133,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 36b0b36..ecffb67 100644
--- a/qemu-malloc.c
+++ b/qemu-malloc.c
@@ -22,6 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
+#include "trace.h"
 #include <stdlib.h>
 
 static void *oom_check(void *ptr)
@@ -34,6 +35,7 @@ static void *oom_check(void *ptr)
 
 void qemu_free(void *ptr)
 {
+    trace_qemu_free(ptr);
     free(ptr);
 }
 
@@ -48,18 +50,24 @@ 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 && !allow_zero_malloc()) {
         abort();
     }
-    return oom_check(realloc(ptr, size ? size : 1));
+    newptr = oom_check(realloc(ptr, size ? size : 1));
+    trace_qemu_realloc(ptr, size, newptr);
+    return newptr;
 }
 
 void *qemu_mallocz(size_t size)
diff --git a/trace-events b/trace-events
index a37d3cc..98250e9 100644
--- a/trace-events
+++ b/trace-events
@@ -22,3 +22,13 @@
 # system may not have the necessary headers included.
 #
 # The <format-string> should be a sprintf()-compatible format string.
+
+# qemu-malloc.c
+qemu_malloc(size_t size, void *ptr) "size %zu ptr %p"
+qemu_realloc(void *ptr, size_t size, void *newptr) "ptr %p size %zu newptr %p"
+qemu_free(void *ptr) "ptr %p"
+
+# osdep.c
+qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu size %zu ptr %p"
+qemu_valloc(size_t size, void *ptr) "size %zu ptr %p"
+qemu_vfree(void *ptr) "ptr %p"
-- 
1.7.1

  parent reply	other threads:[~2010-08-12 13:53 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-12 10:36 [Qemu-devel] [PATCH 00/14] trace: Add static tracing to QEMU Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 01/14] trace: Add trace-events file for declaring trace events Stefan Hajnoczi
2010-08-12 16:07   ` malc
2010-08-12 19:15     ` Stefan Hajnoczi
2010-08-22 21:38   ` Anthony Liguori
2010-08-23 10:00     ` Stefan Hajnoczi
2010-08-12 10:36 ` Stefan Hajnoczi [this message]
2010-08-12 10:36 ` [Qemu-devel] [PATCH 03/14] trace: Trace virtio-blk, multiwrite, and paio_submit Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 04/14] trace: Trace virtqueue operations Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 05/14] trace: Trace port IO Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 06/14] trace: Trace entry point of balloon request handler Stefan Hajnoczi
2010-08-22 21:41   ` Anthony Liguori
2010-08-23 10:10     ` Stefan Hajnoczi
2010-08-23 13:15       ` Anthony Liguori
2010-08-12 10:36 ` [Qemu-devel] [PATCH 07/14] trace: Add simple built-in tracing backend Stefan Hajnoczi
2010-08-12 17:56   ` Blue Swirl
2010-08-12 19:31     ` Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 08/14] trace: Support for dynamically enabling/disabling trace events Stefan Hajnoczi
2010-08-12 18:02   ` Blue Swirl
2010-08-13 13:34     ` Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 09/14] trace: Support disabled events in trace-events Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 10/14] trace: Specify trace file name Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 11/14] trace: Add trace-file command to open/close/flush trace file Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 12/14] trace: Add trace file name command-line option Stefan Hajnoczi
2010-08-22 21:45   ` Anthony Liguori
2010-08-12 10:36 ` [Qemu-devel] [PATCH 13/14] trace: Add LTTng Userspace Tracer backend Stefan Hajnoczi
2010-08-12 10:36 ` [Qemu-devel] [PATCH 14/14] trace: Add user documentation Stefan Hajnoczi
2010-08-22 21:47   ` Anthony Liguori
2010-08-23 10:11     ` Stefan Hajnoczi
2010-08-12 18:10 ` [Qemu-devel] [PATCH 00/14] trace: Add static tracing to QEMU Blue Swirl
2010-08-13 13:45   ` Stefan Hajnoczi
2010-08-13 19:07     ` Lluís
2010-08-22 21:47 ` Anthony Liguori
2010-09-05  7:30 ` [Qemu-devel] " Michael S. Tsirkin
2010-09-06 13:32   ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1281609395-17621-3-git-send-email-stefanha@linux.vnet.ibm.com \
    --to=stefanha@linux.vnet.ibm.com \
    --cc=ju@klipix.org \
    --cc=prerna@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).