Metrics.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Object.h"
  24. #include "../Container/List.h"
  25. namespace Atomic
  26. {
  27. class ATOMIC_API MetricsSnapshot : public RefCounted
  28. {
  29. friend class Metrics;
  30. ATOMIC_REFCOUNTED(MetricsSnapshot)
  31. public:
  32. MetricsSnapshot() {}
  33. String PrintData(unsigned columns = 1, unsigned minCount = 0);
  34. void Clear();
  35. /// Register instance(s) of classname in metrics snapshot
  36. void RegisterInstance(const String& classname, InstantiationType instantiationType, int count = 1);
  37. private:
  38. struct InstanceMetric
  39. {
  40. String classname;
  41. unsigned count;
  42. unsigned nativeInstances;
  43. unsigned jsInstances;
  44. unsigned netInstances;
  45. InstanceMetric()
  46. {
  47. count = nativeInstances = jsInstances = netInstances = 0;
  48. }
  49. };
  50. struct NodeMetric
  51. {
  52. String name;
  53. unsigned count;
  54. NodeMetric()
  55. {
  56. count = 0;
  57. }
  58. };
  59. struct ResourceMetric
  60. {
  61. String name;
  62. String group;
  63. unsigned memoryUsage;
  64. ResourceMetric()
  65. {
  66. memoryUsage = 0;
  67. }
  68. };
  69. static bool CompareInstanceMetrics(const InstanceMetric& lhs, const InstanceMetric& rhs);
  70. // StringHash(classname) => InstanceMetrics
  71. HashMap<StringHash, InstanceMetric> instanceMetrics_;
  72. // StringHash(node name) => NodeMetrics
  73. HashMap<StringHash, NodeMetric> nodeMetrics_;
  74. // StringHash(resource name) => ResourceMetrics
  75. HashMap<StringHash, ResourceMetric> resourceMetrics_;
  76. };
  77. /// Metrics subsystem
  78. class ATOMIC_API Metrics : public Object
  79. {
  80. friend class Application;
  81. ATOMIC_OBJECT(Metrics, Object)
  82. public:
  83. /// Construct.
  84. Metrics(Context* context);
  85. /// Destruct.
  86. virtual ~Metrics();
  87. /// Enable the Metrics subsystem and start capturing instance data, expensive on CPU and may cause stuttering while enabled
  88. bool Enable();
  89. /// Get whether the Metrics subsystem is enabled or not
  90. bool GetEnabled() const { return enabled_; }
  91. // Captures a snapshot of metrics data
  92. void Capture(MetricsSnapshot* snapshot);
  93. /// Prints names of registered node instances output string
  94. String PrintNodeNames() const;
  95. private:
  96. // A RefCountedInfo entry, necessary as we need to access instances in RefCounted constructor/destructor
  97. // and info is not available (pure call access violations on abstract virtual functions)
  98. struct RefCountedInfo
  99. {
  100. // pointer to corresponding RefCounted, null means a free entry
  101. RefCounted *refCounted;
  102. // the has of the typename
  103. StringHash typeID;
  104. // for some types, like ScriptComponent it is useful to override the name to the JSComponent/CSComponent
  105. StringHash typeNameOverride;
  106. // the instantiation type of the instance
  107. InstantiationType instantiationType;
  108. };
  109. void Disable();
  110. void CaptureInstances(MetricsSnapshot* snapshot);
  111. void ProcessInstances();
  112. static void OnRefCountedCreated(RefCounted* refCounted);
  113. static void OnRefCountedDeleted(RefCounted* refCounted);
  114. void AddRefCounted(RefCounted* refCounted);
  115. void RemoveRefCounted(RefCounted* refCounted);
  116. // static instance
  117. static Metrics* metrics_;
  118. static bool everEnabled_;
  119. bool enabled_;
  120. // typeid -> information for individual RefCounted
  121. mutable HashMap<StringHash, PODVector<RefCountedInfo>> instances_;
  122. // instances added since last capture, see note on RefCountedInfo as to why necessary
  123. PODVector<RefCounted*> processInstances_;
  124. // Lookup from string hashes to avoid String thrashing in the metrics subsystem with large numbers of instances
  125. HashMap<StringHash, String> names_;
  126. };
  127. }