Profiler: Replace Result<T, E> use with ErrorOr<T>

This commit is contained in:
Andreas Kling 2021-11-07 13:41:23 +01:00
parent e841f3c283
commit fbe8f185b5
Notes: sideshowbarker 2024-07-18 01:23:17 +09:00
3 changed files with 10 additions and 12 deletions

View file

@ -14,6 +14,7 @@
#include <AK/NonnullOwnPtrVector.h>
#include <AK/QuickSort.h>
#include <AK/RefPtr.h>
#include <AK/Try.h>
#include <LibCore/File.h>
#include <LibELF/Image.h>
#include <LibSymbolication/Symbolication.h>
@ -214,15 +215,13 @@ void Profile::rebuild_tree()
Optional<MappedObject> g_kernel_debuginfo_object;
OwnPtr<Debug::DebugInfo> g_kernel_debug_info;
Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const StringView& path)
ErrorOr<NonnullOwnPtr<Profile>> Profile::load_from_perfcore_file(const StringView& path)
{
auto file = Core::File::construct(path);
if (!file->open(Core::OpenMode::ReadOnly))
return String::formatted("Unable to open {}, error: {}", path, file->error_string());
auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
auto json = JsonValue::from_string(file->read_all());
if (!json.has_value() || !json.value().is_object())
return String { "Invalid perfcore format (not a JSON object)" };
return Error::from_string_literal("Invalid perfcore format (not a JSON object)"sv);
auto& object = json.value().as_object();
@ -237,7 +236,7 @@ Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const St
auto strings_value = object.get_ptr("strings"sv);
if (!strings_value || !strings_value->is_array())
return String { "Malformed profile (strings is not an array)" };
return Error::from_string_literal("Malformed profile (strings is not an array)"sv);
HashMap<FlatPtr, String> profile_strings;
for (FlatPtr string_id = 0; string_id < strings_value->as_array().size(); ++string_id) {
@ -247,7 +246,7 @@ Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const St
auto events_value = object.get_ptr("events");
if (!events_value || !events_value->is_array())
return String { "Malformed profile (events is not an array)" };
return Error::from_string_literal("Malformed profile (events is not an array)"sv);
auto& perf_events = events_value->as_array();
@ -419,7 +418,7 @@ Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const St
}
if (events.is_empty())
return String { "No events captured (targeted process was never on CPU)" };
return Error::from_string_literal("No events captured (targeted process was never on CPU)"sv);
quick_sort(all_processes, [](auto& a, auto& b) {
if (a.pid == b.pid)
@ -432,7 +431,7 @@ Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const St
for (auto& it : all_processes)
processes.append(move(it));
return adopt_own(*new Profile(move(processes), move(events)));
return adopt_nonnull_own_or_enomem(new (nothrow) Profile(move(processes), move(events)));
}
void ProfileNode::sort_children()

View file

@ -20,7 +20,6 @@
#include <AK/MappedFile.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/OwnPtr.h>
#include <AK/Result.h>
#include <AK/Variant.h>
#include <LibELF/Image.h>
#include <LibGUI/Forward.h>
@ -142,7 +141,7 @@ struct ProcessFilter {
class Profile {
public:
static Result<NonnullOwnPtr<Profile>, String> load_from_perfcore_file(const StringView& path);
static ErrorOr<NonnullOwnPtr<Profile>> load_from_perfcore_file(const StringView& path);
GUI::Model& model();
GUI::Model& samples_model();

View file

@ -68,7 +68,7 @@ int main(int argc, char** argv)
auto profile_or_error = Profile::load_from_perfcore_file(perfcore_file);
if (profile_or_error.is_error()) {
GUI::MessageBox::show(nullptr, profile_or_error.error(), "Profiler", GUI::MessageBox::Type::Error);
GUI::MessageBox::show(nullptr, String::formatted("{}", profile_or_error.error()), "Profiler", GUI::MessageBox::Type::Error);
return 0;
}