UserspaceEmulator: Use Core::Stream for writing profiling data

This looks like it should compile, but UserspaceEmulator is currently
broken on any non-i686 platform anyways, so I can't test that.
This commit is contained in:
Tim Schumacher 2023-01-20 02:00:41 +01:00 committed by Andreas Kling
parent 173cc5e6e0
commit 9d7606b8de
Notes: sideshowbarker 2024-07-17 03:00:02 +09:00
4 changed files with 25 additions and 30 deletions

View file

@ -9,7 +9,6 @@
#include "MmapRegion.h"
#include "SimpleRegion.h"
#include "SoftCPU.h"
#include <AK/FileStream.h>
#include <AK/Format.h>
#include <AK/LexicalPath.h>
#include <AK/StringUtils.h>
@ -501,7 +500,7 @@ void Emulator::dump_backtrace()
dump_backtrace(raw_backtrace());
}
void Emulator::emit_profile_sample(AK::OutputStream& output)
void Emulator::emit_profile_sample(Core::Stream::Stream& output)
{
if (!is_in_region_of_interest())
return;
@ -511,17 +510,17 @@ void Emulator::emit_profile_sample(AK::OutputStream& output)
builder.appendff(R"~(, {{"type": "sample", "pid": {}, "tid": {}, "timestamp": {}, "lost_samples": 0, "stack": [)~", getpid(), gettid(), tv.tv_sec * 1000 + tv.tv_usec / 1000);
builder.join(',', raw_backtrace());
builder.append("]}\n"sv);
output.write_or_error(builder.string_view().bytes());
output.write_entire_buffer(builder.string_view().bytes()).release_value_but_fixme_should_propagate_errors();
}
void Emulator::emit_profile_event(AK::OutputStream& output, StringView event_name, DeprecatedString const& contents)
void Emulator::emit_profile_event(Core::Stream::Stream& output, StringView event_name, DeprecatedString const& contents)
{
StringBuilder builder;
timeval tv {};
gettimeofday(&tv, nullptr);
builder.appendff(R"~(, {{"type": "{}", "pid": {}, "tid": {}, "timestamp": {}, "lost_samples": 0, "stack": [], {}}})~", event_name, getpid(), gettid(), tv.tv_sec * 1000 + tv.tv_usec / 1000, contents);
builder.append('\n');
output.write_or_error(builder.string_view().bytes());
output.write_entire_buffer(builder.string_view().bytes()).release_value_but_fixme_should_propagate_errors();
}
DeprecatedString Emulator::create_instruction_line(FlatPtr address, X86::Instruction const& insn)

View file

@ -11,7 +11,6 @@
#include "RangeAllocator.h"
#include "Report.h"
#include "SoftMMU.h"
#include <AK/FileStream.h>
#include <AK/Types.h>
#include <LibCore/MappedFile.h>
#include <LibDebug/DebugInfo.h>
@ -33,7 +32,7 @@ public:
Emulator(DeprecatedString const& executable_path, Vector<StringView> const& arguments, Vector<DeprecatedString> const& environment);
void set_profiling_details(bool should_dump_profile, size_t instruction_interval, OutputFileStream* profile_stream, NonnullOwnPtrVector<DeprecatedString>* profiler_strings, Vector<int>* profiler_string_id_map)
void set_profiling_details(bool should_dump_profile, size_t instruction_interval, Core::Stream::Stream* profile_stream, NonnullOwnPtrVector<DeprecatedString>* profiler_strings, Vector<int>* profiler_string_id_map)
{
m_is_profiling = should_dump_profile;
m_profile_instruction_interval = instruction_interval;
@ -47,7 +46,7 @@ public:
m_is_in_region_of_interest = value;
}
OutputFileStream& profile_stream() { return *m_profile_stream; }
Core::Stream::Stream& profile_stream() { return *m_profile_stream; }
NonnullOwnPtrVector<DeprecatedString>& profiler_strings() { return *m_profiler_strings; }
Vector<int>& profiler_string_id_map() { return *m_profiler_string_id_map; }
@ -140,8 +139,8 @@ private:
void send_signal(int);
void emit_profile_sample(AK::OutputStream&);
void emit_profile_event(AK::OutputStream&, StringView event_name, DeprecatedString const& contents);
void emit_profile_sample(Core::Stream::Stream&);
void emit_profile_event(Core::Stream::Stream&, StringView event_name, DeprecatedString const& contents);
int virt$accept4(FlatPtr);
u32 virt$allocate_tls(FlatPtr, size_t);
@ -296,7 +295,7 @@ private:
RangeAllocator m_range_allocator;
OutputFileStream* m_profile_stream { nullptr };
Core::Stream::Stream* m_profile_stream { nullptr };
Vector<int>* m_profiler_string_id_map { nullptr };
NonnullOwnPtrVector<DeprecatedString>* m_profiler_strings { nullptr };

View file

@ -10,7 +10,6 @@
#include "SimpleRegion.h"
#include "SoftCPU.h"
#include <AK/Debug.h>
#include <AK/FileStream.h>
#include <AK/Format.h>
#include <Kernel/API/SyscallString.h>
#include <alloca.h>

View file

@ -5,7 +5,6 @@
*/
#include "Emulator.h"
#include <AK/FileStream.h>
#include <AK/Format.h>
#include <AK/LexicalPath.h>
#include <AK/StringBuilder.h>
@ -24,7 +23,6 @@ int main(int argc, char** argv, char** env)
Vector<StringView> arguments;
bool pause_on_startup { false };
DeprecatedString profile_dump_path;
FILE* profile_output_file { nullptr };
bool enable_roi_mode { false };
bool dump_profile { false };
unsigned profile_instruction_interval { 0 };
@ -58,29 +56,29 @@ int main(int argc, char** argv, char** env)
if (dump_profile && profile_dump_path.is_empty())
profile_dump_path = DeprecatedString::formatted("{}.{}.profile", LexicalPath(executable_path).basename(), getpid());
OwnPtr<OutputFileStream> profile_stream;
OwnPtr<Core::Stream::Stream> profile_stream;
OwnPtr<NonnullOwnPtrVector<DeprecatedString>> profile_strings;
OwnPtr<Vector<int>> profile_string_id_map;
if (dump_profile) {
profile_output_file = fopen(profile_dump_path.characters(), "w+");
if (profile_output_file == nullptr) {
char const* error_string = strerror(errno);
warnln("Failed to open '{}' for writing: {}", profile_dump_path, error_string);
auto profile_stream_or_error = Core::Stream::File::open(profile_dump_path, Core::Stream::OpenMode::Write);
if (profile_stream_or_error.is_error()) {
warnln("Failed to open '{}' for writing: {}", profile_dump_path, profile_stream_or_error.error());
return 1;
}
profile_stream = make<OutputFileStream>(profile_output_file);
profile_stream = profile_stream_or_error.release_value();
profile_strings = make<NonnullOwnPtrVector<DeprecatedString>>();
profile_string_id_map = make<Vector<int>>();
profile_stream->write_or_error(R"({"events":[)"sv.bytes());
profile_stream->write_entire_buffer(R"({"events":[)"sv.bytes()).release_value_but_fixme_should_propagate_errors();
timeval tv {};
gettimeofday(&tv, nullptr);
profile_stream->write_or_error(
DeprecatedString::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)
.bytes());
profile_stream->write_entire_buffer(
DeprecatedString::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)
.bytes())
.release_value_but_fixme_should_propagate_errors();
}
Vector<DeprecatedString> environment;
@ -116,13 +114,13 @@ int main(int argc, char** argv, char** env)
rc = emulator.exec();
if (dump_profile) {
emulator.profile_stream().write_or_error("], \"strings\": ["sv.bytes());
emulator.profile_stream().write_entire_buffer("], \"strings\": ["sv.bytes()).release_value_but_fixme_should_propagate_errors();
if (emulator.profiler_strings().size()) {
for (size_t i = 0; i < emulator.profiler_strings().size() - 1; ++i)
emulator.profile_stream().write_or_error(DeprecatedString::formatted("\"{}\", ", emulator.profiler_strings().at(i)).bytes());
emulator.profile_stream().write_or_error(DeprecatedString::formatted("\"{}\"", emulator.profiler_strings().last()).bytes());
emulator.profile_stream().write_entire_buffer(DeprecatedString::formatted("\"{}\", ", emulator.profiler_strings().at(i)).bytes()).release_value_but_fixme_should_propagate_errors();
emulator.profile_stream().write_entire_buffer(DeprecatedString::formatted("\"{}\"", emulator.profiler_strings().last()).bytes()).release_value_but_fixme_should_propagate_errors();
}
emulator.profile_stream().write_or_error("]}"sv.bytes());
emulator.profile_stream().write_entire_buffer("]}"sv.bytes()).release_value_but_fixme_should_propagate_errors();
}
return rc;
}