Browse Source

Split profiler in .h and .cpp

Daniele Bartolini 11 years ago
parent
commit
50be4f424f
3 changed files with 167 additions and 94 deletions
  1. 131 0
      engine/core/profiler.cpp
  2. 32 94
      engine/core/profiler.h
  3. 4 0
      engine/crown.cpp

+ 131 - 0
engine/core/profiler.cpp

@@ -0,0 +1,131 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "profiler.h"
+#include "os.h"
+#include "array.h"
+#include "mutex.h"
+#include "memory.h"
+
+namespace crown
+{
+namespace profiler_globals
+{
+	char _mem[sizeof(Buffer)];
+	Buffer* _buffer = NULL;
+
+	void init()
+	{
+		_buffer = new (_mem)Buffer(default_allocator());
+	}
+
+	void shutdown()
+	{
+		_buffer->~Buffer();
+		_buffer = NULL;
+	}
+
+	const Buffer& buffer()
+	{
+		return *_buffer;
+	}
+} // namespace profiler_globals
+
+namespace profiler
+{
+	enum { THREAD_BUFFER_SIZE = 4 * 1024 };
+	char _thread_buffer[THREAD_BUFFER_SIZE];
+	uint32_t _thread_buffer_size = 0;
+	Mutex _buffer_mutex;
+
+	void flush_local_buffer()
+	{
+		ScopedMutex sm(_buffer_mutex);
+		array::push(*profiler_globals::_buffer, _thread_buffer, _thread_buffer_size);
+		_thread_buffer_size = 0;
+	}
+
+	void enter_profile_scope(const char* name)
+	{
+		if (_thread_buffer_size + 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();
+		_thread_buffer_size += sizeof(uint32_t) + sizeof(EnterProfileScope);
+	}
+
+	void leave_profile_scope()
+	{
+		if (_thread_buffer_size + 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();
+		_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)
+			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;
+		_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)
+			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;
+		_thread_buffer_size += sizeof(uint32_t) + sizeof(RecordVector3);
+	}
+} // namespace profiler
+
+namespace profiler_globals
+{
+	void flush()
+	{
+		profiler::flush_local_buffer();	
+	}
+
+	void clear()
+	{
+		array::clear(*_buffer);
+	}
+}
+} // namespace crown

+ 32 - 94
engine/core/profiler.h

@@ -26,18 +26,13 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "os.h"
-#include "memory.h"
-#include "mutex.h"
-#include "scoped_mutex.h"
-#include "macros.h"
+#include "math_types.h"
+#include "container_types.h"
 
 namespace crown
 {
 namespace profiler
 {
-	const char* const GLOBAL_FLOAT = "global.float";
-
 	struct EventType
 	{
 		enum Enum
@@ -64,97 +59,40 @@ namespace profiler
 	struct EnterProfileScope
 	{
 		const char* name;
-		double time;
+		int64_t time;
 	};
 
 	struct LeaveProfileScope
 	{
-		double time;
+		int64_t time;
 	};
 
-	static Array<char> g_buffer(default_allocator());
-	static Mutex g_buffer_mutex;
-
-	#define THREAD_BUFFER_SIZE 1024
-	CE_THREAD char t_buffer[THREAD_BUFFER_SIZE];
-	CE_THREAD uint32_t t_buffer_size = 0;
-
-	inline void flush_local_buffer()
-	{
-		ScopedMutex sm(g_buffer_mutex);
-		//cg_buffer.push(t_buffer, t_buffer_size);
-
-		uint32_t size = t_buffer_size;
-		t_buffer_size = 0;
-
-		uint32_t cur = 0;
-
-		// TODO TODO TODO TODO TODO TODO
-
-		// while (cur < size)
-		// {
-		// 	char* p = t_buffer + cur;
-		// 	uint32_t event_type = *(uint32_t*) p;
-		// 	RecordFloat event = *(RecordFloat*)(p + sizeof(uint32_t));
-		// 	CE_LOGD("ev type = %d, name = %s, value = %f\n", event_type, event.name, 1.0 / event.value);
-		// 	cur += sizeof(uint32_t) + sizeof(RecordFloat);
-		// }
-	}
-
-	inline void enter_profile_scope(const char* name)
-	{
-		if (t_buffer_size + sizeof(uint32_t) + sizeof(EnterProfileScope) >= THREAD_BUFFER_SIZE)
-		{
-			flush_local_buffer();
-		}
-
-		char* p = t_buffer + t_buffer_size;
-		*(uint32_t*) p = EventType::ENTER_PROFILE_SCOPE;
-		(*(EnterProfileScope*)(p + sizeof(uint32_t))).name = name;
-		(*(EnterProfileScope*)(p + sizeof(uint32_t))).time = os::milliseconds();
-		t_buffer_size += sizeof(uint32_t) + sizeof(EnterProfileScope);
-	}
-
-	inline void leave_profile_scope()
-	{
-		if (t_buffer_size + sizeof(uint32_t) + sizeof(LeaveProfileScope) >= THREAD_BUFFER_SIZE)
-		{
-			flush_local_buffer();
-		}
-
-		char* p = t_buffer + t_buffer_size;
-		*(uint32_t*) p = EventType::LEAVE_PROFILE_SCOPE;
-		(*(LeaveProfileScope*)(p + sizeof(uint32_t))).time = os::milliseconds();
-		t_buffer_size += sizeof(uint32_t) + sizeof(LeaveProfileScope);
-	}
-
-	inline void record_float(const char* name, float value)
-	{
-		if (t_buffer_size + sizeof(uint32_t) + sizeof(RecordFloat) >= THREAD_BUFFER_SIZE)
-		{
-			flush_local_buffer();
-		}
-
-		char* p = t_buffer + t_buffer_size;
-		*(uint32_t*) p = EventType::RECORD_FLOAT;
-		(*(RecordFloat*)(p + sizeof(uint32_t))).name = name;
-		(*(RecordFloat*)(p + sizeof(uint32_t))).value = value;
-		t_buffer_size += sizeof(uint32_t) + sizeof(RecordFloat);
-	}
-
-	inline void record_vector3(const char* name, const Vector3& value)
-	{
-		if (t_buffer_size + sizeof(uint32_t) + sizeof(RecordVector3) >= THREAD_BUFFER_SIZE)
-		{
-			flush_local_buffer();
-		}
-
-		char* p = t_buffer + t_buffer_size;
-		*(uint32_t*) p = EventType::RECORD_VECTOR3;
-		(*(RecordVector3*)(p + sizeof(uint32_t))).name = name;
-		(*(RecordVector3*)(p + sizeof(uint32_t))).value = value;
-		t_buffer_size += sizeof(uint32_t) + sizeof(RecordVector3);
-	}
-}
-
+	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
+} // namespace profiler
+
+namespace profiler_globals
+{
+	void init();
+	void shutdown();
+
+	typedef Array<char> Buffer;
+	const Buffer& buffer();
+	void flush();
+	void clear();
+} // namespace profiler_globals
 } // namespace crown

+ 4 - 0
engine/crown.cpp

@@ -42,6 +42,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "config.h"
 #include "math_utils.h"
 #include "lua_system.h"
+#include "profiler.h"
 #include <bgfx.h>
 
 namespace crown
@@ -194,6 +195,7 @@ ConfigSettings parse_config_file(Filesystem& fs)
 
 bool init(Filesystem& fs, const ConfigSettings& cs)
 {
+	profiler_globals::init();
 	input_globals::init();
 	audio_globals::init();
 	physics_globals::init();
@@ -217,6 +219,7 @@ void update()
 		input_globals::keyboard().update();
 		input_globals::mouse().update();
 		input_globals::touch().update();
+		profiler_globals::flush();
 	}
 }
 
@@ -229,5 +232,6 @@ void shutdown()
 	physics_globals::shutdown();
 	audio_globals::shutdown();
 	input_globals::shutdown();
+	profiler_globals::shutdown();
 }
 } // namespace crown