Metrics.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #include "../IO/Log.h"
  23. #include "../Metrics/Metrics.h"
  24. #ifdef ATOMIC_PLATFORM_WEB
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #endif
  28. namespace Atomic
  29. {
  30. Metrics* Metrics::metrics_ = 0;
  31. bool Metrics::everEnabled_ = false;
  32. bool MetricsSnapshot::CompareInstanceMetrics(const MetricsSnapshot::InstanceMetric& lhs, const MetricsSnapshot::InstanceMetric& rhs)
  33. {
  34. // TODO: Introduce various sorting modes, for now alphabetical is best "only" option
  35. return lhs.classname < rhs.classname;
  36. /*
  37. if (lhs.count == rhs.count)
  38. return lhs.classname < rhs.classname;
  39. return lhs.count > rhs.count;
  40. */
  41. }
  42. void MetricsSnapshot::Clear()
  43. {
  44. instanceMetrics_.Clear();
  45. nodeMetrics_.Clear();
  46. resourceMetrics_.Clear();
  47. }
  48. String MetricsSnapshot::PrintData(unsigned columns, unsigned minCount)
  49. {
  50. String output;
  51. Vector<MetricsSnapshot::InstanceMetric> instanceSort;
  52. HashMap<StringHash, InstanceMetric>::ConstIterator instanceItr = instanceMetrics_.Begin();
  53. while (instanceItr != instanceMetrics_.End())
  54. {
  55. instanceSort.Push(instanceItr->second_);
  56. instanceItr++;
  57. }
  58. Sort(instanceSort.Begin(), instanceSort.End(), CompareInstanceMetrics);
  59. Vector<MetricsSnapshot::InstanceMetric>::ConstIterator instanceSortItr = instanceSort.Begin();
  60. static const int NAME_MAX_LENGTH = 20;
  61. static const int ENTRY_MAX_LENGTH = 128;
  62. char name[NAME_MAX_LENGTH];
  63. char entry[ENTRY_MAX_LENGTH];
  64. String line;
  65. String header = "Class Total ( Native / JS / C# ) ";
  66. for (unsigned i = 0; i < columns; i++)
  67. output += header;
  68. output += "\n\n";
  69. unsigned column = 0;
  70. while (instanceSortItr != instanceSort.End())
  71. {
  72. const InstanceMetric& metric = *instanceSortItr;
  73. if (metric.count < minCount)
  74. continue;
  75. snprintf(name, NAME_MAX_LENGTH, "%-20s", metric.classname.CString());
  76. // We use snprintf here as ToString doesn't seem to cover the unsigned %5u format
  77. snprintf(entry, ENTRY_MAX_LENGTH, "%s : %5u ( %5u, %5u, %5u )", name , metric.count, metric.nativeInstances, metric.jsInstances, metric.netInstances);
  78. if (columns == 1 || column == columns)
  79. {
  80. line += "\n";
  81. output += line;
  82. line.Clear();
  83. column = 0;
  84. }
  85. line += String(entry);
  86. column++;
  87. line += " ";
  88. instanceSortItr++;
  89. }
  90. if (line.Length())
  91. output += line;
  92. return output;
  93. }
  94. Metrics::Metrics(Context* context) :
  95. Object(context),
  96. enabled_(false)
  97. {
  98. Metrics::metrics_ = this;
  99. }
  100. Metrics::~Metrics()
  101. {
  102. Disable();
  103. Metrics::metrics_ = 0;
  104. }
  105. void Metrics::CaptureInstances(MetricsSnapshot* snapshot)
  106. {
  107. const String unkClassName("-Unknown Class-");
  108. for (unsigned i = 0; i < instances_.Size(); i++)
  109. {
  110. RefCounted* r = instances_[i];
  111. const String& name = r->GetTypeName();
  112. MetricsSnapshot::InstanceMetric& metric = snapshot->instanceMetrics_[name];
  113. metric.classname = name;
  114. metric.count++;
  115. if (r->GetInstantiationType() == INSTANTIATION_NATIVE)
  116. metric.nativeInstances++;
  117. else if (r->GetInstantiationType() == INSTANTIATION_JAVASCRIPT)
  118. metric.jsInstances++;
  119. else if (r->GetInstantiationType() == INSTANTIATION_NET)
  120. metric.netInstances++;
  121. }
  122. }
  123. void Metrics::Capture(MetricsSnapshot* snapshot)
  124. {
  125. if (!enabled_)
  126. {
  127. ATOMIC_LOGERROR("Metrics::Capture - Metrics subsystem is not enabled");
  128. return;
  129. }
  130. if (!snapshot)
  131. return;
  132. snapshot->Clear();
  133. CaptureInstances(snapshot);
  134. }
  135. bool Metrics::Enable()
  136. {
  137. if (enabled_)
  138. return false;
  139. if (everEnabled_)
  140. {
  141. ATOMIC_LOGERROR("Metrics::Enable - Metrics subsystem is not designed to be restarted");
  142. return false;
  143. }
  144. enabled_ = everEnabled_ = true;
  145. RefCounted::AddRefCountedCreatedFunction(Metrics::OnRefCountedCreated);
  146. RefCounted::AddRefCountedDeletedFunction(Metrics::OnRefCountedDeleted);
  147. return true;
  148. }
  149. void Metrics::Disable()
  150. {
  151. if (!enabled_)
  152. return;
  153. enabled_ = false;
  154. instances_.Clear();
  155. RefCounted::RemoveRefCountedCreatedFunction(Metrics::OnRefCountedCreated);
  156. RefCounted::RemoveRefCountedDeletedFunction(Metrics::OnRefCountedDeleted);
  157. }
  158. void Metrics::OnRefCountedCreated(RefCounted* refCounted)
  159. {
  160. if (!metrics_)
  161. {
  162. ATOMIC_LOGERROR("Metrics::OnRefCountedCreated - null instance");
  163. return;
  164. }
  165. // We're called from the RefCounted constructor, so we don't know whether we're an object, etc
  166. metrics_->instances_.Push(refCounted);
  167. }
  168. void Metrics::OnRefCountedDeleted(RefCounted* refCounted)
  169. {
  170. if (!metrics_)
  171. {
  172. ATOMIC_LOGERROR("Metrics::OnRefCountedDeleted - null instance");
  173. return;
  174. }
  175. Vector<RefCounted*>::Iterator itr = metrics_->instances_.Find(refCounted);
  176. if (itr != metrics_->instances_.End())
  177. {
  178. metrics_->instances_.Erase(itr);
  179. }
  180. }
  181. }