Use thread local for thread id mapping in profiler
This commit is contained in:
parent
b7342a9774
commit
5aafb42b28
@ -28,6 +28,7 @@
|
|||||||
#include "io/file_manager.hpp"
|
#include "io/file_manager.hpp"
|
||||||
#include "utils/file_utils.hpp"
|
#include "utils/file_utils.hpp"
|
||||||
#include "utils/string_utils.hpp"
|
#include "utils/string_utils.hpp"
|
||||||
|
#include "utils/tls.hpp"
|
||||||
#include "utils/vs.hpp"
|
#include "utils/vs.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -97,46 +98,36 @@ Profiler::~Profiler()
|
|||||||
{
|
{
|
||||||
} // ~Profiler
|
} // ~Profiler
|
||||||
|
|
||||||
|
thread_local int g_thread_id = -1;
|
||||||
|
const int MAX_THREADS = 10;
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/** It is split from the constructor so that it can be avoided allocating
|
/** It is split from the constructor so that it can be avoided allocating
|
||||||
* unnecessary memory when the profiler is never used (for example in no
|
* unnecessary memory when the profiler is never used (for example in no
|
||||||
* graphics). */
|
* graphics). */
|
||||||
void Profiler::init()
|
void Profiler::init()
|
||||||
{
|
{
|
||||||
const int MAX_THREADS = 10;
|
|
||||||
m_all_threads_data.resize(MAX_THREADS);
|
m_all_threads_data.resize(MAX_THREADS);
|
||||||
m_thread_mapping.resize(MAX_THREADS);
|
|
||||||
|
|
||||||
// Add this thread to the thread mapping
|
// Add this thread to the thread mapping
|
||||||
m_thread_mapping[0] = pthread_self();
|
g_thread_id = 0;
|
||||||
m_gpu_times.resize(Q_LAST * m_max_frames);
|
m_gpu_times.resize(Q_LAST * m_max_frames);
|
||||||
} // init
|
} // init
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/** Returns a unique index for a thread. If the calling thread is not yet in
|
/** Returns a unique index for a thread. If the calling thread is not yet in
|
||||||
* the mapping, it will assign a new unique id to this thread. This function
|
* the mapping, it will assign a new unique id to this thread. */
|
||||||
* is NOT thread-safe and must be called from a properly protected code
|
|
||||||
* section. */
|
|
||||||
int Profiler::getThreadID()
|
int Profiler::getThreadID()
|
||||||
{
|
{
|
||||||
pthread_t thread = pthread_self();
|
if (g_thread_id == -1)
|
||||||
int i = 0;
|
|
||||||
while(i < m_threads_used)
|
|
||||||
{
|
{
|
||||||
if (memcmp( &m_thread_mapping[i],
|
if (m_threads_used >= MAX_THREADS)
|
||||||
&thread,
|
|
||||||
sizeof(thread)) ==0 )
|
|
||||||
{
|
{
|
||||||
return i;
|
g_thread_id = MAX_THREADS - 1;
|
||||||
|
return g_thread_id;
|
||||||
}
|
}
|
||||||
i++;
|
g_thread_id = m_threads_used.fetch_add(1);
|
||||||
} // for i <m_threads_used
|
}
|
||||||
|
return g_thread_id;
|
||||||
assert(m_threads_used < (int)m_thread_mapping.size());
|
|
||||||
m_thread_mapping[m_threads_used] = thread;
|
|
||||||
m_threads_used++;
|
|
||||||
|
|
||||||
return m_threads_used - 1;
|
|
||||||
} // getThreadID
|
} // getThreadID
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
@ -21,9 +21,9 @@
|
|||||||
#include "utils/synchronised.hpp"
|
#include "utils/synchronised.hpp"
|
||||||
|
|
||||||
#include <irrlicht.h>
|
#include <irrlicht.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <atomic>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -224,14 +224,11 @@ private:
|
|||||||
* is the thread id. */
|
* is the thread id. */
|
||||||
std::vector< ThreadData> m_all_threads_data;
|
std::vector< ThreadData> m_all_threads_data;
|
||||||
|
|
||||||
/** A mapping of thread_t pointers to a unique integer (starting from 0).*/
|
|
||||||
std::vector<pthread_t> m_thread_mapping;
|
|
||||||
|
|
||||||
/** Buffer for the GPU times (in ms). */
|
/** Buffer for the GPU times (in ms). */
|
||||||
std::vector<int> m_gpu_times;
|
std::vector<int> m_gpu_times;
|
||||||
|
|
||||||
/** Counts the threads used, i.e. registered in m_thread_mapping. */
|
/** Counts the threads used. */
|
||||||
int m_threads_used;
|
std::atomic<int> m_threads_used;
|
||||||
|
|
||||||
/** Index of the current frame in the buffer. */
|
/** Index of the current frame in the buffer. */
|
||||||
int m_current_frame;
|
int m_current_frame;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user