[Cbe-oss-dev] [PATCH 03/13]MARS/core: Numa memory allocation

Yuji Mano yuji.mano at am.sony.com
Fri Dec 12 15:33:15 EST 2008


From: Kazunori Asayama <asayama at sm.sony.co.jp>

Make memory allocation NUMA-aware

This patch makes memory allocation functions NUMA-aware, so those
functions follow the caller thread's NUMA policy.

Signed-off-by: Kazunori Asayama <asayama at sm.sony.co.jp>
---
 core/src/host/lib/ea_cell.c |   75 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 71 insertions(+), 4 deletions(-)

Index: mars-src/core/src/host/lib/ea_cell.c
===================================================================
--- mars-src.orig/core/src/host/lib/ea_cell.c
+++ mars-src/core/src/host/lib/ea_cell.c
@@ -44,16 +44,83 @@
 
 #include "mars/core.h"
 
-uint64_t mars_ea_memalign(size_t boundary, size_t size)
+#include "numa_internal.h"
+
+#ifdef MARS_ENABLE_NUMA
+
+union mars_numa_ea_block {
+	struct {
+		void *top;
+		size_t size;
+	} info;
+	unsigned char pad[16];
+};
+
+
+static uint64_t numa_ea_memalign(size_t boundary, size_t size)
 {
-	void *ptr = memalign(boundary, size);
+	uint64_t ea;
+	void *top;
+	union mars_numa_ea_block *block;
+	size_t alloc_size;
 
-	return mars_ptr_to_ea(ptr);
+	if (boundary < sizeof(*block))
+		boundary = sizeof(*block);
+
+	alloc_size = size + sizeof(*block) + boundary - 1;
+	top = numa_alloc(alloc_size);
+	if (!top) return 0;
+
+	ea = ((uintptr_t)top + sizeof(*block) + boundary - 1) &
+		~(uintptr_t)(boundary - 1);
+	block = mars_ea_to_ptr(ea) - sizeof(*block);
+
+	block->info.top = top;
+	block->info.size = alloc_size;
+
+	return ea;
+}
+
+static void numa_ea_free(uint64_t ea)
+{
+	void *ptr = mars_ea_to_ptr(ea);
+	union mars_numa_ea_block *block = ptr - sizeof(*block);
+
+	numa_free(block->info.top, block->info.size);
+}
+
+#else /* ! MARS_ENABLE_NUMA */
+
+/* These functions must not be called */
+static uint64_t numa_ea_memalign(size_t boundary, size_t size)
+{
+	(void)boundary;
+	(void)size;
+
+	return 0;
+}
+
+static void numa_ea_free(uint64_t ea)
+{
+	(void)ea;
+}
+
+#endif /* ! MARS_ENABLE_NUMA */
+
+uint64_t mars_ea_memalign(size_t boundary, size_t size)
+{
+	if (mars_numa_enabled())
+		return numa_ea_memalign(boundary, size);
+	else
+		return mars_ptr_to_ea(memalign(boundary, size));
 }
 
 void mars_ea_free(uint64_t ea)
 {
-	free(mars_ea_to_ptr(ea));
+	if (mars_numa_enabled())
+		numa_ea_free(ea);
+	else
+		free(mars_ea_to_ptr(ea));
 }
 
 /* copy data from host to EA */






More information about the cbe-oss-dev mailing list