Metrics.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. PruneExpiredInstances();
  108. const String unkClassName("-Unknown Class-");
  109. for (unsigned i = 0; i < instances_.Size(); i++)
  110. {
  111. RefCounted* r = instances_[i];
  112. const String& name = r->GetTypeName();
  113. MetricsSnapshot::InstanceMetric& metric = snapshot->instanceMetrics_[name];
  114. metric.classname = name;
  115. metric.count++;
  116. if (r->GetInstantiationType() == INSTANTIATION_NATIVE)
  117. metric.nativeInstances++;
  118. else if (r->GetInstantiationType() == INSTANTIATION_JAVASCRIPT)
  119. metric.jsInstances++;
  120. else if (r->GetInstantiationType() == INSTANTIATION_NET)
  121. metric.netInstances++;
  122. }
  123. }
  124. void Metrics::Capture(MetricsSnapshot* snapshot)
  125. {
  126. if (!enabled_)
  127. {
  128. ATOMIC_LOGERROR("Metrics::Capture - Metrics subsystem is not enabled");
  129. return;
  130. }
  131. if (!snapshot)
  132. return;
  133. snapshot->Clear();
  134. CaptureInstances(snapshot);
  135. }
  136. bool Metrics::Enable()
  137. {
  138. if (enabled_)
  139. return false;
  140. if (everEnabled_)
  141. {
  142. ATOMIC_LOGERROR("Metrics::Enable - Metrics subsystem is not designed to be restarted");
  143. return false;
  144. }
  145. enabled_ = everEnabled_ = true;
  146. RefCounted::AddRefCountedCreatedFunction(Metrics::OnRefCountedCreated);
  147. RefCounted::AddRefCountedDeletedFunction(Metrics::OnRefCountedDeleted);
  148. return true;
  149. }
  150. void Metrics::Disable()
  151. {
  152. if (!enabled_)
  153. return;
  154. enabled_ = false;
  155. instances_.Clear();
  156. RefCounted::RemoveRefCountedCreatedFunction(Metrics::OnRefCountedCreated);
  157. RefCounted::RemoveRefCountedDeletedFunction(Metrics::OnRefCountedDeleted);
  158. }
  159. void Metrics::PruneExpiredInstances()
  160. {
  161. Vector<WeakPtr<RefCounted>> cinstances = instances_;
  162. instances_.Clear();
  163. for (unsigned i = 0; i < cinstances.Size(); i++)
  164. {
  165. if (!cinstances[i].Expired())
  166. {
  167. instances_.Push(cinstances[i]);
  168. }
  169. }
  170. }
  171. void Metrics::OnRefCountedCreated(RefCounted* refCounted)
  172. {
  173. if (!metrics_)
  174. {
  175. ATOMIC_LOGERROR("Metrics::OnRefCountedCreated - null instance");
  176. return;
  177. }
  178. // We're called from the RefCounted constructor, so we don't know whether we're an object, etc
  179. metrics_->instances_.Push(WeakPtr<RefCounted>(refCounted));
  180. // prune expired whenever 8k boundry is crossed
  181. if (!(metrics_->instances_.Size() % 8192))
  182. {
  183. metrics_->PruneExpiredInstances();
  184. }
  185. }
  186. void Metrics::OnRefCountedDeleted(RefCounted* refCounted)
  187. {
  188. if (!metrics_)
  189. {
  190. ATOMIC_LOGERROR("Metrics::OnRefCountedDeleted - null instance");
  191. return;
  192. }
  193. Vector<WeakPtr<RefCounted>>::Iterator itr = metrics_->instances_.Find(WeakPtr<RefCounted>(refCounted));
  194. if (itr != metrics_->instances_.End())
  195. {
  196. metrics_->instances_.Erase(itr);
  197. }
  198. }
  199. }