Skip to content

tracing: add lock_context and migrate synchronization primitives to unified tracing#7264

Open
v4xsh wants to merge 10 commits into
TheHPXProject:masterfrom
v4xsh:tracing-lock-abstraction
Open

tracing: add lock_context and migrate synchronization primitives to unified tracing#7264
v4xsh wants to merge 10 commits into
TheHPXProject:masterfrom
v4xsh:tracing-lock-abstraction

Conversation

@v4xsh
Copy link
Copy Markdown
Contributor

@v4xsh v4xsh commented May 13, 2026

Proposed Changes

  • Add hpx::tracing::lock_context to the unified tracing abstraction layer with four methods: before_lock(), after_lock(), after_try_lock(bool), after_unlock() — Tracy branch delegates to hpx::tracy::lock_prepare, lock_acquired, lock_released; ITT and no-op branches are inline constexpr no-ops
  • Replace raw hpx::tracy::lock_data members and all #if defined(HPX_HAVE_MODULE_TRACY) guards in libs/core/synchronization/mutex.hpp, mutex.cpp, and spinlock.hpp with hpx::tracing::lock_context
  • Replace raw hpx::tracy::lock_data member and all #if defined(HPX_HAVE_MODULE_TRACY) guards in libs/core/concurrency/spinlock.hpp with hpx::tracing::lock_context
  • Use HPX_NO_UNIQUE_ADDRESS on lock_context members in all three call sites so non-Tracy builds have zero size overhead
  • Add hpx_tracing to MODULE_DEPENDENCIES in libs/core/synchronization/CMakeLists.txt and libs/core/concurrency/CMakeLists.txt
  • Introduce HPX_HAVE_TRACING compile-time macro (1 when Tracy or ITT active, 0 otherwise) to unify the constructor guards previously split across HPX_HAVE_ITTNOTIFY and HPX_HAVE_MODULE_TRACY

Checklist

  • I have added a new feature and have added tests to go along with it.
  • I have fixed a bug and have added a regression test.
  • I have added a test using random numbers; I have made sure it uses a seed, and that random numbers generated are valid inputs for the tests.

v4xsh added 4 commits May 13, 2026 22:05
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Copilot AI review requested due to automatic review settings May 13, 2026 16:38
@v4xsh v4xsh requested a review from hkaiser as a code owner May 13, 2026 16:38
@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented May 13, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@StellarBot
Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a hpx::tracing::lock_context abstraction that hides per-tracing-backend (Tracy / ITT / no-op) lock instrumentation behind a uniform interface, and migrates the synchronization primitives in libs/core/synchronization and libs/core/concurrency from raw hpx::tracy::lock_data members and per-call #if defined(HPX_HAVE_MODULE_TRACY) guards to that new abstraction. A new HPX_HAVE_TRACING macro unifies the previously split HPX_HAVE_ITTNOTIFY / HPX_HAVE_MODULE_TRACY constructor guards.

Changes:

  • Add hpx::tracing::lock_context (Tracy-backed; constexpr no-op in ITT/disabled builds) and the HPX_HAVE_TRACING macro in libs/core/tracing.
  • Replace hpx::tracy::lock_data members and HPX_HAVE_MODULE_TRACY guards in mutex and both spinlock types with HPX_NO_UNIQUE_ADDRESS lock_context.
  • Add hpx_tracing to the module dependency list of synchronization and concurrency.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
libs/core/tracing/include/hpx/tracing/tracing.hpp Adds lock_context declarations for Tracy/ITT/disabled branches and defines HPX_HAVE_TRACING.
libs/core/tracing/src/tracing.cpp Out-of-line implementations of lock_context delegating to hpx::tracy lock APIs.
libs/core/synchronization/include/hpx/synchronization/mutex.hpp Replaces tracy member/guards with a HPX_NO_UNIQUE_ADDRESS lock_context; switches constructor guard to HPX_HAVE_TRACING.
libs/core/synchronization/src/mutex.cpp Removes #if HPX_HAVE_MODULE_TRACY guards in lock/try_lock/unlock paths in favor of lock_context calls.
libs/core/synchronization/include/hpx/synchronization/spinlock.hpp Same migration for hpx::spinlock.
libs/core/concurrency/include/hpx/concurrency/spinlock.hpp Same migration for hpx::util::spinlock.
libs/core/synchronization/CMakeLists.txt Adds hpx_tracing module dependency.
libs/core/concurrency/CMakeLists.txt Adds hpx_tracing module dependency.
Comments suppressed due to low confidence (1)

libs/core/tracing/include/hpx/tracing/tracing.hpp:108

  • The tracy-branch lock_context deletes only the copy operations, which implicitly deletes the move operations as well — but the codebase convention (see e.g. hpx::util::detail::lock_data in libs/core/lock_registration/include/hpx/lock_registration/detail/register_locks.hpp:46-49) is to delete copy and move operations explicitly. Consider adding lock_context(lock_context&&) = delete; and lock_context& operator=(lock_context&&) = delete; for clarity and to make the intent explicit.
        lock_context(lock_context const&) = delete;
        lock_context& operator=(lock_context const&) = delete;

Comment on lines +27 to +36
#if HPX_HAVE_ITTNOTIFY != 0 || HPX_HAVE_TRACING != 0
mutex::mutex(char const* const description)
: owner_id_(threads::invalid_thread_id)
, context_(std::string("hpx::mutex") + description)
{
HPX_ITT_SYNC_CREATE(this, "hpx::mutex", description);
#if defined(HPX_HAVE_MODULE_TRACY)
context_ = hpx::tracy::create(std::string("hpx::mutex") + description);
#endif
}
#endif

#if HPX_HAVE_ITTNOTIFY != 0 || defined(HPX_HAVE_MODULE_TRACY)
#if HPX_HAVE_ITTNOTIFY != 0 || HPX_HAVE_TRACING != 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, we should define the HPX_HAVE_TRACING macro in the CMake build system. There we know if any of the tooling backends are enabled.

Comment thread libs/core/synchronization/src/mutex.cpp Outdated
Comment on lines +29 to +30
: owner_id_(threads::invalid_thread_id)
, context_(std::string("hpx::mutex") + description)
};

////////////////////////////////////////////////////////////////////////////
HPX_CXX_CORE_EXPORT struct HPX_CORE_EXPORT lock_context
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
}

explicit spinlock(char const* desc) noexcept
: context_(std::string("util::spinlock#") + desc)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, unconditionally creating a string is not a good idea as it involves an allocation. Can we do something like:

Suggested change
: context_(std::string("util::spinlock#") + desc)
: context_("util::spinlock#", desc)

and create the string(s) only if needed?


explicit spinlock(char const* const desc) noexcept
: v_(false)
, context_(std::string("hpx::spinlock#") + desc)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, let's avoid always creating strings.

Comment on lines +27 to +36
#if HPX_HAVE_ITTNOTIFY != 0 || HPX_HAVE_TRACING != 0
mutex::mutex(char const* const description)
: owner_id_(threads::invalid_thread_id)
, context_(std::string("hpx::mutex") + description)
{
HPX_ITT_SYNC_CREATE(this, "hpx::mutex", description);
#if defined(HPX_HAVE_MODULE_TRACY)
context_ = hpx::tracy::create(std::string("hpx::mutex") + description);
#endif
}
#endif

#if HPX_HAVE_ITTNOTIFY != 0 || defined(HPX_HAVE_MODULE_TRACY)
#if HPX_HAVE_ITTNOTIFY != 0 || HPX_HAVE_TRACING != 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, we should define the HPX_HAVE_TRACING macro in the CMake build system. There we know if any of the tooling backends are enabled.

#if defined(HPX_HAVE_MODULE_TRACY)
#include <hpx/modules/tracy.hpp>

#define HPX_HAVE_TRACING 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said, let's have this defined in the build system.

v4xsh added 3 commits May 14, 2026 13:20
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Copilot AI review requested due to automatic review settings May 14, 2026 07:51
@v4xsh
Copy link
Copy Markdown
Contributor Author

v4xsh commented May 14, 2026

@hkaiser addressed all three points — switched to the prefix/suffix constructor to avoid the allocation, moved HPX_HAVE_TRACING to CMake, and cleaned up the leftover ifdef in the mutex constructor. Let me know if anything else needs fixing!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

v4xsh added 2 commits May 14, 2026 13:39
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Signed-off-by: v4xsh <vanshdobhal11@gmail.com>
Copilot AI review requested due to automatic review settings May 14, 2026 08:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants