AssetSystemDebugComponent.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "AssetSystemDebugComponent.h"
  9. #include "ISystem.h"
  10. #include "AzCore/Asset/AssetManager.h"
  11. #include <AzCore/Interface/Interface.h>
  12. #include <AzCore/Console/IConsole.h>
  13. #include <AzCore/Jobs/JobFunction.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. namespace LmbrCentral
  16. {
  17. AZ_CVAR(bool, cl_assetStatusDebugActiveAssets, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Show debug stats about loading assets."
  18. " Data is not collected while disabled so it is recommended to enable this via command line or config");
  19. AZ_CVAR(bool, cl_assetStatusDebugLoadedAssets, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Show debug stats about loaded assets."
  20. " Data is not collected while disabled so it is recommended to enable this via command line or config");
  21. AZ_CVAR(std::uint8_t, cl_assetStatusDebugDisplayCount, 20, nullptr, AZ::ConsoleFunctorFlags::Null,
  22. "Sets the max number of assets to record and display in debug stats. This will only update after more assets have loaded.");
  23. void AssetSystemDebugComponent::Activate()
  24. {
  25. BusConnect();
  26. AZ::Interface<IDebugAssetEvent>::Register(this);
  27. }
  28. void AssetSystemDebugComponent::Deactivate()
  29. {
  30. AZ::Interface<IDebugAssetEvent>::Unregister(this);
  31. BusDisconnect();
  32. }
  33. AZStd::string StatusToString(AZ::Data::AssetData::AssetStatus status)
  34. {
  35. switch (status)
  36. {
  37. case AZ::Data::AssetData::AssetStatus::NotLoaded:
  38. return "Not Loaded";
  39. case AZ::Data::AssetData::AssetStatus::Queued:
  40. return "Queued";
  41. case AZ::Data::AssetData::AssetStatus::StreamReady:
  42. return "Stream Ready";
  43. case AZ::Data::AssetData::AssetStatus::Loading:
  44. return "Loading";
  45. case AZ::Data::AssetData::AssetStatus::LoadedPreReady:
  46. return "Loaded Pre-Ready";
  47. case AZ::Data::AssetData::AssetStatus::ReadyPreNotify:
  48. return "Ready Pre-Notify";
  49. case AZ::Data::AssetData::AssetStatus::Ready:
  50. return "Ready";
  51. case AZ::Data::AssetData::AssetStatus::Error:
  52. return "Error";
  53. default:
  54. return "Unknown State";
  55. }
  56. }
  57. void AssetSystemDebugComponent::DrawGlobalDebugInfo()
  58. {
  59. if(!cl_assetStatusDebugActiveAssets && !cl_assetStatusDebugLoadedAssets)
  60. {
  61. return;
  62. }
  63. // ToDo: Remove class or update to work with Atom? LYN-3672
  64. /*float x = 10;
  65. float y = 15;
  66. ColorF color(1, 1, 1);
  67. constexpr bool center = false;
  68. constexpr float ySpacing = 14;
  69. constexpr float fontSize = 1.3;
  70. const int maxCount = cl_assetStatusDebugDisplayCount;
  71. if (cl_assetStatusDebugActiveAssets)
  72. {
  73. // Make a local copy of the "oldest active" list. We don't want to hold the eventMutex while calling FindAsset below
  74. // or else we open up the possibility of lock inversion deadlocks. This routines would lock eventMutex then AssetManager
  75. // locks, but modifications to the load data history can happen from multiple loading threads that lock AssetManager mutexes
  76. // before locking the eventMutex.
  77. AZStd::vector<AZ::Data::AssetId> oldestActive(m_oldestActive.size());
  78. {
  79. AZStd::scoped_lock lock(m_eventMutex);
  80. for (auto& info : m_oldestActive)
  81. {
  82. oldestActive.emplace_back(info->m_id);
  83. }
  84. }
  85. auxGeom->Draw2dLabel(x, y, fontSize, color, center, "Last %d assets to start loading, oldest to newest:", maxCount);
  86. y += ySpacing; // Extra space after the "title"
  87. for (auto& oldestActiveId : oldestActive)
  88. {
  89. y += ySpacing;
  90. auto asset = AZ::Data::AssetManager::Instance().FindAsset(oldestActiveId, AZ::Data::AssetLoadBehavior::Default);
  91. auxGeom->Draw2dLabel(x, y, fontSize, color, center, "%s - %s",
  92. asset.GetHint().length() > 0 ? asset.GetHint().c_str() : oldestActiveId.ToString<AZStd::string>().c_str(),
  93. StatusToString(asset.GetStatus()).c_str());
  94. }
  95. }
  96. if (cl_assetStatusDebugLoadedAssets)
  97. {
  98. // Same as above - make a local copy of the list to avoid lock inversion deadlocks.
  99. AZStd::vector<AZStd::pair<AZ::Data::AssetId, AZStd::chrono::duration<float, AZStd::milli>>> activeAssets(m_recentlyCompleted.size());
  100. {
  101. AZStd::scoped_lock lock(m_eventMutex);
  102. for (auto& info : m_recentlyCompleted)
  103. {
  104. activeAssets.emplace_back(info->m_id, info->m_loadFinish - info->m_loadStart);
  105. }
  106. }
  107. if(cl_assetStatusDebugActiveAssets)
  108. {
  109. y = 15;
  110. x = 1200;
  111. }
  112. auxGeom->Draw2dLabelCustom(x, y, fontSize, color, eDrawText_Right, "Last %d assets loaded, most recent first:", maxCount);
  113. y += ySpacing; // Extra space after the "title"
  114. for (auto& [activeAssetId, activeAssetLoadTime] : activeAssets)
  115. {
  116. y += ySpacing;
  117. auto asset = AZ::Data::AssetManager::Instance().FindAsset(activeAssetId, AZ::Data::AssetLoadBehavior::Default);
  118. auxGeom->Draw2dLabelCustom(x, y, fontSize, color, eDrawText_Right, "%s - %s - %fms",
  119. asset.GetHint().length() > 0 ? asset.GetHint().c_str() : activeAssetId.ToString<AZStd::string>().c_str(),
  120. StatusToString(asset.GetStatus()).c_str(),
  121. activeAssetLoadTime.count());
  122. }
  123. }*/
  124. }
  125. void AssetSystemDebugComponent::AssetStatusUpdate(AZ::Data::AssetId id, AZ::Data::AssetData::AssetStatus status)
  126. {
  127. using namespace AZ::Data;
  128. if (!cl_assetStatusDebugActiveAssets && !cl_assetStatusDebugLoadedAssets)
  129. {
  130. return;
  131. }
  132. AZStd::scoped_lock lock(m_eventMutex);
  133. auto itr = m_events.find(id);
  134. bool exists = itr != m_events.end();
  135. if (exists)
  136. {
  137. itr->second.m_status = status;
  138. }
  139. switch (status)
  140. {
  141. case AssetData::AssetStatus::Queued:
  142. {
  143. EventInfo info;
  144. info.m_id = id;
  145. info.m_status = status;
  146. info.m_loadStart = AZStd::chrono::steady_clock::now();
  147. m_events[id] = info;
  148. m_oldestActive.insert(&m_events[id]);
  149. while (m_oldestActive.size() > cl_assetStatusDebugDisplayCount)
  150. {
  151. m_oldestActive.erase(--(m_oldestActive.end()));
  152. }
  153. }
  154. break;
  155. case AssetData::AssetStatus::Error:
  156. case AssetData::AssetStatus::ReadyPreNotify:
  157. {
  158. if(exists)
  159. {
  160. itr->second.m_loadFinish = AZStd::chrono::steady_clock::now();
  161. m_recentlyCompleted.insert(&itr->second);
  162. m_oldestActive.erase(&itr->second);
  163. while (m_recentlyCompleted.size() > cl_assetStatusDebugDisplayCount)
  164. {
  165. auto last = --(m_recentlyCompleted.end());
  166. m_events.erase((*last)->m_id);
  167. m_recentlyCompleted.erase(last);
  168. }
  169. }
  170. }
  171. break;
  172. }
  173. }
  174. void AssetSystemDebugComponent::ReleaseAsset(AZ::Data::AssetId id)
  175. {
  176. AZStd::scoped_lock lock(m_eventMutex);
  177. auto itr = m_events.find(id);
  178. if(itr != m_events.end())
  179. {
  180. m_oldestActive.erase(&itr->second);
  181. m_recentlyCompleted.erase(&itr->second);
  182. m_events.erase(itr);
  183. }
  184. }
  185. void AssetSystemDebugComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  186. {
  187. provided.push_back(AZ_CRC_CE("AssetSystemDebug"));
  188. }
  189. void AssetSystemDebugComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  190. {
  191. incompatible.push_back(AZ_CRC_CE("AssetSystemDebug"));
  192. }
  193. void AssetSystemDebugComponent::Reflect(AZ::ReflectContext* reflection)
  194. {
  195. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(reflection))
  196. {
  197. serialize->Class<AssetSystemDebugComponent, AZ::Component>()
  198. ->Version(0)
  199. ;
  200. }
  201. }
  202. }