JSMetrics.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  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 <Atomic/Scene/Node.h>
  23. #include "JSVM.h"
  24. #include "JSComponent.h"
  25. #include "JSMetrics.h"
  26. #include <Atomic/Container/Sort.h>
  27. namespace Atomic
  28. {
  29. JSMetrics::JSMetrics(Context* context, JSVM *vm) : Object(context),
  30. vm_(vm)
  31. {
  32. }
  33. JSMetrics::~JSMetrics()
  34. {
  35. }
  36. static bool CompareObjectMetrics(const JSMetrics::ObjectMetric& lhs, const JSMetrics::ObjectMetric& rhs)
  37. {
  38. return lhs.count > rhs.count;
  39. }
  40. static bool CompareNodeMetrics(const JSMetrics::NodeMetric& lhs, const JSMetrics::NodeMetric& rhs)
  41. {
  42. return lhs.count > rhs.count;
  43. }
  44. void JSMetrics::Dump()
  45. {
  46. Vector<ObjectMetric> sorted;
  47. HashMap<StringHash, ObjectMetric>::ConstIterator itr = objectMetrics_.Begin();
  48. while (itr != objectMetrics_.End())
  49. {
  50. sorted.Push(itr->second_);
  51. itr++;
  52. }
  53. Sort(sorted.Begin(), sorted.End(), CompareObjectMetrics);
  54. Vector<ObjectMetric>::ConstIterator vitr = sorted.Begin();
  55. while (vitr != sorted.End())
  56. {
  57. const String& classname = (*vitr).classname;
  58. ATOMIC_LOGINFOF("%s %i", classname.CString(), objectMetrics_[classname].count);
  59. vitr++;
  60. }
  61. }
  62. void JSMetrics::DumpNodes()
  63. {
  64. Vector<NodeMetric> sorted;
  65. HashMap<StringHash, NodeMetric>::ConstIterator itr = nodeMetrics_.Begin();
  66. while (itr != nodeMetrics_.End())
  67. {
  68. sorted.Push(itr->second_);
  69. itr++;
  70. }
  71. Sort(sorted.Begin(), sorted.End(), CompareNodeMetrics);
  72. Vector<NodeMetric>::ConstIterator vitr = sorted.Begin();
  73. while (vitr != sorted.End())
  74. {
  75. const String& nodename = (*vitr).name;
  76. ATOMIC_LOGINFOF("%s %i", nodename.CString(), nodeMetrics_[nodename].count);
  77. vitr++;
  78. }
  79. }
  80. void JSMetrics::DumpJSComponents()
  81. {
  82. Vector<String> jsnames;
  83. HashMap<StringHash, int> jscomponents;
  84. HashMap<void*, RefCounted*>::ConstIterator itr = vm_->heapToObject_.Begin();
  85. while (itr != vm_->heapToObject_.End())
  86. {
  87. String classname = "RefCounted";
  88. if (itr->second_->IsObject())
  89. {
  90. classname = ((Object*) itr->second_)->GetTypeName();
  91. }
  92. /*
  93. if (classname == "JSComponent")
  94. {
  95. JSComponent* jsc = (JSComponent*) itr->second_;
  96. const String& classname = jsc->GetClassName();
  97. if (jsc->GetDestroyed())
  98. {
  99. // this is an error
  100. LOGINFOF("Destroyed: %s Node: %p JSHeapPtr: %p", jsc->GetClassName().CString(), (void*) jsc->GetNode(), (void*) jsc->JSGetHeapPtr());
  101. }
  102. if (!jscomponents.Contains(classname))
  103. {
  104. jsnames.Push(classname);
  105. jscomponents[classname] = 1;
  106. }
  107. else
  108. jscomponents[classname]++;
  109. }
  110. */
  111. itr++;
  112. }
  113. for (unsigned i = 0; i < jsnames.Size(); i++)
  114. {
  115. const String& classname = jsnames[i];
  116. ATOMIC_LOGINFOF("%s %i", classname.CString(), jscomponents[classname]);
  117. }
  118. }
  119. void JSMetrics::Capture()
  120. {
  121. objectMetrics_.Clear();
  122. nodeMetrics_.Clear();
  123. // run twice to call finalizers
  124. // see duktape docs
  125. duk_gc(vm_->GetJSContext(), 0);
  126. duk_gc(vm_->GetJSContext(), 0);
  127. HashMap<void*, RefCounted*>::ConstIterator itr = vm_->heapToObject_.Begin();
  128. while (itr != vm_->heapToObject_.End())
  129. {
  130. String classname = "RefCounted";
  131. if (itr->second_->IsObject())
  132. {
  133. classname = ((Object*) itr->second_)->GetTypeName();
  134. if (classname == "Node")
  135. {
  136. String nodename = ((Node*) itr->second_)->GetName();
  137. if (nodename == String::EMPTY)
  138. {
  139. nodename = String("(Anonymous)");
  140. }
  141. if (!nodeMetrics_.Contains(nodename))
  142. {
  143. JSMetrics::NodeMetric metric;
  144. metric.name = nodename;
  145. metric.count = 1;
  146. nodeMetrics_[nodename] = metric;
  147. }
  148. else
  149. {
  150. nodeMetrics_[nodename].count++;
  151. }
  152. }
  153. }
  154. if (!objectMetrics_.Contains(classname))
  155. {
  156. JSMetrics::ObjectMetric metric;
  157. metric.classname = classname;
  158. metric.count = 1;
  159. objectMetrics_[classname] = metric;
  160. }
  161. else
  162. {
  163. objectMetrics_[classname].count++;
  164. }
  165. itr++;
  166. }
  167. }
  168. }