From 0917310521a1bd78a1ccb9943b3bf3a7168bd449 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Fri, 23 Dec 2016 18:18:35 -0800 Subject: [PATCH] add nanosecond resolution for Mac OSX clock / about://tracing timings --- .../ExampleBrowser/OpenGLExampleBrowser.cpp | 63 ++++++++++++++++--- src/LinearMath/btQuickprof.cpp | 41 +++++++++++- 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp index 768e8c781..b9b8d4bb4 100644 --- a/examples/ExampleBrowser/OpenGLExampleBrowser.cpp +++ b/examples/ExampleBrowser/OpenGLExampleBrowser.cpp @@ -188,13 +188,62 @@ struct btTimings m_firstTiming = false; - unsigned long long int startTimeDiv1000 = startTime/1000; - unsigned long long int endTimeDiv1000 = endTime/1000; + unsigned long long int startTimeDiv1000 = startTime/1000; + unsigned long long int endTimeDiv1000 = endTime/1000; - fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 " ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n", - threadId, startTimeDiv1000,name); - fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 " ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}", - threadId, endTimeDiv1000,name); +#if 0 + + fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".123 ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n", + threadId, startTimeDiv1000, name); + fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".234 ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}", + threadId, endTimeDiv1000,name); + +#else + + unsigned int startTimeRem1000 = startTime%1000; + unsigned int endTimeRem1000 = endTime%1000; + + char startTimeRem1000Str[16]; + char endTimeRem1000Str[16]; + + if (startTimeRem1000<10) + { + sprintf(startTimeRem1000Str,"00%d",startTimeRem1000); + } + else + { + if (startTimeRem1000<100) + { + sprintf(startTimeRem1000Str,"0%d",startTimeRem1000); + } else + { + sprintf(startTimeRem1000Str,"%d",startTimeRem1000); + } + } + + if (endTimeRem1000<10) + { + sprintf(endTimeRem1000Str,"00%d",endTimeRem1000); + } + else + { + if (endTimeRem1000<100) + { + sprintf(endTimeRem1000Str,"0%d",endTimeRem1000); + } else + { + sprintf(endTimeRem1000Str,"%d",endTimeRem1000); + } + } + + char newname[1024]; + static int counter2=0; + sprintf(newname,"%s%d",name,counter2++); + fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n", + threadId, startTimeDiv1000,startTimeRem1000Str, newname); + fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}", + threadId, endTimeDiv1000,endTimeRem1000Str,newname); +#endif } m_numTimings = 0; @@ -400,7 +449,7 @@ void MyKeyboardCallback(int key, int state) btSetCustomLeaveProfileZoneFunc(MyDummyLeaveProfileZoneFunc); char fileName[1024]; static int fileCounter = 0; - sprintf(fileName,"d:/timings_%d.json",fileCounter++); + sprintf(fileName,"timings_%d.json",fileCounter++); gTimingFile = fopen(fileName,"w"); fprintf(gTimingFile,"{\"traceEvents\":[\n"); //dump the content to file diff --git a/src/LinearMath/btQuickprof.cpp b/src/LinearMath/btQuickprof.cpp index 63e855db8..56f6c8dfc 100644 --- a/src/LinearMath/btQuickprof.cpp +++ b/src/LinearMath/btQuickprof.cpp @@ -28,6 +28,9 @@ #if defined (SUNOS) || defined (__SUNOS__) #include #endif +#ifdef __APPLE__ +#include +#endif #if defined(WIN32) || defined(_WIN32) @@ -68,6 +71,9 @@ struct btClockData #ifdef __CELLOS_LV2__ uint64_t mStartTime; #else +#ifdef __APPLE__ + uint64_t mStartTimeNano; +#endif struct timeval mStartTime; #endif #endif //__CELLOS_LV2__ @@ -117,6 +123,9 @@ void btClock::reset() SYS_TIMEBASE_GET( newTime ); m_data->mStartTime = newTime; #else +#ifdef __APPLE__ + m_data->mStartTimeNano = mach_absolute_time(); +#endif gettimeofday(&m_data->mStartTime, 0); #endif #endif @@ -218,11 +227,37 @@ unsigned long long int btClock::getTimeNanoseconds() return (unsigned long int)((double(newTime-m_data->mStartTime)) / dFreq); #else +#ifdef __APPLE__ + uint64_t ticks = mach_absolute_time() - m_data->mStartTimeNano; + static long double conversion = 0.0L; + if( 0.0L == conversion ) + { + // attempt to get conversion to nanoseconds + mach_timebase_info_data_t info; + int err = mach_timebase_info( &info ); + if( err ) + { + btAssert(0); + conversion = 1.; + } + conversion = info.numer / info.denom; + } + return (ticks * conversion); - struct timeval currentTime; + +#else//__APPLE__ + + timespec ts; + clock_gettime(CLOCK_REALTIME,&ts); + return 1000000000*ts.tv_sec + ts.tv_nsec; + + + /* struct timeval currentTime; gettimeofday(¤tTime, 0); - return (currentTime.tv_sec - m_data->mStartTime.tv_sec) * 1e9 + - (currentTime.tv_usec - m_data->mStartTime.tv_usec); + return (currentTime.tv_sec - m_data->mStartTime.tv_sec) * 1e9 + + (currentTime.tv_usec - m_data->mStartTime.tv_usec)*1000; + */ +#endif//__APPLE__ #endif//__CELLOS_LV2__ #endif }