PerformanceTracker.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ScriptCanvas/Execution/ExecutionPerformanceTimer.h>
  9. #include <ScriptCanvas/Execution/ExecutionState.h>
  10. #include <ScriptCanvas/PerformanceTracker.h>
  11. namespace ScriptCanvas
  12. {
  13. namespace Execution
  14. {
  15. PerformanceTracker::PerformanceTracker()
  16. {}
  17. PerformanceTracker::~PerformanceTracker()
  18. {
  19. AZStd::lock_guard lock(m_activeTimerMutex);
  20. for (auto iter : m_activeTimers)
  21. {
  22. delete iter.second;
  23. }
  24. m_activeTimers.clear();
  25. for (auto iter : m_timersByAsset)
  26. {
  27. delete iter.second;
  28. }
  29. m_timersByAsset.clear();
  30. }
  31. void PerformanceTracker::CalculateReports()
  32. {
  33. AZStd::lock_guard lock(m_activeTimerMutex);
  34. m_snapshotReport.tracking.activationCount += aznumeric_cast<AZ::u32>(m_activeTimers.size());
  35. m_globalReport.tracking.activationCount += aznumeric_cast<AZ::u32>(m_activeTimers.size());
  36. for (auto& iter : m_activeTimers)
  37. {
  38. const auto report = iter.second->GetReport();
  39. m_snapshotReport.tracking.timing += report;
  40. m_globalReport.tracking.timing += report;
  41. delete iter.second;
  42. }
  43. m_activeTimers.clear();
  44. for (auto& iter : m_timersByAsset)
  45. {
  46. const auto report = iter.second->timer.GetReport();
  47. auto& snapshotByAssetReport = *ModOrCreateReport(m_snapshotReport.byAsset, iter.first);
  48. auto& globalByAssetReport = *ModOrCreateReport(m_globalReport.byAsset, iter.first);
  49. snapshotByAssetReport.timing += report;
  50. snapshotByAssetReport.activationCount += iter.second->assetActivationCount;
  51. globalByAssetReport.timing += report;
  52. globalByAssetReport.activationCount += iter.second->assetActivationCount;
  53. delete iter.second;
  54. }
  55. m_timersByAsset.clear();
  56. m_lastCapturedSnapshot = m_snapshotReport;
  57. m_lastCapturedGlobal = m_globalReport;
  58. m_snapshotReport = {};
  59. }
  60. void PerformanceTracker::ClearGlobalReport()
  61. {
  62. AZStd::lock_guard lock(m_activeTimerMutex);
  63. m_globalReport = {};
  64. }
  65. void PerformanceTracker::ClearSnapshotReport()
  66. {
  67. AZStd::lock_guard lock(m_activeTimerMutex);
  68. m_snapshotReport = {};
  69. }
  70. PerformanceTimer* PerformanceTracker::CreateTimer(PerformanceKey key)
  71. {
  72. AZStd::lock_guard lock(m_activeTimerMutex);
  73. return m_activeTimers.insert({ key, aznew PerformanceTimer() }).first->second;
  74. }
  75. void PerformanceTracker::FinalizeReport(PerformanceKey key)
  76. {
  77. const AZ::Data::AssetId assetId = key->GetAssetId();
  78. AZStd::lock_guard lock(m_activeTimerMutex);
  79. auto iter = m_activeTimers.find(key);
  80. if (iter != m_activeTimers.end())
  81. {
  82. PerformanceTimer* timer = iter->second;
  83. auto report = timer->GetReport();
  84. auto& globalByAssetReport = *ModOrCreateReport(m_globalReport.byAsset, assetId);
  85. auto& snapshotByAssetReport = *ModOrCreateReport(m_snapshotReport.byAsset, assetId);
  86. snapshotByAssetReport.timing += report;
  87. globalByAssetReport.timing += report;
  88. m_snapshotReport.tracking.timing += report;
  89. m_globalReport.tracking.timing += report;
  90. m_activeTimers.erase(iter);
  91. delete timer;
  92. }
  93. }
  94. PerformanceTracker::AssetTimer* PerformanceTracker::GetOrCreateTimer(const AZ::Data::AssetId& key)
  95. {
  96. AZStd::lock_guard lock(m_activeTimerMutex);
  97. auto iter = m_timersByAsset.find(key);
  98. if (iter != m_timersByAsset.end())
  99. {
  100. return iter->second;
  101. }
  102. else
  103. {
  104. return m_timersByAsset.insert({ key, aznew PerformanceTracker::AssetTimer() }).first->second;
  105. }
  106. }
  107. PerformanceTimer* PerformanceTracker::GetOrCreateTimer(PerformanceKey key)
  108. {
  109. AZStd::lock_guard lock(m_activeTimerMutex);
  110. auto iter = m_activeTimers.find(key);
  111. if (iter != m_activeTimers.end())
  112. {
  113. return iter->second;
  114. }
  115. else
  116. {
  117. return m_activeTimers.insert({ key, aznew PerformanceTimer() }).first->second;
  118. }
  119. }
  120. PerformanceTrackingReport PerformanceTracker::GetGlobalReport() const
  121. {
  122. return m_lastCapturedGlobal.tracking;
  123. }
  124. PerformanceTrackingReport PerformanceTracker::GetGlobalReportByAsset(const AZ::Data::AssetId& assetId) const
  125. {
  126. return GetReportByAsset(m_lastCapturedGlobal.byAsset, assetId);
  127. }
  128. const PerformanceReport& PerformanceTracker::GetGlobalReportFull() const
  129. {
  130. return m_globalReport;
  131. }
  132. PerformanceTrackingReport PerformanceTracker::GetReportByAsset(const PerformanceReportByAsset& reports, AZ::Data::AssetId key)
  133. {
  134. auto iter = reports.find(key);
  135. return iter != reports.end() ? iter->second : PerformanceTrackingReport{};
  136. }
  137. PerformanceTrackingReport PerformanceTracker::GetSnapshotReport() const
  138. {
  139. return m_lastCapturedSnapshot.tracking;
  140. }
  141. PerformanceTrackingReport PerformanceTracker::GetSnapshotReportByAsset(const AZ::Data::AssetId& assetId) const
  142. {
  143. return GetReportByAsset(m_lastCapturedSnapshot.byAsset, assetId);
  144. }
  145. const PerformanceReport& PerformanceTracker::GetSnapshotReportFull() const
  146. {
  147. return m_lastCapturedSnapshot;
  148. }
  149. PerformanceTrackingReport* PerformanceTracker::ModOrCreateReport(PerformanceReportByAsset& reports, AZ::Data::AssetId key)
  150. {
  151. auto iter = reports.find(key);
  152. if (iter != reports.end())
  153. {
  154. return &iter->second;
  155. }
  156. else
  157. {
  158. return &reports.insert({ key, PerformanceTrackingReport() }).first->second;
  159. }
  160. }
  161. void PerformanceTracker::ReportExecutionTime(PerformanceKey key, AZStd::sys_time_t time)
  162. {
  163. GetOrCreateTimer(key)->AddExecutionTime(time);
  164. GetOrCreateTimer(key->GetAssetId())->timer.AddExecutionTime(time);
  165. }
  166. void PerformanceTracker::ReportLatentTime(PerformanceKey key, AZStd::sys_time_t time)
  167. {
  168. GetOrCreateTimer(key)->AddLatentTime(time);
  169. GetOrCreateTimer(key->GetAssetId())->timer.AddLatentTime(time);
  170. }
  171. void PerformanceTracker::ReportInitializationTime(PerformanceKey key, AZStd::sys_time_t time)
  172. {
  173. CreateTimer(key)->AddInitializationTime(time);
  174. AssetTimer* assetTimer = GetOrCreateTimer(key->GetAssetId());
  175. assetTimer->timer.AddInitializationTime(time);
  176. ++(assetTimer->assetActivationCount);
  177. }
  178. }
  179. }