2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-12-12 22:01:06 +01:00
|
|
|
#include "Profile.h"
|
2019-12-14 18:44:29 +01:00
|
|
|
#include "ProfileTimelineWidget.h"
|
2020-07-01 21:07:53 +02:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-07-01 20:49:51 +02:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
#include <LibCore/Timer.h>
|
2020-07-28 06:09:19 -04:00
|
|
|
#include <LibGUI/AboutDialog.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
2020-07-01 20:49:51 +02:00
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/Desktop.h>
|
|
|
|
#include <LibGUI/Label.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Menu.h>
|
|
|
|
#include <LibGUI/MenuBar.h>
|
2020-07-01 20:49:51 +02:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibGUI/Model.h>
|
2020-07-28 06:09:19 -04:00
|
|
|
#include <LibGUI/ProcessChooser.h>
|
2020-04-12 10:57:44 +02:00
|
|
|
#include <LibGUI/Splitter.h>
|
2020-04-11 18:46:11 +02:00
|
|
|
#include <LibGUI/TableView.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/TreeView.h>
|
|
|
|
#include <LibGUI/Window.h>
|
2020-07-01 20:49:51 +02:00
|
|
|
#include <serenity.h>
|
2019-12-12 22:01:06 +01:00
|
|
|
#include <stdio.h>
|
2020-07-01 20:49:51 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-07-01 21:07:53 +02:00
|
|
|
static bool generate_profile(pid_t specified_pid);
|
2019-12-12 22:01:06 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-07-01 21:07:53 +02:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
int pid = 0;
|
|
|
|
args_parser.add_option(pid, "PID to profile", "pid", 'p', "PID");
|
|
|
|
args_parser.parse(argc, argv, false);
|
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2020-07-28 06:09:19 -04:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-profiler");
|
2020-07-01 20:49:51 +02:00
|
|
|
|
|
|
|
const char* path = nullptr;
|
2019-12-12 22:01:06 +01:00
|
|
|
if (argc != 2) {
|
2020-07-01 21:07:53 +02:00
|
|
|
if (!generate_profile(pid))
|
2020-07-01 20:49:51 +02:00
|
|
|
return 0;
|
|
|
|
path = "/proc/profile";
|
|
|
|
} else {
|
|
|
|
path = argv[1];
|
2019-12-12 22:01:06 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 10:05:03 +01:00
|
|
|
auto profile = Profile::load_from_perfcore_file(path);
|
2019-12-13 23:38:17 +01:00
|
|
|
|
2019-12-12 22:01:06 +01:00
|
|
|
if (!profile) {
|
2019-12-13 23:38:17 +01:00
|
|
|
fprintf(stderr, "Unable to load profile '%s'\n", path);
|
2019-12-12 22:01:06 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
auto window = GUI::Window::construct();
|
2020-07-01 19:43:17 +02:00
|
|
|
window->set_title("Profiler");
|
2019-12-12 22:01:06 +01:00
|
|
|
window->set_rect(100, 100, 800, 600);
|
2020-07-28 06:09:19 -04:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
2019-12-12 22:01:06 +01:00
|
|
|
|
2020-03-04 09:46:23 +01:00
|
|
|
auto& main_widget = window->set_main_widget<GUI::Widget>();
|
|
|
|
main_widget.set_fill_with_background_color(true);
|
|
|
|
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
2019-12-14 18:44:29 +01:00
|
|
|
|
2020-03-04 19:07:55 +01:00
|
|
|
main_widget.add<ProfileTimelineWidget>(*profile);
|
2019-12-14 18:44:29 +01:00
|
|
|
|
2020-04-12 10:57:44 +02:00
|
|
|
auto& bottom_splitter = main_widget.add<GUI::VerticalSplitter>();
|
2020-04-11 18:46:11 +02:00
|
|
|
|
2020-04-12 10:57:44 +02:00
|
|
|
auto& tree_view = bottom_splitter.add<GUI::TreeView>();
|
2020-03-04 19:07:55 +01:00
|
|
|
tree_view.set_headers_visible(true);
|
|
|
|
tree_view.set_model(profile->model());
|
2019-12-12 22:01:06 +01:00
|
|
|
|
2020-04-12 10:57:44 +02:00
|
|
|
auto& disassembly_view = bottom_splitter.add<GUI::TableView>();
|
2020-04-11 18:46:11 +02:00
|
|
|
|
|
|
|
tree_view.on_selection = [&](auto& index) {
|
|
|
|
profile->set_disassembly_index(index);
|
|
|
|
disassembly_view.set_model(profile->disassembly_model());
|
|
|
|
};
|
|
|
|
|
2020-04-21 16:01:00 +02:00
|
|
|
auto menubar = GUI::MenuBar::construct();
|
2020-07-01 19:43:17 +02:00
|
|
|
auto& app_menu = menubar->add_menu("Profiler");
|
2020-07-04 14:05:19 +02:00
|
|
|
app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
|
2019-12-15 22:55:11 +01:00
|
|
|
|
2020-04-04 12:18:40 +02:00
|
|
|
auto& view_menu = menubar->add_menu("View");
|
2020-04-21 17:19:27 +02:00
|
|
|
auto invert_action = GUI::Action::create_checkable("Invert tree", { Mod_Ctrl, Key_I }, [&](auto& action) {
|
2019-12-16 18:21:05 +01:00
|
|
|
profile->set_inverted(action.is_checked());
|
|
|
|
});
|
2019-12-16 23:46:39 -05:00
|
|
|
invert_action->set_checked(false);
|
2020-04-18 15:31:19 +02:00
|
|
|
view_menu.add_action(invert_action);
|
2019-12-16 18:21:05 +01:00
|
|
|
|
2020-04-21 17:19:27 +02:00
|
|
|
auto percent_action = GUI::Action::create_checkable("Show percentages", { Mod_Ctrl, Key_P }, [&](auto& action) {
|
2020-03-02 21:56:03 +01:00
|
|
|
profile->set_show_percentages(action.is_checked());
|
2020-03-04 19:07:55 +01:00
|
|
|
tree_view.update();
|
2020-04-12 15:22:17 +02:00
|
|
|
disassembly_view.update();
|
2020-03-02 21:56:03 +01:00
|
|
|
});
|
|
|
|
percent_action->set_checked(false);
|
2020-04-04 12:18:40 +02:00
|
|
|
view_menu.add_action(percent_action);
|
2019-12-16 18:21:05 +01:00
|
|
|
|
2020-07-28 06:09:19 -04:00
|
|
|
auto& help_menu = menubar->add_menu("Help");
|
|
|
|
help_menu.add_action(GUI::Action::create("About", [&](auto&) {
|
|
|
|
GUI::AboutDialog::show("Profiler", app_icon.bitmap_for_size(32), window);
|
|
|
|
}));
|
|
|
|
|
2020-07-04 14:05:19 +02:00
|
|
|
app->set_menubar(move(menubar));
|
2019-12-15 22:55:11 +01:00
|
|
|
|
2019-12-12 22:01:06 +01:00
|
|
|
window->show();
|
2020-07-04 14:05:19 +02:00
|
|
|
return app->exec();
|
2019-12-12 22:01:06 +01:00
|
|
|
}
|
2020-07-01 20:49:51 +02:00
|
|
|
|
2020-08-10 23:45:11 +02:00
|
|
|
static bool prompt_to_stop_profiling()
|
2020-07-01 20:49:51 +02:00
|
|
|
{
|
|
|
|
auto window = GUI::Window::construct();
|
|
|
|
window->set_title("Profiling");
|
2020-07-28 06:09:19 -04:00
|
|
|
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"));
|
2020-08-15 16:32:11 +02:00
|
|
|
window->resize(320, 200);
|
|
|
|
window->center_on_screen();
|
2020-07-01 20:49:51 +02:00
|
|
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
|
|
|
widget.set_fill_with_background_color(true);
|
|
|
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
|
|
|
|
auto& timer_label = widget.add<GUI::Label>("...");
|
|
|
|
Core::ElapsedTimer clock;
|
|
|
|
clock.start();
|
|
|
|
auto update_timer = Core::Timer::construct(100, [&] {
|
|
|
|
timer_label.set_text(String::format("%.1f seconds", (float)clock.elapsed() / 1000.0f));
|
|
|
|
});
|
|
|
|
|
|
|
|
auto& stop_button = widget.add<GUI::Button>("Stop");
|
|
|
|
stop_button.on_click = [&](auto) {
|
2020-07-04 16:52:01 +02:00
|
|
|
GUI::Application::the()->quit();
|
2020-07-01 20:49:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
window->show();
|
2020-07-04 16:52:01 +02:00
|
|
|
return GUI::Application::the()->exec() == 0;
|
2020-07-01 20:49:51 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 21:07:53 +02:00
|
|
|
bool generate_profile(pid_t pid)
|
2020-07-01 20:49:51 +02:00
|
|
|
{
|
2020-07-01 21:07:53 +02:00
|
|
|
if (!pid) {
|
2020-07-28 06:09:19 -04:00
|
|
|
auto process_chooser = GUI::ProcessChooser::construct("Profiler", "Profile", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"));
|
2020-07-02 12:12:16 +02:00
|
|
|
if (process_chooser->exec() == GUI::Dialog::ExecCancel)
|
2020-07-01 21:07:53 +02:00
|
|
|
return false;
|
2020-07-02 12:12:16 +02:00
|
|
|
pid = process_chooser->pid();
|
2020-07-01 21:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (profiling_enable(pid) < 0) {
|
|
|
|
int saved_errno = errno;
|
2020-07-15 20:45:11 -06:00
|
|
|
GUI::MessageBox::show(nullptr, String::format("Unable to profile PID %d: %s", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error);
|
2020-07-01 20:49:51 +02:00
|
|
|
return false;
|
2020-07-01 21:07:53 +02:00
|
|
|
}
|
2020-07-01 20:49:51 +02:00
|
|
|
|
|
|
|
if (!prompt_to_stop_profiling())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|