Skip to content

Commit 95bb17f

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 a11c3d4 commit 95bb17f

File tree

11 files changed

+158
-107
lines changed

11 files changed

+158
-107
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ set_option(TRACY_TIMER_FALLBACK "Use lower resolution timers" OFF)
140140
set_option(TRACY_LIBUNWIND_BACKTRACE "Use libunwind backtracing where supported" OFF)
141141
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)
142142
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)
143+
set_option(TRACY_NAME_BUFFER "Enable name buffer for other languages" OFF)
143144
set_option(TRACY_DEBUGINFOD "Enable debuginfod support" OFF)
144145

145146
# advanced
@@ -221,6 +222,16 @@ set(common_includes
221222
${TRACY_PUBLIC_DIR}/common/TracyWinFamily.hpp
222223
${TRACY_PUBLIC_DIR}/common/TracyYield.hpp)
223224

225+
if(TRACY_NAME_BUFFER)
226+
set(TRACY_BUFFER_SIZE 128 CACHE STRING "The size of the name buffer")
227+
set(TRACY_NAME_LENGTH 128 CACHE STRING "The length of a name in the buffer")
228+
229+
list(APPEND common_includes ${TRACY_PUBLIC_DIR}/common/TracyNameBuffer.hpp)
230+
231+
target_compile_definitions(TracyClient PRIVATE TRACY_BUFFER_SIZE=${TRACY_BUFFER_SIZE})
232+
target_compile_definitions(TracyClient PRIVATE TRACY_NAME_LENGTH=${TRACY_NAME_LENGTH})
233+
endif()
234+
224235
install(TARGETS TracyClient
225236
EXPORT TracyConfig
226237
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/$<IF:$<CONFIG:Release>,,$<CONFIG>>
@@ -277,6 +288,9 @@ if(TRACY_CLIENT_PYTHON)
277288
if(TRACY_STATIC)
278289
message(FATAL_ERROR "Python-bindings require a shared client library")
279290
endif()
291+
if(NOT TRACY_NAME_BUFFER)
292+
message(FATAL_ERROR "Python-bindings require name buffer being enabled")
293+
endif()
280294

281295
add_subdirectory(python)
282296
endif()

manual/tracy.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,7 +3630,7 @@ \subsubsection{Timeline view}
36303630
\item \emph{Gray} -- Threads assigned to other programs running in the system.
36313631
\end{itemize}
36323632

3633-
When the \faMousePointer{}~mouse pointer is hovered over either the CPU data zone or the thread timeline label, Tracy will display a line connecting all zones associated with the selected thread. This can be used to quickly see how the thread migrated across the CPU cores.
3633+
When the \faMousePointer{}~mouse pointer is hovered over either the CPU data zone or the thread timeline label, Tracy will display a line connecting all zones associated with the selected thread. This can be used to quickly see how the thread migrated across the CPU cores.
36343634

36353635
It will also add lines starting with a filed circle to denote wake up events. Those are useful to pinpoint the origin of a thread waking up, especially when holding locks. It may also start from an empty region, denoting the time at which the kernel chose to schedule or boost the priority of your thread. Wake ups will have a different color based on the reason for which the thread was waiting to be scheduled.
36363636

@@ -3668,7 +3668,7 @@ \subsubsection{Timeline view}
36683668

36693669
In the above picture, \emph{Thread B} migrates from \emph{Core 3} to \emph{Core 4} due to a wake up from \emph{Thread A}. Then it migrates from \emph{Core 4} to \emph{Core 1}.
36703670

3671-
Clicking the \LMB{}~left mouse button on a tracked thread will make it visible on the timeline if it was either hidden or collapsed before. It will also lock the selected thread so that you may pan and explore data while retaining the visualization of thread migrations and wake up events.
3671+
Clicking the \LMB{}~left mouse button on a tracked thread will make it visible on the timeline if it was either hidden or collapsed before. It will also lock the selected thread so that you may pan and explore data while retaining the visualization of thread migrations and wake up events.
36723672
Clicking again somewhere empty on the timeline with the \LMB{}~left mouse button will unlock the selection.
36733673

36743674
Careful examination of the data presented on this graph may allow you to determine areas where the profiled application was fighting for system resources with other programs (see section~\ref{checkenvironmentos}) or give you a hint to add more instrumentation macros.
@@ -4717,7 +4717,7 @@ \subsubsection{Model selection}
47174717
There are many factors to take into consideration when choosing a model to use. First, you should determine which model family you want to use:
47184718

47194719
\begin{itemize}
4720-
\item \emph{Gemma 3} (\url{https://blog.google/technology/developers/gemma-3/}) is a well rounded model that can converse in multiple languages.
4720+
\item \emph{Gemma 3} (\url{https://blog.google/technology/developers/gemma-3/}) is a well rounded model that can converse in multiple languages.
47214721
\item \emph{Qwen3} (\url{https://qwenlm.github.io/blog/qwen3/}) has a more technical feeling to it, it likes to write bullet point lists.
47224722
\item \emph{Mistral Small} (\url{https://mistral.ai/news/mistral-small-3-1}) may also be considered. Despite the name, it is not small.
47234723
\end{itemize}

public/TracyClient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#ifdef TRACY_ROCPROF
3636
# include "client/TracyRocprof.cpp"
3737
#endif
38+
39+
#ifdef TRACY_NAME_BUFFER
40+
#include "common/TracyNameBuffer.cpp"
41+
#endif
3842
#ifdef _MSC_VER
3943
# pragma comment(lib, "ws2_32.lib")
4044
# pragma comment(lib, "advapi32.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
@@ -273,6 +273,10 @@
273273
# define TracyFiberLeave tracy::Profiler::LeaveFiber()
274274
#endif
275275

276+
#ifdef TRACY_NAME_BUFFER
277+
# include "../common/TracyNameBuffer.hpp"
278+
#endif
279+
276280
#endif
277281

278282
#endif

public/tracy/TracyC.h

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

3737
#ifndef TRACY_ENABLE
3838

39+
#define TracyCEnabled() 0
40+
3941
typedef const void* TracyCZoneCtx;
4042

4143
typedef const void* TracyCLockCtx;
@@ -122,8 +124,15 @@ typedef const void* TracyCLockCtx;
122124
# define TracyCFiberLeave
123125
#endif
124126

127+
#ifdef TRACY_NAME_BUFFER
128+
# define TracyCNameBufferAdd(name, id) 0
129+
# define TracyCNameBufferGet(id) 0
130+
#endif
131+
125132
#else
126133

134+
#define TracyCEnabled() 1
135+
127136
#ifndef TracyConcat
128137
# define TracyConcat(x,y) TracyConcatIndirect(x,y)
129138
#endif
@@ -384,6 +393,14 @@ TRACY_API void ___tracy_fiber_leave( void );
384393
# define TracyCFiberLeave ___tracy_fiber_leave();
385394
#endif
386395

396+
#ifdef TRACY_NAME_BUFFER
397+
TRACY_API const char* ___tracy_name_buffer_add( const char* name, uint16_t* id );
398+
TRACY_API const char* ___tracy_name_buffer_get( uint16_t id );
399+
400+
# define TracyCNameBufferAdd(name, id) ___tracy_name_buffer_add( name, id );
401+
# define TracyCNameBufferGet(id) ___tracy_name_buffer_get( id );
402+
#endif
403+
387404
#endif
388405

389406
#ifdef __cplusplus

python/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ if(EXTERNAL_PYBIND11)
1111
FetchContent_MakeAvailable(pybind11)
1212
endif()
1313

14-
set(BUFFER_SIZE 128 CACHE STRING "The size of the pointer buffer")
15-
set(NAME_LENGTH 128 CACHE STRING "The length of a name in the buffer")
16-
1714
pybind11_add_module(TracyClientBindings SHARED bindings/Module.cpp)
1815
target_link_libraries(TracyClientBindings PUBLIC TracyClient)
1916
target_link_libraries(TracyClientBindings PUBLIC ${Python_LIBRARIES})
20-
target_compile_definitions(TracyClientBindings PUBLIC BUFFER_SIZE=${BUFFER_SIZE})
21-
target_compile_definitions(TracyClientBindings PUBLIC NAME_LENGTH=${NAME_LENGTH})
22-
2317
if (UNIX)
2418
set_target_properties(TracyClientBindings PROPERTIES
2519
BUILD_RPATH_USE_ORIGIN TRUE

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<int32_t>;
@@ -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)