MultiplayerStatSystemComponent.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/EBus/ScheduledEvent.h>
  11. #include <AzCore/IO/Streamer/Statistics.h>
  12. #include <AzCore/IO/Streamer/StreamerConfiguration.h>
  13. #include <Multiplayer/MultiplayerStatSystemInterface.h>
  14. namespace Multiplayer
  15. {
  16. //! @class MultiplayerStatSystemComponent
  17. //! Periodically writes the metrics to AZ::EventLogger. See MultiplayerStatSystem.h for documentation.
  18. class MultiplayerStatSystemComponent final
  19. : public AZ::Component
  20. , public IMultiplayerStatSystem
  21. {
  22. public:
  23. AZ_COMPONENT(MultiplayerStatSystemComponent, "{890831db-3ca4-4d8c-a43e-d53d1197044d}");
  24. static void Reflect(AZ::ReflectContext* context);
  25. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  26. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  27. MultiplayerStatSystemComponent();
  28. ~MultiplayerStatSystemComponent() override;
  29. //! AZ::Component overrides.
  30. //! @{
  31. void Activate() override;
  32. void Deactivate() override;
  33. //! @}
  34. //! IMultiplayerStatSystem overrides.
  35. //! @{
  36. void Register() override;
  37. void Unregister() override;
  38. void SetReportPeriod(AZ::TimeMs period) override;
  39. void DeclareStatGroup(int uniqueGroupId, const char* groupName) override;
  40. void DeclareStat(int uniqueGroupId, int uniqueStatId, const char* statName) override;
  41. void SetStat(int uniqueStatId, double value) override;
  42. void IncrementStat(int uniqueStatId) override;
  43. //! @}
  44. private:
  45. void RecordMetrics();
  46. AZ::ScheduledEvent m_metricsEvent{ [this]()
  47. {
  48. RecordMetrics();
  49. },
  50. AZ::Name("MultiplayerStats") };
  51. struct CumulativeAverage
  52. {
  53. using AverageWindowType = AZ::IO::AverageWindow<double, double, AZ::IO::s_statisticsWindowSize>;
  54. AZStd::string m_name;
  55. AverageWindowType m_average;
  56. double m_lastValue = 0;
  57. AZ::u64 m_counterValue = 0; // Used by counters.
  58. };
  59. //! A custom combined data structure for fast iteration and fast insertion.
  60. //! Items can only be added, never removed.
  61. template<typename ID, typename Value>
  62. class MappedArrayWithNonRemovableItems
  63. {
  64. public:
  65. Value* AddNew(ID newId)
  66. {
  67. auto newItem = &m_items.emplace_back();
  68. m_idToItems[newId] = m_items.size() - 1;
  69. return newItem;
  70. }
  71. Value* Find(ID byId)
  72. {
  73. auto valueIterator = m_idToItems.find(byId);
  74. if (valueIterator != m_idToItems.end())
  75. {
  76. return &m_items[valueIterator->second];
  77. }
  78. return nullptr;
  79. }
  80. AZStd::vector<Value> m_items;
  81. AZStd::unordered_map<ID, AZStd::size_t> m_idToItems;
  82. };
  83. struct StatGroup
  84. {
  85. AZStd::string m_name;
  86. // For fast iteration and fast insertion
  87. MappedArrayWithNonRemovableItems<int, CumulativeAverage> m_stats;
  88. };
  89. MappedArrayWithNonRemovableItems<int, StatGroup> m_statGroups;
  90. AZStd::unordered_map<int, int> m_statIdToGroupId;
  91. AZStd::mutex m_access;
  92. };
  93. } // namespace Multiplayer