Profiler.inl 2.5 KB

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