Skip to content

Commit

Permalink
feat: Add profiling support through Tracy Profiler (neovide#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredizzimo authored and falcucci committed Jun 7, 2023
1 parent 81cd0b8 commit 5e7b1f6
Show file tree
Hide file tree
Showing 14 changed files with 479 additions and 21 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ members = ["neovide-derive"]
[features]
default = []
embed-fonts = []
profiling = ["dep:tracy-client-sys"]
gpu_profiling = ["profiling"]

[dependencies]
async-trait = "0.1.53"
Expand Down Expand Up @@ -45,6 +47,7 @@ swash = "0.1.8"
time = "0.3.9"
tokio = { version = "1.25.0", features = ["full"] }
tokio-util = { version = "0.7.4", features = ["compat"] }
tracy-client-sys = { version = "0.19.0", optional = true }
unicode-segmentation = "1.9.0"
which = "4.2.5"
winit = { git = "https://github.com/neovide/winit", branch = "new-keyboard-all" }
Expand Down Expand Up @@ -79,6 +82,11 @@ lto = true
incremental = true
strip = true

[profile.profiling]
inherits = "release"
strip = false
debug = true

[package.metadata.bundle]
name = "Neovide"
identifier = "com.neovide.neovide"
Expand Down
82 changes: 65 additions & 17 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use log::{error, trace};
use crate::{
bridge::{GuiOption, RedrawEvent, WindowAnchor},
event_aggregator::EVENT_AGGREGATOR,
profiling::tracy_zone,
redraw_scheduler::REDRAW_SCHEDULER,
renderer::DrawCommand,
window::WindowCommand,
Expand Down Expand Up @@ -81,18 +82,24 @@ impl Editor {
match command {
EditorCommand::NeovimRedrawEvent(event) => match event {
RedrawEvent::SetTitle { title } => {
tracy_zone!("EditorSetTitle");
EVENT_AGGREGATOR.send(WindowCommand::TitleChanged(title));
}
RedrawEvent::ModeInfoSet { cursor_modes } => {
tracy_zone!("EditorModeInfoSet");
self.mode_list = cursor_modes;
if let Some(current_mode_i) = self.current_mode_index {
if let Some(current_mode) = self.mode_list.get(current_mode_i as usize) {
self.cursor.change_mode(current_mode, &self.defined_styles)
}
}
}
RedrawEvent::OptionSet { gui_option } => self.set_option(gui_option),
RedrawEvent::OptionSet { gui_option } => {
tracy_zone!("EditorOptionSet");
self.set_option(gui_option);
}
RedrawEvent::ModeChange { mode, mode_index } => {
tracy_zone!("ModeChange");
if let Some(cursor_mode) = self.mode_list.get(mode_index as usize) {
self.cursor.change_mode(cursor_mode, &self.defined_styles);
self.current_mode_index = Some(mode_index)
Expand All @@ -104,26 +111,38 @@ impl Editor {
.ok();
}
RedrawEvent::MouseOn => {
tracy_zone!("EditorMouseOn");
EVENT_AGGREGATOR.send(WindowCommand::SetMouseEnabled(true));
}
RedrawEvent::MouseOff => {
tracy_zone!("EditorMouseOff");
EVENT_AGGREGATOR.send(WindowCommand::SetMouseEnabled(false));
}
RedrawEvent::BusyStart => {
tracy_zone!("EditorBusyStart");
trace!("Cursor off");
self.cursor.enabled = false;
}
RedrawEvent::BusyStop => {
tracy_zone!("EditorBusyStop");
trace!("Cursor on");
self.cursor.enabled = true;
}
RedrawEvent::Flush => {
tracy_zone!("EditorFlush");
trace!("Image flushed");
self.send_cursor_info();
self.draw_command_batcher.send_batch();
REDRAW_SCHEDULER.queue_next_frame();
{
trace!("send_batch");
self.draw_command_batcher.send_batch();
}
{
trace!("queue_next_frame");
REDRAW_SCHEDULER.queue_next_frame();
}
}
RedrawEvent::DefaultColorsSet { colors } => {
tracy_zone!("EditorDefaultColorsSet");
self.draw_command_batcher
.queue(DrawCommand::DefaultStyleChanged(Style::new(colors)))
.ok();
Expand All @@ -132,18 +151,23 @@ impl Editor {
REDRAW_SCHEDULER.queue_next_frame();
}
RedrawEvent::HighlightAttributesDefine { id, style } => {
tracy_zone!("EditorHighlightAttributesDefine");
self.defined_styles.insert(id, Arc::new(style));
}
RedrawEvent::CursorGoto {
grid,
column: left,
row: top,
} => self.set_cursor_position(grid, left, top),
} => {
tracy_zone!("EditorCursorGoto");
self.set_cursor_position(grid, left, top);
}
RedrawEvent::Resize {
grid,
width,
height,
} => {
tracy_zone!("EditorResize");
self.resize_window(grid, width, height);
}
RedrawEvent::GridLine {
Expand All @@ -152,19 +176,24 @@ impl Editor {
column_start,
cells,
} => {
tracy_zone!("EditorGridLine");
let defined_styles = &self.defined_styles;
let window = self.windows.get_mut(&grid);
if let Some(window) = window {
window.draw_grid_line(row, column_start, cells, defined_styles);
}
}
RedrawEvent::Clear { grid } => {
tracy_zone!("EditorClear");
let window = self.windows.get_mut(&grid);
if let Some(window) = window {
window.clear();
}
}
RedrawEvent::Destroy { grid } => self.close_window(grid),
RedrawEvent::Destroy { grid } => {
tracy_zone!("EditorDestroy");
self.close_window(grid)
}
RedrawEvent::Scroll {
grid,
top,
Expand All @@ -174,6 +203,7 @@ impl Editor {
rows,
columns,
} => {
tracy_zone!("EditorScroll");
let window = self.windows.get_mut(&grid);
if let Some(window) = window {
window.scroll_region(top, bottom, left, right, rows, columns);
Expand All @@ -185,7 +215,10 @@ impl Editor {
start_column,
width,
height,
} => self.set_window_position(grid, start_column, start_row, width, height),
} => {
tracy_zone!("EditorWindowPosition");
self.set_window_position(grid, start_column, start_row, width, height)
}
RedrawEvent::WindowFloatPosition {
grid,
anchor,
Expand All @@ -194,33 +227,47 @@ impl Editor {
anchor_row: anchor_top,
sort_order,
..
} => self.set_window_float_position(
grid,
anchor_grid,
anchor,
anchor_left,
anchor_top,
sort_order,
),
} => {
tracy_zone!("EditorWindowFloatPosition");
self.set_window_float_position(
grid,
anchor_grid,
anchor,
anchor_left,
anchor_top,
sort_order,
)
}
RedrawEvent::WindowHide { grid } => {
tracy_zone!("EditorWindowHide");
let window = self.windows.get(&grid);
if let Some(window) = window {
window.hide();
}
}
RedrawEvent::WindowClose { grid } => self.close_window(grid),
RedrawEvent::WindowClose { grid } => {
tracy_zone!("EditorWindowClose");
self.close_window(grid)
}
RedrawEvent::MessageSetPosition { grid, row, .. } => {
tracy_zone!("EditorMessageSetPosition");
self.set_message_position(grid, row)
}
RedrawEvent::WindowViewport {
grid,
// Don't send viewport events if they don't have a scroll delta
scroll_delta: Some(scroll_delta),
..
} => self.send_updated_viewport(grid, scroll_delta),
} => {
tracy_zone!("EditorWindowViewport");
self.send_updated_viewport(grid, scroll_delta)
}
_ => {}
},
EditorCommand::RedrawScreen => self.redraw_screen(),
EditorCommand::RedrawScreen => {
tracy_zone!("EditorRedrawScreen");
self.redraw_screen();
}
};
}

Expand Down Expand Up @@ -423,6 +470,7 @@ impl Editor {
}

fn send_cursor_info(&mut self) {
tracy_zone!("send_cursor_info");
let (grid_left, grid_top) = self.cursor.grid_position;
if let Some(window) = self.windows.get(&self.cursor.parent_window_id) {
let (character, style, double_width) = window.get_cursor_grid_cell(grid_left, grid_top);
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod editor;
mod error_handling;
mod event_aggregator;
mod frame;
mod profiling;
mod redraw_scheduler;
mod renderer;
mod running_tracker;
Expand Down Expand Up @@ -57,6 +58,8 @@ pub use running_tracker::*;
#[cfg(target_os = "windows")]
pub use windows_utils::*;

pub use profiling::startup_profiler;

const BACKTRACES_FILE: &str = "neovide_backtraces.log";
const REQUEST_MESSAGE: &str = "This is a bug and we would love for it to be reported to https://github.com/neovide/neovide/issues";

Expand Down Expand Up @@ -139,6 +142,8 @@ fn protected_main() {
// Multiple other parts of the app "queue_next_frame" function to ensure animations continue
// properly or updates to the graphics are pushed to the screen.

startup_profiler();

#[cfg(target_os = "windows")]
windows_attach_to_console();

Expand Down
9 changes: 9 additions & 0 deletions src/profiling/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[cfg(not(feature = "profiling"))]
mod profiling_disabled;
#[cfg(feature = "profiling")]
mod profiling_enabled;

#[cfg(not(feature = "profiling"))]
pub use profiling_disabled::*;
#[cfg(feature = "profiling")]
pub use profiling_enabled::*;
23 changes: 23 additions & 0 deletions src/profiling/profiling_disabled.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[inline(always)]
pub fn startup_profiler() {}

#[inline(always)]
pub fn emit_frame_mark() {}

#[inline(always)]
pub fn tracy_create_gpu_context(_name: &str) {}

#[inline(always)]
pub fn tracy_gpu_collect() {}

macro_rules! tracy_zone {
($name: expr, $color: expr) => {};
($name: expr) => {};
}
macro_rules! tracy_gpu_zone {
($name: expr, $color: expr) => {};
($name: expr) => {};
}

pub(crate) use tracy_gpu_zone;
pub(crate) use tracy_zone;

0 comments on commit 5e7b1f6

Please sign in to comment.