2017-01-30 04:59:47 +00:00
|
|
|
|
|
|
|
#include "ChromeTraceUtil.h"
|
|
|
|
#include "b3Clock.h"
|
|
|
|
#include "LinearMath/btQuickprof.h"
|
|
|
|
#include "LinearMath/btAlignedObjectArray.h"
|
|
|
|
#include "Bullet3Common/b3Logging.h"
|
2018-07-27 13:48:24 +00:00
|
|
|
#include <stdio.h>
|
2020-08-07 05:06:44 +00:00
|
|
|
#include <climits>
|
2017-01-30 04:59:47 +00:00
|
|
|
|
|
|
|
struct btTiming
|
|
|
|
{
|
|
|
|
const char* m_name;
|
|
|
|
int m_threadId;
|
|
|
|
unsigned long long int m_usStartTime;
|
|
|
|
unsigned long long int m_usEndTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
FILE* gTimingFile = 0;
|
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
2018-09-23 21:17:31 +00:00
|
|
|
#endif //__STDC_FORMAT_MACROS
|
2017-01-30 04:59:47 +00:00
|
|
|
|
|
|
|
//see http://stackoverflow.com/questions/18107426/printf-format-for-unsigned-int64-on-windows
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
#define BT_TIMING_CAPACITY 16 * 65536
|
2017-01-30 04:59:47 +00:00
|
|
|
static bool m_firstTiming = true;
|
|
|
|
|
|
|
|
struct btTimings
|
|
|
|
{
|
|
|
|
btTimings()
|
2018-09-23 21:17:31 +00:00
|
|
|
: m_numTimings(0),
|
|
|
|
m_activeBuffer(0)
|
2017-01-30 04:59:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
void flush()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < m_numTimings; i++)
|
2017-01-30 04:59:47 +00:00
|
|
|
{
|
|
|
|
const char* name = m_timings[m_activeBuffer][i].m_name;
|
|
|
|
int threadId = m_timings[m_activeBuffer][i].m_threadId;
|
|
|
|
unsigned long long int startTime = m_timings[m_activeBuffer][i].m_usStartTime;
|
|
|
|
unsigned long long int endTime = m_timings[m_activeBuffer][i].m_usEndTime;
|
|
|
|
|
|
|
|
if (!m_firstTiming)
|
|
|
|
{
|
|
|
|
fprintf(gTimingFile, ",\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_firstTiming = false;
|
|
|
|
|
2019-07-21 20:08:22 +00:00
|
|
|
if (startTime > endTime)
|
|
|
|
{
|
|
|
|
endTime = startTime;
|
|
|
|
}
|
|
|
|
|
2017-01-30 04:59:47 +00:00
|
|
|
unsigned long long int startTimeDiv1000 = startTime / 1000;
|
|
|
|
unsigned long long int endTimeDiv1000 = endTime / 1000;
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2019-07-21 20:08:22 +00:00
|
|
|
|
2017-01-30 04:59:47 +00:00
|
|
|
unsigned int startTimeRem1000 = startTime % 1000;
|
|
|
|
unsigned int endTimeRem1000 = endTime % 1000;
|
|
|
|
|
|
|
|
char startTimeRem1000Str[16];
|
|
|
|
char endTimeRem1000Str[16];
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (startTimeRem1000 < 10)
|
2017-01-30 04:59:47 +00:00
|
|
|
{
|
|
|
|
sprintf(startTimeRem1000Str, "00%d", startTimeRem1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (startTimeRem1000 < 100)
|
2017-01-30 04:59:47 +00:00
|
|
|
{
|
|
|
|
sprintf(startTimeRem1000Str, "0%d", startTimeRem1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(startTimeRem1000Str, "%d", startTimeRem1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (endTimeRem1000 < 10)
|
2017-01-30 04:59:47 +00:00
|
|
|
{
|
|
|
|
sprintf(endTimeRem1000Str, "00%d", endTimeRem1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (endTimeRem1000 < 100)
|
2017-01-30 04:59:47 +00:00
|
|
|
{
|
|
|
|
sprintf(endTimeRem1000Str, "0%d", endTimeRem1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(endTimeRem1000Str, "%d", endTimeRem1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char newname[1024];
|
|
|
|
static int counter2 = 0;
|
|
|
|
sprintf(newname, "%s%d", name, counter2++);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%I64d.%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n",
|
2018-09-23 21:17:31 +00:00
|
|
|
threadId, startTimeDiv1000, startTimeRem1000Str, newname);
|
2017-01-30 04:59:47 +00:00
|
|
|
fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%I64d.%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}",
|
2018-09-23 21:17:31 +00:00
|
|
|
threadId, endTimeDiv1000, endTimeRem1000Str, newname);
|
2017-01-30 04:59:47 +00:00
|
|
|
#else
|
2020-08-07 05:06:44 +00:00
|
|
|
// Note: on 64b build, PRIu64 resolves in 'lu' whereas timings ('ts') have to be printed as 'llu'.
|
|
|
|
fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%llu.%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n",
|
2018-09-23 21:17:31 +00:00
|
|
|
threadId, startTimeDiv1000, startTimeRem1000Str, newname);
|
2020-08-07 05:06:44 +00:00
|
|
|
fprintf(gTimingFile, "{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%llu.%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}",
|
2018-09-23 21:17:31 +00:00
|
|
|
threadId, endTimeDiv1000, endTimeRem1000Str, newname);
|
2017-01-30 04:59:47 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
m_numTimings = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addTiming(const char* name, int threadId, unsigned long long int startTime, unsigned long long int endTime)
|
|
|
|
{
|
|
|
|
if (m_numTimings >= BT_TIMING_CAPACITY)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_timings[0].size() == 0)
|
|
|
|
{
|
|
|
|
m_timings[0].resize(BT_TIMING_CAPACITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
int slot = m_numTimings++;
|
|
|
|
|
|
|
|
m_timings[m_activeBuffer][slot].m_name = name;
|
|
|
|
m_timings[m_activeBuffer][slot].m_threadId = threadId;
|
|
|
|
m_timings[m_activeBuffer][slot].m_usStartTime = startTime;
|
|
|
|
m_timings[m_activeBuffer][slot].m_usEndTime = endTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_numTimings;
|
|
|
|
int m_activeBuffer;
|
|
|
|
btAlignedObjectArray<btTiming> m_timings[1];
|
|
|
|
};
|
2018-07-27 13:48:24 +00:00
|
|
|
//#ifndef BT_NO_PROFILE
|
2017-01-30 04:59:47 +00:00
|
|
|
btTimings gTimings[BT_QUICKPROF_MAX_THREAD_COUNT];
|
|
|
|
#define MAX_NESTING 1024
|
2018-09-23 21:17:31 +00:00
|
|
|
int gStackDepths[BT_QUICKPROF_MAX_THREAD_COUNT] = {0};
|
2017-01-30 04:59:47 +00:00
|
|
|
const char* gFuncNames[BT_QUICKPROF_MAX_THREAD_COUNT][MAX_NESTING];
|
|
|
|
unsigned long long int gStartTimes[BT_QUICKPROF_MAX_THREAD_COUNT][MAX_NESTING];
|
2018-07-27 13:48:24 +00:00
|
|
|
//#endif
|
2017-01-30 04:59:47 +00:00
|
|
|
|
|
|
|
btClock clk;
|
|
|
|
|
|
|
|
bool gProfileDisabled = true;
|
|
|
|
|
|
|
|
void MyDummyEnterProfileZoneFunc(const char* msg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyDummyLeaveProfileZoneFunc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyEnterProfileZoneFunc(const char* msg)
|
|
|
|
{
|
|
|
|
if (gProfileDisabled)
|
|
|
|
return;
|
2018-11-01 15:19:50 +00:00
|
|
|
|
2017-01-30 04:59:47 +00:00
|
|
|
int threadId = btQuickprofGetCurrentThreadIndex2();
|
2018-09-23 21:17:31 +00:00
|
|
|
if (threadId < 0 || threadId >= BT_QUICKPROF_MAX_THREAD_COUNT)
|
2017-01-30 04:59:47 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (gStackDepths[threadId] >= MAX_NESTING)
|
|
|
|
{
|
|
|
|
btAssert(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gFuncNames[threadId][gStackDepths[threadId]] = msg;
|
|
|
|
gStartTimes[threadId][gStackDepths[threadId]] = clk.getTimeNanoseconds();
|
|
|
|
if (gStartTimes[threadId][gStackDepths[threadId]] <= gStartTimes[threadId][gStackDepths[threadId] - 1])
|
|
|
|
{
|
|
|
|
gStartTimes[threadId][gStackDepths[threadId]] = 1 + gStartTimes[threadId][gStackDepths[threadId] - 1];
|
|
|
|
}
|
|
|
|
gStackDepths[threadId]++;
|
2018-11-01 15:19:50 +00:00
|
|
|
|
2017-01-30 04:59:47 +00:00
|
|
|
}
|
|
|
|
void MyLeaveProfileZoneFunc()
|
|
|
|
{
|
|
|
|
if (gProfileDisabled)
|
|
|
|
return;
|
2018-11-01 15:19:50 +00:00
|
|
|
|
2017-01-30 04:59:47 +00:00
|
|
|
int threadId = btQuickprofGetCurrentThreadIndex2();
|
2018-09-23 21:17:31 +00:00
|
|
|
if (threadId < 0 || threadId >= BT_QUICKPROF_MAX_THREAD_COUNT)
|
|
|
|
return;
|
2017-01-30 04:59:47 +00:00
|
|
|
|
|
|
|
if (gStackDepths[threadId] <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gStackDepths[threadId]--;
|
|
|
|
|
|
|
|
const char* name = gFuncNames[threadId][gStackDepths[threadId]];
|
|
|
|
unsigned long long int startTime = gStartTimes[threadId][gStackDepths[threadId]];
|
|
|
|
|
|
|
|
unsigned long long int endTime = clk.getTimeNanoseconds();
|
|
|
|
gTimings[threadId].addTiming(name, threadId, startTime, endTime);
|
2018-11-01 15:19:50 +00:00
|
|
|
|
2017-01-30 04:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void b3ChromeUtilsStartTimings()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
m_firstTiming = true;
|
|
|
|
gProfileDisabled = false; //true;
|
|
|
|
b3SetCustomEnterProfileZoneFunc(MyEnterProfileZoneFunc);
|
|
|
|
b3SetCustomLeaveProfileZoneFunc(MyLeaveProfileZoneFunc);
|
|
|
|
|
|
|
|
//also for Bullet 2.x API
|
|
|
|
btSetCustomEnterProfileZoneFunc(MyEnterProfileZoneFunc);
|
|
|
|
btSetCustomLeaveProfileZoneFunc(MyLeaveProfileZoneFunc);
|
2017-01-30 04:59:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 00:51:40 +00:00
|
|
|
void b3ChromeUtilsStopTimingsAndWriteJsonFile(const char* fileNamePrefix)
|
2017-01-30 04:59:47 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
b3SetCustomEnterProfileZoneFunc(MyDummyEnterProfileZoneFunc);
|
|
|
|
b3SetCustomLeaveProfileZoneFunc(MyDummyLeaveProfileZoneFunc);
|
|
|
|
//also for Bullet 2.x API
|
|
|
|
btSetCustomEnterProfileZoneFunc(MyDummyEnterProfileZoneFunc);
|
|
|
|
btSetCustomLeaveProfileZoneFunc(MyDummyLeaveProfileZoneFunc);
|
|
|
|
char fileName[1024];
|
|
|
|
static int fileCounter = 0;
|
|
|
|
sprintf(fileName, "%s_%d.json", fileNamePrefix, fileCounter++);
|
|
|
|
gTimingFile = fopen(fileName, "w");
|
|
|
|
if (gTimingFile)
|
|
|
|
{
|
|
|
|
fprintf(gTimingFile, "{\"traceEvents\":[\n");
|
|
|
|
//dump the content to file
|
|
|
|
for (int i = 0; i < BT_QUICKPROF_MAX_THREAD_COUNT; i++)
|
|
|
|
{
|
|
|
|
if (gTimings[i].m_numTimings)
|
2017-07-14 22:12:16 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
printf("Writing %d timings for thread %d\n", gTimings[i].m_numTimings, i);
|
|
|
|
gTimings[i].flush();
|
2017-01-30 04:59:47 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
fprintf(gTimingFile, "\n],\n\"displayTimeUnit\": \"ns\"}");
|
|
|
|
fclose(gTimingFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b3Printf("Error opening file");
|
|
|
|
b3Printf(fileName);
|
|
|
|
}
|
|
|
|
gTimingFile = 0;
|
2017-01-30 04:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void b3ChromeUtilsEnableProfiling()
|
|
|
|
{
|
|
|
|
gProfileDisabled = false;
|
2018-07-27 13:48:24 +00:00
|
|
|
}
|