Metrics.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. namespace Atomic
  25. {
  26. class MetricsSnapshot : public RefCounted
  27. {
  28. friend class Metrics;
  29. ATOMIC_REFCOUNTED(MetricsSnapshot)
  30. public:
  31. MetricsSnapshot() {}
  32. String PrintData(unsigned columns = 1, unsigned minCount = 0);
  33. void Clear();
  34. private:
  35. struct InstanceMetric
  36. {
  37. String classname;
  38. unsigned count;
  39. unsigned nativeInstances;
  40. unsigned jsInstances;
  41. unsigned netInstances;
  42. InstanceMetric()
  43. {
  44. count = nativeInstances = jsInstances = netInstances = 0;
  45. }
  46. };
  47. struct NodeMetric
  48. {
  49. String name;
  50. unsigned count;
  51. NodeMetric()
  52. {
  53. count = 0;
  54. }
  55. };
  56. struct ResourceMetric
  57. {
  58. String name;
  59. String group;
  60. unsigned memoryUsage;
  61. ResourceMetric()
  62. {
  63. memoryUsage = 0;
  64. }
  65. };
  66. static bool CompareInstanceMetrics(const InstanceMetric& lhs, const InstanceMetric& rhs);
  67. // StringHash(classname) => InstanceMetrics
  68. HashMap<StringHash, InstanceMetric> instanceMetrics_;
  69. // StringHash(node name) => NodeMetrics
  70. HashMap<StringHash, NodeMetric> nodeMetrics_;
  71. // StringHash(resource name) => ResourceMetrics
  72. HashMap<StringHash, ResourceMetric> resourceMetrics_;
  73. };
  74. /// Metrics subsystem
  75. class ATOMIC_API Metrics : public Object
  76. {
  77. friend class Application;
  78. ATOMIC_OBJECT(Metrics, Object)
  79. public:
  80. /// Construct.
  81. Metrics(Context* context);
  82. /// Destruct.
  83. virtual ~Metrics();
  84. bool Enable();
  85. bool GetEnabled() const { return enabled_; }
  86. void Capture(MetricsSnapshot* snapshot);
  87. private:
  88. void Disable();
  89. void CaptureInstances(MetricsSnapshot* snapshot);
  90. static void OnRefCountedCreated(RefCounted* refCounted);
  91. static void OnRefCountedDeleted(RefCounted* refCounted);
  92. static Metrics* metrics_;
  93. static bool everEnabled_;
  94. bool enabled_;
  95. Vector<RefCounted*> instances_;
  96. };
  97. }