[vmkit-commits] [vmkit] r198109 - allocate my thread through a custom allocator

Gael Thomas gael.thomas at lip6.fr
Sat Dec 28 02:23:48 PST 2013


Author: gthomas
Date: Sat Dec 28 04:23:47 2013
New Revision: 198109

URL: http://llvm.org/viewvc/llvm-project?rev=198109&view=rev
Log:
allocate my thread through a custom allocator

Modified:
    vmkit/branches/mcjit/include/j3/j3options.h
    vmkit/branches/mcjit/include/vmkit/allocator.h
    vmkit/branches/mcjit/include/vmkit/thread.h
    vmkit/branches/mcjit/include/vmkit/vmkit.h
    vmkit/branches/mcjit/lib/j3/vm/j3.cc
    vmkit/branches/mcjit/lib/j3/vm/j3options.cc
    vmkit/branches/mcjit/lib/vmkit/allocator.cc
    vmkit/branches/mcjit/lib/vmkit/thread.cc
    vmkit/branches/mcjit/lib/vmkit/vmkit.cc

Modified: vmkit/branches/mcjit/include/j3/j3options.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/j3/j3options.h?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/j3/j3options.h (original)
+++ vmkit/branches/mcjit/include/j3/j3options.h Sat Dec 28 04:23:47 2013
@@ -34,6 +34,8 @@ namespace j3 {
 		uint32_t       debugIniting;
 		uint32_t       debugTranslate;
 		uint32_t       debugLinking;
+		
+		uintptr_t      stackSize;
 
 		J3Options();
 

Modified: vmkit/branches/mcjit/include/vmkit/allocator.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/allocator.h?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/allocator.h (original)
+++ vmkit/branches/mcjit/include/vmkit/allocator.h Sat Dec 28 04:23:47 2013
@@ -13,6 +13,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <map>
+#include <vector>
 
 namespace vmkit {
 	class BumpAllocatorNode { /* always the first bytes of a bucket */
@@ -114,6 +115,31 @@ namespace vmkit {
 	struct T_ptr_less_t : public PermanentObject {
     bool operator()(const T& lhs, const T& rhs) const { return lhs < rhs; } 
 	};
+
+	class ThreadAllocator {
+		static const uint32_t refill = 128;
+
+		static ThreadAllocator* _allocator;
+
+		pthread_mutex_t       mutex;
+		std::vector<void*>    spaces;
+		std::vector<void*>    freeThreads;
+		uintptr_t             baseStack;
+		uintptr_t             topStack;
+
+		ThreadAllocator(uintptr_t minThreadStruct, uintptr_t minFullSize);
+	public:
+		static void initialize(uintptr_t minThreadStruct, uintptr_t minFullSize);
+		static ThreadAllocator* allocator() { return _allocator; }
+
+		void* allocate();
+		void  release(void* thread);
+
+		void*     stackAddr(void* thread);
+		size_t    stackSize(void* thread); 
+
+		uintptr_t magic() { return -topStack; }
+	};
 } // end namespace vmkit
 
 #endif // VMKIT_ALLOCATOR_H

Modified: vmkit/branches/mcjit/include/vmkit/thread.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/thread.h?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/thread.h (original)
+++ vmkit/branches/mcjit/include/vmkit/thread.h Sat Dec 28 04:23:47 2013
@@ -9,7 +9,7 @@
 namespace vmkit {
 	class VMKit;
 
-	class Thread : protected PermanentObject {
+	class Thread {
 		VMKit*               _vm;
 		pthread_t            _tid;
 

Modified: vmkit/branches/mcjit/include/vmkit/vmkit.h
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/include/vmkit/vmkit.h?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/include/vmkit/vmkit.h (original)
+++ vmkit/branches/mcjit/include/vmkit/vmkit.h Sat Dec 28 04:23:47 2013
@@ -17,30 +17,21 @@ namespace vmkit {
 	class Thread;
 	class Safepoint;
 
-	class ExceptionDescriptor { /* managed with malloc/free */
-		const llvm::Function* _llvmFunction;
-		uintptr_t             _point;
-		uintptr_t             _landingPad;
-	public:
-		ExceptionDescriptor(const llvm::Function* llvmFunction, uintptr_t point, uintptr_t landingPad);
-	};
-
 	class VMKit {
 		typedef std::map<const char*, llvm::GlobalValue*, Util::char_less_t, 
 										 StdAllocator<std::pair<const char*, llvm::GlobalValue*> > > MangleMap;
 
-		std::map<void*, Safepoint*>                safepointMap;    
-		pthread_mutex_t                            safepointMapLock;
-		std::map<uintptr_t, ExceptionDescriptor*>  exceptionTable;   /* managed with malloc/free */
-		MangleMap                       mangleMap;
-		BumpAllocator*                  _allocator;
-		llvm::Module*                   _self;
-		llvm::DataLayout*               _dataLayout;
-		void*                           ptrTypeInfo;
+		std::map<void*, Safepoint*>             safepointMap;    
+		pthread_mutex_t                         safepointMapLock;
+		MangleMap                               mangleMap;
+		BumpAllocator*                          _allocator;
+		llvm::Module*                           _self;
+		llvm::DataLayout*                       _dataLayout;
+		void*                                   ptrTypeInfo;
 
-		void                       addSymbol(llvm::GlobalValue* gv);
+		void                            addSymbol(llvm::GlobalValue* gv);
 
-		static void                defaultInternalError(const wchar_t* msg, va_list va) __attribute__((noreturn));
+		static void                     defaultInternalError(const wchar_t* msg, va_list va) __attribute__((noreturn));
 	protected:
 		void* operator new(size_t n, BumpAllocator* allocator);
 

Modified: vmkit/branches/mcjit/lib/j3/vm/j3.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3.cc?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3.cc Sat Dec 28 04:23:47 2013
@@ -64,17 +64,16 @@ void J3::introspect() {
 void J3::start(int argc, char** argv) {
 	_options.process(argc, argv);
 
+	vmkit::ThreadAllocator::initialize(sizeof(J3Thread), options()->stackSize);
+
 	vmkit::BumpAllocator* threadAllocator = vmkit::BumpAllocator::create();
 	J3Thread* thread = new(threadAllocator) J3ThreadBootstrap(this, threadAllocator);
 
-	vmkitBootstrap(thread, options()->selfBitCodePath);
-	
-	thread->Thread::start();
-	thread->join();
+
+	vmkitBootstrap(thread, options()->selfBitCodePath);	
 }
 
 void J3::run() {
-	fprintf(stderr, "      Bootstraping j3....\n");
 	introspect();
 
 	vmkit::BumpAllocator* loaderAllocator = vmkit::BumpAllocator::create();

Modified: vmkit/branches/mcjit/lib/j3/vm/j3options.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/j3/vm/j3options.cc?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/j3/vm/j3options.cc (original)
+++ vmkit/branches/mcjit/lib/j3/vm/j3options.cc Sat Dec 28 04:23:47 2013
@@ -23,6 +23,8 @@ J3Options::J3Options() {
 	debugLinking = 1;
 
 	genDebugExecute = debugExecute ? 1 : 0;
+
+	stackSize = 0x80*0x1000;
 }
 
 #define nyi(cmd) ({ fprintf(stderr, "option '%s' not yet implemented\n", cmd); })

Modified: vmkit/branches/mcjit/lib/vmkit/allocator.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/vmkit/allocator.cc?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/vmkit/allocator.cc (original)
+++ vmkit/branches/mcjit/lib/vmkit/allocator.cc Sat Dec 28 04:23:47 2013
@@ -8,6 +8,8 @@
 
 using namespace vmkit;
 
+ThreadAllocator* ThreadAllocator::_allocator = 0;
+
 void* BumpAllocator::operator new(size_t n) {
 	return (void*)((uintptr_t)map(bucketSize) + sizeof(BumpAllocatorNode));
 }
@@ -91,3 +93,68 @@ void PermanentObject::operator delete(vo
 void PermanentObject::operator delete[](void* ptr) {
 	Thread::get()->vm()->internalError(L"should not happen");
 }
+
+ThreadAllocator::ThreadAllocator(uintptr_t minThreadStruct, uintptr_t minFullSize) {
+	pthread_mutex_init(&mutex, 0);
+	spaces.reserve(1);
+	freeThreads.reserve(refill);
+
+	minThreadStruct = ((minThreadStruct - 1) & -PAGE_SIZE) + PAGE_SIZE;
+	baseStack = minThreadStruct;
+
+	uintptr_t min = PTHREAD_STACK_MIN + minThreadStruct + PAGE_SIZE;
+	if(minFullSize < min)
+		minFullSize = min;
+
+	topStack = 1L << (__builtin_clzl(0) - __builtin_clzl(minFullSize-1));
+}
+
+void ThreadAllocator::initialize(uintptr_t minThreadStruct, uintptr_t minFullSize) {
+	if(_allocator) {
+		fprintf(stderr, "Never try to modify the thread structure layout dynamically\n");
+		abort();
+	}
+	_allocator = new ThreadAllocator(minThreadStruct, minFullSize);
+}
+
+void* ThreadAllocator::allocate() {
+	pthread_mutex_lock(&mutex);
+	if(!freeThreads.size()) {
+		void* space = mmap(0, topStack*refill, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
+
+		if(space == MAP_FAILED) {
+			fprintf(stderr, "unable to allocate a thread\n");
+			abort();
+		}
+
+		spaces.push_back(space);
+
+		uintptr_t base = (((uintptr_t)space - 1) & -PAGE_SIZE) + PAGE_SIZE;
+		uint32_t n = (base == (uintptr_t)space) ? refill : (refill - 1);
+
+		for(uint32_t i=0; i<n; i++) {
+			mprotect((void*)(base + baseStack), PROT_NONE, PAGE_SIZE);
+			freeThreads.push_back((void*)base);
+			base += topStack;
+		}
+	}
+
+	void* res = freeThreads.back();
+	freeThreads.pop_back();
+	pthread_mutex_unlock(&mutex);
+	return res;
+}
+
+void ThreadAllocator::release(void* thread) {
+	pthread_mutex_lock(&mutex);
+	freeThreads.push_back(thread);
+	pthread_mutex_unlock(&mutex);
+}
+
+void* ThreadAllocator::stackAddr(void* thread) {
+	return (void*)((uintptr_t)thread + baseStack);
+}
+
+size_t ThreadAllocator::stackSize(void* thread) {
+	return topStack;
+}

Modified: vmkit/branches/mcjit/lib/vmkit/thread.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/vmkit/thread.cc?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/vmkit/thread.cc (original)
+++ vmkit/branches/mcjit/lib/vmkit/thread.cc Sat Dec 28 04:23:47 2013
@@ -11,7 +11,7 @@ Thread::Thread(VMKit* vm) {
 }
 
 void* Thread::operator new(size_t n, BumpAllocator* allocator) {
-	return allocator->allocate(n);
+	return ThreadAllocator::allocator()->allocate();
 }
 
 void Thread::operator delete(void* p) {

Modified: vmkit/branches/mcjit/lib/vmkit/vmkit.cc
URL: http://llvm.org/viewvc/llvm-project/vmkit/branches/mcjit/lib/vmkit/vmkit.cc?rev=198109&r1=198108&r2=198109&view=diff
==============================================================================
--- vmkit/branches/mcjit/lib/vmkit/vmkit.cc (original)
+++ vmkit/branches/mcjit/lib/vmkit/vmkit.cc Sat Dec 28 04:23:47 2013
@@ -124,6 +124,9 @@ void VMKit::vmkitBootstrap(Thread* initi
 
 	if(!ptrTypeInfo)
 		internalError(L"unable to find typeinfo for void*"); 
+
+	initialThread->start();
+	initialThread->join();
 }
 
 
@@ -172,10 +175,3 @@ void VMKit::throwException(void* obj) {
 	fprintf(stderr, " throw exception...\n");
 	abort();
 }
-
-
-ExceptionDescriptor::ExceptionDescriptor(const llvm::Function* llvmFunction, uintptr_t point, uintptr_t landingPad) {
-	_llvmFunction = llvmFunction;
-	_point = point;
-	_landingPad = landingPad;
-}





More information about the vmkit-commits mailing list