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

UserspaceEmulator: Move all the profiling details into the Emulator

Them being in the global namespace doesn't have a lot of fans, it seems.
This commit is contained in:
Ali Mohammad Pur 2021-08-08 15:28:07 +04:30 committed by Ali Mohammad Pur
parent 2128ae4ab0
commit 369e3da6a2
4 changed files with 55 additions and 42 deletions

View file

@ -18,10 +18,6 @@
#include <string.h>
bool g_report_to_debug = false;
bool g_in_region_of_interest = true;
bool g_dump_profile = false;
unsigned g_profile_instruction_interval = 0;
Optional<OutputFileStream> g_profile_stream;
int main(int argc, char** argv, char** env)
{
@ -30,13 +26,15 @@ int main(int argc, char** argv, char** env)
String profile_dump_path;
FILE* profile_output_file { nullptr };
bool enable_roi_mode { false };
bool dump_profile { false };
unsigned profile_instruction_interval { 0 };
Core::ArgsParser parser;
parser.set_stop_on_first_non_option(true);
parser.add_option(g_report_to_debug, "Write reports to the debug log", "report-to-debug", 0);
parser.add_option(pause_on_startup, "Pause on startup", "pause", 'p');
parser.add_option(g_dump_profile, "Generate a ProfileViewer-compatible profile", "profile", 0);
parser.add_option(g_profile_instruction_interval, "Set the profile instruction capture interval, 128 by default", "profile-interval", 'i', "#instructions");
parser.add_option(dump_profile, "Generate a ProfileViewer-compatible profile", "profile", 0);
parser.add_option(profile_instruction_interval, "Set the profile instruction capture interval, 128 by default", "profile-interval", 'i', "#instructions");
parser.add_option(profile_dump_path, "File path for profile dump", "profile-file", 0, "path");
parser.add_option(enable_roi_mode, "Enable Region-of-Interest mode for profiling", "roi", 0);
@ -44,11 +42,8 @@ int main(int argc, char** argv, char** env)
parser.parse(argc, argv);
if (g_dump_profile && g_profile_instruction_interval == 0)
g_profile_instruction_interval = 128;
if (enable_roi_mode)
g_in_region_of_interest = false;
if (dump_profile && profile_instruction_interval == 0)
profile_instruction_interval = 128;
String executable_path;
if (arguments[0].contains("/"sv))
@ -60,22 +55,23 @@ int main(int argc, char** argv, char** env)
return 1;
}
if (g_dump_profile && profile_dump_path.is_empty())
if (dump_profile && profile_dump_path.is_empty())
profile_dump_path = String::formatted("{}.{}.profile", executable_path, getpid());
if (g_dump_profile) {
OwnPtr<OutputFileStream> profile_stream;
if (dump_profile) {
profile_output_file = fopen(profile_dump_path.characters(), "w+");
if (profile_output_file == nullptr) {
auto error_string = strerror(errno);
warnln("Failed to open '{}' for writing: {}", profile_dump_path, error_string);
return 1;
}
g_profile_stream = OutputFileStream { profile_output_file };
profile_stream = make<OutputFileStream>(profile_output_file);
g_profile_stream->write_or_error(R"({"events":[)"sv.bytes());
profile_stream->write_or_error(R"({"events":[)"sv.bytes());
timeval tv {};
gettimeofday(&tv, nullptr);
g_profile_stream->write_or_error(
profile_stream->write_or_error(
String::formatted(
R"~({{"type": "process_create", "parent_pid": 1, "executable": "{}", "pid": {}, "tid": {}, "timestamp": {}, "lost_samples": 0, "stack": []}})~",
executable_path, getpid(), gettid(), tv.tv_sec * 1000 + tv.tv_usec / 1000)
@ -89,6 +85,9 @@ int main(int argc, char** argv, char** env)
// FIXME: It might be nice to tear down the emulator properly.
auto& emulator = *new UserspaceEmulator::Emulator(executable_path, arguments, environment);
emulator.set_profiling_details(dump_profile, profile_instruction_interval, profile_stream);
emulator.set_in_region_of_interest(!enable_roi_mode);
if (!emulator.load_elf())
return 1;
@ -110,8 +109,8 @@ int main(int argc, char** argv, char** env)
rc = emulator.exec();
if (g_dump_profile) {
g_profile_stream->write_or_error(R"(]})"sv.bytes());
}
if (dump_profile)
emulator.profile_stream().write_or_error(R"(]})"sv.bytes());
return rc;
}