Profiler.inl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if (ProfileThread::sInstance == nullptr)
  23. {
  24. // Thread not instrumented
  25. mSample = nullptr;
  26. }
  27. else if (ProfileThread::sInstance->mCurrentSample < ProfileThread::cMaxSamples)
  28. {
  29. // Get pointer to write data to
  30. mSample = &ProfileThread::sInstance->mSamples[ProfileThread::sInstance->mCurrentSample++];
  31. // Start constructing sample (will end up on stack)
  32. mTemp.mName = inName;
  33. mTemp.mColor = inColor;
  34. // Collect start sample last
  35. mTemp.mStartCycle = GetProcessorTickCount();
  36. }
  37. else
  38. {
  39. // Out of samples
  40. if (!sOutOfSamplesReported)
  41. {
  42. Trace("ProfileMeasurement: Too many samples, some data will be lost!");
  43. sOutOfSamplesReported = true;
  44. }
  45. mSample = nullptr;
  46. }
  47. }
  48. ProfileMeasurement::~ProfileMeasurement()
  49. {
  50. if (mSample != nullptr)
  51. {
  52. // Finalize sample
  53. mTemp.mEndCycle = GetProcessorTickCount();
  54. // Write it to the memory buffer bypassing the cache
  55. static_assert(sizeof(ProfileSample) == 32, "Assume 32 bytes");
  56. static_assert(alignof(ProfileSample) == 16, "Assume 16 byte alignment");
  57. #if defined(JPH_USE_SSE)
  58. const __m128i *src = reinterpret_cast<const __m128i *>(&mTemp);
  59. __m128i *dst = reinterpret_cast<__m128i *>(mSample);
  60. __m128i val = _mm_loadu_si128(src);
  61. _mm_stream_si128(dst, val);
  62. val = _mm_loadu_si128(src + 1);
  63. _mm_stream_si128(dst + 1, val);
  64. #elif defined(JPH_USE_NEON)
  65. const int *src = reinterpret_cast<const int *>(&mTemp);
  66. int *dst = reinterpret_cast<int *>(mSample);
  67. int32x4_t val = vld1q_s32(src);
  68. vst1q_s32(dst, val);
  69. val = vld1q_s32(src + 4);
  70. vst1q_s32(dst + 4, val);
  71. #else
  72. memcpy(mSample, &mTemp, sizeof(ProfileSample));
  73. #endif
  74. mSample = nullptr;
  75. }
  76. }
  77. JPH_NAMESPACE_END