Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2003:
[Freeciv-Dev] (PR#6925) Measure memory management overhead
Home

[Freeciv-Dev] (PR#6925) Measure memory management overhead

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#6925) Measure memory management overhead
From: "Raimar Falke" <i-freeciv-lists@xxxxxxxxxxxxx>
Date: Sat, 22 Nov 2003 14:50:05 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=6925 >


The issue of memory handling overhead comes up quite often. The last
time in the safe-genlist thread. It is time to fill the discussion
with some facts. From the profile of an autogame on a Linux system it
we know that the overhead is quite small. The question is what
overhead other systems have. Since not all systems support profiling
the following patch was made.

The overhead depends on the malloc implmentation (is in the libc) and
to a lesser extends on the OS.  If you have a non-glibc system please
apply the patch and run the autogame. The most interresting systems
here are Win32 and commerical unix versions with their native libc
version.

The absolute numbers are not important. Also the autogame isn't
important (the attached just works).

You get an output like this:

[0 malloc    ] calls= 5784117 time=4.518493s 
[1 realloc   ] calls=  104447 time=0.251182s 
[2 strdup    ] calls=    4890 time=0.003150s 
[3 free      ] calls= 5708727 time=3.779969s 
[4 null      ] calls=11602181 time=4.518997s 
[5 total     ] calls=       1 time=762.919344s 
(8.552794-4.518997)/762.919344 = 0.528732%

Please mail this and your libc and OS version. I for example have used
glibc-2.2 and linux-2.4.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "Sit, disk, sit. Good boy. Now spin up. Very good. Here's a netscape
  cookie for you. Fetch me some data. Come on, you can do it. No, not that
  data. Bad disk. Bad." 
    -- Calle Dybedahl, alt.sysadmin.recovery

Index: common/game.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/game.c,v
retrieving revision 1.172
diff -u -u -r1.172 game.c
--- common/game.c       2003/11/19 15:02:29     1.172
+++ common/game.c       2003/11/22 22:48:11
@@ -331,6 +331,7 @@
   idex_free();
   ruleset_data_free();
   cm_free();
+  mem_stop();
 }
 
 /***************************************************************
Index: common/mem.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/mem.c,v
retrieving revision 1.8
diff -u -u -r1.8 mem.c
--- common/mem.c        2002/12/11 10:39:42     1.8
+++ common/mem.c        2003/11/22 22:48:11
@@ -23,13 +23,31 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
 
 #include "fcintl.h"
 #include "log.h"
 #include "shared.h"            /* TRUE, FALSE */
+#include "timing.h"
 
 #include "mem.h"
 
+#define FC_MALLOC 0
+#define FC_REALLOC 1
+#define FC_STRDUP 2
+#define FC_FREE 3
+#define FC_NULL 4
+#define TOTAL 5
+
+static int calls[6];
+static struct timer *timer[6];
+static char *names[]={"malloc","realloc","strdup","free","null","total"};
+static int disable;
+
+#define START(x) if(!disable){start_timer(timer[x]);calls[x]++;}
+#define STOP(x)  if(!disable){stop_timer(timer[x]);}
+#define M_NULL   START(FC_NULL) STOP(FC_NULL)
+
 /**********************************************************************
  Do whatever we should do when malloc fails.
  At the moment this just prints a log message and calls exit(EXIT_FAILURE)
@@ -55,7 +73,10 @@
            called_as, (unsigned long)size, line, file);
     return NULL;
   }
+  START(FC_MALLOC);
   ptr = malloc(size);
+  STOP(FC_MALLOC);
+  M_NULL;
   if(!ptr) {
     handle_alloc_failure(size, called_as, line, file);
   }
@@ -80,7 +101,10 @@
     free(ptr);
     return NULL;
   }
+  START(FC_REALLOC);
   new_ptr = realloc(ptr, size);
+  STOP(FC_REALLOC);
+  M_NULL;
   if(!new_ptr) {
     handle_alloc_failure(size, called_as, line, file);
   }
@@ -117,6 +141,54 @@
 {
   char *dest = (char *)fc_real_malloc(strlen(str)+1, called_as, line, file);
   /* no need to check whether dest is non-NULL! */
+  START(FC_STRDUP);
   strcpy(dest, str);
+  STOP(FC_STRDUP);
+  M_NULL;
   return dest;
+}
+
+void mem_start(void)
+{
+  int i;
+
+  disable=1;
+
+  for (i = 0; i < 6; i++) {
+    timer[i] = new_timer(TIMER_USER, TIMER_ACTIVE);
+    calls[i] = 0;
+  }
+  disable=0;
+  START(TOTAL);
+}
+
+void mem_stop(void)
+{
+  int i;
+  double t=0.0,t2,frac,n;
+
+  STOP(TOTAL);
+
+  for (i = 0; i < 6; i++) {
+    printf("\n[%d %-10s] calls=%8d time=%fs ", i, names[i],
+          calls[i],read_timer_seconds(timer[i])
+          );
+  }
+  printf("\n");
+  for (i = 0; i < 4; i++) {
+      t+=read_timer_seconds(timer[i]);
+  }
+  t2=read_timer_seconds(timer[TOTAL]);
+  n=read_timer_seconds(timer[FC_NULL]);
+  frac=((t-n)/t2)*100.0;
+  printf("(%f-%f)/%f = %f%%\n",t,n,t2,frac);
+}
+
+void real_free(void *p)
+{
+  START(FC_FREE);
+#undef free
+  free(p);
+  STOP(FC_FREE);
+  M_NULL;
 }
Index: common/mem.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/mem.h,v
retrieving revision 1.2
diff -u -u -r1.2 mem.h
--- common/mem.h        1999/05/01 03:57:23     1.2
+++ common/mem.h        2003/11/22 22:48:11
@@ -32,6 +32,7 @@
 
 #define mystrdup(str)      real_mystrdup((str), "strdup", \
                                         __LINE__, __FILE__)
+#define free(p)                   real_free(p)
      
 /***********************************************************************/
 
@@ -47,5 +48,9 @@
 
 char *real_mystrdup(const char *str, 
                    const char *called_as, int line, const char *file);
+void real_free(void *p);
+
+void mem_start(void);
+void mem_stop(void);
 
 #endif /* FC__MEM_H */
Index: server/civserver.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/civserver.c,v
retrieving revision 1.216
diff -u -u -r1.216 civserver.c
--- server/civserver.c  2003/11/19 11:16:52     1.216
+++ server/civserver.c  2003/11/22 22:48:18
@@ -26,6 +26,7 @@
 
 #include "fcintl.h"
 #include "log.h"
+#include "mem.h"
 #include "shared.h"
 #include "support.h"
 #include "version.h"
@@ -53,6 +54,7 @@
   bool showvers = FALSE;
   char *option = NULL;
 
+  mem_start();
   /* initialize server */
   srv_init();
 
set aifill 30
hard
set timeout -1
create b
set seed 1500703052
set randseed 1065770216
start

[Prev in Thread] Current Thread [Next in Thread]