Skip to content

Commit fbd9347

Browse files
committed
Add name buffer to tracy client library
To be used by Python and Go bindings to store const char * accessible via a lookup
1 parent 6199b2f commit fbd9347

File tree

10 files changed

+155
-104
lines changed

10 files changed

+155
-104
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ set_option(TRACY_TIMER_FALLBACK "Use lower resolution timers" OFF)
8282
set_option(TRACY_LIBUNWIND_BACKTRACE "Use libunwind backtracing where supported" OFF)
8383
set_option(TRACY_SYMBOL_OFFLINE_RESOLVE "Instead of full runtime symbol resolution, only resolve the image path and offset to enable offline symbol resolution" OFF)
8484
set_option(TRACY_LIBBACKTRACE_ELF_DYNLOAD_SUPPORT "Enable libbacktrace to support dynamically loaded elfs in symbol resolution resolution after the first symbol resolve operation" OFF)
85+
set_option(TRACY_NAME_BUFFER "Enable name buffer for other languages" OFF)
8586

8687
if(NOT TRACY_STATIC)
8788
target_compile_definitions(TracyClient PRIVATE TRACY_EXPORTS)
@@ -141,6 +142,16 @@ set(common_includes
141142
${TRACY_PUBLIC_DIR}/common/TracyUwp.hpp
142143
${TRACY_PUBLIC_DIR}/common/TracyYield.hpp)
143144

145+
if(TRACY_NAME_BUFFER)
146+
set(TRACY_BUFFER_SIZE 128 CACHE STRING "The size of the name buffer")
147+
set(TRACY_NAME_LENGTH 128 CACHE STRING "The length of a name in the buffer")
148+
149+
list(APPEND common_includes ${TRACY_PUBLIC_DIR}/common/TracyNameBuffer.hpp)
150+
151+
target_compile_definitions(TracyClient PRIVATE TRACY_BUFFER_SIZE=${TRACY_BUFFER_SIZE})
152+
target_compile_definitions(TracyClient PRIVATE TRACY_NAME_LENGTH=${TRACY_NAME_LENGTH})
153+
endif()
154+
144155
install(TARGETS TracyClient
145156
EXPORT TracyConfig
146157
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
@@ -169,6 +180,9 @@ if(TRACY_CLIENT_PYTHON)
169180
if(TRACY_STATIC)
170181
message(FATAL_ERROR "Python-bindings require a shared client library")
171182
endif()
183+
if(NOT TRACY_NAME_BUFFER)
184+
message(FATAL_ERROR "Python-bindings require name buffer being enabled")
185+
endif()
172186

173187
add_subdirectory(python)
174188
endif()

public/TracyClient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
# endif
5050
#endif
5151

52+
#ifdef TRACY_NAME_BUFFER
53+
#include "common/TracyNameBuffer.cpp"
54+
#endif
55+
5256
#ifdef _MSC_VER
5357
# pragma comment(lib, "ws2_32.lib")
5458
# pragma comment(lib, "dbghelp.lib")

public/common/TracyNameBuffer.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "TracyNameBuffer.hpp"
2+
using namespace tracy;
3+
4+
#include "TracyApi.h"
5+
6+
#ifndef TRACY_BUFFER_SIZE
7+
#define TRACY_BUFFER_SIZE = 128
8+
#endif
9+
10+
#ifndef TRACY_NAME_LENGTH
11+
#define TRACY_NAME_LENGTH = 128
12+
#endif
13+
14+
NameBuffer::NameBuffer() : m_buffer(TRACY_BUFFER_SIZE, nullptr), m_index(0ul) {
15+
for (std::size_t index = 0ul, end = m_buffer.size(); index < end; ++index)
16+
m_buffer[index] = new char[TRACY_NAME_LENGTH];
17+
}
18+
19+
BufferEntry NameBuffer::add( const std::string& name ) {
20+
std::lock_guard<std::mutex> lock(m_mutex);
21+
if (m_index >= TRACY_BUFFER_SIZE || name.size() > TRACY_NAME_LENGTH)
22+
return std::make_pair(std::nullopt, nullptr);
23+
24+
auto index = m_index++;
25+
name.copy(m_buffer[index], name.size());
26+
return std::make_pair(index, m_buffer[index]);
27+
}
28+
29+
const char* NameBuffer::get( uint16_t index ) {
30+
std::lock_guard<std::mutex> lock(m_mutex);
31+
if (index >= TRACY_BUFFER_SIZE) return nullptr;
32+
return m_buffer[index];
33+
}
34+
35+
#ifdef TRACY_NAME_BUFFER
36+
TRACY_API const char* ___tracy_name_buffer_add( const char* name, uint16_t* id ) {
37+
auto entry = NameBuffer::Add(name);
38+
if (!entry.first) return nullptr;
39+
40+
if (id != nullptr) *id = *entry.first;
41+
return entry.second;
42+
}
43+
TRACY_API const char* ___tracy_name_buffer_get( uint16_t id ) { return NameBuffer::Get(id); }
44+
#endif

public/common/TracyNameBuffer.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
3+
#include <mutex>
4+
#include <optional>
5+
#include <string>
6+
#include <vector>
7+
8+
namespace tracy {
9+
using OptionalNumber = std::optional<uint16_t>;
10+
using BufferEntry = std::pair<OptionalNumber, const char*>;
11+
12+
class NameBuffer {
13+
public:
14+
static inline BufferEntry Add( const std::string& name ) {
15+
return getBuffer().add(name);
16+
}
17+
18+
static inline const char* Get( uint16_t index ) {
19+
return getBuffer().get(index);
20+
}
21+
22+
private:
23+
NameBuffer();
24+
25+
std::mutex m_mutex;
26+
std::vector<char*> m_buffer;
27+
std::size_t m_index;
28+
29+
static inline NameBuffer& getBuffer() {
30+
static NameBuffer buffer;
31+
return buffer;
32+
}
33+
34+
BufferEntry add( const std::string& name );
35+
const char* get( uint16_t index );
36+
};
37+
} // namespace tracy

public/tracy/Tracy.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@
291291
# define TracyFiberLeave tracy::Profiler::LeaveFiber()
292292
#endif
293293

294+
#ifdef TRACY_NAME_BUFFER
295+
# include "../common/TracyNameBuffer.hpp"
296+
#endif
297+
294298
#endif
295299

296300
#endif

public/tracy/TracyC.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ TRACY_API void ___tracy_set_thread_name( const char* name );
3737

3838
#ifndef TRACY_ENABLE
3939

40+
#define TracyCEnabled() 0
41+
4042
typedef const void* TracyCZoneCtx;
4143

4244
typedef const void* TracyCLockCtx;
@@ -116,8 +118,15 @@ typedef const void* TracyCLockCtx;
116118
# define TracyCFiberLeave
117119
#endif
118120

121+
#ifdef TRACY_NAME_BUFFER
122+
# define TracyCNameBufferAdd(name, id) 0
123+
# define TracyCNameBufferGet(id) 0
124+
#endif
125+
119126
#else
120127

128+
#define TracyCEnabled() 1
129+
121130
#ifndef TracyConcat
122131
# define TracyConcat(x,y) TracyConcatIndirect(x,y)
123132
#endif
@@ -408,6 +417,14 @@ TRACY_API void ___tracy_fiber_leave( void );
408417
# define TracyCFiberLeave ___tracy_fiber_leave();
409418
#endif
410419

420+
#ifdef TRACY_NAME_BUFFER
421+
TRACY_API const char* ___tracy_name_buffer_add( const char* name, uint16_t* id );
422+
TRACY_API const char* ___tracy_name_buffer_get( uint16_t id );
423+
424+
# define TracyCNameBufferAdd(name, id) ___tracy_name_buffer_add( name, id );
425+
# define TracyCNameBufferGet(id) ___tracy_name_buffer_get( id );
426+
#endif
427+
411428
#endif
412429

413430
#ifdef __cplusplus

python/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@ if(EXTERNAL_PYBIND11)
99
FetchContent_MakeAvailable(pybind11)
1010
endif()
1111

12-
set(BUFFER_SIZE 128 CACHE STRING "The size of the pointer buffer")
13-
set(NAME_LENGTH 128 CACHE STRING "The length of a name in the buffer")
14-
1512
pybind11_add_module(TracyClientBindings SHARED bindings/Module.cpp)
1613
target_link_libraries(TracyClientBindings PUBLIC TracyClient)
17-
target_compile_definitions(TracyClientBindings PUBLIC BUFFER_SIZE=${BUFFER_SIZE})
18-
target_compile_definitions(TracyClientBindings PUBLIC NAME_LENGTH=${NAME_LENGTH})
19-
2014
set(TRACY_PYTHON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tracy_client)
2115
set(TRACY_LIB_SYMLINK $<TARGET_FILE_PREFIX:TracyClient>$<TARGET_FILE_BASE_NAME:TracyClient>$<TARGET_FILE_SUFFIX:TracyClient>)
2216

python/bindings/Memory.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <pybind11/pybind11.h>
44
namespace py = pybind11;
55

6-
#include "NameBuffer.hpp"
76
#include "tracy/Tracy.hpp"
7+
using namespace tracy;
88

99
using OptionalString = std::optional<std::string>;
1010
using OptionalInt = std::optional<int>;
@@ -61,6 +61,7 @@ bool MemoryFree(const Type &type, const OptionalNumber &id = std::nullopt,
6161
return true;
6262
}
6363
#else
64+
using OptionalNumber = std::optional<uint16_t>;
6465

6566
template <typename Type = uint64_t>
6667
OptionalNumber MemoryAllocate(const Type &, std::size_t, const OptionalString &,

python/bindings/Module.cpp

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
#include "Memory.hpp"
22
#include "ScopedZone.hpp"
33
#include "tracy/TracyC.h"
4+
using namespace tracy;
45

56
namespace tracy {
67
#ifndef TRACY_ENABLE
78
enum class PlotFormatType : uint8_t { Number, Memory, Percentage };
89
#endif
9-
10-
constexpr static inline bool IsEnabled() {
11-
#ifdef TRACY_ENABLE
12-
return true;
13-
#else
14-
return false;
15-
#endif
16-
}
1710
} // namespace tracy
1811

1912
PYBIND11_MODULE(TracyClientBindings, m) {
2013
m.doc() = "Tracy Client Bindings";
2114

22-
m.def("is_enabled", &tracy::IsEnabled);
15+
m.def("is_enabled", []() -> bool { return TracyCEnabled(); });
2316

2417
py::enum_<tracy::Color::ColorType>(m, "ColorType")
2518
.value("Snow", tracy::Color::Snow)
@@ -703,26 +696,26 @@ PYBIND11_MODULE(TracyClientBindings, m) {
703696
m.def(
704697
"program_name",
705698
[](const std::string &name) {
706-
if (!tracy::IsEnabled()) return true;
707-
auto entry = NameBuffer::Add(name);
708-
if (!entry.first) return false;
709-
TracySetProgramName(entry.second);
699+
if (!TracyCEnabled()) return true;
700+
auto ptr = TracyCNameBufferAdd(name.c_str(), nullptr);
701+
if (!ptr) return false;
702+
TracySetProgramName(ptr);
710703
return true;
711704
},
712705
"name"_a.none(false));
713706

714707
m.def(
715708
"thread_name",
716709
[](const std::string &name) {
717-
if (!tracy::IsEnabled()) return;
710+
if (!TracyCEnabled()) return;
718711
tracy::SetThreadName(name.c_str());
719712
},
720713
"name"_a.none(false));
721714

722715
m.def(
723716
"app_info",
724717
[](const std::string &text) {
725-
if (!tracy::IsEnabled()) return true;
718+
if (!TracyCEnabled()) return true;
726719
if (text.size() >= std::numeric_limits<uint16_t>::max()) return false;
727720
TracyAppInfo(text.c_str(), text.size());
728721
return true;
@@ -732,7 +725,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
732725
m.def(
733726
"message",
734727
[](const std::string &message) {
735-
if (!tracy::IsEnabled()) return true;
728+
if (!TracyCEnabled()) return true;
736729
if (message.size() >= std::numeric_limits<uint16_t>::max())
737730
return false;
738731
TracyMessage(message.c_str(), message.size());
@@ -743,7 +736,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
743736
m.def(
744737
"message",
745738
[](const std::string &message, uint32_t pColor) {
746-
if (!tracy::IsEnabled()) return true;
739+
if (!TracyCEnabled()) return true;
747740
if (message.size() >= std::numeric_limits<uint16_t>::max())
748741
return false;
749742
TracyMessageC(message.c_str(), message.size(), pColor);
@@ -755,20 +748,21 @@ PYBIND11_MODULE(TracyClientBindings, m) {
755748

756749
m.def(
757750
"frame_mark_start",
758-
[](const std::string &name) {
759-
if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
760-
auto entry = NameBuffer::Add(name);
761-
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
762-
FrameMarkStart(entry.second);
763-
return entry.first;
751+
[](const std::string &name) -> OptionalNumber {
752+
if (!TracyCEnabled()) return 0ul;
753+
uint16_t id = 0ul;
754+
auto ptr = TracyCNameBufferAdd(name.c_str(), &id);
755+
if (!ptr) return static_cast<OptionalNumber>(std::nullopt);
756+
FrameMarkStart(ptr);
757+
return id;
764758
},
765759
"name"_a.none(false));
766760

767761
m.def(
768762
"frame_mark_end",
769763
[](std::size_t id) {
770-
if (!tracy::IsEnabled()) return true;
771-
auto ptr = NameBuffer::Get(id);
764+
if (!TracyCEnabled()) return true;
765+
auto ptr = TracyCNameBufferGet(id);
772766
if (!ptr) return false;
773767
FrameMarkEnd(ptr);
774768
return true;
@@ -779,7 +773,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
779773
"frame_image",
780774
[](const py::bytes &image, uint16_t width, uint16_t height,
781775
uint8_t offset = 0, bool flip = false) {
782-
if (!tracy::IsEnabled()) return true;
776+
if (!TracyCEnabled()) return true;
783777
if (width % 4 != 0 || height % 4 != 0) return false;
784778
TracyCFrameImage(std::string(image).data(), width, height, offset,
785779
flip);
@@ -821,12 +815,13 @@ PYBIND11_MODULE(TracyClientBindings, m) {
821815
m.def(
822816
"_plot_config",
823817
[](const std::string &name, int type, bool step, bool fill,
824-
uint32_t color = 0) {
825-
if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
826-
auto entry = NameBuffer::Add(name);
827-
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
828-
TracyCPlotConfig(entry.second, type, step, fill, color);
829-
return entry.first;
818+
uint32_t color = 0) -> OptionalNumber {
819+
if (!TracyCEnabled()) return 0ul;
820+
uint16_t id = 0ul;
821+
auto ptr = TracyCNameBufferAdd(name.c_str(), &id);
822+
if (!ptr) return static_cast<OptionalNumber>(std::nullopt);
823+
TracyCPlotConfig(ptr, type, step, fill, color);
824+
return id;
830825
},
831826
"name"_a.none(false), "type"_a.none(false), "step"_a.none(false),
832827
"fill"_a.none(false), "color"_a.none(false));
@@ -840,8 +835,8 @@ PYBIND11_MODULE(TracyClientBindings, m) {
840835
m.def(
841836
"plot",
842837
[](std::size_t id, double value) {
843-
if (!tracy::IsEnabled()) return true;
844-
auto ptr = NameBuffer::Get(id);
838+
if (!TracyCEnabled()) return true;
839+
auto ptr = TracyCNameBufferGet(id);
845840
if (!ptr) return false;
846841
TracyCPlot(ptr, value);
847842
return true;
@@ -850,8 +845,8 @@ PYBIND11_MODULE(TracyClientBindings, m) {
850845
m.def(
851846
"plot",
852847
[](std::size_t id, float value) {
853-
if (!tracy::IsEnabled()) return true;
854-
auto ptr = NameBuffer::Get(id);
848+
if (!TracyCEnabled()) return true;
849+
auto ptr = TracyCNameBufferGet(id);
855850
if (!ptr) return false;
856851
TracyCPlotF(ptr, value);
857852
return true;
@@ -860,8 +855,8 @@ PYBIND11_MODULE(TracyClientBindings, m) {
860855
m.def(
861856
"plot",
862857
[](std::size_t id, int64_t value) {
863-
if (!tracy::IsEnabled()) return true;
864-
auto ptr = NameBuffer::Get(id);
858+
if (!TracyCEnabled()) return true;
859+
auto ptr = TracyCNameBufferGet(id);
865860
if (!ptr) return false;
866861
TracyCPlotI(ptr, value);
867862
return true;

0 commit comments

Comments
 (0)