Skip to content

Commit

Permalink
Add name buffer to tracy client library
Browse files Browse the repository at this point in the history
To be used by Python and Go bindings to store const char * accessible via a lookup
  • Loading branch information
SeeChange-CI committed May 4, 2024
1 parent 6199b2f commit fbd9347
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 104 deletions.
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()
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
71 changes: 33 additions & 38 deletions python/bindings/Module.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
#include "Memory.hpp"
#include "ScopedZone.hpp"
#include "tracy/TracyC.h"
using namespace tracy;

namespace tracy {
#ifndef TRACY_ENABLE
enum class PlotFormatType : uint8_t { Number, Memory, Percentage };
#endif

constexpr static inline bool IsEnabled() {
#ifdef TRACY_ENABLE
return true;
#else
return false;
#endif
}
} // namespace tracy

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

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

py::enum_<tracy::Color::ColorType>(m, "ColorType")
.value("Snow", tracy::Color::Snow)
Expand Down Expand Up @@ -703,26 +696,26 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def(
"program_name",
[](const std::string &name) {
if (!tracy::IsEnabled()) return true;
auto entry = NameBuffer::Add(name);
if (!entry.first) return false;
TracySetProgramName(entry.second);
if (!TracyCEnabled()) return true;
auto ptr = TracyCNameBufferAdd(name.c_str(), nullptr);
if (!ptr) return false;
TracySetProgramName(ptr);
return true;
},
"name"_a.none(false));

m.def(
"thread_name",
[](const std::string &name) {
if (!tracy::IsEnabled()) return;
if (!TracyCEnabled()) return;
tracy::SetThreadName(name.c_str());
},
"name"_a.none(false));

m.def(
"app_info",
[](const std::string &text) {
if (!tracy::IsEnabled()) return true;
if (!TracyCEnabled()) return true;
if (text.size() >= std::numeric_limits<uint16_t>::max()) return false;
TracyAppInfo(text.c_str(), text.size());
return true;
Expand All @@ -732,7 +725,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def(
"message",
[](const std::string &message) {
if (!tracy::IsEnabled()) return true;
if (!TracyCEnabled()) return true;
if (message.size() >= std::numeric_limits<uint16_t>::max())
return false;
TracyMessage(message.c_str(), message.size());
Expand All @@ -743,7 +736,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def(
"message",
[](const std::string &message, uint32_t pColor) {
if (!tracy::IsEnabled()) return true;
if (!TracyCEnabled()) return true;
if (message.size() >= std::numeric_limits<uint16_t>::max())
return false;
TracyMessageC(message.c_str(), message.size(), pColor);
Expand All @@ -755,20 +748,21 @@ PYBIND11_MODULE(TracyClientBindings, m) {

m.def(
"frame_mark_start",
[](const std::string &name) {
if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
auto entry = NameBuffer::Add(name);
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
FrameMarkStart(entry.second);
return entry.first;
[](const std::string &name) -> OptionalNumber {
if (!TracyCEnabled()) return 0ul;
uint16_t id = 0ul;
auto ptr = TracyCNameBufferAdd(name.c_str(), &id);
if (!ptr) return static_cast<OptionalNumber>(std::nullopt);
FrameMarkStart(ptr);
return id;
},
"name"_a.none(false));

m.def(
"frame_mark_end",
[](std::size_t id) {
if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id);
if (!TracyCEnabled()) return true;
auto ptr = TracyCNameBufferGet(id);
if (!ptr) return false;
FrameMarkEnd(ptr);
return true;
Expand All @@ -779,7 +773,7 @@ PYBIND11_MODULE(TracyClientBindings, m) {
"frame_image",
[](const py::bytes &image, uint16_t width, uint16_t height,
uint8_t offset = 0, bool flip = false) {
if (!tracy::IsEnabled()) return true;
if (!TracyCEnabled()) return true;
if (width % 4 != 0 || height % 4 != 0) return false;
TracyCFrameImage(std::string(image).data(), width, height, offset,
flip);
Expand Down Expand Up @@ -821,12 +815,13 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def(
"_plot_config",
[](const std::string &name, int type, bool step, bool fill,
uint32_t color = 0) {
if (!tracy::IsEnabled()) return static_cast<OptionalNumber>(0ul);
auto entry = NameBuffer::Add(name);
if (!entry.first) return static_cast<OptionalNumber>(std::nullopt);
TracyCPlotConfig(entry.second, type, step, fill, color);
return entry.first;
uint32_t color = 0) -> OptionalNumber {
if (!TracyCEnabled()) return 0ul;
uint16_t id = 0ul;
auto ptr = TracyCNameBufferAdd(name.c_str(), &id);
if (!ptr) return static_cast<OptionalNumber>(std::nullopt);
TracyCPlotConfig(ptr, type, step, fill, color);
return id;
},
"name"_a.none(false), "type"_a.none(false), "step"_a.none(false),
"fill"_a.none(false), "color"_a.none(false));
Expand All @@ -840,8 +835,8 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def(
"plot",
[](std::size_t id, double value) {
if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id);
if (!TracyCEnabled()) return true;
auto ptr = TracyCNameBufferGet(id);
if (!ptr) return false;
TracyCPlot(ptr, value);
return true;
Expand All @@ -850,8 +845,8 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def(
"plot",
[](std::size_t id, float value) {
if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id);
if (!TracyCEnabled()) return true;
auto ptr = TracyCNameBufferGet(id);
if (!ptr) return false;
TracyCPlotF(ptr, value);
return true;
Expand All @@ -860,8 +855,8 @@ PYBIND11_MODULE(TracyClientBindings, m) {
m.def(
"plot",
[](std::size_t id, int64_t value) {
if (!tracy::IsEnabled()) return true;
auto ptr = NameBuffer::Get(id);
if (!TracyCEnabled()) return true;
auto ptr = TracyCNameBufferGet(id);
if (!ptr) return false;
TracyCPlotI(ptr, value);
return true;
Expand Down
Loading

0 comments on commit fbd9347

Please sign in to comment.