Skip to content

Commit

Permalink
Update bindings and wrapper based on Tracy 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarnp committed Nov 6, 2022
1 parent c83d5fb commit e6609f4
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 175 deletions.
87 changes: 49 additions & 38 deletions allocator.odin
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,74 @@ import "core:c"
import "core:mem"

TrackedAllocatorData :: struct {
backing_allocator : mem.Allocator,
tracked_allocator : mem.Allocator,
callstack_enable : bool,
callstack_size : i32,
backing_allocator: mem.Allocator,
tracked_allocator: mem.Allocator,
callstack_size: i32,
secure: b32,
}

TrackedAllocator :: proc(
self : ^TrackedAllocatorData,
callstack_enable : bool = false,
callstack_size : i32 = 5,
self: ^TrackedAllocatorData,
callstack_size: i32 = TRACY_CALLSTACK,
secure: b32 = false,
backing_allocator := context.allocator) -> mem.Allocator {

self.callstack_enable = callstack_enable;
self.callstack_size = callstack_size;
self.backing_allocator = backing_allocator;
self.callstack_size = callstack_size
self.secure = secure
self.backing_allocator = backing_allocator
self.tracked_allocator = mem.Allocator{
data = self,
procedure = proc(allocator_data : rawptr, mode : mem.Allocator_Mode, size, alignment : int, old_memory :
rawptr, old_size : int, location := #caller_location) -> ([]byte, mem.Allocator_Error) {
self := cast(^TrackedAllocatorData) allocator_data;
new_memory, error := self.backing_allocator.procedure(self.backing_allocator.data, mode, size, alignment, old_memory, old_size, location);
procedure = proc(allocator_data: rawptr, mode: mem.Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, mem.Allocator_Error) {
using self := cast(^TrackedAllocatorData) allocator_data
new_memory, error := self.backing_allocator.procedure(self.backing_allocator.data, mode, size, alignment, old_memory, old_size, location)
if error == .None {
switch mode {
case .Alloc, .Alloc_Non_Zeroed:
if new_memory != nil {
if self.callstack_enable {
___tracy_emit_memory_alloc_callstack(raw_data(new_memory), c.size_t(size), self.callstack_size, 1);
} else {
___tracy_emit_memory_alloc(raw_data(new_memory), c.size_t(size), 1);
}
}
EmitAlloc(new_memory, size, callstack_size, secure)
case .Free:
if old_memory != nil {
___tracy_emit_memory_free(old_memory, 1);
}
EmitFree(old_memory, callstack_size, secure)
case .Free_All:
// NOTE: Free_All not supported by this allocator
case .Resize:
if old_memory != nil {
___tracy_emit_memory_free(old_memory, 1);
}
if new_memory != nil {
if self.callstack_enable {
___tracy_emit_memory_alloc_callstack(raw_data(new_memory), c.size_t(size), self.callstack_size, 1);
}
else {
___tracy_emit_memory_alloc(raw_data(new_memory), c.size_t(size), 1);
}
}
EmitFree(old_memory, callstack_size, secure)
EmitAlloc(new_memory, size, callstack_size, secure)
case .Query_Info:
// TODO
case .Query_Features:
// TODO
}
}
return new_memory, error;
return new_memory, error
},
};
return self.tracked_allocator;
}
return self.tracked_allocator
}

@(private="file")
EmitAlloc :: #force_inline proc(new_memory: []byte, size: int, callstack_size: i32, secure: b32) {
when TRACY_HAS_CALLSTACK {
if callstack_size > 0 {
___tracy_emit_memory_alloc_callstack(raw_data(new_memory), c.size_t(size), callstack_size, secure)
} else {
___tracy_emit_memory_alloc(raw_data(new_memory), c.size_t(size), secure)
}
} else {
___tracy_emit_memory_alloc(raw_data(new_memory), c.size_t(size), secure)
}
}

@(private="file")
EmitFree :: #force_inline proc(old_memory: rawptr, callstack_size: i32, secure: b32) {
if old_memory == nil { return }
when TRACY_HAS_CALLSTACK {
if callstack_size > 0 {
___tracy_emit_memory_free_callstack(old_memory, callstack_size, secure)
} else {
___tracy_emit_memory_free(old_memory, secure)
}
} else {
___tracy_emit_memory_free(old_memory, secure)
}
}


161 changes: 122 additions & 39 deletions bindings.odin
Original file line number Diff line number Diff line change
@@ -1,54 +1,137 @@
package tracy

import "core:c"
import "core:os"

when os.OS == .Darwin do foreign import tracy "tracy.dylib"
when os.OS == .Windows do foreign import tracy "tracy.lib"
when os.OS == .Linux do foreign import tracy "tracy.o"
when ODIN_OS == .Darwin do foreign import tracy "tracy.dylib"
when ODIN_OS == .Windows do foreign import tracy "tracy.lib"
when ODIN_OS == .Linux do foreign import tracy "tracy.o"

___tracy_source_location_data :: struct {
name : cstring,
function : cstring,
file : cstring,
line : u32,
color : u32,
name: cstring,
function: cstring,
file: cstring,
line: u32,
color: u32,
}

___tracy_c_zone_context :: struct {
id : u32,
active : c.int,
id: u32,
active: b32,
}

TracyCZoneCtx :: ___tracy_c_zone_context;
___tracy_gpu_time_data :: struct {
gpuTime: i64,
queryId: u16,
_context: u8,
}

___tracy_gpu_zone_begin_data :: struct {
srcloc: u64,
queryId: u16,
_context: u8,
}

___tracy_gpu_zone_begin_callstack_data :: struct {
srcloc: u64,
depth: i32,
queryId: u16,
_context: u8,
}

___tracy_gpu_zone_end_data :: struct {
queryId: u16,
_context: u8,
}

___tracy_gpu_new_context_data :: struct {
gpuTime: i64,
period: f32,
_context: u8,
flags: u8,
type: u8,
}

___tracy_gpu_context_name_data :: struct {
_context: u8,
name: cstring,
len: u16,
}

___tracy_gpu_calibration_data :: struct {
gpuTime: i64,
cpuDelta: i64,
_context: u8,
}

when #config(TRACY_MANUAL_LIFETIME, false) {
@(default_calling_convention="c")
foreign tracy {
___tracy_startup_profiler :: proc() ---
___tracy_shutdown_profiler :: proc() ---
}
}

@(default_calling_convention="c")
foreign tracy {
___tracy_set_thread_name :: proc ( name : cstring ) --- ;
___tracy_init_thread :: proc () --- ;
___tracy_alloc_srcloc :: proc ( line : u32, source : cstring, sourceSz : c.size_t, function : cstring, functionSz : c.size_t ) -> u64 --- ;
___tracy_alloc_srcloc_name :: proc ( line : u32, source : cstring, sourceSz : c.size_t, function : cstring, functionSz : c.size_t, name : cstring, nameSz : c.size_t ) -> u64 --- ;
___tracy_emit_zone_begin :: proc ( srcloc : ^___tracy_source_location_data, active : c.int ) -> TracyCZoneCtx --- ;
___tracy_emit_zone_begin_callstack :: proc ( srcloc : ^___tracy_source_location_data, depth : c.int, active : c.int ) -> TracyCZoneCtx --- ;
___tracy_emit_zone_begin_alloc :: proc ( srcloc : u64, active : c.int ) -> TracyCZoneCtx --- ;
___tracy_emit_zone_begin_alloc_callstack :: proc ( srcloc : u64, depth : c.int, active : c.int ) -> TracyCZoneCtx --- ;
___tracy_emit_zone_end :: proc ( ctx : TracyCZoneCtx ) --- ;
___tracy_emit_zone_text :: proc ( ctx : TracyCZoneCtx, txt : cstring, size : c.size_t ) --- ;
___tracy_emit_zone_name :: proc ( ctx : TracyCZoneCtx, txt : cstring, size : c.size_t ) --- ;
___tracy_emit_zone_color :: proc ( ctx : TracyCZoneCtx, color : u32 ) --- ;
___tracy_emit_zone_value :: proc ( ctx : TracyCZoneCtx, value : u64 ) --- ;
___tracy_emit_memory_alloc :: proc ( ptr : rawptr, size : c.size_t, secure : c.int ) --- ;
___tracy_emit_memory_alloc_callstack :: proc ( ptr : rawptr, size : c.size_t, depth : c.int, secure : c.int ) --- ;
___tracy_emit_memory_free :: proc ( ptr : rawptr, secure : c.int ) --- ;
___tracy_emit_memory_free_callstack :: proc ( ptr : rawptr, depth : c.int, secure : c.int ) --- ;
___tracy_emit_message :: proc ( txt : cstring, size : c.size_t, callstack : c.int ) --- ;
___tracy_emit_messageL :: proc ( txt : cstring, callstack : c.int ) --- ;
___tracy_emit_messageC :: proc ( txt : cstring, size : c.size_t, color : u32, callstack : c.int ) --- ;
___tracy_emit_messageLC :: proc ( txt : cstring, color : u32, callstack : c.int ) --- ;
___tracy_emit_frame_mark :: proc ( name : cstring ) --- ;
___tracy_emit_frame_mark_start :: proc ( name : cstring ) --- ;
___tracy_emit_frame_mark_end :: proc ( name : cstring ) --- ;
___tracy_emit_frame_image :: proc ( image : cstring, w : u16, h : u16, offset : u8, flip : c.int ) --- ;
___tracy_emit_plot :: proc ( name : cstring, val : f64 ) --- ;
___tracy_emit_message_appinfo :: proc ( txt : cstring, size : c.size_t ) --- ;
___tracy_set_thread_name :: proc( name: cstring ) ---

___tracy_alloc_srcloc :: proc( line: u32, source: cstring, sourceSz: c.size_t, function: cstring, functionSz: c.size_t ) -> u64 ---
___tracy_alloc_srcloc_name :: proc( line: u32, source: cstring, sourceSz: c.size_t, function: cstring, functionSz: c.size_t, name: cstring, nameSz: c.size_t ) -> u64 ---

___tracy_emit_zone_begin :: proc( srcloc: ^___tracy_source_location_data, active: b32 ) -> ___tracy_c_zone_context ---
___tracy_emit_zone_begin_callstack :: proc( srcloc: ^___tracy_source_location_data, depth: i32, active: b32 ) -> ___tracy_c_zone_context ---
___tracy_emit_zone_begin_alloc :: proc( srcloc: u64, active: b32 ) -> ___tracy_c_zone_context ---
___tracy_emit_zone_begin_alloc_callstack :: proc( srcloc: u64, depth: i32, active: b32 ) -> ___tracy_c_zone_context ---
___tracy_emit_zone_end :: proc( ctx: ___tracy_c_zone_context ) ---
___tracy_emit_zone_text :: proc( ctx: ___tracy_c_zone_context, txt: cstring, size: c.size_t ) ---
___tracy_emit_zone_name :: proc( ctx: ___tracy_c_zone_context, txt: cstring, size: c.size_t ) ---
___tracy_emit_zone_color :: proc( ctx: ___tracy_c_zone_context, color: u32 ) ---
___tracy_emit_zone_value :: proc( ctx: ___tracy_c_zone_context, value: u64 ) ---

___tracy_emit_gpu_zone_begin :: proc( ___tracy_gpu_zone_begin_data ) ---
___tracy_emit_gpu_zone_begin_callstack :: proc( ___tracy_gpu_zone_begin_callstack_data ) ---
___tracy_emit_gpu_zone_begin_alloc :: proc( ___tracy_gpu_zone_begin_data ) ---
___tracy_emit_gpu_zone_begin_alloc_callstack :: proc( ___tracy_gpu_zone_begin_callstack_data ) ---
___tracy_emit_gpu_zone_end :: proc( ___tracy_gpu_zone_end_data ) ---
___tracy_emit_gpu_time :: proc( ___tracy_gpu_time_data ) ---
___tracy_emit_gpu_new_context :: proc( ___tracy_gpu_new_context_data ) ---
___tracy_emit_gpu_context_name :: proc( ___tracy_gpu_context_name_data ) ---
___tracy_emit_gpu_calibration :: proc( ___tracy_gpu_calibration_data ) ---

___tracy_emit_gpu_zone_begin_serial :: proc( ___tracy_gpu_zone_begin_data ) ---
___tracy_emit_gpu_zone_begin_callstack_serial :: proc( ___tracy_gpu_zone_begin_callstack_data ) ---
___tracy_emit_gpu_zone_begin_alloc_serial :: proc( ___tracy_gpu_zone_begin_data ) ---
___tracy_emit_gpu_zone_begin_alloc_callstack_serial :: proc( ___tracy_gpu_zone_begin_callstack_data ) ---
___tracy_emit_gpu_zone_end_serial :: proc( ___tracy_gpu_zone_end_data ) ---
___tracy_emit_gpu_time_serial :: proc( ___tracy_gpu_time_data ) ---
___tracy_emit_gpu_new_context_serial :: proc( ___tracy_gpu_new_context_data ) ---
___tracy_emit_gpu_context_name_serial :: proc( ___tracy_gpu_context_name_data ) ---
___tracy_emit_gpu_calibration_serial :: proc( ___tracy_gpu_calibration_data ) ---

___tracy_connected :: proc() -> b32 ---

___tracy_emit_memory_alloc :: proc( ptr: rawptr, size: c.size_t, secure: b32 ) ---
___tracy_emit_memory_alloc_callstack :: proc( ptr: rawptr, size: c.size_t, depth: i32, secure: b32 ) ---
___tracy_emit_memory_free :: proc( ptr: rawptr, secure: b32 ) ---
___tracy_emit_memory_free_callstack :: proc( ptr: rawptr, depth: i32, secure: b32 ) ---
___tracy_emit_memory_alloc_named :: proc( ptr: rawptr, size: c.size_t, secure: b32, name: cstring ) ---
___tracy_emit_memory_alloc_callstack_named :: proc( ptr: rawptr, size: c.size_t, depth: i32, secure: b32, name: cstring ) ---
___tracy_emit_memory_free_named :: proc( ptr: rawptr, secure: b32, name: cstring ) ---
___tracy_emit_memory_free_callstack_named :: proc( ptr: rawptr, depth: i32, secure: b32, name: cstring ) ---

___tracy_emit_message :: proc( txt: cstring, size: c.size_t, callstack: i32 ) ---
___tracy_emit_messageL :: proc( txt: cstring, callstack: i32 ) ---
___tracy_emit_messageC :: proc( txt: cstring, size: c.size_t, color: u32, callstack: i32 ) ---
___tracy_emit_messageLC :: proc( txt: cstring, color: u32, callstack: i32 ) ---

___tracy_emit_frame_mark :: proc( name: cstring ) ---
___tracy_emit_frame_mark_start :: proc( name: cstring ) ---
___tracy_emit_frame_mark_end :: proc( name: cstring ) ---
___tracy_emit_frame_image :: proc( image: rawptr, w, h: u16, offset: u8, flip: i32 ) ---

___tracy_emit_plot :: proc( name: cstring, val: f64 ) ---
___tracy_emit_message_appinfo :: proc( txt: cstring, size: c.size_t ) ---

___tracy_fiber_enter :: proc( fiber: cstring ) ---
___tracy_fiber_leave :: proc() ---
}
17 changes: 12 additions & 5 deletions demo/demo.odin
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package demo

TRACY_ENABLE :: #config(TRACY_ENABLE, false)

when !TRACY_ENABLE {
#panic("TRACY_ENABLE need to be set to true for this demo to be useful.")
}

import "core:fmt"
import "core:time"
import "core:thread"
Expand Down Expand Up @@ -30,17 +36,17 @@ main :: proc() {
thread.run(worker, context);
}

// Track heap allocations with Tracy for this context.
// Profile heap allocations with Tracy for this context.
context.allocator = tracy.TrackedAllocator(
self = &tracy.TrackedAllocatorData{},
callstack_enable = true,
callstack_size = 5,
backing_allocator = context.allocator,
);
secure = true
)

for {
// Marks the end of the frame. This is optional. Useful for applications
// which has a concept of a frame.
// Marks the end of the frame. This is optional. Useful for
// applications which has a concept of a frame.
defer tracy.FrameMark();

{
Expand Down Expand Up @@ -100,3 +106,4 @@ random_sleep :: proc (r : ^rand.Rand) {
random_alloc :: proc (r : ^rand.Rand) -> rawptr {
return mem.alloc(1 + rand.int_max(1024, r));
}

Loading

0 comments on commit e6609f4

Please sign in to comment.