Profile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "Profile.h"
  2. #include "ProfileModel.h"
  3. #include <AK/QuickSort.h>
  4. #include <LibCore/CFile.h>
  5. #include <stdio.h>
  6. static void sort_profile_nodes(Vector<NonnullRefPtr<ProfileNode>>& nodes)
  7. {
  8. quick_sort(nodes.begin(), nodes.end(), [](auto& a, auto& b) {
  9. return a->sample_count() >= b->sample_count();
  10. });
  11. for (auto& child : nodes)
  12. child->sort_children();
  13. }
  14. Profile::Profile(const JsonArray& json)
  15. : m_json(json)
  16. {
  17. m_first_timestamp = m_json.at(0).as_object().get("timestamp").to_number<u64>();
  18. m_last_timestamp = m_json.at(m_json.size() - 1).as_object().get("timestamp").to_number<u64>();
  19. m_model = ProfileModel::create(*this);
  20. m_samples.ensure_capacity(m_json.size());
  21. for (auto& sample_value : m_json.values()) {
  22. auto& sample_object = sample_value.as_object();
  23. Sample sample;
  24. sample.timestamp = sample_object.get("timestamp").to_number<u64>();
  25. auto frames_value = sample_object.get("frames");
  26. auto& frames_array = frames_value.as_array();
  27. if (frames_array.size() < 2)
  28. continue;
  29. sample.in_kernel = frames_array.at(1).as_object().get("address").to_number<u32>() < (8 * MB);
  30. for (int i = frames_array.size() - 1; i >= 1; --i) {
  31. auto& frame_value = frames_array.at(i);
  32. auto& frame_object = frame_value.as_object();
  33. Frame frame;
  34. frame.symbol = frame_object.get("symbol").as_string_or({});
  35. frame.address = frame_object.get("address").as_u32();
  36. frame.offset = frame_object.get("offset").as_u32();
  37. sample.frames.append(move(frame));
  38. };
  39. m_deepest_stack_depth = max((u32)frames_array.size(), m_deepest_stack_depth);
  40. m_samples.append(move(sample));
  41. }
  42. rebuild_tree();
  43. }
  44. Profile::~Profile()
  45. {
  46. }
  47. GModel& Profile::model()
  48. {
  49. return *m_model;
  50. }
  51. void Profile::rebuild_tree()
  52. {
  53. Vector<NonnullRefPtr<ProfileNode>> roots;
  54. auto find_or_create_root = [&roots](const String& symbol, u32 address, u32 offset, u64 timestamp) -> ProfileNode& {
  55. for (int i = 0; i < roots.size(); ++i) {
  56. auto& root = roots[i];
  57. if (root->symbol() == symbol) {
  58. return root;
  59. }
  60. }
  61. auto new_root = ProfileNode::create(symbol, address, offset, timestamp);
  62. roots.append(new_root);
  63. return new_root;
  64. };
  65. for (auto& sample : m_samples) {
  66. if (has_timestamp_filter_range()) {
  67. auto timestamp = sample.timestamp;
  68. if (timestamp < m_timestamp_filter_range_start || timestamp > m_timestamp_filter_range_end)
  69. continue;
  70. }
  71. ProfileNode* node = nullptr;
  72. auto for_each_frame = [&]<typename Callback>(Callback callback)
  73. {
  74. if (!m_inverted) {
  75. for (int i = 0; i < sample.frames.size(); ++i) {
  76. if (callback(sample.frames.at(i)) == IterationDecision::Break)
  77. break;
  78. }
  79. } else {
  80. for (int i = sample.frames.size() - 1; i >= 0; --i) {
  81. if (callback(sample.frames.at(i)) == IterationDecision::Break)
  82. break;
  83. }
  84. }
  85. };
  86. for_each_frame([&](const Frame& frame) {
  87. auto& symbol = frame.symbol;
  88. auto& address = frame.address;
  89. auto& offset = frame.offset;
  90. if (symbol.is_empty())
  91. return IterationDecision::Break;
  92. if (!node)
  93. node = &find_or_create_root(symbol, address, offset, sample.timestamp);
  94. else
  95. node = &node->find_or_create_child(symbol, address, offset, sample.timestamp);
  96. node->increment_sample_count();
  97. return IterationDecision::Continue;
  98. });
  99. }
  100. sort_profile_nodes(roots);
  101. m_roots = move(roots);
  102. m_model->update();
  103. }
  104. OwnPtr<Profile> Profile::load_from_file(const StringView& path)
  105. {
  106. auto file = CFile::construct(path);
  107. if (!file->open(CIODevice::ReadOnly)) {
  108. fprintf(stderr, "Unable to open %s, error: %s\n", String(path).characters(), file->error_string());
  109. return nullptr;
  110. }
  111. auto json = JsonValue::from_string(file->read_all());
  112. if (!json.is_array()) {
  113. fprintf(stderr, "Invalid format (not a JSON array)\n");
  114. return nullptr;
  115. }
  116. auto& samples = json.as_array();
  117. if (samples.is_empty())
  118. return nullptr;
  119. return NonnullOwnPtr<Profile>(NonnullOwnPtr<Profile>::Adopt, *new Profile(move(samples)));
  120. }
  121. void ProfileNode::sort_children()
  122. {
  123. sort_profile_nodes(m_children);
  124. }
  125. void Profile::set_timestamp_filter_range(u64 start, u64 end)
  126. {
  127. if (m_has_timestamp_filter_range && m_timestamp_filter_range_start == start && m_timestamp_filter_range_end == end)
  128. return;
  129. m_has_timestamp_filter_range = true;
  130. m_timestamp_filter_range_start = min(start, end);
  131. m_timestamp_filter_range_end = max(start, end);
  132. rebuild_tree();
  133. }
  134. void Profile::clear_timestamp_filter_range()
  135. {
  136. if (!m_has_timestamp_filter_range)
  137. return;
  138. m_has_timestamp_filter_range = false;
  139. rebuild_tree();
  140. }
  141. void Profile::set_inverted(bool inverted)
  142. {
  143. if (m_inverted == inverted)
  144. return;
  145. m_inverted = inverted;
  146. rebuild_tree();
  147. }