Metrics.cpp 6.6 KB

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