List:Internals« Previous MessageNext Message »
From:tomas Date:November 21 2005 4:45pm
Subject:bk commit into 5.1 tree (tomas:1.1957)
View as plain text  
Below is the list of changes that have just been committed into a local
5.1 repository of tomas. When tomas does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet
  1.1957 05/11/21 17:44:52 tomas@stripped +3 -0
  added experimental option to ndbd to use sbrk and trace memory allocation

  storage/ndb/src/kernel/vm/ndbd_malloc.hpp
    1.3 05/11/21 17:44:46 tomas@stripped +3 -0
    added experimental option to ndbd to use sbrk and trace memory allocation

  storage/ndb/src/kernel/vm/ndbd_malloc.cpp
    1.3 05/11/21 17:44:46 tomas@stripped +52 -19
    added experimental option to ndbd to use sbrk and trace memory allocation

  storage/ndb/src/kernel/vm/Configuration.cpp
    1.44 05/11/21 17:44:46 tomas@stripped +20 -1
    added experimental option to ndbd to use sbrk and trace memory allocation

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	tomas
# Host:	poseidon.ndb.mysql.com
# Root:	/home/tomas/mysql-5.1-wl2325-5.0

--- 1.43/storage/ndb/src/kernel/vm/Configuration.cpp	2005-10-07 10:58:06 +02:00
+++ 1.44/storage/ndb/src/kernel/vm/Configuration.cpp	2005-11-21 17:44:46 +01:00
@@ -38,6 +38,7 @@
 #include "pc.hpp"
 #include <LogLevel.hpp>
 #include <NdbSleep.h>
+#include <ndbd_malloc.hpp>
 
 extern "C" {
   void ndbSetOwnVersion();
@@ -48,11 +49,14 @@
 
 enum ndbd_options {
   OPT_INITIAL = NDB_STD_OPTIONS_LAST,
-  OPT_NODAEMON
+  OPT_NODAEMON,
+  OPT_NDBD_MALLOC_TRACE,
+  OPT_NDBD_MALLOC_USE_SBRK
 };
 
 NDB_STD_OPTS_VARS;
 static int _daemon, _no_daemon, _initial, _no_start;
+static int _ndbd_malloc_trace, _ndbd_malloc_use_sbrk;
 /**
  * Arguments to NDB process
  */ 
@@ -75,6 +79,16 @@
     "Do not start ndbd as daemon, provided for testing purposes",
     (gptr*) &_no_daemon, (gptr*) &_no_daemon, 0,
     GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
+  { "malloc-trace", OPT_NDBD_MALLOC_TRACE,
+    "Experimental (may not be supported in the future): "
+    "print trace of memory allocation as they occur to stderr",
+    (gptr*) &_ndbd_malloc_trace, (gptr*) &_ndbd_malloc_trace, 0,
+    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
+  { "malloc-use-sbrk", OPT_NDBD_MALLOC_USE_SBRK,
+    "Experimental (may not be supported in the future): "
+    "use sbrk as primary memory allocation method (mmap used if that fails)",
+    (gptr*) &_ndbd_malloc_use_sbrk, (gptr*) &_ndbd_malloc_use_sbrk, 0,
+    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
   { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 };
 static void short_usage_sub(void)
@@ -139,6 +153,11 @@
     _programName = strdup("");
   
   globalData.ownId= 0;
+
+  if (_ndbd_malloc_trace)
+    ndbd_malloc_trace();
+  if (_ndbd_malloc_use_sbrk)
+    ndbd_malloc_use_sbrk();
 
   return true;
 }

--- 1.2/storage/ndb/src/kernel/vm/ndbd_malloc.cpp	2005-10-07 01:28:17 +02:00
+++ 1.3/storage/ndb/src/kernel/vm/ndbd_malloc.cpp	2005-11-21 17:44:46 +01:00
@@ -15,49 +15,82 @@
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
 #include <ndb_global.h>
+#include <stdio.h>
 #include "ndbd_malloc.hpp"
 #include <NdbMem.h>
 
-//#define TRACE_MALLOC
-#ifdef TRACE_MALLOC
-#include <stdio.h>
-#endif
-
-static void xxx(size_t size, size_t *s_m, size_t *s_k, size_t *s_b)
+static void get_mem_size_units(size_t size, size_t *s_m, size_t *s_k, size_t *s_b)
 {
   *s_m = size/1024/1024;
   *s_k = (size - *s_m*1024*1024)/1024;
   *s_b = size - *s_m*1024*1024-*s_k*1024;
 }
 
-static Uint64 g_allocated_memory;
+static Uint64 s_allocated_memory = 0;
+static int s_ndbd_malloc_use_sbrk = 0;
+static int s_ndbd_malloc_trace = 0;
+
+void ndbd_malloc_trace()
+{
+  s_ndbd_malloc_trace = 1;
+}
+
+void ndbd_malloc_use_sbrk()
+{
+  // Important: this must never be unset once set!
+  // setting this will make free impossible
+  s_ndbd_malloc_use_sbrk = 1;
+}
+
+static const char malloc_str[] = "malloc";
+static const char sbrk_str[] = "sbrk";
+static const char mmap_str[] = "mmap";
+
 void *ndbd_malloc(size_t size)
 {
-  void *p = NdbMem_Allocate(size);
+  void *p;
+  const char *op_str = malloc_str;
+  if (!s_ndbd_malloc_use_sbrk)
+    p = NdbMem_Allocate(size);
+  else
+  {
+    p = sbrk(size);
+    op_str = sbrk_str;
+    if (p == (void *)-1)
+    {
+      op_str = mmap_str;
+      p = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
+      if (p == (void *)-1)
+	p = 0;
+    }
+  }
+
   if (p)
   {
-    g_allocated_memory += size;
-#ifdef TRACE_MALLOC
+    s_allocated_memory += size;
+    if (s_ndbd_malloc_trace)
     {
       size_t s_m, s_k, s_b;
-      xxx(size, &s_m, &s_k, &s_b);
-      fprintf(stderr, "%p malloc(%um %uk %ub)", p, s_m, s_k, s_b);
-      xxx(g_allocated_memory, &s_m, &s_k, &s_b);
+      get_mem_size_units(size, &s_m, &s_k, &s_b);
+      fprintf(stderr, "%s: %p ndbd_malloc(%um %uk %ub)", op_str, p, s_m, s_k, s_b);
+      get_mem_size_units(s_allocated_memory, &s_m, &s_k, &s_b);
       fprintf(stderr, "\t\ttotal(%um %uk %ub)\n", s_m, s_k, s_b);
     }
-#endif
   }
   return p;
 }
 
 void ndbd_free(void *p, size_t size)
 {
-  NdbMem_Free(p);
+  if (!s_ndbd_malloc_use_sbrk)
+    NdbMem_Free(p); // not possible to free sbrk'd memory
+  else if (p && s_ndbd_malloc_trace)
+    fprintf(stderr, "(NOOP) ");
+
   if (p)
   {
-    g_allocated_memory -= size;
-#ifdef TRACE_MALLOC
-    fprintf(stderr, "%p free(%d)\n", p, size);
-#endif
+    s_allocated_memory -= size;
+    if (s_ndbd_malloc_trace)
+      fprintf(stderr, "%p ndbd_free(%d)\n", p, size);
   }
 }

--- 1.2/storage/ndb/src/kernel/vm/ndbd_malloc.hpp	2005-10-07 01:28:19 +02:00
+++ 1.3/storage/ndb/src/kernel/vm/ndbd_malloc.hpp	2005-11-21 17:44:46 +01:00
@@ -19,7 +19,10 @@
 
 /**
  * common memory allocation function for ndbd kernel
+ *
  */
+void ndbd_malloc_trace();
+void ndbd_malloc_use_sbrk();
 void *ndbd_malloc(size_t size);
 void ndbd_free(void *p, size_t size);
 
Thread
bk commit into 5.1 tree (tomas:1.1957)tomas21 Nov