Profile.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "DisassemblyModel.h"
  28. #include "ProfileModel.h"
  29. #include <AK/HashTable.h>
  30. #include <AK/MappedFile.h>
  31. #include <AK/QuickSort.h>
  32. #include <AK/RefPtr.h>
  33. #include <LibCore/File.h>
  34. #include <LibELF/Loader.h>
  35. #include <stdio.h>
  36. static void sort_profile_nodes(Vector<NonnullRefPtr<ProfileNode>>& nodes)
  37. {
  38. quick_sort(nodes.begin(), nodes.end(), [](auto& a, auto& b) {
  39. return a->event_count() >= b->event_count();
  40. });
  41. for (auto& child : nodes)
  42. child->sort_children();
  43. }
  44. Profile::Profile(String executable_path, Vector<Event> events)
  45. : m_executable_path(move(executable_path))
  46. , m_events(move(events))
  47. {
  48. m_first_timestamp = m_events.first().timestamp;
  49. m_last_timestamp = m_events.last().timestamp;
  50. m_model = ProfileModel::create(*this);
  51. for (auto& event : m_events) {
  52. m_deepest_stack_depth = max((u32)event.frames.size(), m_deepest_stack_depth);
  53. }
  54. rebuild_tree();
  55. }
  56. Profile::~Profile()
  57. {
  58. }
  59. GUI::Model& Profile::model()
  60. {
  61. return *m_model;
  62. }
  63. void Profile::rebuild_tree()
  64. {
  65. u32 filtered_event_count = 0;
  66. Vector<NonnullRefPtr<ProfileNode>> roots;
  67. auto find_or_create_root = [&roots](const String& symbol, u32 address, u32 offset, u64 timestamp) -> ProfileNode& {
  68. for (size_t i = 0; i < roots.size(); ++i) {
  69. auto& root = roots[i];
  70. if (root->symbol() == symbol) {
  71. return root;
  72. }
  73. }
  74. auto new_root = ProfileNode::create(symbol, address, offset, timestamp);
  75. roots.append(new_root);
  76. return new_root;
  77. };
  78. HashTable<FlatPtr> live_allocations;
  79. for (auto& event : m_events) {
  80. if (has_timestamp_filter_range()) {
  81. auto timestamp = event.timestamp;
  82. if (timestamp < m_timestamp_filter_range_start || timestamp > m_timestamp_filter_range_end)
  83. continue;
  84. }
  85. if (event.type == "malloc")
  86. live_allocations.set(event.ptr);
  87. else if (event.type == "free")
  88. live_allocations.remove(event.ptr);
  89. }
  90. for (auto& event : m_events) {
  91. if (has_timestamp_filter_range()) {
  92. auto timestamp = event.timestamp;
  93. if (timestamp < m_timestamp_filter_range_start || timestamp > m_timestamp_filter_range_end)
  94. continue;
  95. }
  96. if (event.type == "malloc" && !live_allocations.contains(event.ptr))
  97. continue;
  98. if (event.type == "free")
  99. continue;
  100. ProfileNode* node = nullptr;
  101. auto for_each_frame = [&]<typename Callback>(Callback callback)
  102. {
  103. if (!m_inverted) {
  104. for (size_t i = 0; i < event.frames.size(); ++i) {
  105. if (callback(event.frames.at(i), i == event.frames.size() - 1) == IterationDecision::Break)
  106. break;
  107. }
  108. } else {
  109. for (ssize_t i = event.frames.size() - 1; i >= 0; --i) {
  110. if (callback(event.frames.at(i), static_cast<size_t>(i) == event.frames.size() - 1) == IterationDecision::Break)
  111. break;
  112. }
  113. }
  114. };
  115. for_each_frame([&](const Frame& frame, bool is_innermost_frame) {
  116. auto& symbol = frame.symbol;
  117. auto& address = frame.address;
  118. auto& offset = frame.offset;
  119. if (symbol.is_empty())
  120. return IterationDecision::Break;
  121. if (!node)
  122. node = &find_or_create_root(symbol, address, offset, event.timestamp);
  123. else
  124. node = &node->find_or_create_child(symbol, address, offset, event.timestamp);
  125. node->increment_event_count();
  126. if (is_innermost_frame) {
  127. node->add_event_address(address);
  128. node->increment_self_count();
  129. }
  130. return IterationDecision::Continue;
  131. });
  132. ++filtered_event_count;
  133. }
  134. sort_profile_nodes(roots);
  135. m_filtered_event_count = filtered_event_count;
  136. m_roots = move(roots);
  137. m_model->update();
  138. }
  139. OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
  140. {
  141. auto file = Core::File::construct(path);
  142. if (!file->open(Core::IODevice::ReadOnly)) {
  143. fprintf(stderr, "Unable to open %s, error: %s\n", path.to_string().characters(), file->error_string());
  144. return nullptr;
  145. }
  146. auto json = JsonValue::from_string(file->read_all());
  147. if (!json.is_object()) {
  148. fprintf(stderr, "Invalid perfcore format (not a JSON object)\n");
  149. return nullptr;
  150. }
  151. auto& object = json.as_object();
  152. auto executable_path = object.get("executable").to_string();
  153. MappedFile elf_file(executable_path);
  154. if (!elf_file.is_valid()) {
  155. fprintf(stderr, "Unable to open executable '%s' for symbolication.\n", executable_path.characters());
  156. return nullptr;
  157. }
  158. auto elf_loader = ELF::Loader::create(static_cast<const u8*>(elf_file.data()), elf_file.size());
  159. MappedFile kernel_elf_file("/boot/Kernel");
  160. RefPtr<ELF::Loader> kernel_elf_loader;
  161. if (kernel_elf_file.is_valid())
  162. kernel_elf_loader = ELF::Loader::create(static_cast<const u8*>(kernel_elf_file.data()), kernel_elf_file.size());
  163. auto events_value = object.get("events");
  164. if (!events_value.is_array())
  165. return nullptr;
  166. auto& perf_events = events_value.as_array();
  167. if (perf_events.is_empty())
  168. return nullptr;
  169. Vector<Event> events;
  170. for (auto& perf_event_value : perf_events.values()) {
  171. auto& perf_event = perf_event_value.as_object();
  172. Event event;
  173. event.timestamp = perf_event.get("timestamp").to_number<u64>();
  174. event.type = perf_event.get("type").to_string();
  175. if (event.type == "malloc") {
  176. event.ptr = perf_event.get("ptr").to_number<FlatPtr>();
  177. event.size = perf_event.get("size").to_number<size_t>();
  178. } else if (event.type == "free") {
  179. event.ptr = perf_event.get("ptr").to_number<FlatPtr>();
  180. }
  181. auto stack_array = perf_event.get("stack").as_array();
  182. for (ssize_t i = stack_array.values().size() - 1; i >= 0; --i) {
  183. auto& frame = stack_array.at(i);
  184. auto ptr = frame.to_number<u32>();
  185. u32 offset = 0;
  186. String symbol;
  187. if (ptr >= 0xc0000000) {
  188. if (kernel_elf_loader) {
  189. symbol = kernel_elf_loader->symbolicate(ptr, &offset);
  190. } else {
  191. symbol = "??";
  192. }
  193. } else {
  194. symbol = elf_loader->symbolicate(ptr, &offset);
  195. }
  196. event.frames.append({ symbol, ptr, offset });
  197. }
  198. if (event.frames.size() < 2)
  199. continue;
  200. FlatPtr innermost_frame_address = event.frames.at(1).address;
  201. event.in_kernel = innermost_frame_address >= 0xc0000000;
  202. events.append(move(event));
  203. }
  204. return NonnullOwnPtr<Profile>(NonnullOwnPtr<Profile>::Adopt, *new Profile(executable_path, move(events)));
  205. }
  206. void ProfileNode::sort_children()
  207. {
  208. sort_profile_nodes(m_children);
  209. }
  210. void Profile::set_timestamp_filter_range(u64 start, u64 end)
  211. {
  212. if (m_has_timestamp_filter_range && m_timestamp_filter_range_start == start && m_timestamp_filter_range_end == end)
  213. return;
  214. m_has_timestamp_filter_range = true;
  215. m_timestamp_filter_range_start = min(start, end);
  216. m_timestamp_filter_range_end = max(start, end);
  217. rebuild_tree();
  218. }
  219. void Profile::clear_timestamp_filter_range()
  220. {
  221. if (!m_has_timestamp_filter_range)
  222. return;
  223. m_has_timestamp_filter_range = false;
  224. rebuild_tree();
  225. }
  226. void Profile::set_inverted(bool inverted)
  227. {
  228. if (m_inverted == inverted)
  229. return;
  230. m_inverted = inverted;
  231. rebuild_tree();
  232. }
  233. void Profile::set_show_percentages(bool show_percentages)
  234. {
  235. if (m_show_percentages == show_percentages)
  236. return;
  237. m_show_percentages = show_percentages;
  238. }
  239. void Profile::set_disassembly_index(const GUI::ModelIndex& index)
  240. {
  241. if (m_disassembly_index == index)
  242. return;
  243. m_disassembly_index = index;
  244. auto* node = static_cast<ProfileNode*>(index.internal_data());
  245. m_disassembly_model = DisassemblyModel::create(*this, *node);
  246. }
  247. GUI::Model* Profile::disassembly_model()
  248. {
  249. return m_disassembly_model;
  250. }