Skip to content

Commit

Permalink
Modify async stacks to keep track of a vector of ASRs (#642)
Browse files Browse the repository at this point in the history
* Modify async stacks to keep track of a vector of ASRs

* Address comments

* Fix errors

* Address comments

* address comment

* address comments

* Remove threadid changes; will be added in a separate commit for portability
  • Loading branch information
jesswong authored Nov 26, 2024
1 parent c57bff3 commit ef9db15
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
1 change: 1 addition & 0 deletions include/unifex/tracing/async_stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <atomic>
#include <cassert>
#include <cstdint>
#include <thread>

#include <unifex/detail/prologue.hpp>

Expand Down
78 changes: 65 additions & 13 deletions source/async_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
#include <cassert>
#include <mutex>

#if !defined(UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD)
#if defined(__linux__)
# define FOLLY_ASYNC_STACK_ROOT_USE_PTHREAD 1
# define UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD 1
#else
# define FOLLY_ASYNC_STACK_ROOT_USE_PTHREAD 0
// defaults to using vector to store AsyncStackRoots instead of a pthread key
# define UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD 0
#endif
#endif

#if FOLLY_ASYNC_STACK_ROOT_USE_PTHREAD
#if UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD

# include <pthread.h>

Expand All @@ -38,11 +41,52 @@ extern "C" {
// Initialise to some value that will be interpreted as an invalid key.
inline pthread_key_t folly_async_stack_root_tls_key = 0xFFFF'FFFFu;
}

#endif // FOLLY_ASYNC_STACK_ROOT_USE_PTHREAD
#else
# include <vector>
#endif //UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD

namespace unifex {

#if UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0

struct AsyncStackRootHolderList {
std::vector<void*> asyncStackRootHolders_;
std::mutex mutex_;

AsyncStackRootHolderList() {
// reserve space to avoid frequent reallocations
asyncStackRootHolders_.reserve(50);
}

void add(void* holder) {
std::lock_guard<std::mutex> lock(mutex_);
asyncStackRootHolders_.push_back(holder);
}

void remove(void* holder) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = std::find(asyncStackRootHolders_.begin(), asyncStackRootHolders_.end(), holder);
if (it != asyncStackRootHolders_.end()) {
std::swap(*it, asyncStackRootHolders_.back());
asyncStackRootHolders_.pop_back();
}
}

std::vector<void*> getAsyncStackRoots() {
if (!mutex_.try_lock()) {
// assume we crashed holding the lock and give up
return {};
}
std::lock_guard<std::mutex> lock(mutex_, std::adopt_lock);
return asyncStackRootHolders_;
}
};

extern "C" {
auto* kUnifexAsyncStackRootHolderList = new AsyncStackRootHolderList();
}
#endif // UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD == 0

namespace {

// Folly's async stack library uses Folly's benchmarking tools to force a
Expand All @@ -51,7 +95,7 @@ namespace {
UNIFEX_NO_INLINE void compiler_must_not_elide(instruction_ptr) {
}

#if FOLLY_ASYNC_STACK_ROOT_USE_PTHREAD
#if UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD
static pthread_once_t initialiseTlsKeyFlag = PTHREAD_ONCE_INIT;

static void ensureAsyncRootTlsKeyIsInitialised() noexcept {
Expand All @@ -61,18 +105,26 @@ static void ensureAsyncRootTlsKeyIsInitialised() noexcept {
UNIFEX_ASSERT(result == 0);
});
}

#endif

struct AsyncStackRootHolder {
#if FOLLY_ASYNC_STACK_ROOT_USE_PTHREAD

AsyncStackRootHolder() noexcept {
ensureAsyncRootTlsKeyIsInitialised();
[[maybe_unused]] const int result =
pthread_setspecific(folly_async_stack_root_tls_key, this);
UNIFEX_ASSERT(result == 0);
#if UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD
ensureAsyncRootTlsKeyIsInitialised();
[[maybe_unused]] const int result =
pthread_setspecific(folly_async_stack_root_tls_key, this);
UNIFEX_ASSERT(result == 0);
#else
kUnifexAsyncStackRootHolderList->add(this);
#endif
}
#endif

#if !UNIFEX_ASYNC_STACK_ROOT_USE_PTHREAD
~AsyncStackRootHolder() noexcept {
kUnifexAsyncStackRootHolderList->remove(this);
}
#endif

AsyncStackRoot* get() const noexcept {
return value.load(std::memory_order_relaxed);
Expand Down

0 comments on commit ef9db15

Please sign in to comment.