1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 19:37:34 +00:00

Profiler: Allow specifying a PID to profile with --pid

This commit is contained in:
Andreas Kling 2020-07-01 21:07:53 +02:00
parent 8d52e200ee
commit d4c92bd1b7

View file

@ -27,6 +27,7 @@
#include "Profile.h" #include "Profile.h"
#include "ProfileTimelineWidget.h" #include "ProfileTimelineWidget.h"
#include "RunningProcessesModel.h" #include "RunningProcessesModel.h"
#include <LibCore/ArgsParser.h>
#include <LibCore/ElapsedTimer.h> #include <LibCore/ElapsedTimer.h>
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibCore/Timer.h> #include <LibCore/Timer.h>
@ -49,23 +50,26 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
static bool generate_profile(); static bool generate_profile(pid_t specified_pid);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Core::ArgsParser args_parser;
int pid = 0;
args_parser.add_option(pid, "PID to profile", "pid", 'p', "PID");
args_parser.parse(argc, argv, false);
GUI::Application app(argc, argv); GUI::Application app(argc, argv);
const char* path = nullptr; const char* path = nullptr;
if (argc != 2) { if (argc != 2) {
if (!generate_profile()) if (!generate_profile(pid))
return 0; return 0;
path = "/proc/profile"; path = "/proc/profile";
} else { } else {
path = argv[1]; path = argv[1];
} }
printf("We're here!\n");
auto profile = Profile::load_from_perfcore_file(path); auto profile = Profile::load_from_perfcore_file(path);
if (!profile) { if (!profile) {
@ -121,9 +125,9 @@ int main(int argc, char** argv)
return app.exec(); return app.exec();
} }
bool prompt_for_process_to_profile() pid_t prompt_for_process_to_profile()
{ {
bool success = false; pid_t pid = 0;
auto window = GUI::Window::construct(); auto window = GUI::Window::construct();
window->set_title("Profiler"); window->set_title("Profiler");
Gfx::IntRect window_rect { 0, 0, 480, 360 }; Gfx::IntRect window_rect { 0, 0, 480, 360 };
@ -147,24 +151,18 @@ bool prompt_for_process_to_profile()
} }
auto index = table_view.selection().first(); auto index = table_view.selection().first();
auto pid_as_variant = table_view.model()->data(index, GUI::Model::Role::Custom); auto pid_as_variant = table_view.model()->data(index, GUI::Model::Role::Custom);
auto pid = pid_as_variant.as_i32(); pid = pid_as_variant.as_i32();
if (profiling_enable(pid) < 0) {
int saved_errno = errno;
GUI::MessageBox::show(String::format("Unable to profile PID %d: %s", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
return;
}
success = true;
GUI::Application::the().quit(0); GUI::Application::the().quit(0);
}; };
auto& cancel_button = button_container.add<GUI::Button>("Cancel"); auto& cancel_button = button_container.add<GUI::Button>("Cancel");
cancel_button.on_click = [](auto) { cancel_button.on_click = [](auto) {
GUI::Application::the().quit(1); GUI::Application::the().quit();
}; };
table_view.model()->update(); table_view.model()->update();
window->show(); window->show();
return GUI::Application::the().exec() == 0; GUI::Application::the().exec();
return pid;
} }
bool prompt_to_stop_profiling() bool prompt_to_stop_profiling()
@ -194,10 +192,19 @@ bool prompt_to_stop_profiling()
return GUI::Application::the().exec() == 0; return GUI::Application::the().exec() == 0;
} }
bool generate_profile() bool generate_profile(pid_t pid)
{ {
if (!prompt_for_process_to_profile()) if (!pid) {
pid = prompt_for_process_to_profile();
if (!pid)
return false;
}
if (profiling_enable(pid) < 0) {
int saved_errno = errno;
GUI::MessageBox::show(String::format("Unable to profile PID %d: %s", pid, strerror(saved_errno)), "Profiler", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
return false; return false;
}
if (!prompt_to_stop_profiling()) if (!prompt_to_stop_profiling())
return false; return false;