Profiler.inl 2.4 KB

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