[Spice-devel,06/19] qxl_mem: add debug flags, simple accounting and valgrind enabled

Submitted by Alon Levy on May 31, 2012, 10:24 a.m.

Details

Message ID 1338459893-1980-7-git-send-email-alevy@redhat.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Alon Levy May 31, 2012, 10:24 a.m.
adds preprocessor definitions DEBUG_QXL_MEM & DEBUG_QXL_MEM_VERBOSE
---
 src/qxl.h        |    6 ++++++
 src/qxl_driver.c |    8 ++++++++
 src/qxl_mem.c    |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 67 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/qxl.h b/src/qxl.h
index ed12680..c6d3793 100644
--- a/src/qxl.h
+++ b/src/qxl.h
@@ -415,6 +415,12 @@  void *            qxl_allocnf          (qxl_screen_t           *qxl,
 					unsigned long           size);
 int		   qxl_garbage_collect (qxl_screen_t *qxl);
 
+#ifdef DEBUG_QXL_MEM
+void qxl_mem_unverifiable(struct qxl_mem *mem);
+#else
+inline void qxl_mem_unverifiable(struct qxl_mem *mem) {}
+#endif
+
 /*
  * I/O port commands
  */
diff --git a/src/qxl_driver.c b/src/qxl_driver.c
index a765c9f..4c08637 100644
--- a/src/qxl_driver.c
+++ b/src/qxl_driver.c
@@ -558,6 +558,13 @@  qxl_reset (qxl_screen_t *qxl)
 #endif
 }
 
+static void
+qxl_mark_mem_unverifiable(qxl_screen_t *qxl)
+{
+    qxl_mem_unverifiable(qxl->mem);
+    qxl_mem_unverifiable(qxl->surf_mem);
+}
+
 static Bool
 qxl_close_screen(int scrnIndex, ScreenPtr pScreen)
 {
@@ -582,6 +589,7 @@  qxl_close_screen(int scrnIndex, ScreenPtr pScreen)
     if (pScrn->vtSema)
     {
         qxl_restore_state(pScrn);
+        qxl_mark_mem_unverifiable(qxl);
 	qxl_unmap_memory(qxl, scrnIndex);
     }
     pScrn->vtSema = FALSE;
diff --git a/src/qxl_mem.c b/src/qxl_mem.c
index 6e57a94..dd77f25 100644
--- a/src/qxl_mem.c
+++ b/src/qxl_mem.c
@@ -27,13 +27,30 @@ 
 #include "qxl.h"
 #include "mspace.h"
 
+#ifdef DEBUG_QXL_MEM
+#include <valgrind/memcheck.h>
+#endif
+
 struct qxl_mem
 {
     mspace	space;
     void *	base;
     unsigned long n_bytes;
+#ifdef DEBUG_QXL_MEM
+    size_t used_initial;
+    int unverifiable;
+    int missing;
+#endif
 };
 
+#ifdef DEBUG_QXL_MEM
+void
+qxl_mem_unverifiable(struct qxl_mem *mem)
+{
+    mem->unverifiable = 1;
+}
+#endif
+
 struct qxl_mem *
 qxl_mem_create       (void                   *base,
 		      unsigned long           n_bytes)
@@ -51,6 +68,17 @@  qxl_mem_create       (void                   *base,
     mem->base = base;
     mem->n_bytes = n_bytes;
 
+#ifdef DEBUG_QXL_MEM
+    {
+        size_t used;
+
+        mspace_malloc_stats_return(mem->space, NULL, NULL, &used);
+        mem->used_initial = used;
+        mem->unverifiable = 0;
+        mem->missing = 0;
+    }
+#endif
+
 out:
     return mem;
 
@@ -69,7 +97,15 @@  void *
 qxl_alloc            (struct qxl_mem         *mem,
 		      unsigned long           n_bytes)
 {
-    return mspace_malloc (mem->space, n_bytes);
+    void *addr = mspace_malloc (mem->space, n_bytes);
+
+#ifdef DEBUG_QXL_MEM
+    VALGRIND_MALLOCLIKE_BLOCK(addr, n_bytes, 0, 0);
+#ifdef DEBUG_QXL_MEM_VERBOSE
+    fprintf(stderr, "alloc %p: %ld\n", addr, n_bytes);
+#endif
+#endif
+    return addr;
 }
 
 void
@@ -77,11 +113,27 @@  qxl_free             (struct qxl_mem         *mem,
 		      void                   *d)
 {
     mspace_free (mem->space, d);
+#ifdef DEBUG_QXL_MEM
+#ifdef DEBUG_QXL_MEM_VERBOSE
+    fprintf(stderr, "free  %p\n", d);
+#endif
+    VALGRIND_FREELIKE_BLOCK(d, 0);
+#endif
 }
 
 void
 qxl_mem_free_all     (struct qxl_mem         *mem)
 {
+    size_t maxfp, fp, used;
+
+#ifdef DEBUG_QXL_MEM
+    if (mem->space) {
+        mspace_malloc_stats_return(mem->space, &maxfp, &fp, &used);
+        mem->missing = used - mem->used_initial;
+        ErrorF ("untracked %zd bytes (%s)", used - mem->used_initial,
+            mem->unverifiable ? "marked unverifiable" : "oops");
+    }
+#endif
     mem->space = create_mspace_with_base (mem->base, mem->n_bytes, 0, NULL);
 }