Profile.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Profile.h"
  27. #include "ProfileModel.h"
  28. #include <AK/QuickSort.h>
  29. #include <LibCore/CFile.h>
  30. #include <stdio.h>
  31. static void sort_profile_nodes(Vector<NonnullRefPtr<ProfileNode>>& nodes)
  32. {
  33. quick_sort(nodes.begin(), nodes.end(), [](auto& a, auto& b) {
  34. return a->sample_count() >= b->sample_count();
  35. });
  36. for (auto& child : nodes)
  37. child->sort_children();
  38. }
  39. Profile::Profile(const JsonArray& json)
  40. : m_json(json)
  41. {
  42. m_first_timestamp = m_json.at(0).as_object().get("timestamp").to_number<u64>();
  43. m_last_timestamp = m_json.at(m_json.size() - 1).as_object().get("timestamp").to_number<u64>();
  44. m_model = ProfileModel::create(*this);
  45. m_samples.ensure_capacity(m_json.size());
  46. for (auto& sample_value : m_json.values()) {
  47. auto& sample_object = sample_value.as_object();
  48. Sample sample;
  49. sample.timestamp = sample_object.get("timestamp").to_number<u64>();
  50. auto frames_value = sample_object.get("frames");
  51. auto& frames_array = frames_value.as_array();
  52. if (frames_array.size() < 2)
  53. continue;
  54. u32 innermost_frame_address = frames_array.at(1).as_object().get("address").to_number<u32>();
  55. sample.in_kernel = innermost_frame_address >= 0xc0000000;
  56. for (int i = frames_array.size() - 1; i >= 1; --i) {
  57. auto& frame_value = frames_array.at(i);
  58. auto& frame_object = frame_value.as_object();
  59. Frame frame;
  60. frame.symbol = frame_object.get("symbol").as_string_or({});
  61. frame.address = frame_object.get("address").as_u32();
  62. frame.offset = frame_object.get("offset").as_u32();
  63. sample.frames.append(move(frame));
  64. };
  65. m_deepest_stack_depth = max((u32)frames_array.size(), m_deepest_stack_depth);
  66. m_samples.append(move(sample));
  67. }
  68. rebuild_tree();
  69. }
  70. Profile::~Profile()
  71. {
  72. }
  73. GModel& Profile::model()
  74. {
  75. return *m_model;
  76. }
  77. void Profile::rebuild_tree()
  78. {
  79. Vector<NonnullRefPtr<ProfileNode>> roots;
  80. auto find_or_create_root = [&roots](const String& symbol, u32 address, u32 offset, u64 timestamp) -> ProfileNode& {
  81. for (int i = 0; i < roots.size(); ++i) {
  82. auto& root = roots[i];
  83. if (root->symbol() == symbol) {
  84. return root;
  85. }
  86. }
  87. auto new_root = ProfileNode::create(symbol, address, offset, timestamp);
  88. roots.append(new_root);
  89. return new_root;
  90. };
  91. for (auto& sample : m_samples) {
  92. if (has_timestamp_filter_range()) {
  93. auto timestamp = sample.timestamp;
  94. if (timestamp < m_timestamp_filter_range_start || timestamp > m_timestamp_filter_range_end)
  95. continue;
  96. }
  97. ProfileNode* node = nullptr;
  98. auto for_each_frame = [&]<typename Callback>(Callback callback)
  99. {
  100. if (!m_inverted) {
  101. for (int i = 0; i < sample.frames.size(); ++i) {
  102. if (callback(sample.frames.at(i)) == IterationDecision::Break)
  103. break;
  104. }
  105. } else {
  106. for (int i = sample.frames.size() - 1; i >= 0; --i) {
  107. if (callback(sample.frames.at(i)) == IterationDecision::Break)
  108. break;
  109. }
  110. }
  111. };
  112. for_each_frame([&](const Frame& frame) {
  113. auto& symbol = frame.symbol;
  114. auto& address = frame.address;
  115. auto& offset = frame.offset;
  116. if (symbol.is_empty())
  117. return IterationDecision::Break;
  118. if (!node)
  119. node = &find_or_create_root(symbol, address, offset, sample.timestamp);
  120. else
  121. node = &node->find_or_create_child(symbol, address, offset, sample.timestamp);
  122. node->increment_sample_count();
  123. return IterationDecision::Continue;
  124. });
  125. }
  126. sort_profile_nodes(roots);
  127. m_roots = move(roots);
  128. m_model->update();
  129. }
  130. OwnPtr<Profile> Profile::load_from_file(const StringView& path)
  131. {
  132. auto file = Core::File::construct(path);
  133. if (!file->open(Core::IODevice::ReadOnly)) {
  134. fprintf(stderr, "Unable to open %s, error: %s\n", String(path).characters(), file->error_string());
  135. return nullptr;
  136. }
  137. auto json = JsonValue::from_string(file->read_all());
  138. if (!json.is_array()) {
  139. fprintf(stderr, "Invalid format (not a JSON array)\n");
  140. return nullptr;
  141. }
  142. auto& samples = json.as_array();
  143. if (samples.is_empty())
  144. return nullptr;
  145. return NonnullOwnPtr<Profile>(NonnullOwnPtr<Profile>::Adopt, *new Profile(move(samples)));
  146. }
  147. void ProfileNode::sort_children()
  148. {
  149. sort_profile_nodes(m_children);
  150. }
  151. void Profile::set_timestamp_filter_range(u64 start, u64 end)
  152. {
  153. if (m_has_timestamp_filter_range && m_timestamp_filter_range_start == start && m_timestamp_filter_range_end == end)
  154. return;
  155. m_has_timestamp_filter_range = true;
  156. m_timestamp_filter_range_start = min(start, end);
  157. m_timestamp_filter_range_end = max(start, end);
  158. rebuild_tree();
  159. }
  160. void Profile::clear_timestamp_filter_range()
  161. {
  162. if (!m_has_timestamp_filter_range)
  163. return;
  164. m_has_timestamp_filter_range = false;
  165. rebuild_tree();
  166. }
  167. void Profile::set_inverted(bool inverted)
  168. {
  169. if (m_inverted == inverted)
  170. return;
  171. m_inverted = inverted;
  172. rebuild_tree();
  173. }