2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
#include "TimelineTrack.h"
|
2019-12-14 18:44:29 +01:00
|
|
|
#include "Profile.h"
|
2021-05-06 18:59:51 +02:00
|
|
|
#include "TimelineView.h"
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Painter.h>
|
2021-02-06 14:36:11 +11:00
|
|
|
#include <LibGfx/Font.h>
|
2021-05-06 18:44:31 +02:00
|
|
|
#include <LibGfx/Palette.h>
|
2019-12-14 18:44:29 +01:00
|
|
|
|
2021-05-04 17:45:02 +02:00
|
|
|
namespace Profiler {
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
TimelineTrack::TimelineTrack(TimelineView& view, Profile& profile, Process const& process)
|
2021-05-06 18:59:51 +02:00
|
|
|
: m_view(view)
|
|
|
|
, m_profile(profile)
|
2021-05-06 18:44:31 +02:00
|
|
|
, m_process(process)
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
|
|
|
set_fill_with_background_color(true);
|
2021-05-06 18:46:20 +02:00
|
|
|
set_background_role(Gfx::ColorRole::Base);
|
2021-05-06 18:44:31 +02:00
|
|
|
set_fixed_height(40);
|
|
|
|
set_fixed_width(m_profile.length_in_ms() / 10);
|
2021-05-06 18:46:20 +02:00
|
|
|
set_frame_thickness(1);
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
TimelineTrack::~TimelineTrack()
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
void TimelineTrack::paint_event(GUI::PaintEvent& event)
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::Frame::paint_event(event);
|
2019-12-14 18:44:29 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
GUI::Painter painter(*this);
|
2019-12-14 18:44:29 +01:00
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
2021-02-24 20:36:28 +11:00
|
|
|
const u64 start_of_trace = m_profile.first_timestamp();
|
|
|
|
const u64 end_of_trace = start_of_trace + m_profile.length_in_ms();
|
|
|
|
|
|
|
|
const auto clamp_timestamp = [start_of_trace, end_of_trace](u64 timestamp) -> u64 {
|
|
|
|
return min(end_of_trace, max(timestamp, start_of_trace));
|
|
|
|
};
|
|
|
|
|
2019-12-14 18:44:29 +01:00
|
|
|
float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
|
2019-12-15 18:08:20 +01:00
|
|
|
float frame_height = (float)frame_inner_rect().height() / (float)m_profile.deepest_stack_depth();
|
2019-12-14 18:44:29 +01:00
|
|
|
|
2020-03-02 20:02:45 +01:00
|
|
|
for (auto& event : m_profile.events()) {
|
2021-05-06 18:44:31 +02:00
|
|
|
if (event.pid != m_process.pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!m_process.valid_at(event.timestamp))
|
|
|
|
continue;
|
|
|
|
|
2021-02-24 20:36:28 +11:00
|
|
|
u64 t = clamp_timestamp(event.timestamp) - start_of_trace;
|
2019-12-14 18:44:29 +01:00
|
|
|
int x = (int)((float)t * column_width);
|
|
|
|
int cw = max(1, (int)column_width);
|
|
|
|
|
2020-03-02 20:02:45 +01:00
|
|
|
int column_height = frame_inner_rect().height() - (int)((float)event.frames.size() * frame_height);
|
2019-12-15 18:08:20 +01:00
|
|
|
|
2020-03-02 20:02:45 +01:00
|
|
|
bool in_kernel = event.in_kernel;
|
2019-12-14 18:44:29 +01:00
|
|
|
Color color = in_kernel ? Color::from_rgb(0xc25e5a) : Color::from_rgb(0x5a65c2);
|
2021-02-06 14:36:11 +11:00
|
|
|
for (int i = 1; i <= cw; ++i)
|
2019-12-15 18:08:20 +01:00
|
|
|
painter.draw_line({ x + i, frame_thickness() + column_height }, { x + i, height() - frame_thickness() * 2 }, color);
|
2019-12-14 19:25:33 +01:00
|
|
|
}
|
2019-12-14 19:10:12 +01:00
|
|
|
|
2021-05-06 18:59:51 +02:00
|
|
|
u64 normalized_start_time = clamp_timestamp(min(m_view.select_start_time(), m_view.select_end_time()));
|
|
|
|
u64 normalized_end_time = clamp_timestamp(max(m_view.select_start_time(), m_view.select_end_time()));
|
|
|
|
u64 normalized_hover_time = clamp_timestamp(m_view.hover_time());
|
2019-12-14 19:10:12 +01:00
|
|
|
|
2021-02-24 20:36:28 +11:00
|
|
|
int select_start_x = (int)((float)(normalized_start_time - start_of_trace) * column_width);
|
|
|
|
int select_end_x = (int)((float)(normalized_end_time - start_of_trace) * column_width);
|
|
|
|
int select_hover_x = (int)((float)(normalized_hover_time - start_of_trace) * column_width);
|
2019-12-14 19:10:12 +01:00
|
|
|
painter.fill_rect({ select_start_x, frame_thickness(), select_end_x - select_start_x, height() - frame_thickness() * 2 }, Color(0, 0, 0, 60));
|
2021-02-06 14:36:11 +11:00
|
|
|
painter.fill_rect({ select_hover_x, frame_thickness(), 1, height() - frame_thickness() * 2 }, Color::NamedColor::Black);
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
u64 TimelineTrack::timestamp_at_x(int x) const
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2019-12-14 19:10:12 +01:00
|
|
|
float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms();
|
|
|
|
float ms_into_profile = (float)x / column_width;
|
|
|
|
return m_profile.first_timestamp() + (u64)ms_into_profile;
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
void TimelineTrack::mousedown_event(GUI::MouseEvent& event)
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.button() != GUI::MouseButton::Left)
|
2019-12-14 19:10:12 +01:00
|
|
|
return;
|
|
|
|
|
2021-05-06 18:59:51 +02:00
|
|
|
m_view.set_selecting({}, true);
|
|
|
|
m_view.set_select_start_time({}, timestamp_at_x(event.x()));
|
|
|
|
m_view.set_select_end_time({}, m_view.select_start_time());
|
|
|
|
m_profile.set_timestamp_filter_range(m_view.select_start_time(), m_view.select_end_time());
|
2019-12-14 19:10:12 +01:00
|
|
|
update();
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
void TimelineTrack::mousemove_event(GUI::MouseEvent& event)
|
2019-12-14 18:44:29 +01:00
|
|
|
{
|
2021-05-06 18:59:51 +02:00
|
|
|
m_view.set_hover_time({}, timestamp_at_x(event.x()));
|
2021-02-06 14:36:11 +11:00
|
|
|
|
2021-05-06 18:59:51 +02:00
|
|
|
if (m_view.is_selecting()) {
|
|
|
|
m_view.set_select_end_time({}, m_view.hover_time());
|
|
|
|
m_profile.set_timestamp_filter_range(m_view.select_start_time(), m_view.select_end_time());
|
2021-02-06 14:36:11 +11:00
|
|
|
}
|
2019-12-14 19:10:12 +01:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:00:48 +02:00
|
|
|
void TimelineTrack::mouseup_event(GUI::MouseEvent& event)
|
2019-12-14 19:10:12 +01:00
|
|
|
{
|
2020-02-02 15:07:41 +01:00
|
|
|
if (event.button() != GUI::MouseButton::Left)
|
2019-12-14 19:10:12 +01:00
|
|
|
return;
|
|
|
|
|
2021-05-06 18:59:51 +02:00
|
|
|
m_view.set_selecting({}, false);
|
|
|
|
if (m_view.select_start_time() == m_view.select_end_time())
|
2019-12-14 19:10:12 +01:00
|
|
|
m_profile.clear_timestamp_filter_range();
|
2019-12-14 18:44:29 +01:00
|
|
|
}
|
2021-05-04 17:45:02 +02:00
|
|
|
|
|
|
|
}
|