| 1 |
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 |
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 |
// SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
JPH_NAMESPACE_BEGIN |
| 6 |
|
| 7 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 8 |
// ProfileThread |
| 9 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 10 |
|
| 11 |
ProfileThread::ProfileThread(const string_view &inThreadName) : |
| 12 |
mThreadName(inThreadName) |
| 13 |
{ |
| 14 |
Profiler::sInstance->AddThread(this); |
| 15 |
} |
| 16 |
|
| 17 |
ProfileThread::~ProfileThread() |
| 18 |
{ |
| 19 |
Profiler::sInstance->RemoveThread(this); |
| 20 |
} |
| 21 |
|
| 22 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 23 |
// ProfileMeasurement |
| 24 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
JPH_TSAN_NO_SANITIZE // TSAN reports a race on sOutOfSamplesReported, however the worst case is that we report the out of samples message multiple times |
| 27 |
ProfileMeasurement::ProfileMeasurement(const char *inName, uint32 inColor) |
| 28 |
{ |
| 29 |
ProfileThread *current_thread = ProfileThread::sGetInstance(); |
| 30 |
if (current_thread == nullptr) |
| 31 |
{ |
| 32 |
// Thread not instrumented |
| 33 |
mSample = nullptr; |
| 34 |
} |
| 35 |
else if (current_thread->mCurrentSample < ProfileThread::cMaxSamples) |
| 36 |
{ |
| 37 |
// Get pointer to write data to |
| 38 |
mSample = ¤t_thread->mSamples[current_thread->mCurrentSample++]; |
| 39 |
|
| 40 |
// Start constructing sample (will end up on stack) |
| 41 |
mTemp.mName = inName; |
| 42 |
mTemp.mColor = inColor; |
| 43 |
|
| 44 |
// Collect start sample last |
| 45 |
mTemp.mStartCycle = GetProcessorTickCount(); |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
// Out of samples |
| 50 |
if (!sOutOfSamplesReported) |
| 51 |
{ |
| 52 |
sOutOfSamplesReported = true; |
| 53 |
Trace("ProfileMeasurement: Too many samples, some data will be lost!"); |
| 54 |
} |
| 55 |
mSample = nullptr; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
ProfileMeasurement::~ProfileMeasurement() |
| 60 |
{ |
| 61 |
if (mSample != nullptr) |
| 62 |
{ |
| 63 |
// Finalize sample |
| 64 |
mTemp.mEndCycle = GetProcessorTickCount(); |
| 65 |
|
| 66 |
// Write it to the memory buffer bypassing the cache |
| 67 |
static_assert(sizeof(ProfileSample) == 32, "Assume 32 bytes"); |
| 68 |
static_assert(alignof(ProfileSample) == 16, "Assume 16 byte alignment"); |
| 69 |
#if defined(JPH_USE_SSE) |
| 70 |
const __m128i *src = reinterpret_cast<const __m128i *>(&mTemp); |
| 71 |
__m128i *dst = reinterpret_cast<__m128i *>(mSample); |
| 72 |
__m128i val = _mm_loadu_si128(src); |
| 73 |
_mm_stream_si128(dst, val); |
| 74 |
val = _mm_loadu_si128(src + 1); |
| 75 |
_mm_stream_si128(dst + 1, val); |
| 76 |
#elif defined(JPH_USE_NEON) |
| 77 |
const int *src = reinterpret_cast<const int *>(&mTemp); |
| 78 |
int *dst = reinterpret_cast<int *>(mSample); |
| 79 |
int32x4_t val = vld1q_s32(src); |
| 80 |
vst1q_s32(dst, val); |
| 81 |
val = vld1q_s32(src + 4); |
| 82 |
vst1q_s32(dst + 4, val); |
| 83 |
#else |
| 84 |
memcpy(mSample, &mTemp, sizeof(ProfileSample)); |
| 85 |
#endif |
| 86 |
mSample = nullptr; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
JPH_NAMESPACE_END |