diff --git a/Userland/DevTools/Profiler/DisassemblyModel.cpp b/Userland/DevTools/Profiler/DisassemblyModel.cpp index ee085cfc63..5d3c1303bc 100644 --- a/Userland/DevTools/Profiler/DisassemblyModel.cpp +++ b/Userland/DevTools/Profiler/DisassemblyModel.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2023, Jelle Raaijmakers * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause @@ -7,6 +8,7 @@ #include "DisassemblyModel.h" #include "Gradient.h" +#include "PercentageFormatting.h" #include "Profile.h" #include #include @@ -185,10 +187,15 @@ GUI::Variant DisassemblyModel::data(GUI::ModelIndex const& index, GUI::ModelRole return colors.value().foreground; } + if (role == GUI::ModelRole::TextAlignment) { + if (index.column() == Column::SampleCount) + return Gfx::TextAlignment::CenterRight; + } + if (role == GUI::ModelRole::Display) { if (index.column() == Column::SampleCount) { if (m_profile.show_percentages()) - return ((float)insn.event_count / (float)m_node.event_count()) * 100.0f; + return format_percentage(insn.event_count, m_node.event_count()); return insn.event_count; } diff --git a/Userland/DevTools/Profiler/PercentageFormatting.h b/Userland/DevTools/Profiler/PercentageFormatting.h new file mode 100644 index 0000000000..cf6bdc383b --- /dev/null +++ b/Userland/DevTools/Profiler/PercentageFormatting.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Profiler { + +// Number of digits after the decimal point for sample percentages. +static constexpr int const number_of_percent_digits = 2; +static constexpr int const percent_digits_rounding = AK::pow(10, number_of_percent_digits); + +DeprecatedString format_percentage(auto value, auto total) +{ + auto percentage_full_precision = round_to(value * 100.f / total * percent_digits_rounding); + return DeprecatedString::formatted( + "{}.{:02}", + percentage_full_precision / percent_digits_rounding, + percentage_full_precision % percent_digits_rounding); +}; + +} diff --git a/Userland/DevTools/Profiler/ProfileModel.cpp b/Userland/DevTools/Profiler/ProfileModel.cpp index c4a686d18d..3274443b59 100644 --- a/Userland/DevTools/Profiler/ProfileModel.cpp +++ b/Userland/DevTools/Profiler/ProfileModel.cpp @@ -1,11 +1,13 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2023, Jelle Raaijmakers * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include "ProfileModel.h" +#include "PercentageFormatting.h" #include "Profile.h" #include #include @@ -111,25 +113,14 @@ GUI::Variant ProfileModel::data(GUI::ModelIndex const& index, GUI::ModelRole rol return {}; } if (role == GUI::ModelRole::Display) { - auto round_percentages = [this](auto value) { - auto percentage_full_precision = round_to( - static_cast(value) - * 100.f - / static_cast(m_profile.filtered_event_indices().size()) - * percent_digits_rounding); - return DeprecatedString::formatted( - "{}.{:02}", - percentage_full_precision / percent_digits_rounding, - percentage_full_precision % percent_digits_rounding); - }; if (index.column() == Column::SampleCount) { if (m_profile.show_percentages()) - return round_percentages(node->event_count()); + return format_percentage(node->event_count(), m_profile.filtered_event_indices().size()); return node->event_count(); } if (index.column() == Column::SelfCount) { if (m_profile.show_percentages()) - return round_percentages(node->self_count()); + return format_percentage(node->self_count(), m_profile.filtered_event_indices().size()); return node->self_count(); } if (index.column() == Column::ObjectName) diff --git a/Userland/DevTools/Profiler/ProfileModel.h b/Userland/DevTools/Profiler/ProfileModel.h index 2eb7dc92c4..70ebd1c5c1 100644 --- a/Userland/DevTools/Profiler/ProfileModel.h +++ b/Userland/DevTools/Profiler/ProfileModel.h @@ -14,10 +14,6 @@ namespace Profiler { class Profile; -// Number of digits after the decimal point for sample percentages. -static constexpr int const number_of_percent_digits = 2; -static constexpr int const percent_digits_rounding = AK::pow(10, number_of_percent_digits); - class ProfileModel final : public GUI::Model { public: static NonnullRefPtr create(Profile& profile) diff --git a/Userland/DevTools/Profiler/main.cpp b/Userland/DevTools/Profiler/main.cpp index eda8f043ac..f1c1b84064 100644 --- a/Userland/DevTools/Profiler/main.cpp +++ b/Userland/DevTools/Profiler/main.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2021, Julius Heijmen + * Copyright (c) 2023, Jelle Raaijmakers * * SPDX-License-Identifier: BSD-2-Clause */ @@ -223,10 +224,9 @@ ErrorOr serenity_main(Main::Arguments arguments) }; // FIXME: Make this constexpr once String is able to. - auto const sample_count_percent_format_string = DeprecatedString::formatted("{{:.{}f}}%", number_of_percent_digits); - auto const format_sample_count = [&profile, sample_count_percent_format_string](auto const sample_count) { + auto const format_sample_count = [&profile](auto const sample_count) { if (profile->show_percentages()) - return DeprecatedString::formatted(sample_count_percent_format_string, sample_count.as_float_or(0.0)); + return DeprecatedString::formatted("{}%", sample_count.as_string()); return DeprecatedString::formatted("{} Samples", sample_count.to_i32()); };