| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use minidl::Library; |
| 6 |
|
| 7 |
// Must match `___tracy_source_location_data` struct in TracyC.h |
| 8 |
#[repr(C)] |
| 9 |
pub struct SourceLocation { |
| 10 |
pub name: *const u8, |
| 11 |
pub function: *const u8, |
| 12 |
pub file: *const u8, |
| 13 |
pub line: u32, |
| 14 |
pub color: u32, |
| 15 |
} |
| 16 |
|
| 17 |
// Must match `___tracy_c_zone_context` in TracyC.h |
| 18 |
#[repr(C)] |
| 19 |
#[derive(Copy, Clone)] |
| 20 |
pub struct ZoneContext { |
| 21 |
id: u32, |
| 22 |
active: i32, |
| 23 |
} |
| 24 |
|
| 25 |
// Define a set of no-op implementation functions that are called if the tracy |
| 26 |
// shared library is not loaded. |
| 27 |
extern "C" fn unimpl_mark_frame(_: *const u8) {} |
| 28 |
extern "C" fn unimpl_mark_frame_start(_: *const u8) {} |
| 29 |
extern "C" fn unimpl_mark_frame_end(_: *const u8) {} |
| 30 |
extern "C" fn unimpl_zone_begin(_: *const SourceLocation, _: i32) -> ZoneContext { |
| 31 |
ZoneContext { |
| 32 |
id: 0, |
| 33 |
active: 0, |
| 34 |
} |
| 35 |
} |
| 36 |
extern "C" fn unimpl_zone_end(_: ZoneContext) {} |
| 37 |
extern "C" fn unimpl_zone_text(_: ZoneContext, _: *const u8, _: usize) {} |
| 38 |
|
| 39 |
extern "C" fn unimpl_plot(_: *const u8, _: f64) {} |
| 40 |
|
| 41 |
// Function pointers to the tracy API functions (if loaded), or the no-op functions above. |
| 42 |
pub static mut MARK_FRAME: extern "C" fn(name: *const u8) = unimpl_mark_frame; |
| 43 |
pub static mut MARK_FRAME_START: extern "C" fn(name: *const u8) = unimpl_mark_frame_start; |
| 44 |
pub static mut MARK_FRAME_END: extern "C" fn(name: *const u8) = unimpl_mark_frame_end; |
| 45 |
pub static mut EMIT_ZONE_BEGIN: extern "C" fn(srcloc: *const SourceLocation, active: i32) -> ZoneContext = unimpl_zone_begin; |
| 46 |
pub static mut EMIT_ZONE_END: extern "C" fn(ctx: ZoneContext) = unimpl_zone_end; |
| 47 |
pub static mut EMIT_ZONE_TEXT: extern "C" fn(ctx: ZoneContext, txt: *const u8, size: usize) = unimpl_zone_text; |
| 48 |
pub static mut EMIT_PLOT: extern "C" fn(name: *const u8, val: f64) = unimpl_plot; |
| 49 |
|
| 50 |
// Load the tracy library, and get function pointers. This is unsafe since: |
| 51 |
// - It must not be called while other threads are calling the functions. |
| 52 |
// - It doesn't ensure that the library is not unloaded during use. |
| 53 |
pub unsafe fn load(path: &str) -> bool { |
| 54 |
match Library::load(path) { |
| 55 |
Ok(lib) => { |
| 56 |
// If lib loading succeeds, assume we can find all the symbols required. |
| 57 |
MARK_FRAME = lib.sym("___tracy_emit_frame_mark\0").unwrap(); |
| 58 |
MARK_FRAME_START = lib.sym("___tracy_emit_frame_mark_start\0").unwrap(); |
| 59 |
MARK_FRAME_END = lib.sym("___tracy_emit_frame_mark_end\0").unwrap(); |
| 60 |
EMIT_ZONE_BEGIN = lib.sym("___tracy_emit_zone_begin\0").unwrap(); |
| 61 |
EMIT_ZONE_END = lib.sym("___tracy_emit_zone_end\0").unwrap(); |
| 62 |
EMIT_ZONE_TEXT = lib.sym("___tracy_emit_zone_text\0").unwrap(); |
| 63 |
EMIT_PLOT = lib.sym("___tracy_emit_plot\0").unwrap(); |
| 64 |
|
| 65 |
true |
| 66 |
} |
| 67 |
Err(..) => { |
| 68 |
println!("Failed to load the tracy profiling library!"); |
| 69 |
|
| 70 |
false |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/// A simple stack scope for tracing function execution time |
| 76 |
pub struct ProfileScope { |
| 77 |
ctx: ZoneContext, |
| 78 |
} |
| 79 |
|
| 80 |
impl ProfileScope { |
| 81 |
pub fn new(callsite: &'static SourceLocation) -> Self { |
| 82 |
let ctx = unsafe { EMIT_ZONE_BEGIN(callsite, 1) }; |
| 83 |
|
| 84 |
ProfileScope { |
| 85 |
ctx, |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
pub fn text(&self, text: &[u8]) { |
| 90 |
unsafe { EMIT_ZONE_TEXT(self.ctx, text.as_ptr(), text.len()) }; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl Drop for ProfileScope { |
| 95 |
fn drop(&mut self) { |
| 96 |
unsafe { |
| 97 |
EMIT_ZONE_END(self.ctx) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/// Define a profiling scope. |
| 103 |
/// |
| 104 |
/// This macro records a Tracy 'zone' starting at the point at which the macro |
| 105 |
/// occurs, and extending to the end of the block. More precisely, the zone ends |
| 106 |
/// when a variable declared by this macro would be dropped, so early returns |
| 107 |
/// exit the zone. |
| 108 |
/// |
| 109 |
/// For example: |
| 110 |
/// |
| 111 |
/// fn an_operation_that_may_well_take_a_long_time() { |
| 112 |
/// profile_scope!("an_operation_that_may_well_take_a_long_time"); |
| 113 |
/// ... |
| 114 |
/// } |
| 115 |
/// |
| 116 |
/// The first argument to `profile_scope!` is the name of the zone, and must be |
| 117 |
/// a string literal. |
| 118 |
/// |
| 119 |
/// Tracy zones can also have associated 'text' values that vary from one |
| 120 |
/// execution to the next - for example, a zone's text might record the name of |
| 121 |
/// the file being processed. The text must be a `CStr` value. You can provide |
| 122 |
/// zone text like this: |
| 123 |
/// |
| 124 |
/// fn run_reftest(test_name: &str) { |
| 125 |
/// profile_scope!("run_reftest", text: test_name); |
| 126 |
/// ... |
| 127 |
/// } |
| 128 |
#[macro_export] |
| 129 |
macro_rules! profile_scope { |
| 130 |
($string:literal $(, text: $text:expr )? ) => { |
| 131 |
const CALLSITE: $crate::profiler::SourceLocation = $crate::profiler::SourceLocation { |
| 132 |
name: concat!($string, "\0").as_ptr(), |
| 133 |
function: concat!(module_path!(), "\0").as_ptr(), |
| 134 |
file: concat!(file!(), "\0").as_ptr(), |
| 135 |
line: line!(), |
| 136 |
color: 0xff0000ff, |
| 137 |
}; |
| 138 |
|
| 139 |
let _profile_scope = $crate::profiler::ProfileScope::new(&CALLSITE); |
| 140 |
|
| 141 |
$( |
| 142 |
_profile_scope.text(str::as_bytes($text)); |
| 143 |
)? |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/// Define the main frame marker, typically placed after swap_buffers or similar. |
| 148 |
#[macro_export] |
| 149 |
macro_rules! tracy_frame_marker { |
| 150 |
() => { |
| 151 |
unsafe { |
| 152 |
$crate::profiler::MARK_FRAME(std::ptr::null()) |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/// Define start of an explicit frame marker, typically used for sub-frames. |
| 158 |
#[macro_export] |
| 159 |
macro_rules! tracy_begin_frame { |
| 160 |
($name:expr) => { |
| 161 |
unsafe { |
| 162 |
$crate::profiler::MARK_FRAME_START(concat!($name, "\0").as_ptr()) |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/// Define end of an explicit frame marker, typically used for sub-frames. |
| 168 |
#[macro_export] |
| 169 |
macro_rules! tracy_end_frame { |
| 170 |
($name:expr) => { |
| 171 |
unsafe { |
| 172 |
$crate::profiler::MARK_FRAME_END(concat!($name, "\0").as_ptr()) |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[macro_export] |
| 178 |
macro_rules! tracy_plot { |
| 179 |
($name:expr, $value:expr) => { |
| 180 |
unsafe { |
| 181 |
$crate::profiler::EMIT_PLOT(concat!($name, "\0").as_ptr(), $value) |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// Provide a name for this thread to the profiler |
| 187 |
pub fn register_thread_with_profiler(_: String) { |
| 188 |
// TODO(gw): Add support for passing this to the tracy api |
| 189 |
} |