Skip to content

Commit 9e4af3a

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 9e4af3a

File tree

11 files changed

+159
-108
lines changed

11 files changed

+159
-108
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()

manual/tracy.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,15 +2316,15 @@ \subsubsection{Bindings}
23162316
\subsubsection{Building the Python package}
23172317

23182318
To build the Python package, you will need to use the CMake build system to compile the Tracy-Client.
2319-
The CMake option \texttt{-D TRACY\_CLIENT\_PYTHON=ON} is used to enable the generation of the Python bindings in conjunction with a mandatory creation of a shared Tracy-Client library via one of the CMake options \texttt{-D BUILD\_SHARED\_LIBS=ON} or \texttt{-D DEFAULT\_STATIC=OFF}.
2319+
The CMake option \texttt{-D TRACY\_CLIENT\_PYTHON=ON} is used to enable the generation of the Python bindings in conjunction with a mandatory creation of a shared Tracy-Client library via one of the CMake options \texttt{-D BUILD\_SHARED\_LIBS=ON} or \texttt{-D DEFAULT\_STATIC=OFF}. Moreover, the tracy name buffer needs to be built into the client via \texttt{-D TRACY\_NAME\_BUFFER=ON}.
23202320

23212321
The following other variables are available in addition:
23222322

23232323
\begin{itemize}
23242324
\item \texttt{EXTERNAL\_PYBIND11} --- Can be used to disable the download of pybind11 when Tracy is embedded in another CMake project that already uses pybind11.
23252325
\item \texttt{TRACY\_CLIENT\_PYTHON\_TARGET} --- Optional directory to copy Tracy Python bindings to when Tracy is embedded in another CMake project.
2326-
\item \texttt{BUFFER\_SIZE} --- The size of the global pointer buffer (defaults to 128) for naming Tracy profiling entities like frame marks, plots, and memory locations.
2327-
\item \texttt{NAME\_LENGTH} --- The maximum length (defaults to 128) of a name stored in the global pointer buffer.
2326+
\item \texttt{TRACY\_BUFFER\_SIZE} --- The size of the global pointer buffer (defaults to 128) for naming Tracy profiling entities like frame marks, plots, and memory locations.
2327+
\item \texttt{TRACY\_NAME\_LENGTH} --- The maximum length (defaults to 128) of a name stored in the global pointer buffer.
23282328
\end{itemize}
23292329

23302330
Be aware that the memory allocated by this buffer is global and is not freed, see section~\ref{uniquepointers}.
@@ -2334,7 +2334,7 @@ \subsubsection{Building the Python package}
23342334
\begin{lstlisting}
23352335
mkdir build
23362336
cd build
2337-
cmake -DTRACY_STATIC=OFF -DTRACY_CLIENT_PYTHON=ON ../
2337+
cmake -DTRACY_STATIC=OFF -DTRACY_NAME_BUFFER=ON -DTRACY_CLIENT_PYTHON=ON ../
23382338
\end{lstlisting}
23392339

23402340
Once this has finished building the Python package can be built as follows:

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 &,

0 commit comments

Comments
 (0)