main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2018-2021, 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 "IndividualSampleModel.h"
  27. #include "Profile.h"
  28. #include "ProfileTimelineWidget.h"
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/ElapsedTimer.h>
  31. #include <LibCore/EventLoop.h>
  32. #include <LibCore/ProcessStatisticsReader.h>
  33. #include <LibCore/Timer.h>
  34. #include <LibDesktop/Launcher.h>
  35. #include <LibGUI/Action.h>
  36. #include <LibGUI/Application.h>
  37. #include <LibGUI/BoxLayout.h>
  38. #include <LibGUI/Button.h>
  39. #include <LibGUI/Label.h>
  40. #include <LibGUI/Menu.h>
  41. #include <LibGUI/Menubar.h>
  42. #include <LibGUI/MessageBox.h>
  43. #include <LibGUI/Model.h>
  44. #include <LibGUI/ProcessChooser.h>
  45. #include <LibGUI/Splitter.h>
  46. #include <LibGUI/TabWidget.h>
  47. #include <LibGUI/TableView.h>
  48. #include <LibGUI/TreeView.h>
  49. #include <LibGUI/Window.h>
  50. #include <serenity.h>
  51. #include <string.h>
  52. static bool generate_profile(pid_t& pid);
  53. int main(int argc, char** argv)
  54. {
  55. int pid = 0;
  56. const char* perfcore_file_arg = nullptr;
  57. Core::ArgsParser args_parser;
  58. args_parser.add_option(pid, "PID to profile", "pid", 'p', "PID");
  59. args_parser.add_positional_argument(perfcore_file_arg, "Path of perfcore file", "perfcore-file", Core::ArgsParser::Required::No);
  60. args_parser.parse(argc, argv);
  61. if (pid && perfcore_file_arg) {
  62. warnln("-p/--pid option and perfcore-file argument must not be used together!");
  63. return 1;
  64. }
  65. auto app = GUI::Application::construct(argc, argv);
  66. auto app_icon = GUI::Icon::default_icon("app-profiler");
  67. String perfcore_file;
  68. if (!perfcore_file_arg) {
  69. if (!generate_profile(pid))
  70. return 0;
  71. perfcore_file = String::formatted("/proc/{}/perf_events", pid);
  72. } else {
  73. perfcore_file = perfcore_file_arg;
  74. }
  75. auto profile_or_error = Profile::load_from_perfcore_file(perfcore_file);
  76. if (profile_or_error.is_error()) {
  77. GUI::MessageBox::show(nullptr, profile_or_error.error(), "Profiler", GUI::MessageBox::Type::Error);
  78. return 0;
  79. }
  80. auto& profile = profile_or_error.value();
  81. auto window = GUI::Window::construct();
  82. if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
  83. "/bin/Help",
  84. { URL::create_with_file_protocol("/usr/share/man/man1/Profiler.md") })
  85. || !Desktop::Launcher::seal_allowlist()) {
  86. warnln("Failed to set up allowed launch URLs");
  87. return 1;
  88. }
  89. window->set_title("Profiler");
  90. window->set_icon(app_icon.bitmap_for_size(16));
  91. window->resize(800, 600);
  92. auto& main_widget = window->set_main_widget<GUI::Widget>();
  93. main_widget.set_fill_with_background_color(true);
  94. main_widget.set_layout<GUI::VerticalBoxLayout>();
  95. main_widget.add<ProfileTimelineWidget>(*profile);
  96. auto& tab_widget = main_widget.add<GUI::TabWidget>();
  97. auto& tree_tab = tab_widget.add_tab<GUI::Widget>("Call Tree");
  98. tree_tab.set_layout<GUI::VerticalBoxLayout>();
  99. tree_tab.layout()->set_margins({ 4, 4, 4, 4 });
  100. auto& bottom_splitter = tree_tab.add<GUI::VerticalSplitter>();
  101. auto& tree_view = bottom_splitter.add<GUI::TreeView>();
  102. tree_view.set_should_fill_selected_rows(true);
  103. tree_view.set_column_headers_visible(true);
  104. tree_view.set_model(profile->model());
  105. auto& disassembly_view = bottom_splitter.add<GUI::TableView>();
  106. tree_view.on_selection = [&](auto& index) {
  107. profile->set_disassembly_index(index);
  108. disassembly_view.set_model(profile->disassembly_model());
  109. };
  110. auto& samples_tab = tab_widget.add_tab<GUI::Widget>("Samples");
  111. samples_tab.set_layout<GUI::VerticalBoxLayout>();
  112. samples_tab.layout()->set_margins({ 4, 4, 4, 4 });
  113. auto& samples_splitter = samples_tab.add<GUI::HorizontalSplitter>();
  114. auto& samples_table_view = samples_splitter.add<GUI::TableView>();
  115. samples_table_view.set_model(profile->samples_model());
  116. auto& individual_sample_view = samples_splitter.add<GUI::TableView>();
  117. samples_table_view.on_selection = [&](const GUI::ModelIndex& index) {
  118. auto model = IndividualSampleModel::create(*profile, index.data(GUI::ModelRole::Custom).to_integer<size_t>());
  119. individual_sample_view.set_model(move(model));
  120. };
  121. auto menubar = GUI::Menubar::construct();
  122. auto& app_menu = menubar->add_menu("&File");
  123. app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
  124. auto& view_menu = menubar->add_menu("&View");
  125. auto invert_action = GUI::Action::create_checkable("&Invert Tree", { Mod_Ctrl, Key_I }, [&](auto& action) {
  126. profile->set_inverted(action.is_checked());
  127. });
  128. invert_action->set_checked(false);
  129. view_menu.add_action(invert_action);
  130. auto top_functions_action = GUI::Action::create_checkable("&Top Functions", { Mod_Ctrl, Key_T }, [&](auto& action) {
  131. profile->set_show_top_functions(action.is_checked());
  132. });
  133. top_functions_action->set_checked(false);
  134. view_menu.add_action(top_functions_action);
  135. auto percent_action = GUI::Action::create_checkable("Show &Percentages", { Mod_Ctrl, Key_P }, [&](auto& action) {
  136. profile->set_show_percentages(action.is_checked());
  137. tree_view.update();
  138. disassembly_view.update();
  139. });
  140. percent_action->set_checked(false);
  141. view_menu.add_action(percent_action);
  142. auto& help_menu = menubar->add_menu("&Help");
  143. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  144. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/Profiler.md"), "/bin/Help");
  145. }));
  146. help_menu.add_action(GUI::CommonActions::make_about_action("Profiler", app_icon, window));
  147. window->set_menubar(move(menubar));
  148. window->show();
  149. return app->exec();
  150. }
  151. static bool prompt_to_stop_profiling(pid_t pid, const String& process_name)
  152. {
  153. auto window = GUI::Window::construct();
  154. window->set_title(String::formatted("Profiling {}({})", process_name, pid));
  155. window->resize(240, 100);
  156. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"));
  157. window->center_on_screen();
  158. auto& widget = window->set_main_widget<GUI::Widget>();
  159. widget.set_fill_with_background_color(true);
  160. auto& layout = widget.set_layout<GUI::VerticalBoxLayout>();
  161. layout.set_margins(GUI::Margins(0, 0, 0, 16));
  162. auto& timer_label = widget.add<GUI::Label>("...");
  163. Core::ElapsedTimer clock;
  164. clock.start();
  165. auto update_timer = Core::Timer::construct(100, [&] {
  166. timer_label.set_text(String::formatted("{:.1} seconds", clock.elapsed() / 1000.0f));
  167. });
  168. auto& stop_button = widget.add<GUI::Button>("Stop");
  169. stop_button.set_fixed_size(140, 22);
  170. stop_button.on_click = [&](auto) {
  171. GUI::Application::the()->quit();
  172. };
  173. window->show();
  174. return GUI::Application::the()->exec() == 0;
  175. }
  176. bool generate_profile(pid_t& pid)
  177. {
  178. if (!pid) {
  179. auto process_chooser = GUI::ProcessChooser::construct("Profiler", "Profile", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"));
  180. if (process_chooser->exec() == GUI::Dialog::ExecCancel)
  181. return false;
  182. pid = process_chooser->pid();
  183. }
  184. String process_name;
  185. auto all_processes = Core::ProcessStatisticsReader::get_all();
  186. if (all_processes.has_value()) {
  187. if (auto it = all_processes.value().find(pid); it != all_processes.value().end())
  188. process_name = it->value.name;
  189. else
  190. process_name = "(unknown)";
  191. } else {
  192. process_name = "(unknown)";
  193. }
  194. if (profiling_enable(pid) < 0) {
  195. int saved_errno = errno;
  196. GUI::MessageBox::show(nullptr, String::formatted("Unable to profile process {}({}): {}", process_name, pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error);
  197. return false;
  198. }
  199. if (!prompt_to_stop_profiling(pid, process_name))
  200. return false;
  201. if (profiling_disable(pid) < 0) {
  202. return false;
  203. }
  204. return true;
  205. }