Просмотр исходного кода

Add memory alloc/dealloc profiler events

Daniele Bartolini 11 лет назад
Родитель
Сommit
d2f9a73bf6
2 измененных файлов с 82 добавлено и 29 удалено
  1. 46 13
      engine/core/profiler.cpp
  2. 36 16
      engine/core/profiler.h

+ 46 - 13
engine/core/profiler.cpp

@@ -13,6 +13,7 @@ namespace crown
 {
 namespace profiler_globals
 {
+	typedef Array<char> Buffer;
 	char _mem[sizeof(Buffer)];
 	Buffer* _buffer = NULL;
 
@@ -27,9 +28,9 @@ namespace profiler_globals
 		_buffer = NULL;
 	}
 
-	const Buffer& buffer()
+	const char* buffer()
 	{
-		return *_buffer;
+		return array::begin(*_buffer);
 	}
 } // namespace profiler_globals
 
@@ -49,50 +50,80 @@ namespace profiler
 
 	void enter_profile_scope(const char* name)
 	{
-		if (_thread_buffer_size + sizeof(uint32_t) + sizeof(EnterProfileScope) >= THREAD_BUFFER_SIZE)
+		if (_thread_buffer_size + 2*sizeof(uint32_t) + sizeof(EnterProfileScope) >= THREAD_BUFFER_SIZE)
 			flush_local_buffer();
 
 		char* p = _thread_buffer + _thread_buffer_size;
 		*(uint32_t*) p = EventType::ENTER_PROFILE_SCOPE;
-		(*(EnterProfileScope*)(p + sizeof(uint32_t))).name = name;
-		(*(EnterProfileScope*)(p + sizeof(uint32_t))).time = os::clocktime();
+		(*(uint32_t*)(p + sizeof(uint32_t))) = sizeof(EnterProfileScope);
+		(*(EnterProfileScope*)(p + 2*sizeof(uint32_t))).name = name;
+		(*(EnterProfileScope*)(p + 2*sizeof(uint32_t))).time = os::clocktime();
 		_thread_buffer_size += sizeof(uint32_t) + sizeof(EnterProfileScope);
 	}
 
 	void leave_profile_scope()
 	{
-		if (_thread_buffer_size + sizeof(uint32_t) + sizeof(LeaveProfileScope) >= THREAD_BUFFER_SIZE)
+		if (_thread_buffer_size + 2*sizeof(uint32_t) + sizeof(LeaveProfileScope) >= THREAD_BUFFER_SIZE)
 			flush_local_buffer();
 
 		char* p = _thread_buffer + _thread_buffer_size;
 		*(uint32_t*) p = EventType::LEAVE_PROFILE_SCOPE;
-		(*(LeaveProfileScope*)(p + sizeof(uint32_t))).time = os::clocktime();
+		(*(uint32_t*)(p + sizeof(uint32_t))) = sizeof(LeaveProfileScope);
+		(*(LeaveProfileScope*)(p + 2*sizeof(uint32_t))).time = os::clocktime();
 		_thread_buffer_size += sizeof(uint32_t) + sizeof(LeaveProfileScope);
 	}
 
 	void record_float(const char* name, float value)
 	{
-		if (_thread_buffer_size + sizeof(uint32_t) + sizeof(RecordFloat) >= THREAD_BUFFER_SIZE)
+		if (_thread_buffer_size + 2*sizeof(uint32_t) + sizeof(RecordFloat) >= THREAD_BUFFER_SIZE)
 			flush_local_buffer();
 
 		char* p = _thread_buffer + _thread_buffer_size;
 		*(uint32_t*) p = EventType::RECORD_FLOAT;
-		(*(RecordFloat*)(p + sizeof(uint32_t))).name = name;
-		(*(RecordFloat*)(p + sizeof(uint32_t))).value = value;
+		(*(uint32_t*)(p + sizeof(uint32_t))) = sizeof(RecordFloat);
+		(*(RecordFloat*)(p + 2*sizeof(uint32_t))).name = name;
+		(*(RecordFloat*)(p + 2*sizeof(uint32_t))).value = value;
 		_thread_buffer_size += sizeof(uint32_t) + sizeof(RecordFloat);
 	}
 
 	void record_vector3(const char* name, const Vector3& value)
 	{
-		if (_thread_buffer_size + sizeof(uint32_t) + sizeof(RecordVector3) >= THREAD_BUFFER_SIZE)
+		if (_thread_buffer_size + 2*sizeof(uint32_t) + sizeof(RecordVector3) >= THREAD_BUFFER_SIZE)
 			flush_local_buffer();
 
 		char* p = _thread_buffer + _thread_buffer_size;
 		*(uint32_t*) p = EventType::RECORD_VECTOR3;
-		(*(RecordVector3*)(p + sizeof(uint32_t))).name = name;
-		(*(RecordVector3*)(p + sizeof(uint32_t))).value = value;
+		(*(uint32_t*)(p + sizeof(uint32_t))) = sizeof(RecordVector3);
+		(*(RecordVector3*)(p + 2*sizeof(uint32_t))).name = name;
+		(*(RecordVector3*)(p + 2*sizeof(uint32_t))).value = value;
 		_thread_buffer_size += sizeof(uint32_t) + sizeof(RecordVector3);
 	}
+
+	void allocate_memory(const char* name, uint32_t size)
+	{
+		if (_thread_buffer_size + 2*sizeof(uint32_t) + sizeof(AllocateMemory) >= THREAD_BUFFER_SIZE)
+			flush_local_buffer();
+
+		char* p = _thread_buffer + _thread_buffer_size;
+		*(uint32_t*) p = EventType::ALLOCATE_MEMORY;
+		(*(uint32_t*)(p + sizeof(uint32_t))) = sizeof(AllocateMemory);
+		(*(AllocateMemory*)(p + 2*sizeof(uint32_t))).name = name;
+		(*(AllocateMemory*)(p + 2*sizeof(uint32_t))).size = size;
+		_thread_buffer_size += sizeof(uint32_t) + sizeof(AllocateMemory);
+	}
+
+	void deallocate_memory(const char* name, uint32_t size)
+	{
+		if (_thread_buffer_size + 2*sizeof(uint32_t) + sizeof(DeallocateMemory) >= THREAD_BUFFER_SIZE)
+			flush_local_buffer();
+
+		char* p = _thread_buffer + _thread_buffer_size;
+		*(uint32_t*) p = EventType::DEALLOCATE_MEMORY;
+		(*(uint32_t*)(p + sizeof(uint32_t))) = sizeof(DeallocateMemory);
+		(*(DeallocateMemory*)(p + 2*sizeof(uint32_t))).name = name;
+		(*(DeallocateMemory*)(p + 2*sizeof(uint32_t))).size = size;
+		_thread_buffer_size += sizeof(uint32_t) + sizeof(DeallocateMemory);
+	}
 } // namespace profiler
 
 namespace profiler_globals
@@ -100,6 +131,8 @@ namespace profiler_globals
 	void flush()
 	{
 		profiler::flush_local_buffer();
+		uint32_t end = profiler::EventType::COUNT;
+		array::push(*_buffer, (const char*)&end, (uint32_t)sizeof(end));
 	}
 
 	void clear()

+ 36 - 16
engine/core/profiler.h

@@ -6,7 +6,6 @@
 #pragma once
 
 #include "math_types.h"
-#include "container_types.h"
 
 namespace crown
 {
@@ -19,7 +18,11 @@ namespace profiler
 			ENTER_PROFILE_SCOPE = 0,
 			LEAVE_PROFILE_SCOPE = 1,
 			RECORD_FLOAT = 2,
-			RECORD_VECTOR3 = 3
+			RECORD_VECTOR3 = 3,
+			ALLOCATE_MEMORY = 4,
+			DEALLOCATE_MEMORY = 5,
+
+			COUNT,
 		};
 	};
 
@@ -46,22 +49,24 @@ namespace profiler
 		int64_t time;
 	};
 
+	struct AllocateMemory
+	{
+		const char* name;
+		uint32_t size;
+	};
+
+	struct DeallocateMemory
+	{
+		const char* name;
+		uint32_t size;
+	};
+
 	void enter_profile_scope(const char* name);
 	void leave_profile_scope();
 	void record_float(const char* name, float value);
 	void record_vector3(const char* name, const Vector3& value);
-
-#ifdef CROWN_DEBUG
-	#define ENTER_PROFILE_SCOPE(name) enter_profile_scope(name);
-	#define LEAVE_PROFILE_SCOPE() leave_profile_scope(name);
-	#define RECORD_FLOAT(name, value) record_float(name, value);
-	#define RECORD_VECTOR3(name, value) record_vector3(name, value);
-#else
-	#define ENTER_PROFILE_SCOPE(name) ((void)0)
-	#define LEAVE_PROFILE_SCOPE() ((void)0)
-	#define RECORD_FLOAT(name, value) ((void)0)
-	#define RECORD_VECTOR3(name, value) ((void)0)
-#endif
+	void allocate_memory(const char* name, uint32_t size);
+	void deallocate_memory(const char* name, uint32_t size);
 } // namespace profiler
 
 namespace profiler_globals
@@ -69,9 +74,24 @@ namespace profiler_globals
 	void init();
 	void shutdown();
 
-	typedef Array<char> Buffer;
-	const Buffer& buffer();
+	const char* buffer();
 	void flush();
 	void clear();
 } // namespace profiler_globals
 } // namespace crown
+
+#ifdef CROWN_DEBUG
+	#define ENTER_PROFILE_SCOPE(name) profiler::enter_profile_scope(name)
+	#define LEAVE_PROFILE_SCOPE() profiler::leave_profile_scope(name)
+	#define RECORD_FLOAT(name, value) profiler::record_float(name, value)
+	#define RECORD_VECTOR3(name, value) profiler::record_vector3(name, value)
+	#define ALLOCATE_MEMORY(name, size) profiler::allocate_memory(name, size)
+	#define DEALLOCATE_MEMORY(name, size) profiler::deallocate_memory(name, size)
+#else
+	#define ENTER_PROFILE_SCOPE(name) ((void)0)
+	#define LEAVE_PROFILE_SCOPE() ((void)0)
+	#define RECORD_FLOAT(name, value) ((void)0)
+	#define RECORD_VECTOR3(name, value) ((void)0)
+	#define ALLOCATE_MEMORY(name, size) ((void)0)
+	#define DEALLOCATE_MEMORY(name, size) ((void)0)
+#endif