Metrics.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "../Scene/Node.h"
  24. #include "../Script/ScriptComponent.h"
  25. #include "../Metrics/Metrics.h"
  26. #ifdef ATOMIC_PLATFORM_WEB
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #endif
  30. namespace Atomic
  31. {
  32. Metrics* Metrics::metrics_ = 0;
  33. bool Metrics::everEnabled_ = false;
  34. bool MetricsSnapshot::CompareInstanceMetrics(const MetricsSnapshot::InstanceMetric& lhs, const MetricsSnapshot::InstanceMetric& rhs)
  35. {
  36. // TODO: Introduce various sorting modes, for now alphabetical is best "only" option
  37. return lhs.classname < rhs.classname;
  38. /*
  39. if (lhs.count == rhs.count)
  40. return lhs.classname < rhs.classname;
  41. return lhs.count > rhs.count;
  42. */
  43. }
  44. void MetricsSnapshot::Clear()
  45. {
  46. instanceMetrics_.Clear();
  47. nodeMetrics_.Clear();
  48. resourceMetrics_.Clear();
  49. }
  50. void MetricsSnapshot::RegisterInstance(const String& classname, InstantiationType instantiationType, int count)
  51. {
  52. InstanceMetric *metric = &instanceMetrics_[classname];
  53. if (!metric->classname.Length())
  54. metric->classname = classname;
  55. metric->count += count;
  56. switch (instantiationType)
  57. {
  58. case INSTANTIATION_NATIVE:
  59. metric->nativeInstances++;
  60. break;
  61. case INSTANTIATION_JAVASCRIPT:
  62. metric->jsInstances++;
  63. break;
  64. case INSTANTIATION_NET:
  65. metric->netInstances++;
  66. break;
  67. }
  68. }
  69. String MetricsSnapshot::PrintData(unsigned columns, unsigned minCount)
  70. {
  71. String output;
  72. Vector<MetricsSnapshot::InstanceMetric> instanceSort;
  73. HashMap<StringHash, InstanceMetric>::ConstIterator instanceItr = instanceMetrics_.Begin();
  74. while (instanceItr != instanceMetrics_.End())
  75. {
  76. instanceSort.Push(instanceItr->second_);
  77. instanceItr++;
  78. }
  79. Sort(instanceSort.Begin(), instanceSort.End(), CompareInstanceMetrics);
  80. Vector<MetricsSnapshot::InstanceMetric>::ConstIterator instanceSortItr = instanceSort.Begin();
  81. static const int NAME_MAX_LENGTH = 20;
  82. static const int ENTRY_MAX_LENGTH = 128;
  83. char name[NAME_MAX_LENGTH];
  84. char entry[ENTRY_MAX_LENGTH];
  85. String line;
  86. String header = "Class Total ( Native / JS / C# ) ";
  87. for (unsigned i = 0; i < columns; i++)
  88. output += header;
  89. output += "\n\n";
  90. unsigned column = 0;
  91. while (instanceSortItr != instanceSort.End())
  92. {
  93. const InstanceMetric& metric = *instanceSortItr;
  94. if (metric.count < minCount)
  95. continue;
  96. snprintf(name, NAME_MAX_LENGTH, "%-20s", metric.classname.CString());
  97. // We use snprintf here as ToString doesn't seem to cover the unsigned %5u format
  98. snprintf(entry, ENTRY_MAX_LENGTH, "%s : %5u ( %5u, %5u, %5u )", name , metric.count, metric.nativeInstances, metric.jsInstances, metric.netInstances);
  99. if (columns == 1 || column == columns)
  100. {
  101. line += "\n";
  102. output += line;
  103. line.Clear();
  104. column = 0;
  105. }
  106. line += String(entry);
  107. column++;
  108. line += " ";
  109. instanceSortItr++;
  110. }
  111. if (line.Length())
  112. output += line;
  113. return output;
  114. }
  115. Metrics::Metrics(Context* context) :
  116. Object(context),
  117. enabled_(false)
  118. {
  119. Metrics::metrics_ = this;
  120. }
  121. Metrics::~Metrics()
  122. {
  123. Disable();
  124. Metrics::metrics_ = 0;
  125. }
  126. void Metrics::ProcessInstances()
  127. {
  128. const static StringHash scriptComponentType("ScriptComponent");
  129. const static StringHash jsComponentType("JSComponent");
  130. const static StringHash csComponentType("CSComponent");
  131. RefCountedInfo ninfo;
  132. PODVector<RefCounted*>::ConstIterator itr = processInstances_.Begin();
  133. while (itr != processInstances_.End())
  134. {
  135. RefCounted* r = *itr;
  136. Object* o = r->IsObject() ? (Object *)r : 0;
  137. const String& typeName = r->GetTypeName();
  138. ninfo.refCounted = r;
  139. ninfo.instantiationType = r->GetInstantiationType();
  140. ninfo.typeID = StringHash(typeName);
  141. if (!names_.Contains(ninfo.typeID))
  142. {
  143. names_[ninfo.typeID] = typeName;
  144. }
  145. ninfo.typeNameOverride = StringHash::ZERO;
  146. // for script components, setup typeNameOverride
  147. if (o && o->IsInstanceOf(scriptComponentType))
  148. {
  149. const String& classname = ((ScriptComponent*)o)->GetComponentClassName();
  150. if (classname.Length())
  151. {
  152. String name;
  153. if (o->GetType() == jsComponentType)
  154. {
  155. name = classname + " (JS)";
  156. }
  157. else
  158. {
  159. name = classname + " (C#)";
  160. }
  161. ninfo.typeNameOverride = StringHash(name);
  162. if (!names_.Contains(ninfo.typeNameOverride))
  163. {
  164. names_[ninfo.typeNameOverride] = name;
  165. }
  166. }
  167. }
  168. PODVector<RefCountedInfo>& infos = instances_[ninfo.typeID];
  169. RefCountedInfo* info = infos.Buffer();
  170. int count = (int) infos.Size();
  171. while (count > 0)
  172. {
  173. if (!info->refCounted)
  174. {
  175. *info = ninfo;
  176. break;
  177. }
  178. count--;
  179. info++;
  180. }
  181. if (!count)
  182. {
  183. if (infos.Capacity() <= (infos.Size() + 1))
  184. {
  185. infos.Reserve(infos.Size() + 32768);
  186. }
  187. infos.Push(ninfo);
  188. }
  189. itr++;
  190. }
  191. processInstances_.Clear();
  192. }
  193. void Metrics::CaptureInstances(MetricsSnapshot* snapshot)
  194. {
  195. ProcessInstances();
  196. HashMap<StringHash, PODVector<RefCountedInfo>>::ConstIterator itr = instances_.Begin();
  197. while (itr != instances_.End())
  198. {
  199. StringHash typeID = itr->first_;
  200. String* typeName = &names_[typeID];
  201. RefCountedInfo* info = itr->second_.Buffer();
  202. int count = (int) itr->second_.Size();
  203. while (count > 0)
  204. {
  205. // free spot
  206. if (!info->refCounted)
  207. {
  208. info++;
  209. count--;
  210. continue;
  211. }
  212. if (info->typeNameOverride != StringHash::ZERO)
  213. typeName = &names_[info->typeNameOverride];
  214. snapshot->RegisterInstance(*typeName, info->instantiationType);
  215. info++;
  216. count--;
  217. }
  218. itr++;
  219. }
  220. }
  221. void Metrics::Capture(MetricsSnapshot* snapshot)
  222. {
  223. if (!enabled_)
  224. {
  225. ATOMIC_LOGERROR("Metrics::Capture - Metrics subsystem is not enabled");
  226. return;
  227. }
  228. if (!snapshot)
  229. return;
  230. snapshot->Clear();
  231. CaptureInstances(snapshot);
  232. }
  233. bool Metrics::Enable()
  234. {
  235. if (enabled_)
  236. return false;
  237. if (everEnabled_)
  238. {
  239. ATOMIC_LOGERROR("Metrics::Enable - Metrics subsystem is not designed to be restarted");
  240. return false;
  241. }
  242. enabled_ = everEnabled_ = true;
  243. ATOMIC_LOGINFO("Metrics subsystem enabled, performance will be degraded and there may be stutter while instrumenting");
  244. ATOMIC_LOGINFO("IMPORTANT: Do not ship applications with performance metrics enabled");
  245. RefCounted::AddRefCountedCreatedFunction(Metrics::OnRefCountedCreated);
  246. RefCounted::AddRefCountedDeletedFunction(Metrics::OnRefCountedDeleted);
  247. return true;
  248. }
  249. void Metrics::Disable()
  250. {
  251. if (!enabled_)
  252. return;
  253. enabled_ = false;
  254. instances_.Clear();
  255. RefCounted::RemoveRefCountedCreatedFunction(Metrics::OnRefCountedCreated);
  256. RefCounted::RemoveRefCountedDeletedFunction(Metrics::OnRefCountedDeleted);
  257. }
  258. String Metrics::PrintNodeNames() const
  259. {
  260. const static StringHash nodeType("Node");
  261. String output;
  262. PODVector<RefCountedInfo>& infos = instances_[nodeType];
  263. PODVector<RefCountedInfo>::Iterator itr = infos.Begin();
  264. for (; itr != infos.End(); itr++)
  265. {
  266. RefCounted* r = itr->refCounted;
  267. if (!r)
  268. continue;
  269. Object* o = r->IsObject() ? (Object *)r : 0;
  270. if (!o)
  271. continue;
  272. if (o->GetType() == nodeType)
  273. {
  274. const String& name = ((Node*)o)->GetName();
  275. output.AppendWithFormat("Node: %s\n", name.Length() ? name.CString() : "Anonymous Node");
  276. }
  277. }
  278. return output;
  279. }
  280. void Metrics::AddRefCounted(RefCounted* refCounted)
  281. {
  282. // We're called from the RefCounted constructor, so we don't know whether we're an object, etc
  283. if (processInstances_.Capacity() <= (processInstances_.Size() + 1))
  284. {
  285. processInstances_.Reserve(processInstances_.Size() + 32768);
  286. }
  287. processInstances_.Push(refCounted);
  288. }
  289. void Metrics::RemoveRefCounted(RefCounted* refCounted)
  290. {
  291. processInstances_.Remove(refCounted);
  292. // this is called from RefCounted destructor, so can't access refCounted though pointer address is still valid
  293. // we also don't want to hash every RefCounted ptr due to possible collisions, so need to search:
  294. HashMap<StringHash, PODVector<RefCountedInfo>>::Iterator itr = instances_.Begin();
  295. while (itr != instances_.End())
  296. {
  297. RefCountedInfo* info = itr->second_.Buffer();
  298. int count = (int)itr->second_.Size();
  299. while (count > 0)
  300. {
  301. if (info->refCounted == refCounted)
  302. {
  303. info->refCounted = 0;
  304. break;
  305. }
  306. info++;
  307. count--;
  308. }
  309. if (count)
  310. break;
  311. itr++;
  312. }
  313. }
  314. void Metrics::OnRefCountedCreated(RefCounted* refCounted)
  315. {
  316. if (!metrics_)
  317. {
  318. ATOMIC_LOGERROR("Metrics::OnRefCountedCreated - null instance");
  319. return;
  320. }
  321. metrics_->AddRefCounted(refCounted);
  322. }
  323. void Metrics::OnRefCountedDeleted(RefCounted* refCounted)
  324. {
  325. if (!metrics_)
  326. {
  327. ATOMIC_LOGERROR("Metrics::OnRefCountedDeleted - null instance");
  328. return;
  329. }
  330. metrics_->RemoveRefCounted(refCounted);
  331. }
  332. }