Skip to content
Merged
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
7 changes: 4 additions & 3 deletions docs/sphinx/manual/profiling_and_tracing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@ You can follow a task on its fiber row when it moves from one worker to
another. The color of a zone on a fiber row depends on the worker it ran on, so
a change of color means that the task moved.

|hpx| reuses thread objects. A later task that reuses the same thread object
appears on the same fiber row, so a fiber row can show more than one task under
one name.
|hpx| reuses thread objects. A later task with the same description that reuses
the same thread object appears on the same fiber row, so a fiber row can show
more than one task, one after the other, all with that description. A task with
a different description gets its own fiber row.

Messages
~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,17 @@ namespace hpx::threads {

#if defined(HPX_HAVE_TRACY)
private:
mutable char fiber_name_[64];
// Tracy keeps reading a fiber name after this object is reused for
// another task, so the name lives in a set that is never freed.
mutable char const* fiber_name_ = nullptr;
// 1-in-N tracing sample decision, set at ctor/rebind_base only.
bool emit_lifecycle_ = false;
#endif

public:
// With Tracy, this is nullptr until the task first runs while a
// profiler is connected, and always for stackless tasks, which don't
// get a fiber.
char const* get_fiber_name() const noexcept;

#if !defined(HPX_HAVE_THREAD_PARENT_REFERENCE)
Expand Down
19 changes: 10 additions & 9 deletions libs/core/threading_base/src/thread_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <memory>
#include <utility>
Expand Down Expand Up @@ -115,7 +114,7 @@ namespace hpx::threads {
#endif
set_timer_data(init_data.timer_data);
#if defined(HPX_HAVE_TRACY)
fiber_name_[0] = '\0';
fiber_name_ = nullptr;
// Decide once per task creation whether this task's lifecycle is
// sampled. Safe unlocked: the object is not yet observable.
emit_lifecycle_ = hpx::threads::detail::should_sample_next();
Expand Down Expand Up @@ -302,7 +301,7 @@ namespace hpx::threads {
#endif

#if defined(HPX_HAVE_TRACY)
fiber_name_[0] = '\0';
fiber_name_ = nullptr;
#endif

LTM_(debug).format("thread::thread({}), description({}), rebind", this,
Expand Down Expand Up @@ -393,13 +392,15 @@ namespace hpx::threads {
char const* thread_data::get_fiber_name() const noexcept
{
#if defined(HPX_HAVE_TRACY)
if (fiber_name_[0] == '\0')
// Making the name takes a lock, so it's done once per task and only
// when the task will enter a fiber. The suffix is the address of this
// object, so tasks running at the same time get different fibers.
if (fiber_name_ == nullptr && !is_stackless() &&
hpx::tracy::is_profiler_connected())
{
char const* name = get_safe_description(get_description(), "fiber");
// Use the HPX thread_id pointer as the unique numeric suffix
// so each HPX task gets its own fiber track in Tracy.
std::snprintf(fiber_name_, sizeof(fiber_name_), "%s_%p", name,
static_cast<void*>(get_thread_id().get()));
fiber_name_ = hpx::tracy::fiber_name(
get_safe_description(get_description(), "fiber"),
get_thread_id().get());
}
return fiber_name_;
#else
Expand Down
5 changes: 4 additions & 1 deletion libs/core/tracing/src/backends/tracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ namespace hpx::tracing {
fiber_region_init_data const& data,
std::size_t const num_thread) noexcept
{
bool const enabled = data.name != nullptr && !data.is_stackless;
// fiber_name is null if the profiler connected after the task's name
// was looked up; that task enters a fiber from its next switch on.
bool const enabled = data.name != nullptr &&
data.fiber_name != nullptr && !data.is_stackless;
char const* fiber_name = enabled ? data.fiber_name : nullptr;

// Use num_thread as color seed so each worker thread gets a distinct
Expand Down
7 changes: 7 additions & 0 deletions libs/core/tracy/include/hpx/tracy/tracy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ namespace hpx::tracy {
HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT void emit_appinfo(
char const* text, std::size_t size) noexcept;

// Tracy keys a fiber by the address of its name and asks for the text
// only once, later. This returns "<description>_<id>" from a set that is
// never freed, so the same text always gets the same, valid address. A
// description of 256 bytes or more is cut, with a number to keep it unique.
HPX_CXX_CORE_EXPORT HPX_CORE_EXPORT char const* fiber_name(
char const* description, void const* id) noexcept;

namespace detail {

HPX_CORE_EXPORT void enter_fiber(char const* fiber_name,
Expand Down
52 changes: 52 additions & 0 deletions libs/core/tracy/src/tracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>

#include <tracy/Tracy.hpp>
Expand Down Expand Up @@ -57,6 +59,56 @@ namespace hpx::tracy {
TracyCAppInfo(text, size);
}

char const* fiber_name(char const* description, void const* id) noexcept
{
// Tracy reads the name later from its own thread, without this lock.
// That is safe: a stored string is never changed, moved or freed, even
// when the set or the map grows.
struct names_type
{
// Not an HPX lock: this runs in the scheduling loop, with no HPX
// thread for hpx::mutex to suspend, HPX spinlocks emit Tracy lock
// events, and either would make hpx_tracy depend on modules that
// depend on it. It is held for one insert, once per task.
std::mutex mtx;
Comment thread
v4xsh marked this conversation as resolved.
Resolved
std::unordered_set<std::string> names;
std::unordered_map<std::string, std::string> long_names;
};
static names_type* const names = new names_type;

// Tracy sends a name with a 16-bit length and asserts or cuts it
// above that, and its UI can't show long names anyway.
constexpr std::size_t max_description = 256;

char suffix[32];
std::snprintf(suffix, sizeof(suffix), "_%p", id);
std::size_t const size = std::strlen(description);
std::string name(description, size);
name += suffix;

std::lock_guard<std::mutex> l(names->mtx);
if (size < max_description)
{
return names->names.insert(HPX_MOVE(name)).first->c_str();
}

// A long description is cut to max_description bytes ending in a
// number no other cut name has, so two descriptions with the same
// start still get different names, and no cut name equals a short one.
// The map is keyed by the full name, so a task keeps its cut name.
auto it = names->long_names.find(name);
if (it == names->long_names.end())
{
std::string const number =
"~" + std::to_string(names->long_names.size());
std::string cut(description, max_description - number.size());
cut += number;
cut += suffix;
it = names->long_names.emplace(HPX_MOVE(name), HPX_MOVE(cut)).first;
}
return it->second.c_str();
}

namespace detail {

// Expose Tracy fibers support
Expand Down
18 changes: 18 additions & 0 deletions libs/core/tracy/tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

set(tests fiber_name)

foreach(test ${tests})
set(sources ${test}.cpp)

source_group("Source Files" FILES ${sources})

add_hpx_executable(
${test}_test INTERNAL_FLAGS
SOURCES ${sources}
EXCLUDE_FROM_ALL
HPX_PREFIX ${HPX_BUILD_PREFIX}
FOLDER "Tests/Unit/Modules/Core/Tracy"
)

add_hpx_unit_test("modules.tracy" ${test})
endforeach()

if(HPX_WITH_COMPILE_ONLY_TESTS)
set(compile_tests compile_active_tracy)

Expand Down
86 changes: 86 additions & 0 deletions libs/core/tracy/tests/unit/fiber_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2026 The STE||AR Group
// Copyright (c) 2026 Vansh Dobhal
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// Tracy keys a fiber by the address of its name and reads the text later.
// HPX reuses a thread object for many tasks, so the name of a task on a reused
// object must get its own address, and earlier names must not change. This
// needs no Tracy server.

#include <hpx/modules/testing.hpp>
#include <hpx/modules/tracy.hpp>

#include <cstddef>
#include <cstdio>
#include <cstring>
#include <string>

int main()
{
// The address of a local stands in for a reused HPX thread object. Names
// are compared as addresses, as Tracy does, so a failure prints those.
int thread_object = 0;
void const* id = &thread_object;
auto const key = [](char const* description, void const* object) {
return static_cast<void const*>(
hpx::tracy::fiber_name(description, object));
};
auto const text = [](void const* name) {
return std::string(static_cast<char const*>(name));
};

char suffix[32];
std::snprintf(suffix, sizeof(suffix), "_%p", id);

void const* a = key("task_a", id);
void const* b = key("task_b", id);

// Two tasks on the same thread object get different fibers, and each
// name says which task and thread object it belongs to.
HPX_TEST_NEQ(a, b);
HPX_TEST_EQ(text(a), std::string("task_a") + suffix);
HPX_TEST_EQ(text(b), std::string("task_b") + suffix);

// The same task name on the same thread object keeps its fiber, so
// later tasks of that kind share the row instead of adding new ones.
HPX_TEST_EQ(a, key("task_a", id));

// Many more names make the set rehash. Earlier names must keep their
// address and text, as Tracy may read them at any time.
for (int i = 0; i != 10000; ++i)
{
(void) key(std::to_string(i).c_str(), id);
}
HPX_TEST_EQ(a, key("task_a", id));
HPX_TEST_EQ(text(a), std::string("task_a") + suffix);

// The old 64-byte buffer cut long names short. A long name keeps its
// suffix now, so it still names one thread object.
std::string const long_name(200, 'x');
HPX_TEST_EQ(text(key(long_name.c_str(), id)), long_name + suffix);

// Tracy can't send a name of 64 KB or more, so a very long description
// is cut to the cap in tracy.cpp, but the suffix stays whole.
std::size_t const cap = 256;
std::size_t const suffix_size = std::strlen(suffix);
std::string const long_a = std::string(100000, 'y') + "a";
std::string const long_b = std::string(100000, 'y') + "b";
void const* la = key(long_a.c_str(), id);
void const* lb = key(long_b.c_str(), id);
std::string const text_a = text(la);
HPX_TEST_LTE(text_a.size(), cap + suffix_size);
HPX_TEST_EQ(text_a.compare(0, 100, long_a, 0, 100), 0);
HPX_TEST_EQ(
text_a.compare(text_a.size() - suffix_size, suffix_size, suffix), 0);

// Descriptions that differ only after the cut are still different tasks,
// and a cut name stays the same for the same description.
HPX_TEST_NEQ(la, lb);
HPX_TEST_NEQ(text_a, text(lb));
HPX_TEST_EQ(la, key(long_a.c_str(), id));

return hpx::util::report_errors();
}
Loading