JSMetrics.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <Atomic/Container/Sort.h>
  2. #include "JSVM.h"
  3. #include "JSComponent.h"
  4. #include "JSMetrics.h"
  5. namespace Atomic
  6. {
  7. JSMetrics::JSMetrics(Context* context, JSVM *vm) : Object(context),
  8. vm_(vm)
  9. {
  10. }
  11. JSMetrics::~JSMetrics()
  12. {
  13. }
  14. static bool CompareObjectMetrics(const JSMetrics::ObjectMetric& lhs, const JSMetrics::ObjectMetric& rhs)
  15. {
  16. return lhs.count > rhs.count;
  17. }
  18. void JSMetrics::Dump()
  19. {
  20. Vector<ObjectMetric> sorted;
  21. HashMap<StringHash, ObjectMetric>::ConstIterator itr = objectMetrics_.Begin();
  22. while (itr != objectMetrics_.End())
  23. {
  24. sorted.Push(itr->second_);
  25. itr++;
  26. }
  27. Sort(sorted.Begin(), sorted.End(), CompareObjectMetrics);
  28. Vector<ObjectMetric>::ConstIterator vitr = sorted.Begin();
  29. while (vitr != sorted.End())
  30. {
  31. const String& classname = (*vitr).classname;
  32. LOGINFOF("%s %i", classname.CString(), objectMetrics_[classname].count);
  33. vitr++;
  34. }
  35. }
  36. void JSMetrics::DumpJSComponents()
  37. {
  38. Vector<String> jsnames;
  39. HashMap<StringHash, int> jscomponents;
  40. HashMap<void*, RefCounted*>::ConstIterator itr = vm_->heapToObject_.Begin();
  41. while (itr != vm_->heapToObject_.End())
  42. {
  43. String classname = "RefCounted";
  44. if (itr->second_->IsObject())
  45. {
  46. classname = ((Object*) itr->second_)->GetTypeName();
  47. }
  48. if (classname == "JSComponent")
  49. {
  50. JSComponent* jsc = (JSComponent*) itr->second_;
  51. const String& classname = jsc->GetClassName();
  52. if (!jscomponents.Contains(classname))
  53. {
  54. jsnames.Push(classname);
  55. jscomponents[classname] = 1;
  56. }
  57. else
  58. jscomponents[classname]++;
  59. }
  60. itr++;
  61. }
  62. for (unsigned i = 0; i < jsnames.Size(); i++)
  63. {
  64. const String& classname = jsnames[i];
  65. LOGINFOF("%s %i", classname.CString(), jscomponents[classname]);
  66. }
  67. }
  68. void JSMetrics::Capture()
  69. {
  70. objectMetrics_.Clear();
  71. // run twice to call finalizers
  72. // see duktape docs
  73. duk_gc(vm_->GetJSContext(), 0);
  74. duk_gc(vm_->GetJSContext(), 0);
  75. HashMap<void*, RefCounted*>::ConstIterator itr = vm_->heapToObject_.Begin();
  76. while (itr != vm_->heapToObject_.End())
  77. {
  78. String classname = "RefCounted";
  79. if (itr->second_->IsObject())
  80. {
  81. classname = ((Object*) itr->second_)->GetTypeName();
  82. }
  83. if (!objectMetrics_.Contains(classname))
  84. {
  85. JSMetrics::ObjectMetric metric;
  86. metric.classname = classname;
  87. metric.count = 1;
  88. objectMetrics_[classname] = metric;
  89. }
  90. else
  91. {
  92. objectMetrics_[classname].count++;
  93. }
  94. itr++;
  95. }
  96. }
  97. }