23 #include <sys/sys_time.h> 24 #include <sys/time_util.h> 28 #if defined (SUNOS) || defined (__SUNOS__) 32 #include <mach/mach_time.h> 33 #include <TargetConditionals.h> 36 #if defined(WIN32) || defined(_WIN32) 38 #define BT_USE_WINDOWS_TIMERS 39 #define WIN32_LEAN_AND_MEAN 50 #define GetTickCount64 GetTickCount 61 #ifdef BT_LINUX_REALTIME 64 #endif //BT_LINUX_REALTIME 68 #define mymin(a,b) (a > b ? a : b) 73 #ifdef BT_USE_WINDOWS_TIMERS 84 struct timeval mStartTime;
86 #endif //__CELLOS_LV2__ 94 #ifdef BT_USE_WINDOWS_TIMERS 95 QueryPerformanceFrequency(&m_data->mClockFrequency);
121 #ifdef BT_USE_WINDOWS_TIMERS 122 QueryPerformanceCounter(&m_data->mStartTime);
125 #ifdef __CELLOS_LV2__ 130 SYS_TIMEBASE_GET( newTime );
131 m_data->mStartTime = newTime;
134 m_data->mStartTimeNano = mach_absolute_time();
136 gettimeofday(&m_data->mStartTime, 0);
145 #ifdef BT_USE_WINDOWS_TIMERS 146 LARGE_INTEGER currentTime;
147 QueryPerformanceCounter(¤tTime);
148 LONGLONG elapsedTime = currentTime.QuadPart -
149 m_data->mStartTime.QuadPart;
151 unsigned long msecTicks = (
unsigned long)(1000 * elapsedTime /
152 m_data->mClockFrequency.QuadPart);
157 #ifdef __CELLOS_LV2__ 158 uint64_t freq=sys_time_get_timebase_frequency();
159 double dFreq=((double) freq) / 1000.0;
162 SYS_TIMEBASE_GET( newTime );
165 return (
unsigned long int)((double(newTime-m_data->mStartTime)) / dFreq);
168 struct timeval currentTime;
169 gettimeofday(¤tTime, 0);
170 return (currentTime.tv_sec - m_data->mStartTime.tv_sec) * 1000 +
171 (currentTime.tv_usec - m_data->mStartTime.tv_usec) / 1000;
172 #endif //__CELLOS_LV2__ 180 #ifdef BT_USE_WINDOWS_TIMERS 182 LARGE_INTEGER currentTime, elapsedTime;
184 QueryPerformanceCounter(¤tTime);
185 elapsedTime.QuadPart = currentTime.QuadPart -
186 m_data->mStartTime.QuadPart;
187 elapsedTime.QuadPart *= 1000000;
188 elapsedTime.QuadPart /= m_data->mClockFrequency.QuadPart;
190 return (
unsigned long long) elapsedTime.QuadPart;
193 #ifdef __CELLOS_LV2__ 194 uint64_t freq=sys_time_get_timebase_frequency();
195 double dFreq=((double) freq)/ 1000000.0;
199 SYS_TIMEBASE_GET( newTime );
201 return (
unsigned long int)((double(newTime-m_data->mStartTime)) / dFreq);
204 struct timeval currentTime;
205 gettimeofday(¤tTime, 0);
206 return (currentTime.tv_sec - m_data->mStartTime.tv_sec) * 1000000 +
207 (currentTime.tv_usec - m_data->mStartTime.tv_usec);
208 #endif//__CELLOS_LV2__ 214 #ifdef BT_USE_WINDOWS_TIMERS 216 LARGE_INTEGER currentTime, elapsedTime;
218 QueryPerformanceCounter(¤tTime);
219 elapsedTime.QuadPart = currentTime.QuadPart -
220 m_data->mStartTime.QuadPart;
221 elapsedTime.QuadPart *= 1000000000;
222 elapsedTime.QuadPart /= m_data->mClockFrequency.QuadPart;
224 return (
unsigned long long) elapsedTime.QuadPart;
227 #ifdef __CELLOS_LV2__ 228 uint64_t freq=sys_time_get_timebase_frequency();
229 double dFreq=((double) freq)/ 1e9;
233 SYS_TIMEBASE_GET( newTime );
235 return (
unsigned long int)((double(newTime-m_data->mStartTime)) / dFreq);
238 uint64_t ticks = mach_absolute_time() - m_data->mStartTimeNano;
239 static long double conversion = 0.0L;
240 if( 0.0L == conversion )
243 mach_timebase_info_data_t info;
244 int err = mach_timebase_info( &info );
250 conversion = info.numer / info.denom;
252 return (ticks * conversion);
257 #ifdef BT_LINUX_REALTIME 259 clock_gettime(CLOCK_REALTIME,&ts);
260 return 1000000000*ts.tv_sec + ts.tv_nsec;
262 struct timeval currentTime;
263 gettimeofday(¤tTime, 0);
264 return (currentTime.tv_sec - m_data->mStartTime.tv_sec) * 1e9 +
265 (currentTime.tv_usec - m_data->mStartTime.tv_usec)*1000;
266 #endif //BT_LINUX_REALTIME 269 #endif//__CELLOS_LV2__ 279 return btScalar(getTimeMicroseconds()) * microseconds_to_seconds;
282 #ifndef BT_NO_PROFILE 321 RecursionCounter( 0 ),
358 if ( child->
Name == name ) {
400 unsigned long int time;
417 CurrentParent = start;
418 CurrentChild = CurrentParent->
Get_Child();
424 CurrentChild = CurrentParent->Get_Child();
430 CurrentChild = CurrentChild->Get_Sibling();
436 return CurrentChild == NULL;
442 CurrentChild = CurrentParent->Get_Child();
443 while ( (CurrentChild != NULL) && (index != 0) ) {
445 CurrentChild = CurrentChild->Get_Sibling();
448 if ( CurrentChild != NULL ) {
449 CurrentParent = CurrentChild;
450 CurrentChild = CurrentParent->Get_Child();
457 if ( CurrentParent->Get_Parent() != NULL ) {
458 CurrentParent = CurrentParent->Get_Parent();
460 CurrentChild = CurrentParent->Get_Child();
495 &gRoots[ 0], &gRoots[ 1], &gRoots[ 2], &gRoots[ 3],
496 &gRoots[ 4], &gRoots[ 5], &gRoots[ 6], &gRoots[ 7],
497 &gRoots[ 8], &gRoots[ 9], &gRoots[10], &gRoots[11],
498 &gRoots[12], &gRoots[13], &gRoots[14], &gRoots[15],
499 &gRoots[16], &gRoots[17], &gRoots[18], &gRoots[19],
500 &gRoots[20], &gRoots[21], &gRoots[22], &gRoots[23],
501 &gRoots[24], &gRoots[25], &gRoots[26], &gRoots[27],
502 &gRoots[28], &gRoots[29], &gRoots[30], &gRoots[31],
503 &gRoots[32], &gRoots[33], &gRoots[34], &gRoots[35],
504 &gRoots[36], &gRoots[37], &gRoots[38], &gRoots[39],
505 &gRoots[40], &gRoots[41], &gRoots[42], &gRoots[43],
506 &gRoots[44], &gRoots[45], &gRoots[46], &gRoots[47],
507 &gRoots[48], &gRoots[49], &gRoots[50], &gRoots[51],
508 &gRoots[52], &gRoots[53], &gRoots[54], &gRoots[55],
509 &gRoots[56], &gRoots[57], &gRoots[58], &gRoots[59],
510 &gRoots[60], &gRoots[61], &gRoots[62], &gRoots[63],
555 if (name != gCurrentNodes[threadIndex]->
Get_Name()) {
556 gCurrentNodes[threadIndex] = gCurrentNodes[threadIndex]->
Get_Sub_Node( name );
559 gCurrentNodes[threadIndex]->
Call();
574 if (gCurrentNodes[threadIndex]->
Return()) {
575 gCurrentNodes[threadIndex] = gCurrentNodes[threadIndex]->
Get_Parent();
591 gProfileClock.
reset();
595 gRoots[threadIndex].
Reset();
596 gRoots[threadIndex].
Call();
616 unsigned long int time;
626 profileIterator->
First();
627 if (profileIterator->
Is_Done())
633 for (i=0;i<spacing;i++) printf(
".");
634 printf(
"----------------------------------\n");
635 for (i=0;i<spacing;i++) printf(
".");
636 printf(
"Profiling: %s (total running time: %.3f ms) ---\n", profileIterator->
Get_Current_Parent_Name(), parent_time );
637 float totalTime = 0.f;
642 for (i = 0; !profileIterator->
Is_Done(); i++,profileIterator->
Next())
646 accumulated_time += current_total_time;
647 float fraction = parent_time >
SIMD_EPSILON ? (current_total_time / parent_time) * 100 : 0.f;
649 int i;
for (i=0;i<spacing;i++) printf(
".");
651 printf(
"%d -- %s (%.2f %%) :: %.3f ms / frame (%d calls)\n",i, profileIterator->
Get_Current_Name(), fraction,(current_total_time / (double)frames_since_reset),profileIterator->
Get_Current_Total_Calls());
652 totalTime += current_total_time;
656 if (parent_time < accumulated_time)
660 for (i=0;i<spacing;i++) printf(
".");
661 printf(
"%s (%.3f %%) :: %.3f ms\n",
"Unaccounted:",parent_time >
SIMD_EPSILON ? ((parent_time - accumulated_time) / parent_time) * 100 : 0.f, parent_time - accumulated_time);
663 for (i=0;i<numChildren;i++)
666 dumpRecursive(profileIterator,spacing+3);
678 dumpRecursive(profileIterator,0);
684 #if defined(_WIN32) && (defined(__MINGW32__) || defined(__MINGW64__)) 685 #define BT_HAVE_TLS 1 686 #elif __APPLE__ && !TARGET_OS_IPHONE 688 #define BT_HAVE_TLS 1 690 #define BT_HAVE_TLS 1 695 #if defined(__ANDROID__) && defined(__clang__) 696 #if __has_include(<android/ndk-version.h>) 697 #include <android/ndk-version.h> 698 #endif // __has_include(<android/ndk-version.h>) 699 #if defined(__NDK_MAJOR__) && \ 700 ((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1))) 703 #endif // defined(__ANDROID__) && defined(__clang__) 707 const unsigned int kNullIndex = ~0
U;
712 #if defined(BT_HAVE_TLS) 713 static __thread
unsigned int sThreadIndex = kNullIndex;
714 #elif defined(_WIN32) 715 __declspec(thread)
static unsigned int sThreadIndex = kNullIndex;
717 unsigned int sThreadIndex = 0;
723 if (sThreadIndex == kNullIndex) {
724 sThreadIndex = gThreadCounter++;
727 #endif //BT_THREADSAFE 745 #endif //BT_NO_PROFILE CProfileNode * Get_Sub_Node(const char *name)
CProfileSample(const char *name)
CProfileNode * Get_Child(void)
unsigned long long int uint64_t
static CProfileIterator * Get_Iterator(void)
CProfileIterator(CProfileNode *start)
CProfileNode gRoots[BT_QUICKPROF_MAX_THREAD_COUNT]
static void Start_Profile(const char *name)
static void CleanupMemory(void)
btEnterProfileZoneFunc * btGetCurrentEnterProfileZoneFunc()
btScalar getTimeSeconds()
Returns the time in s since the last call to reset or since the Clock was created.
unsigned int btQuickprofGetCurrentThreadIndex2()
unsigned long long int getTimeNanoseconds()
const char * Get_Current_Name(void)
float Get_Current_Parent_Total_Time(void)
The btClock is a portable basic clock that measures accurate time in seconds, use for profiling...
float Get_Current_Total_Time(void)
void Enter_Child(int index)
void reset()
Resets the initial reference time.
An iterator to navigate through the tree.
static void Stop_Profile(void)
float Profile_Get_Tick_Rate(void)
void btSetCustomEnterProfileZoneFunc(btEnterProfileZoneFunc *enterFunc)
btLeaveProfileZoneFunc * btGetCurrentLeaveProfileZoneFunc()
void btEnterProfileZone(const char *name)
static void dumpRecursive(CProfileIterator *profileIterator, int spacing)
static unsigned long int ResetTime
btClock()
The btClock is a portable basic clock that measures accurate time in seconds, use for profiling...
struct btClockData * m_data
static ThreadsafeCounter gThreadCounter
void btLeaveProfileZone()
void Profile_Get_Ticks(unsigned long int *ticks)
unsigned long long int getTimeMicroseconds()
Returns the time in us since the last call to reset or since the Clock was created.
CProfileNode * gCurrentNodes[BT_QUICKPROF_MAX_THREAD_COUNT]
void btSetCustomLeaveProfileZoneFunc(btLeaveProfileZoneFunc *leaveFunc)
static void Release_Iterator(CProfileIterator *iterator)
btClock & operator=(const btClock &other)
void( btLeaveProfileZoneFunc)()
static btClock gProfileClock
static float Get_Time_Since_Reset(void)
unsigned long long int getTimeMilliseconds()
Returns the time in ms since the last call to reset or since the btClock was created.
A node in the Profile Hierarchy Tree.
static void Increment_Frame_Counter(void)
CProfileNode * Get_Parent(void)
const unsigned int BT_QUICKPROF_MAX_THREAD_COUNT
static btEnterProfileZoneFunc * bts_enterFunc
int Get_Current_Total_Calls(void)
void btLeaveProfileZoneDefault()
const char * Get_Name(void)
unsigned long int StartTime
CProfileNode(const char *name, CProfileNode *parent)
LARGE_INTEGER mClockFrequency
static int Get_Frame_Count_Since_Reset(void)
unsigned int btGetCurrentThreadIndex()
void( btEnterProfileZoneFunc)(const char *msg)
const char * Get_Current_Parent_Name(void)
static btLeaveProfileZoneFunc * bts_leaveFunc
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
void btEnterProfileZoneDefault(const char *name)