1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 03:58:12 +00:00

ProfileViewer: Color code the instruction rows by sample percentage

We now "heat color" the instruction-level samples so you can get a nice
visual picture of where time is spent in functions. :^)
This commit is contained in:
Andreas Kling 2020-04-11 20:59:55 +02:00
parent a4006e19d7
commit 8dd96ad143
2 changed files with 52 additions and 5 deletions

View file

@ -28,10 +28,28 @@
#include "Profile.h" #include "Profile.h"
#include <AK/MappedFile.h> #include <AK/MappedFile.h>
#include <LibELF/ELFLoader.h> #include <LibELF/ELFLoader.h>
#include <LibGUI/Painter.h>
#include <LibX86/Disassembler.h> #include <LibX86/Disassembler.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
static const Gfx::Bitmap& heat_gradient()
{
static RefPtr<Gfx::Bitmap> bitmap;
if (!bitmap) {
bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 100, 1 });
GUI::Painter painter(*bitmap);
painter.fill_rect_with_gradient(Orientation::Horizontal, bitmap->rect(), Color::from_rgb(0xffc080), Color::from_rgb(0xff3000));
}
return *bitmap;
}
static Color color_for_percent(int percent)
{
ASSERT(percent >= 0 && percent <= 100);
return heat_gradient().get_pixel(percent, 0);
}
DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node) DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
: m_profile(profile) : m_profile(profile)
, m_node(node) , m_node(node)
@ -57,8 +75,9 @@ DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length()); StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
size_t samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0); size_t samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0);
float percent = ((float)samples_at_this_instruction / (float)m_node.event_count()) * 100.0f;
m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction }); m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction, percent });
offset_into_symbol += insn.value().length(); offset_into_symbol += insn.value().length();
} }
@ -97,20 +116,47 @@ GUI::Model::ColumnMetadata DisassemblyModel::column_metadata(int column) const
return {}; return {};
} }
struct ColorPair {
Color background;
Color foreground;
};
static Optional<ColorPair> color_pair_for(const InstructionData& insn)
{
if (insn.percent == 0)
return {};
Color background = color_for_percent(insn.percent);
Color foreground;
if (insn.percent > 50)
foreground = Color::White;
else
foreground = Color::Black;
return ColorPair { background, foreground };
}
GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, Role role) const GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, Role role) const
{ {
auto& insn = m_instructions[index.row()]; auto& insn = m_instructions[index.row()];
if (role == Role::BackgroundColor) { if (role == Role::BackgroundColor) {
if (insn.event_count > 0) auto colors = color_pair_for(insn);
return Color(Color::Yellow); if (!colors.has_value())
return {}; return {};
return colors.value().background;
}
if (role == Role::ForegroundColor) {
auto colors = color_pair_for(insn);
if (!colors.has_value())
return {};
return colors.value().foreground;
} }
if (role == Role::Display) { if (role == Role::Display) {
if (index.column() == Column::SampleCount) { if (index.column() == Column::SampleCount) {
if (m_profile.show_percentages()) if (m_profile.show_percentages())
return ((float)insn.event_count / (float)m_profile.filtered_event_count()) * 100.0f; return ((float)insn.event_count / (float)m_node.event_count()) * 100.0f;
return insn.event_count; return insn.event_count;
} }
if (index.column() == Column::Address) if (index.column() == Column::Address)

View file

@ -38,6 +38,7 @@ struct InstructionData {
StringView bytes; StringView bytes;
FlatPtr address { 0 }; FlatPtr address { 0 };
u32 event_count { 0 }; u32 event_count { 0 };
float percent { 0 };
}; };
class DisassemblyModel final : public GUI::Model { class DisassemblyModel final : public GUI::Model {