StatsSet.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Core/StatsSet.h>
  6. namespace anki {
  7. #if ANKI_STATS_ENABLED
  8. StatsSet::~StatsSet()
  9. {
  10. if(m_statCounterArr)
  11. {
  12. ANKI_ASSERT(m_statCounterArrSize > 0);
  13. free(m_statCounterArr);
  14. m_statCounterArr = nullptr;
  15. m_statCounterArrSize = 0;
  16. m_statCounterArrStorageSize = 0;
  17. }
  18. }
  19. void StatsSet::endFrame()
  20. {
  21. for(U32 i = 0; i < m_statCounterArrSize; ++i)
  22. {
  23. StatCounter& counter = *m_statCounterArr[i];
  24. const Bool needsReset = !!(counter.m_flags & StatFlag::kZeroEveryFrame);
  25. const Bool atomic = !(counter.m_flags & StatFlag::kMainThreadUpdates);
  26. const Bool isFloat = !!(counter.m_flags & StatFlag::kFloat);
  27. // Store the previous value
  28. if(isFloat)
  29. {
  30. if(atomic)
  31. {
  32. LockGuard lock(counter.m_floatLock);
  33. counter.m_prevValuef = counter.m_f;
  34. if(needsReset)
  35. {
  36. counter.m_f = 0.0;
  37. }
  38. }
  39. else
  40. {
  41. counter.m_prevValuef = counter.m_f;
  42. if(needsReset)
  43. {
  44. counter.m_f = 0;
  45. }
  46. }
  47. }
  48. else
  49. {
  50. if(atomic && needsReset)
  51. {
  52. counter.m_prevValueu = counter.m_atomic.exchange(0);
  53. }
  54. else if(atomic && !needsReset)
  55. {
  56. counter.m_prevValueu = counter.m_atomic.load();
  57. }
  58. else if(!atomic && needsReset)
  59. {
  60. counter.m_prevValueu = counter.m_u;
  61. counter.m_u = 0;
  62. }
  63. else if(!atomic && !needsReset)
  64. {
  65. counter.m_prevValueu = counter.m_u;
  66. }
  67. }
  68. }
  69. }
  70. void StatsSet::registerCounter(StatCounter* counter)
  71. {
  72. ANKI_ASSERT(counter);
  73. for(U32 i = 0; i < m_statCounterArrSize; ++i)
  74. {
  75. ANKI_ASSERT(m_statCounterArr[i]->m_name != counter->m_name);
  76. }
  77. // Try grow the array
  78. if(m_statCounterArrSize + 1 > m_statCounterArrStorageSize)
  79. {
  80. m_statCounterArrStorageSize = max(2u, m_statCounterArrStorageSize * 2);
  81. StatCounter** newArr = static_cast<StatCounter**>(malloc(sizeof(StatCounter*) * m_statCounterArrStorageSize));
  82. if(m_statCounterArrSize > 0)
  83. {
  84. memcpy(newArr, m_statCounterArr, sizeof(StatCounter*) * m_statCounterArrSize);
  85. free(m_statCounterArr);
  86. }
  87. m_statCounterArr = newArr;
  88. }
  89. m_statCounterArr[m_statCounterArrSize++] = counter;
  90. std::sort(m_statCounterArr, m_statCounterArr + m_statCounterArrSize, [](const StatCounter* a, const StatCounter* b) {
  91. if(a->m_category != b->m_category)
  92. {
  93. return a->m_category < b->m_category;
  94. }
  95. else
  96. {
  97. return strcmp(a->m_name, b->m_name) < 0;
  98. }
  99. });
  100. }
  101. #endif
  102. } // end namespace anki