add nanosecond resolution for Mac OSX clock / about://tracing timings

This commit is contained in:
Erwin Coumans
2016-12-23 18:18:35 -08:00
parent 4c06fd27b3
commit 0917310521
2 changed files with 94 additions and 10 deletions

View File

@@ -191,11 +191,60 @@ struct btTimings
unsigned long long int startTimeDiv1000 = startTime/1000; unsigned long long int startTimeDiv1000 = startTime/1000;
unsigned long long int endTimeDiv1000 = endTime/1000; unsigned long long int endTimeDiv1000 = endTime/1000;
fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 " ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n", #if 0
threadId, startTimeDiv1000,name);
fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 " ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}", 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); 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; m_numTimings = 0;
@@ -400,7 +449,7 @@ void MyKeyboardCallback(int key, int state)
btSetCustomLeaveProfileZoneFunc(MyDummyLeaveProfileZoneFunc); btSetCustomLeaveProfileZoneFunc(MyDummyLeaveProfileZoneFunc);
char fileName[1024]; char fileName[1024];
static int fileCounter = 0; static int fileCounter = 0;
sprintf(fileName,"d:/timings_%d.json",fileCounter++); sprintf(fileName,"timings_%d.json",fileCounter++);
gTimingFile = fopen(fileName,"w"); gTimingFile = fopen(fileName,"w");
fprintf(gTimingFile,"{\"traceEvents\":[\n"); fprintf(gTimingFile,"{\"traceEvents\":[\n");
//dump the content to file //dump the content to file

View File

@@ -28,6 +28,9 @@
#if defined (SUNOS) || defined (__SUNOS__) #if defined (SUNOS) || defined (__SUNOS__)
#include <stdio.h> #include <stdio.h>
#endif #endif
#ifdef __APPLE__
#include <mach/mach_time.h>
#endif
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
@@ -68,6 +71,9 @@ struct btClockData
#ifdef __CELLOS_LV2__ #ifdef __CELLOS_LV2__
uint64_t mStartTime; uint64_t mStartTime;
#else #else
#ifdef __APPLE__
uint64_t mStartTimeNano;
#endif
struct timeval mStartTime; struct timeval mStartTime;
#endif #endif
#endif //__CELLOS_LV2__ #endif //__CELLOS_LV2__
@@ -117,6 +123,9 @@ void btClock::reset()
SYS_TIMEBASE_GET( newTime ); SYS_TIMEBASE_GET( newTime );
m_data->mStartTime = newTime; m_data->mStartTime = newTime;
#else #else
#ifdef __APPLE__
m_data->mStartTimeNano = mach_absolute_time();
#endif
gettimeofday(&m_data->mStartTime, 0); gettimeofday(&m_data->mStartTime, 0);
#endif #endif
#endif #endif
@@ -218,11 +227,37 @@ unsigned long long int btClock::getTimeNanoseconds()
return (unsigned long int)((double(newTime-m_data->mStartTime)) / dFreq); return (unsigned long int)((double(newTime-m_data->mStartTime)) / dFreq);
#else #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(&currentTime, 0); gettimeofday(&currentTime, 0);
return (currentTime.tv_sec - m_data->mStartTime.tv_sec) * 1e9 + return (currentTime.tv_sec - m_data->mStartTime.tv_sec) * 1e9 +
(currentTime.tv_usec - m_data->mStartTime.tv_usec); (currentTime.tv_usec - m_data->mStartTime.tv_usec)*1000;
*/
#endif//__APPLE__
#endif//__CELLOS_LV2__ #endif//__CELLOS_LV2__
#endif #endif
} }