StreamerProfilerSystemComponent.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/IO/IStreamerProfiler.h>
  11. #include <AzCore/IO/Streamer/Statistics.h>
  12. #include <AzCore/std/containers/array.h>
  13. #include <AzCore/std/containers/vector.h>
  14. #include <AzCore/std/containers/unordered_map.h>
  15. #include <AzCore/std/string/fixed_string.h>
  16. #include <AzCore/std/string/string_view.h>
  17. #include <AzCore/std/parallel/atomic.h>
  18. #include <AzCore/std/limits.h>
  19. namespace AZ::IO
  20. {
  21. class IStreamer;
  22. }
  23. namespace Streamer
  24. {
  25. class StreamerProfilerSystemComponent final
  26. : public AZ::Component
  27. , public AZ::IO::IStreamerProfiler
  28. {
  29. public:
  30. AZ_COMPONENT(StreamerProfilerSystemComponent, "{6b5a5e7f-81ee-4fb1-a005-107773dfc531}");
  31. static void Reflect(AZ::ReflectContext* context);
  32. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  33. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  34. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  35. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  36. StreamerProfilerSystemComponent();
  37. ~StreamerProfilerSystemComponent() override;
  38. void DrawStatistics(bool& keepDrawing) override;
  39. protected:
  40. static constexpr size_t GraphStoreElementCount = 256; // Needs to be a power of 2.
  41. class GraphStore
  42. {
  43. public:
  44. GraphStore();
  45. GraphStore(float minValue, float maxValue);
  46. void AddValue(float value);
  47. float operator[](size_t index);
  48. float GetMin() const;
  49. float GetMax() const;
  50. private:
  51. AZStd::array<float, GraphStoreElementCount> m_values;
  52. float m_minValue{ AZStd::numeric_limits<float>::max() };
  53. float m_maxValue{ AZStd::numeric_limits<float>::min() };
  54. size_t m_front{ 0 };
  55. };
  56. using FullStatName = AZStd::fixed_string<1024>;
  57. using StatsContainer = AZStd::vector<AZ::IO::Statistic>;
  58. ////////////////////////////////////////////////////////////////////////
  59. // AZ::Component interface implementation
  60. void Init() override;
  61. void Activate() override;
  62. void Deactivate() override;
  63. ////////////////////////////////////////////////////////////////////////
  64. void DrawLiveStats(AZ::IO::IStreamer& streamer);
  65. void DrawHardwareInfo(AZ::IO::IStreamer& streamer);
  66. void DrawStackConfiguration(AZ::IO::IStreamer& streamer);
  67. void DrawFileLocks(AZ::IO::IStreamer& streamer);
  68. void DrawGraph(const AZ::IO::Statistic::Value& value, GraphStore& values, bool useHistogram);
  69. void DrawStatisticValue(
  70. const AZ::IO::Statistic::Value& value,
  71. float capturedMin = AZStd::numeric_limits<float>::max(),
  72. float capturedMax = AZStd::numeric_limits<float>::min());
  73. static void AppendByteSize(AZStd::fixed_string<256>& text, AZ::u64 value);
  74. static void AppendTime(AZStd::fixed_string<256>& text, AZ::IO::Statistic::TimeValue value);
  75. static void AppendBytesPerSecond(AZStd::fixed_string<256>& text, double value);
  76. static GraphStore CreateGraph(const AZ::IO::Statistic::Value& value);
  77. static void DrawToolTip(AZStd::string_view text);
  78. static void DrawToolTipV(const char* text, ...);
  79. AZStd::unordered_map<FullStatName, GraphStore> m_graphInfo;
  80. StatsContainer m_stats;
  81. StatsContainer m_stackConfiguration;
  82. StatsContainer m_fileLocks[2];
  83. StatsContainer* m_readingFileLocks{ m_fileLocks };
  84. AZStd::atomic<StatsContainer*> m_transferFileLocks{ };
  85. StatsContainer* m_displayingFileLocks{ m_fileLocks + 1};
  86. AZStd::atomic_bool m_stackConfigurationAvailable{ false };
  87. };
  88. } // namespace Streamer