main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "ProfileTimelineWidget.h"
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/ElapsedTimer.h>
  30. #include <LibCore/EventLoop.h>
  31. #include <LibCore/Timer.h>
  32. #include <LibGUI/AboutDialog.h>
  33. #include <LibGUI/Action.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/BoxLayout.h>
  36. #include <LibGUI/Button.h>
  37. #include <LibGUI/Desktop.h>
  38. #include <LibGUI/Label.h>
  39. #include <LibGUI/Menu.h>
  40. #include <LibGUI/MenuBar.h>
  41. #include <LibGUI/MessageBox.h>
  42. #include <LibGUI/Model.h>
  43. #include <LibGUI/ProcessChooser.h>
  44. #include <LibGUI/Splitter.h>
  45. #include <LibGUI/TableView.h>
  46. #include <LibGUI/TreeView.h>
  47. #include <LibGUI/Window.h>
  48. #include <serenity.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. static bool generate_profile(pid_t specified_pid);
  52. int main(int argc, char** argv)
  53. {
  54. Core::ArgsParser args_parser;
  55. int pid = 0;
  56. args_parser.add_option(pid, "PID to profile", "pid", 'p', "PID");
  57. args_parser.parse(argc, argv, false);
  58. auto app = GUI::Application::construct(argc, argv);
  59. auto app_icon = GUI::Icon::default_icon("app-profiler");
  60. const char* path = nullptr;
  61. if (argc != 2) {
  62. if (!generate_profile(pid))
  63. return 0;
  64. path = "/proc/profile";
  65. } else {
  66. path = argv[1];
  67. }
  68. auto profile = Profile::load_from_perfcore_file(path);
  69. if (!profile) {
  70. fprintf(stderr, "Unable to load profile '%s'\n", path);
  71. return 1;
  72. }
  73. auto window = GUI::Window::construct();
  74. window->set_title("Profiler");
  75. window->set_icon(app_icon.bitmap_for_size(16));
  76. window->resize(800, 600);
  77. auto& main_widget = window->set_main_widget<GUI::Widget>();
  78. main_widget.set_fill_with_background_color(true);
  79. main_widget.set_layout<GUI::VerticalBoxLayout>();
  80. main_widget.add<ProfileTimelineWidget>(*profile);
  81. auto& bottom_splitter = main_widget.add<GUI::VerticalSplitter>();
  82. auto& tree_view = bottom_splitter.add<GUI::TreeView>();
  83. tree_view.set_headers_visible(true);
  84. tree_view.set_model(profile->model());
  85. auto& disassembly_view = bottom_splitter.add<GUI::TableView>();
  86. tree_view.on_selection = [&](auto& index) {
  87. profile->set_disassembly_index(index);
  88. disassembly_view.set_model(profile->disassembly_model());
  89. };
  90. auto menubar = GUI::MenuBar::construct();
  91. auto& app_menu = menubar->add_menu("Profiler");
  92. app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
  93. auto& view_menu = menubar->add_menu("View");
  94. auto invert_action = GUI::Action::create_checkable("Invert tree", { Mod_Ctrl, Key_I }, [&](auto& action) {
  95. profile->set_inverted(action.is_checked());
  96. });
  97. invert_action->set_checked(false);
  98. view_menu.add_action(invert_action);
  99. auto percent_action = GUI::Action::create_checkable("Show percentages", { Mod_Ctrl, Key_P }, [&](auto& action) {
  100. profile->set_show_percentages(action.is_checked());
  101. tree_view.update();
  102. disassembly_view.update();
  103. });
  104. percent_action->set_checked(false);
  105. view_menu.add_action(percent_action);
  106. auto& help_menu = menubar->add_menu("Help");
  107. help_menu.add_action(GUI::Action::create("About", [&](auto&) {
  108. GUI::AboutDialog::show("Profiler", app_icon.bitmap_for_size(32), window);
  109. }));
  110. app->set_menubar(move(menubar));
  111. window->show();
  112. return app->exec();
  113. }
  114. static bool prompt_to_stop_profiling()
  115. {
  116. auto window = GUI::Window::construct();
  117. window->set_title("Profiling");
  118. window->resize(320, 200);
  119. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"));
  120. window->center_on_screen();
  121. auto& widget = window->set_main_widget<GUI::Widget>();
  122. widget.set_fill_with_background_color(true);
  123. widget.set_layout<GUI::VerticalBoxLayout>();
  124. auto& timer_label = widget.add<GUI::Label>("...");
  125. Core::ElapsedTimer clock;
  126. clock.start();
  127. auto update_timer = Core::Timer::construct(100, [&] {
  128. timer_label.set_text(String::format("%.1f seconds", (float)clock.elapsed() / 1000.0f));
  129. });
  130. auto& stop_button = widget.add<GUI::Button>("Stop");
  131. stop_button.on_click = [&](auto) {
  132. GUI::Application::the()->quit();
  133. };
  134. window->show();
  135. return GUI::Application::the()->exec() == 0;
  136. }
  137. bool generate_profile(pid_t pid)
  138. {
  139. if (!pid) {
  140. auto process_chooser = GUI::ProcessChooser::construct("Profiler", "Profile", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"));
  141. if (process_chooser->exec() == GUI::Dialog::ExecCancel)
  142. return false;
  143. pid = process_chooser->pid();
  144. }
  145. if (profiling_enable(pid) < 0) {
  146. int saved_errno = errno;
  147. GUI::MessageBox::show(nullptr, String::format("Unable to profile PID %d: %s", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error);
  148. return false;
  149. }
  150. if (!prompt_to_stop_profiling())
  151. return false;
  152. return true;
  153. }