Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add name buffer to tracy client library #784

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ set_option(TRACY_TIMER_FALLBACK "Use lower resolution timers" OFF)
set_option(TRACY_LIBUNWIND_BACKTRACE "Use libunwind backtracing where supported" OFF)
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)
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)
set_option(TRACY_NAME_BUFFER "Enable name buffer for other languages" OFF)

if(NOT TRACY_STATIC)
target_compile_definitions(TracyClient PRIVATE TRACY_EXPORTS)
Expand Down Expand Up @@ -141,6 +142,16 @@ set(common_includes
${TRACY_PUBLIC_DIR}/common/TracyUwp.hpp
${TRACY_PUBLIC_DIR}/common/TracyYield.hpp)

if(TRACY_NAME_BUFFER)
set(TRACY_BUFFER_SIZE 128 CACHE STRING "The size of the name buffer")
set(TRACY_NAME_LENGTH 128 CACHE STRING "The length of a name in the buffer")

list(APPEND common_includes ${TRACY_PUBLIC_DIR}/common/TracyNameBuffer.hpp)

target_compile_definitions(TracyClient PRIVATE TRACY_BUFFER_SIZE=${TRACY_BUFFER_SIZE})
target_compile_definitions(TracyClient PRIVATE TRACY_NAME_LENGTH=${TRACY_NAME_LENGTH})
endif()

install(TARGETS TracyClient
EXPORT TracyConfig
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
Expand Down Expand Up @@ -169,6 +180,9 @@ if(TRACY_CLIENT_PYTHON)
if(TRACY_STATIC)
message(FATAL_ERROR "Python-bindings require a shared client library")
endif()
if(NOT TRACY_NAME_BUFFER)
message(FATAL_ERROR "Python-bindings require name buffer being enabled")
endif()

add_subdirectory(python)
endif()
8 changes: 4 additions & 4 deletions manual/tracy.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2316,15 +2316,15 @@ \subsubsection{Bindings}
\subsubsection{Building the Python package}

To build the Python package, you will need to use the CMake build system to compile the Tracy-Client.
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}.
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}.

The following other variables are available in addition:

\begin{itemize}
\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.
\item \texttt{TRACY\_CLIENT\_PYTHON\_TARGET} --- Optional directory to copy Tracy Python bindings to when Tracy is embedded in another CMake project.
\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.
\item \texttt{NAME\_LENGTH} --- The maximum length (defaults to 128) of a name stored in the global pointer buffer.
\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.
\item \texttt{TRACY\_NAME\_LENGTH} --- The maximum length (defaults to 128) of a name stored in the global pointer buffer.
\end{itemize}

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

Once this has finished building the Python package can be built as follows:
Expand Down
4 changes: 4 additions & 0 deletions public/TracyClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
# endif
#endif

#ifdef TRACY_NAME_BUFFER
#include "common/TracyNameBuffer.cpp"
#endif

#ifdef _MSC_VER
# pragma comment(lib, "ws2_32.lib")
# pragma comment(lib, "dbghelp.lib")
Expand Down
44 changes: 44 additions & 0 deletions public/common/TracyNameBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "TracyNameBuffer.hpp"
using namespace tracy;

#include "TracyApi.h"

#ifndef TRACY_BUFFER_SIZE
#define TRACY_BUFFER_SIZE = 128
#endif

#ifndef TRACY_NAME_LENGTH
#define TRACY_NAME_LENGTH = 128
#endif

NameBuffer::NameBuffer() : m_buffer(TRACY_BUFFER_SIZE, nullptr), m_index(0ul) {
for (std::size_t index = 0ul, end = m_buffer.size(); index < end; ++index)
m_buffer[index] = new char[TRACY_NAME_LENGTH];
}

BufferEntry NameBuffer::add( const std::string& name ) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_index >= TRACY_BUFFER_SIZE || name.size() > TRACY_NAME_LENGTH)
return std::make_pair(std::nullopt, nullptr);

auto index = m_index++;
name.copy(m_buffer[index], name.size());
return std::make_pair(index, m_buffer[index]);
}

const char* NameBuffer::get( uint16_t index ) {
std::lock_guard<std::mutex> lock(m_mutex);
if (index >= TRACY_BUFFER_SIZE) return nullptr;
return m_buffer[index];
}

#ifdef TRACY_NAME_BUFFER
TRACY_API const char* ___tracy_name_buffer_add( const char* name, uint16_t* id ) {
auto entry = NameBuffer::Add(name);
if (!entry.first) return nullptr;

if (id != nullptr) *id = *entry.first;
return entry.second;
}
TRACY_API const char* ___tracy_name_buffer_get( uint16_t id ) { return NameBuffer::Get(id); }
#endif
37 changes: 37 additions & 0 deletions public/common/TracyNameBuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <mutex>
#include <optional>
#include <string>
#include <vector>

namespace tracy {
using OptionalNumber = std::optional<uint16_t>;
using BufferEntry = std::pair<OptionalNumber, const char*>;

class NameBuffer {
public:
static inline BufferEntry Add( const std::string& name ) {
return getBuffer().add(name);
}

static inline const char* Get( uint16_t index ) {
return getBuffer().get(index);
}

private:
NameBuffer();

std::mutex m_mutex;
std::vector<char*> m_buffer;
std::size_t m_index;

static inline NameBuffer& getBuffer() {
static NameBuffer buffer;
return buffer;
}

BufferEntry add( const std::string& name );
const char* get( uint16_t index );
};
} // namespace tracy
4 changes: 4 additions & 0 deletions public/tracy/Tracy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@
# define TracyFiberLeave tracy::Profiler::LeaveFiber()
#endif

#ifdef TRACY_NAME_BUFFER
# include "../common/TracyNameBuffer.hpp"
#endif

#endif

#endif
17 changes: 17 additions & 0 deletions public/tracy/TracyC.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ TRACY_API void ___tracy_set_thread_name( const char* name );

#ifndef TRACY_ENABLE

#define TracyCEnabled() 0

typedef const void* TracyCZoneCtx;

typedef const void* TracyCLockCtx;
Expand Down Expand Up @@ -116,8 +118,15 @@ typedef const void* TracyCLockCtx;
# define TracyCFiberLeave
#endif

#ifdef TRACY_NAME_BUFFER
# define TracyCNameBufferAdd(name, id) 0
# define TracyCNameBufferGet(id) 0
#endif

#else

#define TracyCEnabled() 1

#ifndef TracyConcat
# define TracyConcat(x,y) TracyConcatIndirect(x,y)
#endif
Expand Down Expand Up @@ -408,6 +417,14 @@ TRACY_API void ___tracy_fiber_leave( void );
# define TracyCFiberLeave ___tracy_fiber_leave();
#endif

#ifdef TRACY_NAME_BUFFER
TRACY_API const char* ___tracy_name_buffer_add( const char* name, uint16_t* id );
TRACY_API const char* ___tracy_name_buffer_get( uint16_t id );

# define TracyCNameBufferAdd(name, id) ___tracy_name_buffer_add( name, id );
# define TracyCNameBufferGet(id) ___tracy_name_buffer_get( id );
#endif

#endif

#ifdef __cplusplus
Expand Down
6 changes: 0 additions & 6 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ if(EXTERNAL_PYBIND11)
FetchContent_MakeAvailable(pybind11)
endif()

set(BUFFER_SIZE 128 CACHE STRING "The size of the pointer buffer")
set(NAME_LENGTH 128 CACHE STRING "The length of a name in the buffer")

pybind11_add_module(TracyClientBindings SHARED bindings/Module.cpp)
target_link_libraries(TracyClientBindings PUBLIC TracyClient)
target_compile_definitions(TracyClientBindings PUBLIC BUFFER_SIZE=${BUFFER_SIZE})
target_compile_definitions(TracyClientBindings PUBLIC NAME_LENGTH=${NAME_LENGTH})

set(TRACY_PYTHON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tracy_client)
set(TRACY_LIB_SYMLINK $<TARGET_FILE_PREFIX:TracyClient>$<TARGET_FILE_BASE_NAME:TracyClient>$<TARGET_FILE_SUFFIX:TracyClient>)

Expand Down
3 changes: 2 additions & 1 deletion python/bindings/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <pybind11/pybind11.h>
namespace py = pybind11;

#include "NameBuffer.hpp"
#include "tracy/Tracy.hpp"
using namespace tracy;

using OptionalString = std::optional<std::string>;
using OptionalInt = std::optional<int>;
Expand Down Expand Up @@ -61,6 +61,7 @@ bool MemoryFree(const Type &type, const OptionalNumber &id = std::nullopt,
return true;
}
#else
using OptionalNumber = std::optional<uint16_t>;

template <typename Type = uint64_t>
OptionalNumber MemoryAllocate(const Type &, std::size_t, const OptionalString &,
Expand Down
Loading
Loading