From e2e5af1c6135f846a0995e45cff0a73bc5a2063d Mon Sep 17 00:00:00 2001 From: traycold Date: Thu, 22 Mar 2012 02:08:31 +0100 Subject: [PATCH] added some debug code to better understand memory usage by muPdf --- Makefile | 6 +-- pdf.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 137 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index ce64f0778..8d9fb99b0 100644 --- a/Makefile +++ b/Makefile @@ -31,14 +31,14 @@ ARM_CFLAGS:=-march=armv6 # in that case. ifdef EMULATE_READER - CC:=$(HOSTCC) + CC:=$(HOSTCC) -g CXX:=$(HOSTCXX) EMULATE_READER_W?=824 EMULATE_READER_H?=1200 EMU_CFLAGS?=$(shell sdl-config --cflags) EMU_CFLAGS+= -DEMULATE_READER \ -DEMULATE_READER_W=$(EMULATE_READER_W) \ - -DEMULATE_READER_H=$(EMULATE_READER_H) + -DEMULATE_READER_H=$(EMULATE_READER_H) EMU_LDFLAGS?=$(shell sdl-config --libs) else CFLAGS+= $(ARM_CFLAGS) @@ -131,7 +131,7 @@ $(MUPDFLIBS) $(THIRDPARTYLIBS): $(MUPDFDIR)/cmapdump.host $(MUPDFDIR)/fontdump.h CFLAGS="$(CFLAGS)" make -C mupdf CC="$(CC)" CMAPDUMP=cmapdump.host FONTDUMP=fontdump.host MUPDF= XPS_APPS= $(DJVULIBS): - -mkdir $(DJVUDIR)/build + -mkdir $(DJVUDIR)/build ifdef EMULATE_READER cd $(DJVUDIR)/build && ../configure --disable-desktopfiles --disable-shared --enable-static else diff --git a/pdf.c b/pdf.c index b335d65c7..5583e09a0 100644 --- a/pdf.c +++ b/pdf.c @@ -20,6 +20,10 @@ #include "blitbuffer.h" #include "drawcontext.h" #include "pdf.h" +#include +#include +#include + typedef struct PdfDocument { fz_document *xref; @@ -35,16 +39,141 @@ typedef struct PdfPage { PdfDocument *doc; } PdfPage; + +static double LOG_TRESHOLD_PERC = 0.05; // 5% + +enum { + MAGIC = 0x3795d42b, +}; + +typedef struct header { + int magic; + size_t sz; +} header; + +static size_t msize=0; +static size_t msize_prev; +static size_t msize_max; +static size_t msize_min; +static size_t msize_iniz; +static int is_realloc=0; + +char* readable_fs(double size/*in bytes*/, char *buf) { + int i = 0; + const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; + while (size > 1024) { + size /= 1024; + i++; + } + sprintf(buf, "%.*f %s", i, size, units[i]); + return buf; +} + +static void resetMsize(){ + msize_iniz = msize; + msize_prev = 0; + msize_max = 0; + msize_min = (size_t)-1; +} + +static void showMsize(){ + char buf[15],buf2[15],buf3[15],buf4[15]; + printf("§§§ now: %s was: %s - min: %s - max: %s\n",readable_fs(msize,buf),readable_fs(msize_iniz,buf2),readable_fs(msize_min,buf3),readable_fs(msize_max,buf4)); + resetMsize(); +} + +static void log_size(char *funcName){ + if(msize_max < msize) + msize_max = msize; + if(msize_min > msize) + msize_min = msize; + if(1==0 && abs(msize-msize_prev)>msize_prev*LOG_TRESHOLD_PERC){ + char buf[15],buf2[15]; + printf("§§§ %s - total: %s (was %s)\n",funcName, readable_fs(msize,buf),readable_fs(msize_prev,buf2)); + msize_prev = msize; + } +} + +static void * +my_malloc_default(void *opaque, unsigned int size) +{ + struct header * h = malloc(size + sizeof(header)); + if (h == NULL) + return NULL; + + h -> magic = MAGIC; + h -> sz = size; + msize += size + sizeof(struct header); + if(is_realloc!=1) + log_size("alloc"); + return (void *)(h + 1); +} + +static void +my_free_default(void *opaque, void *ptr) +{ + if (ptr != NULL) { + struct header * h = ((struct header *)ptr) - 1; + if (h -> magic != MAGIC) { /* Not allocated by us */ + } else { + msize -= h -> sz + sizeof(struct header); + free(h); + } + } + if(is_realloc!=1) + log_size("free"); +} + +static void * +my_realloc_default(void *opaque, void *old, unsigned int size) +{ + void * newp; + if (old==NULL) { //practically, it's a malloc + newp = my_malloc_default(opaque, size); + } else { + struct header * h = ((struct header *)old) - 1; + if (h -> magic != MAGIC) { // Not allocated by my_malloc_default + printf("§§§ warn: not allocated by my_malloc_default, new size: %i\n",size); + newp = realloc(old,size); + } else { // malloc + free + is_realloc = 1; + size_t oldsize = h -> sz; + //printf("realloc %i -> %i\n",oldsize,size); + newp = my_malloc_default(opaque, size); + if (NULL != newp) { + memcpy(newp, old, oldsizecontext = fz_new_context(NULL, NULL, cachesize); + doc->context = fz_new_context(&my_alloc_default, NULL, cachesize); fz_try(doc->context) { doc->xref = fz_open_document(doc->context, filename); @@ -116,7 +245,7 @@ static int walkTableOfContent(lua_State *L, fz_outline* ol, int *count, int dept lua_settable(L, -3); lua_pushstring(L, "depth"); - lua_pushnumber(L, depth); + lua_pushnumber(L, depth); lua_settable(L, -3); lua_pushstring(L, "title"); @@ -178,6 +307,7 @@ static int openPage(lua_State *L) { fz_catch(doc->context) { return luaL_error(L, "cannot open page #%d", pageno); } + showMsize(); return 1; } @@ -192,8 +322,8 @@ static int getPageSize(lua_State *L) { ctm = fz_scale(dc->zoom, dc->zoom) ; ctm = fz_concat(ctm, fz_rotate(dc->rotate)); bbox = fz_transform_rect(ctm, bounds); - - lua_pushnumber(L, bbox.x1-bbox.x0); + + lua_pushnumber(L, bbox.x1-bbox.x0); lua_pushnumber(L, bbox.y1-bbox.y0); return 2;