Skip to content

Commit 1ea744d

Browse files
committed
files from monorepo @ bde42aaeec763c1bd8781d26285b0f4dc65b1a4c
0 parents  commit 1ea744d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+45937
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ignore some special directories
2+
.zig-cache
3+
zig-out
4+
5+
# Ignore some special OS files
6+
*.DS_Store

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Michal Ziulek
4+
Copyright (c) 2024 zig-gamedev contributors
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [ztracy](https://github.com/zig-gamedev/ztracy)
2+
3+
Performance markers for [Tracy 0.11.1](https://github.com/wolfpld/tracy) in Zig
4+
5+
Initial Zig bindings created by [Martin Wickham](https://github.com/SpexGuy/Zig-Tracy)
6+
7+
## Getting started
8+
9+
Example `build.zig`:
10+
11+
```zig
12+
pub fn build(b: *std.Build) void {
13+
const options = .{
14+
.enable_ztracy = b.option(
15+
bool,
16+
"enable_ztracy",
17+
"Enable Tracy profile markers",
18+
) orelse false,
19+
.enable_fibers = b.option(
20+
bool,
21+
"enable_fibers",
22+
"Enable Tracy fiber support",
23+
) orelse false,
24+
.on_demand = b.option(
25+
bool,
26+
"on_demand",
27+
"Build tracy with TRACY_ON_DEMAND",
28+
) orelse false,
29+
};
30+
31+
const exe = b.addExecutable(.{ ... });
32+
33+
const ztracy = b.dependency("ztracy", .{
34+
.enable_ztracy = options.enable_ztracy,
35+
.enable_fibers = options.enable_fibers,
36+
.on_demand = options.on_demand,
37+
});
38+
exe.root_module.addImport("ztracy", ztracy.module("root"));
39+
exe.linkLibrary(ztracy.artifact("tracy"));
40+
}
41+
```
42+
43+
Now in your code you may import and use `ztracy`. To build your project with Tracy enabled run:
44+
45+
`zig build -Denable_ztracy=true`
46+
47+
```zig
48+
const ztracy = @import("ztracy");
49+
50+
pub fn main() !void {
51+
{
52+
const tracy_zone = ztracy.ZoneNC(@src(), "Compute Magic", 0x00_ff_00_00);
53+
defer tracy_zone.End();
54+
...
55+
}
56+
}
57+
```
58+
59+
## Async "Fibers" support
60+
61+
Tracy has support for marking fibers (also called green threads,
62+
coroutines, and other forms of cooperative multitasking). This support requires
63+
an additional option passed through when compiling the Tracy library, so:
64+
65+
```zig
66+
...
67+
const optimize = b.standardOptimizeOption(.{});
68+
const target = b.standardTargetOptions(.{});
69+
70+
const ztracy_pkg = ztracy.package(b, target, optimize, .{
71+
.options = .{ .enable_ztracy = true, .enable_fibers = true },
72+
});
73+
74+
ztracy_pkg.link(exe);
75+
```

build.zig

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const optimize = b.standardOptimizeOption(.{});
5+
const target = b.standardTargetOptions(.{});
6+
7+
const options = .{
8+
.enable_ztracy = b.option(
9+
bool,
10+
"enable_ztracy",
11+
"Enable Tracy profile markers",
12+
) orelse false,
13+
.enable_fibers = b.option(
14+
bool,
15+
"enable_fibers",
16+
"Enable Tracy fiber support",
17+
) orelse false,
18+
.on_demand = b.option(
19+
bool,
20+
"on_demand",
21+
"Build tracy with TRACY_ON_DEMAND",
22+
) orelse false,
23+
.shared = b.option(
24+
bool,
25+
"shared",
26+
"Build as shared library",
27+
) orelse false,
28+
};
29+
30+
const options_step = b.addOptions();
31+
inline for (std.meta.fields(@TypeOf(options))) |field| {
32+
options_step.addOption(field.type, field.name, @field(options, field.name));
33+
}
34+
35+
const options_module = options_step.createModule();
36+
37+
const translate_c = b.addTranslateC(.{
38+
.root_source_file = b.path("libs/tracy/tracy/TracyC.h"),
39+
.target = target,
40+
.optimize = optimize,
41+
.link_libc = true,
42+
});
43+
translate_c.addIncludePath(b.path("libs/tracy/tracy"));
44+
translate_c.defineCMacro("TRACY_ENABLE", "");
45+
translate_c.defineCMacro("TRACY_IMPORTS", "");
46+
47+
const ztracy = b.addModule("root", .{
48+
.root_source_file = b.path("src/ztracy.zig"),
49+
.imports = &.{
50+
.{ .name = "ztracy_options", .module = options_module },
51+
},
52+
});
53+
ztracy.addImport("c", translate_c.createModule());
54+
55+
const tracy = if (options.shared) blk: {
56+
const lib = b.addSharedLibrary(.{
57+
.name = "tracy",
58+
.target = target,
59+
.optimize = optimize,
60+
});
61+
lib.defineCMacro("TRACY_EXPORTS", "");
62+
break :blk lib;
63+
} else b.addStaticLibrary(.{
64+
.name = "tracy",
65+
.target = target,
66+
.optimize = optimize,
67+
});
68+
69+
tracy.addIncludePath(b.path("libs/tracy/tracy"));
70+
tracy.addCSourceFile(.{
71+
.file = b.path("libs/tracy/TracyClient.cpp"),
72+
.flags = &.{
73+
if (options.enable_ztracy) "-DTRACY_ENABLE" else "",
74+
if (options.enable_fibers) "-DTRACY_FIBERS" else "",
75+
"-fno-sanitize=undefined",
76+
},
77+
});
78+
79+
if (options.on_demand) tracy.defineCMacro("TRACY_ON_DEMAND", null);
80+
81+
tracy.linkLibC();
82+
if (target.result.abi != .msvc) {
83+
tracy.linkLibCpp();
84+
} else {
85+
tracy.defineCMacro("fileno", "_fileno");
86+
}
87+
88+
switch (target.result.os.tag) {
89+
.windows => {
90+
tracy.linkSystemLibrary("ws2_32");
91+
tracy.linkSystemLibrary("dbghelp");
92+
},
93+
.macos => {
94+
if (b.lazyDependency("system_sdk", .{})) |system_sdk| {
95+
tracy.addFrameworkPath(system_sdk.path("System/Library/Frameworks"));
96+
}
97+
},
98+
else => {},
99+
}
100+
101+
b.installArtifact(tracy);
102+
103+
const test_step = b.step("test", "Run ztracy tests");
104+
105+
const tests = b.addTest(.{
106+
.name = "ztracy-tests",
107+
.root_source_file = b.path("src/ztracy.zig"),
108+
.target = target,
109+
.optimize = optimize,
110+
});
111+
tests.linkLibrary(tracy);
112+
b.installArtifact(tests);
113+
114+
test_step.dependOn(&b.addRunArtifact(tests).step);
115+
}

build.zig.zon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.{
2+
.name = "ztracy",
3+
.version = "0.14.0-dev",
4+
.paths = .{
5+
"build.zig",
6+
"build.zig.zon",
7+
"libs",
8+
"src",
9+
"README.md",
10+
},
11+
.dependencies = .{
12+
.system_sdk = .{
13+
.url = "https://github.com/zig-gamedev/system-sdk/archive/refs/tags/v0.2.0.tar.gz",
14+
.hash = "122035336d7535f655fe0ece31cea0fbf8ad2f1e42f64b073995050a425ca8f8fbdc",
15+
.lazy = true,
16+
},
17+
},
18+
}

libs/tracy/TracyClient.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Tracy profiler
3+
// ----------------
4+
//
5+
// For fast integration, compile and
6+
// link with this source file (and none
7+
// other) in your executable (or in the
8+
// main DLL / shared object on multi-DLL
9+
// projects).
10+
//
11+
12+
// Define TRACY_ENABLE to enable profiler.
13+
14+
#include "common/TracySystem.cpp"
15+
16+
#ifdef TRACY_ENABLE
17+
18+
#ifdef _MSC_VER
19+
# pragma warning(push, 0)
20+
#endif
21+
22+
#include "common/tracy_lz4.cpp"
23+
#include "client/TracyProfiler.cpp"
24+
#include "client/TracyCallstack.cpp"
25+
#include "client/TracySysPower.cpp"
26+
#include "client/TracySysTime.cpp"
27+
#include "client/TracySysTrace.cpp"
28+
#include "common/TracySocket.cpp"
29+
#include "client/tracy_rpmalloc.cpp"
30+
#include "client/TracyDxt1.cpp"
31+
#include "client/TracyAlloc.cpp"
32+
#include "client/TracyOverride.cpp"
33+
#include "client/TracyKCore.cpp"
34+
35+
#if defined(TRACY_HAS_CALLSTACK)
36+
# if TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3 || TRACY_HAS_CALLSTACK == 4 || TRACY_HAS_CALLSTACK == 6
37+
# include "libbacktrace/alloc.cpp"
38+
# include "libbacktrace/dwarf.cpp"
39+
# include "libbacktrace/fileline.cpp"
40+
# include "libbacktrace/mmapio.cpp"
41+
# include "libbacktrace/posix.cpp"
42+
# include "libbacktrace/sort.cpp"
43+
# include "libbacktrace/state.cpp"
44+
# if TRACY_HAS_CALLSTACK == 4
45+
# include "libbacktrace/macho.cpp"
46+
# else
47+
# include "libbacktrace/elf.cpp"
48+
# endif
49+
# include "common/TracyStackFrames.cpp"
50+
# endif
51+
#endif
52+
53+
#ifdef _MSC_VER
54+
# pragma comment(lib, "ws2_32.lib")
55+
# pragma comment(lib, "dbghelp.lib")
56+
# pragma comment(lib, "advapi32.lib")
57+
# pragma comment(lib, "user32.lib")
58+
# pragma warning(pop)
59+
#endif
60+
61+
#endif

libs/tracy/client/TracyAlloc.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "../common/TracyAlloc.hpp"
2+
3+
#ifdef TRACY_USE_RPMALLOC
4+
5+
#include <atomic>
6+
7+
#include "../common/TracyForceInline.hpp"
8+
#include "../common/TracyYield.hpp"
9+
10+
namespace tracy
11+
{
12+
13+
extern thread_local bool RpThreadInitDone;
14+
extern std::atomic<int> RpInitDone;
15+
extern std::atomic<int> RpInitLock;
16+
17+
tracy_no_inline static void InitRpmallocPlumbing()
18+
{
19+
const auto done = RpInitDone.load( std::memory_order_acquire );
20+
if( !done )
21+
{
22+
int expected = 0;
23+
while( !RpInitLock.compare_exchange_weak( expected, 1, std::memory_order_release, std::memory_order_relaxed ) ) { expected = 0; YieldThread(); }
24+
const auto done = RpInitDone.load( std::memory_order_acquire );
25+
if( !done )
26+
{
27+
rpmalloc_initialize();
28+
RpInitDone.store( 1, std::memory_order_release );
29+
}
30+
RpInitLock.store( 0, std::memory_order_release );
31+
}
32+
rpmalloc_thread_initialize();
33+
RpThreadInitDone = true;
34+
}
35+
36+
TRACY_API void InitRpmalloc()
37+
{
38+
if( !RpThreadInitDone ) InitRpmallocPlumbing();
39+
}
40+
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)