Profile.cpp 9.1 KB

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