Skip to content

Commit

Permalink
Work in progress Tracy bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarnp committed Mar 21, 2021
1 parent 888423b commit 80afcac
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tracy"]
path = tracy
url = https://github.com/wolfpld/tracy.git
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2020, Oskar Nordquist
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,64 @@
# odin-tracy
Tracy profiler for The Odin programming language
Tracy profiler for the Odin programming language.

## 0. Prerequisites
This assumes you are using the latest nightly build or GitHub master of the Odin compiler. Since Odin is still under development this means these bindings might break in the future. Please create an issue or PR if that happens.

## 1. Cloning the sources
```sh
git clone --recurse-submodules https://github.com/oskarnp/odin-tracy
```
Or if you already had this repo cloned:
```sh
git submodule update --init
```

## 2. Building the Tracy profiler server

### Mac OS
#### Install dependencies
```sh
brew install pkg-config glfw freetype capstone
```
#### Build profiler
```
cd tracy/profiler/build/unix
make release
```
#### Run profiler
```
./tracy/profiler/build/unix/Tracy-release
```

## Windows
#### Install dependencies
(This will download and install external dependencies (glfw3, libcapstone, libfreetype) to vcpkg local directory. This writes files only to the vcpkg\vcpkg directory and makes no other changes on your machine.)
```sh
cd tracy\vcpkg
install_vcpkg_dependencies.bat
```
#### Build profiler
```sh
cd tracy\profiler\build\win32
msbuild Tracy.sln -t:Build -p:Configuration=Release
```
(or open solution with Visual Studio and build from there)
#### Run profiler
```sh
x64\Release\Tracy.exe
```

## Linux
TODO

## 3. Building the Tracy profiler client library

### Mac OS
TODO
### Windows
```sh
cl -MT -O2 -DTRACY_ENABLE -c tracy\TracyClient.cpp -Fotracy
lib tracy.obj
```
### Linux
TODO
63 changes: 63 additions & 0 deletions allocator.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tracy

import "core:c"
import "core:mem"

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

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

self.callstack_enable = callstack_enable;
self.callstack_size = callstack_size;
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, flags : u64, location := #caller_location) -> rawptr {
self := cast(^TrackedAllocatorData) allocator_data;
new_memory := self.backing_allocator.procedure(self.backing_allocator.data, mode, size, alignment, old_memory, old_size, flags, location);
switch mode {
case .Alloc:
if new_memory != nil {
if self.callstack_enable {
___tracy_emit_memory_alloc_callstack(new_memory, c.size_t(size), self.callstack_size, 1);
} else {
___tracy_emit_memory_alloc(new_memory, c.size_t(size), 1);
}
}
case .Free:
if old_memory != nil {
___tracy_emit_memory_free(old_memory, 1);
}
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(new_memory, c.size_t(size), self.callstack_size, 1);
}
else {
___tracy_emit_memory_alloc(new_memory, c.size_t(size), 1);
}
}
case .Query_Info:
// TODO
case .Query_Features:
// TODO
}
return new_memory;
}
};
return self.tracked_allocator;
}
54 changes: 54 additions & 0 deletions bindings.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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"

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

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

TracyCZoneCtx :: ___tracy_c_zone_context;

@(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 ) --- ;
}
106 changes: 106 additions & 0 deletions demo/demo.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package demo

import "core:fmt"
import "core:time"
import "core:thread"
import "core:mem"
import "core:sync"
import "core:strings"
import "core:math/rand"
import tracy ".."

/*
Dummy example to show using multiple threads with Tracy.
Build with: odin build . -define:TRACY_ENABLE=true
N.B! the strings passed to Zone* procs need to be globally unique as they
identify a particular source code location.
*/

main :: proc() {
r : rand.Rand;
rand.init(&r, u64(context.user_index));

tracy.SetThreadName("main");

NUM_WORKERS :: 3;

sync.barrier_init(&bar, 1 + NUM_WORKERS);
defer sync.barrier_destroy(&bar);

for i in 1..NUM_WORKERS {
context.user_index = i;
thread.run(worker, context);
}

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

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

{
// No name given receives the name of the calling procedure
tracy.Zone();

ptr := random_alloc(&r);
random_sleep(&r);
free(ptr);

// Do some deliberate leaking
new(int);
}

// Sync all workers to current frame.
sync.barrier_wait(&bar);
}
}

worker :: proc() {
r : rand.Rand;
rand.init(&r, u64(context.user_index));

thread_name := strings.clone_to_cstring(fmt.tprintf("worker%i", context.user_index));
defer delete(thread_name);

tracy.SetThreadName(thread_name);

for {
{
// No name given receives the name of the calling procedure
tracy.Zone();
random_sleep(&r);
}
{
tracy.ZoneN("worker doing stuff");
random_sleep(&r);
}
{
// Name + Color. Colors in 0xRRGGBB format. 0 means "no color" (use a value
// close to 0 for black).
tracy.ZoneNC("worker doing stuff", 0xff0000);
random_sleep(&r);
}

// sync with main thread for next frame
sync.barrier_wait(&bar);
}
}

bar : sync.Barrier;

random_sleep :: proc (r : ^rand.Rand) {
time.sleep(time.Duration(rand.int_max(25, r)) * time.Millisecond);
}

random_alloc :: proc (r : ^rand.Rand) -> rawptr {
return mem.alloc(1 + rand.int_max(1024, r));
}
1 change: 1 addition & 0 deletions tracy
Submodule tracy added at eaef96
Loading

0 comments on commit 80afcac

Please sign in to comment.