SWDEV-389161:Adding fix for tensorflow app hang with rocprofv2
Change-Id: I48ade2e853468940aa5e1fca58ba22f686d89f26
This commit is contained in:
committed by
Ammar ELWazir
parent
b817742d89
commit
cdc74d31db
@@ -21,6 +21,7 @@ target_include_directories(rocprofiler_tool
|
||||
${PROJECT_SOURCE_DIR}/src)
|
||||
|
||||
target_compile_definitions(rocprofiler_tool
|
||||
PUBLIC AMD_INTERNAL_BUILD
|
||||
PRIVATE HIP_PROF_HIP_API_STRING=1 __HIP_PLATFORM_HCC__=1)
|
||||
|
||||
if(ASAN)
|
||||
|
||||
+167
-10
@@ -22,6 +22,11 @@
|
||||
#include <dlfcn.h>
|
||||
#include <fcntl.h>
|
||||
#include <hsa/hsa.h>
|
||||
#include <hsa/hsa_ext_amd.h>
|
||||
#include "hsa_prof_str.h"
|
||||
#include <hip/hip_runtime.h>
|
||||
#include <hip/amd_detail/hip_prof_str.h>
|
||||
|
||||
#include "rocprofiler.h"
|
||||
#include "rocprofiler_plugin.h"
|
||||
#include <sys/syscall.h>
|
||||
@@ -52,6 +57,7 @@
|
||||
#include <chrono>
|
||||
|
||||
#include "utils/helper.h"
|
||||
#include "trace_buffer.h"
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
|
||||
@@ -60,7 +66,7 @@ namespace fs = std::experimental::filesystem;
|
||||
do { \
|
||||
if ((call) != ROCPROFILER_STATUS_SUCCESS) rocmtools::fatal("Error: ROCProfiler API Call Error!"); \
|
||||
} while (false)
|
||||
|
||||
TRACE_BUFFER_INSTANTIATE();
|
||||
namespace {
|
||||
|
||||
struct shmd_t {
|
||||
@@ -93,15 +99,12 @@ class rocprofiler_plugin_t {
|
||||
reinterpret_cast<decltype(rocprofiler_plugin_write_buffer_records)*>(
|
||||
dlsym(plugin_handle_, "rocprofiler_plugin_write_buffer_records"));
|
||||
if (!rocprofiler_plugin_write_buffer_records_) return;
|
||||
|
||||
rocprofiler_plugin_write_record_ = reinterpret_cast<decltype(rocprofiler_plugin_write_record)*>(
|
||||
dlsym(plugin_handle_, "rocprofiler_plugin_write_record"));
|
||||
if (!rocprofiler_plugin_write_record_) return;
|
||||
|
||||
rocprofiler_plugin_finalize_ = reinterpret_cast<decltype(rocprofiler_plugin_finalize)*>(
|
||||
dlsym(plugin_handle_, "rocprofiler_plugin_finalize"));
|
||||
if (!rocprofiler_plugin_finalize_) return;
|
||||
|
||||
if (auto* initialize = reinterpret_cast<decltype(rocprofiler_plugin_initialize)*>(
|
||||
dlsym(plugin_handle_, "rocprofiler_plugin_initialize"));
|
||||
initialize != nullptr)
|
||||
@@ -123,8 +126,7 @@ class rocprofiler_plugin_t {
|
||||
assert(is_valid());
|
||||
return rocprofiler_plugin_write_buffer_records_(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
bool valid_{false};
|
||||
void* plugin_handle_;
|
||||
|
||||
@@ -135,6 +137,77 @@ class rocprofiler_plugin_t {
|
||||
|
||||
std::optional<rocprofiler_plugin_t> plugin;
|
||||
|
||||
struct hsa_api_trace_entry_t {
|
||||
std::atomic<uint32_t> valid;
|
||||
rocprofiler_record_tracer_t record;
|
||||
const char* function_name;
|
||||
hsa_api_trace_entry_t(rocprofiler_record_tracer_t tracer_record, const char* function_name_str,
|
||||
const hsa_api_data_t& data)
|
||||
: valid(rocprofiler::TRACE_ENTRY_INIT) {
|
||||
record = tracer_record;
|
||||
function_name = function_name_str ? strdup(function_name_str) : nullptr;
|
||||
record.api_data_handle.handle = &data;
|
||||
}
|
||||
~hsa_api_trace_entry_t() { if (function_name != nullptr) free(const_cast<char*>(function_name)); }
|
||||
};
|
||||
|
||||
struct roctx_trace_entry_t {
|
||||
std::atomic<rocprofiler::TraceEntryState> valid;
|
||||
rocprofiler_record_tracer_t record;
|
||||
roctx_trace_entry_t(rocprofiler_record_tracer_t tracer_record, const char* roctx_message_str)
|
||||
: valid(rocprofiler::TRACE_ENTRY_INIT) {
|
||||
record = tracer_record;
|
||||
roctx_message_str? record.api_data_handle.handle=strdup(roctx_message_str):nullptr;
|
||||
}
|
||||
~roctx_trace_entry_t() { }
|
||||
};
|
||||
|
||||
struct hip_api_trace_entry_t {
|
||||
|
||||
std::atomic<uint32_t> valid;
|
||||
rocprofiler_record_tracer_t record;
|
||||
const char* function_name;
|
||||
const char* kernel_name;
|
||||
|
||||
hip_api_trace_entry_t(rocprofiler_record_tracer_t tracer_record, const char* kernel_name_str,
|
||||
const char* function_name_str, const hip_api_data_t& data)
|
||||
: valid(rocprofiler::TRACE_ENTRY_INIT) {
|
||||
record = tracer_record;
|
||||
kernel_name = kernel_name_str ? strdup(kernel_name_str) : nullptr;
|
||||
function_name = function_name_str ? strdup(function_name_str) : nullptr;
|
||||
record.api_data_handle.handle=&data;
|
||||
}
|
||||
~hip_api_trace_entry_t() {
|
||||
if (function_name != nullptr) free(const_cast<char*>(function_name));
|
||||
if (kernel_name != nullptr) free(const_cast<char*>(kernel_name));
|
||||
}
|
||||
};
|
||||
|
||||
rocprofiler::TraceBuffer<hip_api_trace_entry_t> hip_api_buffer(
|
||||
"HIP API", 0x200000, [](hip_api_trace_entry_t* entry) {
|
||||
assert(plugin && "plugin is not initialized");
|
||||
rocprofiler_plugin_tracer_extra_data_t tracer_extra_data;
|
||||
tracer_extra_data.function_name = entry->function_name;
|
||||
tracer_extra_data.kernel_name = entry->kernel_name;
|
||||
plugin->write_callback_record(entry->record, tracer_extra_data);
|
||||
});
|
||||
rocprofiler::TraceBuffer<hsa_api_trace_entry_t> hsa_api_buffer(
|
||||
"HSA API", 0x200000, [](hsa_api_trace_entry_t* entry) {
|
||||
assert(plugin && "plugin is not initialized");
|
||||
rocprofiler_plugin_tracer_extra_data_t tracer_extra_data;
|
||||
tracer_extra_data.function_name = entry->function_name;
|
||||
plugin->write_callback_record(entry->record,
|
||||
tracer_extra_data);
|
||||
});
|
||||
rocprofiler::TraceBuffer<roctx_trace_entry_t> roctx_trace_buffer(
|
||||
"rocTX API", 0x200000, [](roctx_trace_entry_t* entry) {
|
||||
assert(plugin && "plugin is not initialized");
|
||||
rocprofiler_plugin_tracer_extra_data_t tracer_extra_data;
|
||||
tracer_extra_data.function_name = nullptr;
|
||||
plugin->write_callback_record(
|
||||
entry->record, tracer_extra_data);
|
||||
});
|
||||
|
||||
} // namespace
|
||||
|
||||
uint64_t getFlushIntervalFromEnv() {
|
||||
@@ -325,10 +398,94 @@ void plugins_load() {
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* A callback function for synchronous trace records.
|
||||
* This function queries the api infoemation and populates the
|
||||
* api_trace buffer and adds it to the trace buffer.
|
||||
*/
|
||||
void sync_api_trace_callback(rocprofiler_record_tracer_t tracer_record, rocprofiler_session_id_t session_id) {
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_HIP_API) {
|
||||
size_t kernel_name_size = 0, function_name_size = 0;
|
||||
char *function_name_c = nullptr, *kernel_name_c = nullptr;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info_size(
|
||||
session_id, ROCPROFILER_HIP_FUNCTION_NAME, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &function_name_size));
|
||||
if (function_name_size > 1) {
|
||||
//function_name_c = (char*)malloc(function_name_size);
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info(
|
||||
session_id, ROCPROFILER_HIP_FUNCTION_NAME, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &function_name_c));
|
||||
}
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info_size(
|
||||
session_id, ROCPROFILER_HIP_KERNEL_NAME, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &kernel_name_size));
|
||||
if (kernel_name_size > 1) {
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info(
|
||||
session_id, ROCPROFILER_HIP_KERNEL_NAME, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &kernel_name_c));
|
||||
}
|
||||
char* data = nullptr;
|
||||
size_t size = 0;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info_size(
|
||||
session_id, ROCPROFILER_HIP_API_DATA, tracer_record.api_data_handle, tracer_record.operation_id, &size));
|
||||
if(size > 0)
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info(
|
||||
session_id, ROCPROFILER_HIP_API_DATA, tracer_record.api_data_handle, tracer_record.operation_id, &data));
|
||||
hip_api_data_t hip_api_data = *reinterpret_cast<hip_api_data_t*>(data);
|
||||
//std::cout << "in api calback" << strlen(function_name_c) << "\t" << function_name_c << std::endl;
|
||||
hip_api_trace_entry_t& entry = hip_api_buffer.Emplace(
|
||||
tracer_record,
|
||||
(const char*)kernel_name_c ? strdup(kernel_name_c) : nullptr,
|
||||
(const char*)(function_name_c) ? strdup(function_name_c):nullptr,
|
||||
hip_api_data);
|
||||
entry.valid.store(rocprofiler::TRACE_ENTRY_COMPLETE, std::memory_order_release);
|
||||
}
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_HSA_API) {
|
||||
size_t function_name_size = 0;
|
||||
char *function_name_c = nullptr;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hsa_tracer_api_data_info_size(
|
||||
session_id, ROCPROFILER_HSA_FUNCTION_NAME, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &function_name_size));
|
||||
if (function_name_size > 1) {
|
||||
|
||||
void plugin_write_record(rocprofiler_record_tracer_t record, rocprofiler_session_id_t session_id) {
|
||||
if (plugin) plugin->write_callback_record(record, session_id);
|
||||
}
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hsa_tracer_api_data_info(
|
||||
session_id, ROCPROFILER_HSA_FUNCTION_NAME, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &function_name_c));
|
||||
}
|
||||
char* data = nullptr;
|
||||
size_t size=0;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info_size(
|
||||
session_id, ROCPROFILER_HIP_API_DATA, tracer_record.api_data_handle, tracer_record.operation_id, &size));
|
||||
CHECK_ROCPROFILER(rocprofiler_query_hip_tracer_api_data_info(
|
||||
session_id, ROCPROFILER_HIP_API_DATA, tracer_record.api_data_handle, tracer_record.operation_id, &data));
|
||||
hsa_api_data_t hsa_api_data = *reinterpret_cast<hsa_api_data_t*>(data);
|
||||
hsa_api_trace_entry_t& entry = hsa_api_buffer.Emplace(
|
||||
tracer_record,
|
||||
(const char*)(function_name_c),
|
||||
hsa_api_data);
|
||||
entry.valid.store(rocprofiler::TRACE_ENTRY_COMPLETE, std::memory_order_release);
|
||||
}
|
||||
if (tracer_record.domain == ACTIVITY_DOMAIN_ROCTX) {
|
||||
size_t roctx_message_size = 0;
|
||||
char *roctx_message_str = nullptr;
|
||||
uint64_t roctx_id=0;
|
||||
CHECK_ROCPROFILER(rocprofiler_query_roctx_tracer_api_data_info_size(
|
||||
session_id, ROCPROFILER_ROCTX_MESSAGE, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &roctx_message_size));
|
||||
if (roctx_message_size > 1) {
|
||||
roctx_message_str =(char*)malloc(roctx_message_size * sizeof(char));
|
||||
CHECK_ROCPROFILER(rocprofiler_query_roctx_tracer_api_data_info(
|
||||
session_id, ROCPROFILER_ROCTX_MESSAGE, tracer_record.api_data_handle,
|
||||
tracer_record.operation_id, &roctx_message_str));
|
||||
if (roctx_message_str)
|
||||
roctx_message_str ? rocmtools::cxx_demangle(std::string(strdup(roctx_message_str))).c_str() : nullptr;
|
||||
}
|
||||
roctx_trace_entry_t& entry = roctx_trace_buffer.Emplace(
|
||||
tracer_record,
|
||||
roctx_message_str);
|
||||
entry.valid.store(rocprofiler::TRACE_ENTRY_COMPLETE, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
void wait_for_amdsys() {
|
||||
while (amd_sys_handler.load(std::memory_order_relaxed)) {
|
||||
@@ -542,7 +699,7 @@ ROCPROFILER_EXPORT bool OnLoad(void* table, uint64_t runtime_version,
|
||||
apis_requested.size(), &filter_id, property));
|
||||
CHECK_ROCPROFILER(rocprofiler_set_filter_buffer(session_id, filter_id, buffer_id));
|
||||
CHECK_ROCPROFILER(
|
||||
rocprofiler_set_api_trace_sync_callback(session_id, filter_id, plugin_write_record));
|
||||
rocprofiler_set_api_trace_sync_callback(session_id, filter_id, sync_api_trace_callback));
|
||||
filter_ids.emplace_back(filter_id);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
/* Copyright (c) 2018-2022 Advanced Micro Devices, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
|
||||
#ifndef TOOL_TRACE_BUFFER_H_
|
||||
#define TOOL_TRACE_BUFFER_H_
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace rocprofiler {
|
||||
|
||||
class TraceBufferBase {
|
||||
public:
|
||||
static void FlushAll() {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
for (auto* trace_buffer = head_; trace_buffer != nullptr; trace_buffer = trace_buffer->next_)
|
||||
trace_buffer->Flush();
|
||||
}
|
||||
|
||||
static void Register(TraceBufferBase* elem) {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
auto** prev_ptr = &head_;
|
||||
while (*prev_ptr != nullptr && elem->priority_ > (*prev_ptr)->priority_)
|
||||
prev_ptr = &(*prev_ptr)->next_;
|
||||
|
||||
elem->next_ = *prev_ptr;
|
||||
*prev_ptr = elem;
|
||||
}
|
||||
|
||||
static void Unregister(TraceBufferBase* elem) {
|
||||
std::lock_guard lock(mutex_);
|
||||
|
||||
auto** prev_ptr = &head_;
|
||||
while (*prev_ptr != nullptr && *prev_ptr != elem) prev_ptr = &(*prev_ptr)->next_;
|
||||
|
||||
assert(*prev_ptr != nullptr && "elem is not in the list");
|
||||
*prev_ptr = elem->next_;
|
||||
}
|
||||
|
||||
TraceBufferBase(std::string name, int priority)
|
||||
: name_(std::move(name)), priority_(priority), next_(nullptr) {}
|
||||
|
||||
TraceBufferBase(const TraceBufferBase&) = delete;
|
||||
TraceBufferBase& operator=(const TraceBufferBase&) = delete;
|
||||
|
||||
virtual ~TraceBufferBase() { Unregister(this); }
|
||||
|
||||
virtual void Flush() = 0;
|
||||
|
||||
std::string name() && { return std::move(name_); }
|
||||
const std::string& name() const& { return name_; }
|
||||
|
||||
private:
|
||||
const std::string name_;
|
||||
const int priority_;
|
||||
TraceBufferBase* next_;
|
||||
|
||||
static TraceBufferBase* head_;
|
||||
static std::mutex mutex_;
|
||||
};
|
||||
|
||||
enum TraceEntryState { TRACE_ENTRY_INVALID = 0, TRACE_ENTRY_INIT = 1, TRACE_ENTRY_COMPLETE = 2 };
|
||||
|
||||
template <typename Entry, typename Allocator = std::allocator<Entry>>
|
||||
class TraceBuffer : protected TraceBufferBase {
|
||||
public:
|
||||
using callback_t = std::function<void(Entry*)>;
|
||||
|
||||
TraceBuffer(std::string name, uint64_t size, callback_t flush_callback, int priority = 0)
|
||||
: TraceBufferBase(std::move(name), priority),
|
||||
flush_callback_(std::move(flush_callback)),
|
||||
size_(size) {
|
||||
assert(size_ != 0 && "cannot create an empty trace buffer");
|
||||
|
||||
Entry* write_buffer = allocator_.allocate(size_);
|
||||
assert(write_buffer != nullptr);
|
||||
buffer_list_.push_back(write_buffer);
|
||||
|
||||
read_index_ = 0;
|
||||
write_index_ = {0, write_buffer};
|
||||
|
||||
AllocateFreeBuffer();
|
||||
|
||||
// Add this instance to the link list of all trace buffers in the process.
|
||||
Register(this);
|
||||
}
|
||||
|
||||
~TraceBuffer() override {
|
||||
// Flush the remaining records. After flushing, there should not be any records left in the
|
||||
// trace buffer.
|
||||
Flush();
|
||||
assert(read_index_ == write_index_.load().index);
|
||||
|
||||
// Acquire both the writer and worker lock as we are accessing shared variables they protect.
|
||||
std::unique_lock writer_lock(write_mutex_, std::defer_lock);
|
||||
std::unique_lock worker_lock(worker_mutex_, std::defer_lock);
|
||||
std::lock(writer_lock, worker_lock);
|
||||
|
||||
// Deallocate the buffers.
|
||||
allocator_.deallocate(write_index_.load().buffer, size_);
|
||||
allocator_.deallocate(free_buffer_, size_);
|
||||
|
||||
// Stop the worker thread. The worker thread loop checks the 'worker_thread_' std::optional
|
||||
// after waking up, and exits if it does not have a value.
|
||||
if (worker_thread_) {
|
||||
std::thread worker_thread = std::move(worker_thread_.value());
|
||||
{
|
||||
// Tell the worker thread loop to exit.
|
||||
worker_thread_.reset();
|
||||
free_buffer_ = nullptr;
|
||||
worker_cond_.notify_one();
|
||||
}
|
||||
// Release the worker lock to allow the worker thread to exit.
|
||||
worker_lock.unlock();
|
||||
worker_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
// Flush all entries between read_pointer and write_pointer. read_pointer and write_pointer are
|
||||
// monotonically increasing indices, with read_pointer % size always indexing inside the first
|
||||
// buffer in the list. Stop flushing if an incomplete entry is found, it will be flushed with
|
||||
// the next invocation after changing its state to 'complete'.
|
||||
void Flush() override {
|
||||
std::lock_guard lock(write_mutex_);
|
||||
auto write_index = write_index_.load(std::memory_order_relaxed);
|
||||
|
||||
for (auto it = buffer_list_.begin(); it != buffer_list_.end();) {
|
||||
auto end_of_buffer = read_index_ - read_index_ % size_ + size_;
|
||||
|
||||
while (read_index_ < std::min(write_index.index, end_of_buffer)) {
|
||||
Entry* entry = &(*it)[read_index_ % size_];
|
||||
|
||||
// The entry is not yet complete, stop flushing here.
|
||||
if (entry->valid.load(std::memory_order_acquire) != TRACE_ENTRY_COMPLETE) return;
|
||||
|
||||
flush_callback_(entry);
|
||||
entry->~Entry();
|
||||
|
||||
++read_index_;
|
||||
}
|
||||
|
||||
// The buffer is still in use or the read pointer did not reach the end of the buffer.
|
||||
if (*it == write_index.buffer || read_index_ != end_of_buffer) return;
|
||||
|
||||
// All entries in the current buffer are now processed. Destroy the buffer and move onto the
|
||||
// next buffer in the list.
|
||||
allocator_.deallocate(*it, size_);
|
||||
it = buffer_list_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename... Args> Entry& Emplace(Args... args) {
|
||||
return *new (GetEntry()) Entry(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
private:
|
||||
Entry* GetEntry() {
|
||||
auto current = write_index_.load(std::memory_order_relaxed);
|
||||
|
||||
while (true) {
|
||||
// If the pointer is at the end of the current buffer, switch to the available free buffer and
|
||||
// notify the worker thread to allocate a new buffer.
|
||||
if (current.index != 0 && current.index % size_ == 0) {
|
||||
std::lock_guard lock(write_mutex_);
|
||||
|
||||
// If the worker thread wasn't already started, start it now. This avoids starting a new
|
||||
// thread when the trace buffer is created.
|
||||
if (!worker_thread_) {
|
||||
std::promise<void> ready;
|
||||
auto future = ready.get_future();
|
||||
{
|
||||
std::lock_guard worker_lock(worker_mutex_);
|
||||
worker_thread_.emplace(&TraceBuffer::WorkerThreadLoop, this, std::move(ready));
|
||||
}
|
||||
future.wait();
|
||||
}
|
||||
|
||||
// Re-check the pointer overflow under the writer lock, another thread could have beaten us
|
||||
// to it and already bumped the write_index_.
|
||||
current = write_index_.load(std::memory_order_relaxed);
|
||||
if (current.index % size_ == 0) {
|
||||
std::unique_lock worker_lock(worker_mutex_);
|
||||
|
||||
// Wait for the free buffer to become available.
|
||||
worker_cond_.wait(worker_lock, [this]() { return free_buffer_ != nullptr; });
|
||||
|
||||
current.buffer = free_buffer_;
|
||||
buffer_list_.push_back(current.buffer);
|
||||
write_index_.store({current.index + 1, current.buffer}, std::memory_order_relaxed);
|
||||
|
||||
// Tell the worker thread to allocate a new free buffer.
|
||||
free_buffer_ = nullptr;
|
||||
worker_cond_.notify_one();
|
||||
|
||||
// We successfully allocated a new buffer, return the first element.
|
||||
return ¤t.buffer[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (write_index_.compare_exchange_weak(current, {current.index + 1, current.buffer},
|
||||
std::memory_order_relaxed))
|
||||
return ¤t.buffer[current.index % size_];
|
||||
}
|
||||
}
|
||||
|
||||
void AllocateFreeBuffer() {
|
||||
assert(free_buffer_ == nullptr);
|
||||
|
||||
free_buffer_ = allocator_.allocate(size_);
|
||||
assert(free_buffer_ != nullptr);
|
||||
|
||||
for (size_t i = 0; i < size_; ++i)
|
||||
free_buffer_[i].valid.store(TRACE_ENTRY_INVALID, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void WorkerThreadLoop(std::promise<void> ready) {
|
||||
std::unique_lock lock(worker_mutex_);
|
||||
|
||||
// This worker thread is now ready to accept work.
|
||||
ready.set_value();
|
||||
|
||||
while (true) {
|
||||
worker_cond_.wait(lock, [this]() { return free_buffer_ == nullptr; });
|
||||
if (!worker_thread_) break;
|
||||
AllocateFreeBuffer();
|
||||
worker_cond_.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
// The WriteIndex is used to store both the index and the buffer associated with that index (the
|
||||
// buffer contains the trace buffer records at [index - index % size, index - index % size_t +
|
||||
// size_ - 1]) in a single atomic variable.
|
||||
struct WriteIndex {
|
||||
uint64_t index;
|
||||
Entry* buffer;
|
||||
};
|
||||
|
||||
const callback_t flush_callback_;
|
||||
const uint64_t size_;
|
||||
|
||||
uint64_t read_index_; // The index of the next record to flush.
|
||||
std::atomic<WriteIndex> write_index_; // The index of the next record that could be written.
|
||||
Entry* free_buffer_{nullptr}; // The next available free buffer.
|
||||
|
||||
std::optional<std::thread> worker_thread_;
|
||||
std::mutex worker_mutex_;
|
||||
std::condition_variable worker_cond_;
|
||||
|
||||
std::mutex write_mutex_;
|
||||
std::list<Entry*> buffer_list_;
|
||||
Allocator allocator_;
|
||||
};
|
||||
} // namespace rocprofiler
|
||||
|
||||
#define TRACE_BUFFER_INSTANTIATE() \
|
||||
rocprofiler::TraceBufferBase* rocprofiler::TraceBufferBase::head_ = nullptr; \
|
||||
std::mutex rocprofiler::TraceBufferBase::mutex_;
|
||||
|
||||
#endif // TOOL_TRACE_BUFFER_H_
|
||||
Reference in New Issue
Block a user