diff --git a/DevTools/Profiler/CMakeLists.txt b/DevTools/Profiler/CMakeLists.txt index a0d0bd9624..ba6c22ab9e 100644 --- a/DevTools/Profiler/CMakeLists.txt +++ b/DevTools/Profiler/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES DisassemblyModel.cpp main.cpp + ProcessChooser.cpp Profile.cpp ProfileModel.cpp ProfileTimelineWidget.cpp diff --git a/DevTools/Profiler/ProcessChooser.cpp b/DevTools/Profiler/ProcessChooser.cpp new file mode 100644 index 0000000000..b464ef0535 --- /dev/null +++ b/DevTools/Profiler/ProcessChooser.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2020, Andreas Kling + * 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. + */ + +#include "ProcessChooser.h" +#include "RunningProcessesModel.h" +#include +#include +#include +#include +#include +#include + +namespace Profiler { + +ProcessChooser::ProcessChooser(GUI::Window* parent_window) + : Dialog(parent_window) +{ + build(); +} + +void ProcessChooser::build() +{ + set_title("Profiler"); + Gfx::IntRect window_rect { 0, 0, 480, 360 }; + window_rect.center_within(GUI::Desktop::the().rect()); + set_rect(window_rect); + + auto& widget = set_main_widget(); + widget.set_fill_with_background_color(true); + widget.set_layout(); + auto& table_view = widget.add(); + table_view.set_model(GUI::SortingProxyModel::create(Profiler::RunningProcessesModel::create())); + table_view.model()->set_key_column_and_sort_order(Profiler::RunningProcessesModel::Column::PID, GUI::SortOrder::Descending); + auto& button_container = widget.add(); + button_container.set_preferred_size(0, 30); + button_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); + button_container.set_layout(); + auto& profile_button = button_container.add("Profile"); + profile_button.on_click = [&](auto) { + if (table_view.selection().is_empty()) { + GUI::MessageBox::show("No process selected!", "Profiler", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, this); + return; + } + auto index = table_view.selection().first(); + auto pid_as_variant = table_view.model()->data(index, GUI::Model::Role::Custom); + m_pid = pid_as_variant.as_i32(); + done(ExecOK); + }; + auto& cancel_button = button_container.add("Cancel"); + cancel_button.on_click = [this](auto) { + done(ExecCancel); + }; + + table_view.model()->update(); +} + +} diff --git a/DevTools/Profiler/ProcessChooser.h b/DevTools/Profiler/ProcessChooser.h new file mode 100644 index 0000000000..eb5e722db2 --- /dev/null +++ b/DevTools/Profiler/ProcessChooser.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020, Andreas Kling + * 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. + */ + +#pragma once + +#include + +namespace Profiler { + +class ProcessChooser final : public GUI::Dialog { + C_OBJECT(ProcessChooser); + +public: + pid_t pid() const { return m_pid; } + +private: + ProcessChooser(GUI::Window* parent_window = nullptr); + + void build(); + + pid_t m_pid { 0 }; +}; + +} diff --git a/DevTools/Profiler/main.cpp b/DevTools/Profiler/main.cpp index 9185731389..0383bb944f 100644 --- a/DevTools/Profiler/main.cpp +++ b/DevTools/Profiler/main.cpp @@ -24,9 +24,9 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "ProcessChooser.h" #include "Profile.h" #include "ProfileTimelineWidget.h" -#include "RunningProcessesModel.h" #include #include #include @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #include @@ -125,46 +124,6 @@ int main(int argc, char** argv) return app.exec(); } -pid_t prompt_for_process_to_profile() -{ - pid_t pid = 0; - auto window = GUI::Window::construct(); - window->set_title("Profiler"); - Gfx::IntRect window_rect { 0, 0, 480, 360 }; - window_rect.center_within(GUI::Desktop::the().rect()); - window->set_rect(window_rect); - auto& widget = window->set_main_widget(); - widget.set_fill_with_background_color(true); - widget.set_layout(); - auto& table_view = widget.add(); - table_view.set_model(GUI::SortingProxyModel::create(Profiler::RunningProcessesModel::create())); - table_view.model()->set_key_column_and_sort_order(Profiler::RunningProcessesModel::Column::PID, GUI::SortOrder::Descending); - auto& button_container = widget.add(); - button_container.set_preferred_size(0, 30); - button_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); - button_container.set_layout(); - auto& profile_button = button_container.add("Profile"); - profile_button.on_click = [&](auto) { - if (table_view.selection().is_empty()) { - GUI::MessageBox::show("No process selected!", "Profiler", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window); - return; - } - auto index = table_view.selection().first(); - auto pid_as_variant = table_view.model()->data(index, GUI::Model::Role::Custom); - pid = pid_as_variant.as_i32(); - GUI::Application::the().quit(0); - }; - auto& cancel_button = button_container.add("Cancel"); - cancel_button.on_click = [](auto) { - GUI::Application::the().quit(); - }; - - table_view.model()->update(); - window->show(); - GUI::Application::the().exec(); - return pid; -} - bool prompt_to_stop_profiling() { auto window = GUI::Window::construct(); @@ -195,9 +154,10 @@ bool prompt_to_stop_profiling() bool generate_profile(pid_t pid) { if (!pid) { - pid = prompt_for_process_to_profile(); - if (!pid) + auto process_chooser = Profiler::ProcessChooser::construct(); + if (process_chooser->exec() == GUI::Dialog::ExecCancel) return false; + pid = process_chooser->pid(); } if (profiling_enable(pid) < 0) {