Profile.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/MappedFile.h>
  29. #include <AK/QuickSort.h>
  30. #include <LibCore/CFile.h>
  31. #include <LibELF/ELFLoader.h>
  32. #include <stdio.h>
  33. static void sort_profile_nodes(Vector<NonnullRefPtr<ProfileNode>>& nodes)
  34. {
  35. quick_sort(nodes.begin(), nodes.end(), [](auto& a, auto& b) {
  36. return a->sample_count() >= b->sample_count();
  37. });
  38. for (auto& child : nodes)
  39. child->sort_children();
  40. }
  41. Profile::Profile(const JsonArray& json)
  42. : m_json(json)
  43. {
  44. m_first_timestamp = m_json.at(0).as_object().get("timestamp").to_number<u64>();
  45. m_last_timestamp = m_json.at(m_json.size() - 1).as_object().get("timestamp").to_number<u64>();
  46. m_model = ProfileModel::create(*this);
  47. m_samples.ensure_capacity(m_json.size());
  48. for (auto& sample_value : m_json.values()) {
  49. auto& sample_object = sample_value.as_object();
  50. Sample sample;
  51. sample.timestamp = sample_object.get("timestamp").to_number<u64>();
  52. auto frames_value = sample_object.get("frames");
  53. auto& frames_array = frames_value.as_array();
  54. if (frames_array.size() < 2)
  55. continue;
  56. u32 innermost_frame_address = frames_array.at(1).as_object().get("address").to_number<u32>();
  57. sample.in_kernel = innermost_frame_address >= 0xc0000000;
  58. for (int i = frames_array.size() - 1; i >= 1; --i) {
  59. auto& frame_value = frames_array.at(i);
  60. auto& frame_object = frame_value.as_object();
  61. Frame frame;
  62. frame.symbol = frame_object.get("symbol").as_string_or({});
  63. frame.address = frame_object.get("address").as_u32();
  64. frame.offset = frame_object.get("offset").as_u32();
  65. sample.frames.append(move(frame));
  66. };
  67. m_deepest_stack_depth = max((u32)frames_array.size(), m_deepest_stack_depth);
  68. m_samples.append(move(sample));
  69. }
  70. rebuild_tree();
  71. }
  72. Profile::~Profile()
  73. {
  74. }
  75. GUI::Model& Profile::model()
  76. {
  77. return *m_model;
  78. }
  79. void Profile::rebuild_tree()
  80. {
  81. Vector<NonnullRefPtr<ProfileNode>> roots;
  82. auto find_or_create_root = [&roots](const String& symbol, u32 address, u32 offset, u64 timestamp) -> ProfileNode& {
  83. for (int i = 0; i < roots.size(); ++i) {
  84. auto& root = roots[i];
  85. if (root->symbol() == symbol) {
  86. return root;
  87. }
  88. }
  89. auto new_root = ProfileNode::create(symbol, address, offset, timestamp);
  90. roots.append(new_root);
  91. return new_root;
  92. };
  93. for (auto& sample : m_samples) {
  94. if (has_timestamp_filter_range()) {
  95. auto timestamp = sample.timestamp;
  96. if (timestamp < m_timestamp_filter_range_start || timestamp > m_timestamp_filter_range_end)
  97. continue;
  98. }
  99. ProfileNode* node = nullptr;
  100. auto for_each_frame = [&]<typename Callback>(Callback callback)
  101. {
  102. if (!m_inverted) {
  103. for (int i = 0; i < sample.frames.size(); ++i) {
  104. if (callback(sample.frames.at(i)) == IterationDecision::Break)
  105. break;
  106. }
  107. } else {
  108. for (int i = sample.frames.size() - 1; i >= 0; --i) {
  109. if (callback(sample.frames.at(i)) == IterationDecision::Break)
  110. break;
  111. }
  112. }
  113. };
  114. for_each_frame([&](const Frame& frame) {
  115. auto& symbol = frame.symbol;
  116. auto& address = frame.address;
  117. auto& offset = frame.offset;
  118. if (symbol.is_empty())
  119. return IterationDecision::Break;
  120. if (!node)
  121. node = &find_or_create_root(symbol, address, offset, sample.timestamp);
  122. else
  123. node = &node->find_or_create_child(symbol, address, offset, sample.timestamp);
  124. node->increment_sample_count();
  125. return IterationDecision::Continue;
  126. });
  127. }
  128. sort_profile_nodes(roots);
  129. m_roots = move(roots);
  130. m_model->update();
  131. }
  132. OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
  133. {
  134. auto file = Core::File::construct(path);
  135. if (!file->open(Core::IODevice::ReadOnly)) {
  136. fprintf(stderr, "Unable to open %s, error: %s\n", String(path).characters(), file->error_string());
  137. return nullptr;
  138. }
  139. auto json = JsonValue::from_string(file->read_all());
  140. if (!json.is_object()) {
  141. fprintf(stderr, "Invalid perfcore format (not a JSON object)\n");
  142. return nullptr;
  143. }
  144. auto& object = json.as_object();
  145. auto executable_path = object.get("executable").to_string();
  146. MappedFile elf_file(executable_path);
  147. if (!elf_file.is_valid()) {
  148. fprintf(stderr, "Unable to open executable '%s' for symbolication.\n", executable_path.characters());
  149. return nullptr;
  150. }
  151. auto elf_loader = make<ELFLoader>(static_cast<const u8*>(elf_file.data()), elf_file.size());
  152. auto events_value = object.get("events");
  153. if (!events_value.is_array())
  154. return nullptr;
  155. auto& perf_events = events_value.as_array();
  156. if (perf_events.is_empty())
  157. return nullptr;
  158. JsonArray profile_events;
  159. for (auto& perf_event_value : perf_events.values()) {
  160. auto& perf_event = perf_event_value.as_object();
  161. JsonObject object;
  162. object.set("timestamp", perf_event.get("timestamp"));
  163. JsonArray frames_array;
  164. auto stack_array = perf_event.get("stack").as_array();
  165. for (auto& frame : stack_array.values()) {
  166. auto ptr = frame.to_number<u32>();
  167. u32 offset = 0;
  168. auto symbol = elf_loader->symbolicate(ptr, &offset);
  169. JsonObject frame_object;
  170. frame_object.set("address", ptr);
  171. frame_object.set("symbol", symbol);
  172. frame_object.set("offset", offset);
  173. frames_array.append(move(frame_object));
  174. }
  175. object.set("frames", move(frames_array));
  176. profile_events.append(move(object));
  177. }
  178. return NonnullOwnPtr<Profile>(NonnullOwnPtr<Profile>::Adopt, *new Profile(move(profile_events)));
  179. }
  180. OwnPtr<Profile> Profile::load_from_file(const StringView& path)
  181. {
  182. auto file = Core::File::construct(path);
  183. if (!file->open(Core::IODevice::ReadOnly)) {
  184. fprintf(stderr, "Unable to open %s, error: %s\n", String(path).characters(), file->error_string());
  185. return nullptr;
  186. }
  187. auto json = JsonValue::from_string(file->read_all());
  188. if (!json.is_array()) {
  189. fprintf(stderr, "Invalid format (not a JSON array)\n");
  190. return nullptr;
  191. }
  192. auto& samples = json.as_array();
  193. if (samples.is_empty())
  194. return nullptr;
  195. return NonnullOwnPtr<Profile>(NonnullOwnPtr<Profile>::Adopt, *new Profile(move(samples)));
  196. }
  197. void ProfileNode::sort_children()
  198. {
  199. sort_profile_nodes(m_children);
  200. }
  201. void Profile::set_timestamp_filter_range(u64 start, u64 end)
  202. {
  203. if (m_has_timestamp_filter_range && m_timestamp_filter_range_start == start && m_timestamp_filter_range_end == end)
  204. return;
  205. m_has_timestamp_filter_range = true;
  206. m_timestamp_filter_range_start = min(start, end);
  207. m_timestamp_filter_range_end = max(start, end);
  208. rebuild_tree();
  209. }
  210. void Profile::clear_timestamp_filter_range()
  211. {
  212. if (!m_has_timestamp_filter_range)
  213. return;
  214. m_has_timestamp_filter_range = false;
  215. rebuild_tree();
  216. }
  217. void Profile::set_inverted(bool inverted)
  218. {
  219. if (m_inverted == inverted)
  220. return;
  221. m_inverted = inverted;
  222. rebuild_tree();
  223. }