adding context pool

Change-Id: Id8c6ee43ac6c86480848a5d3e759c42730cf388a
This commit is contained in:
Evgeny
2019-03-03 01:04:00 -06:00
rodzic c6a20178dc
commit 632309a626
7 zmienionych plików z 514 dodań i 50 usunięć
+15 -5
Wyświetl plik
@@ -153,11 +153,20 @@ class Context {
public:
typedef std::map<std::string, rocprofiler_feature_t*> info_map_t;
static void Create(Context** context, const util::AgentInfo* agent_info, Queue* queue, rocprofiler_feature_t* info,
static void Create(Context* obj, const util::AgentInfo* agent_info, Queue* queue, rocprofiler_feature_t* info,
const uint32_t info_count, rocprofiler_handler_t handler, void* handler_arg)
{
*context = NULL;
new (obj) Context(agent_info, queue, info, info_count, handler, handler_arg);
obj->Construct(agent_info, queue, info, info_count, handler, handler_arg);
}
static void Release(Context* obj) { obj->Destruct(); }
static Context* Create(const util::AgentInfo* agent_info, Queue* queue, rocprofiler_feature_t* info,
const uint32_t info_count, rocprofiler_handler_t handler, void* handler_arg)
{
Context* obj = new Context(agent_info, queue, info, info_count, handler, handler_arg);
if (obj == NULL) EXC_RAISING(HSA_STATUS_ERROR, "allocation error");
try {
obj->Construct(agent_info, queue, info, info_count, handler, handler_arg);
} catch(...) {
@@ -165,7 +174,7 @@ class Context {
obj = NULL;
throw;
}
*context = obj;
return obj;
}
static void Destroy(Context* obj) { if (obj != NULL) delete obj; }
@@ -300,7 +309,9 @@ class Context {
handler_arg_(handler_arg)
{}
~Context() {
~Context() { Destruct(); }
void Destruct() {
for (const auto& v : info_map_) {
const std::string& name = v.first;
const rocprofiler_feature_t* info = v.second;
@@ -311,7 +322,6 @@ class Context {
}
}
void Construct(const util::AgentInfo* agent_info, Queue* queue, rocprofiler_feature_t* info,
const uint32_t info_count, rocprofiler_handler_t handler, void* handler_arg)
{
+193
Wyświetl plik
@@ -0,0 +1,193 @@
/******************************************************************************
Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
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 SRC_CORE_CONTEXT_POOL_H_
#define SRC_CORE_CONTEXT_POOL_H_
#include "inc/rocprofiler.h"
#include <thread>
#include "core/context.h"
namespace rocprofiler {
class ContextPool {
public:
typedef uint64_t index_t;
typedef std::mutex mutex_t;
struct entry_t {
ContextPool* pool;
Context* context;
std::atomic<bool> completed;
};
static ContextPool* Create(
uint32_t num_entries,
uint32_t payload_bytes,
const util::AgentInfo* agent_info,
rocprofiler_feature_t* info,
const uint32_t info_count,
rocprofiler_pool_handler_t handler,
void* handler_arg)
{
ContextPool* obj = new ContextPool(num_entries, payload_bytes, agent_info, info, info_count, handler, handler_arg);
if (obj == NULL) EXC_RAISING(HSA_STATUS_ERROR, "allocation error");
return obj;
}
static void Destroy(ContextPool* pool) { delete pool; }
void Fetch(rocprofiler_pool_entry_t* pool_entry) {
if (constructed_ == false) {
Construct(agent_info_, info_, info_count_);
}
const index_t write_index = write_index_.fetch_add(entry_size_bytes_, std::memory_order_relaxed);
while (write_index >= (read_index_.load(std::memory_order_acquire) + array_size_bytes_)) {
check_completed();
std::this_thread::yield();
}
entry_t* entry = GetPoolEntry(write_index, pool_entry);
if (entry->completed.load(std::memory_order_relaxed) != false) EXC_RAISING(HSA_STATUS_ERROR, "Corrupted pool entry");
}
void Flush() {
check_completed();
}
private:
static unsigned aligned64(const unsigned& size) { return (size + 0x3f) & ~0x3fu; }
static bool context_handler(rocprofiler_group_t group, void* arg) {
entry_t* entry = reinterpret_cast<entry_t*>(arg);
entry->completed.store(true, std::memory_order_release);
entry->pool->check_completed();
return true;
}
ContextPool(
uint32_t num_entries,
uint32_t payload_bytes,
const util::AgentInfo* agent_info,
rocprofiler_feature_t* info,
const uint32_t info_count,
rocprofiler_pool_handler_t pool_handler,
void* pool_handler_arg
) :
payload_off_(aligned64(sizeof(entry_t))),
entry_size_bytes_(payload_off_ + aligned64(payload_bytes)),
array_size_bytes_(entry_size_bytes_ * num_entries),
array_(NULL),
read_index_(0),
write_index_(0),
sync_flag_(false),
agent_info_(agent_info),
info_(info),
info_count_(info_count),
pool_handler_(pool_handler),
pool_handler_arg_(pool_handler_arg),
constructed_(false)
{}
void Construct(const util::AgentInfo* agent_info, rocprofiler_feature_t* info, const uint32_t info_count) {
std::lock_guard<mutex_t> lck(mutex_);
if (constructed_ == false) {
array_data_ = (char*) malloc(array_size_bytes_ + 0x3f);
array_ = reinterpret_cast<char*>(((intptr_t)array_data_ + 0x3f) >> 6 << 6);
if (((intptr_t)array_ & 0x3f) != 0) EXC_RAISING(HSA_STATUS_ERROR, "Pool array is not aligned");
memset(array_, 0, array_size_bytes_);
const char* end = array_ + array_size_bytes_;
for (char* ptr = array_; ptr < end; ptr += entry_size_bytes_) {
entry_t* entry = reinterpret_cast<entry_t*>(ptr);
entry->pool = this;
entry->context = Context::Create(agent_info, NULL, info, info_count, ContextPool::context_handler, ptr);
}
constructed_ = true;
}
}
~ContextPool() {
const char* end = array_ + array_size_bytes_;
for (char* ptr = array_; ptr < end; ptr += entry_size_bytes_) {
entry_t* entry = reinterpret_cast<entry_t*>(ptr);
Context::Destroy(entry->context);
}
free(array_);
}
char* GetArrayPtr(const uint32_t& index) { return array_ + (index % array_size_bytes_); }
entry_t* GetPoolEntry(const uint32_t& index, rocprofiler_pool_entry_t* pool_entry) {
char* ptr = GetArrayPtr(index);
entry_t* entry = reinterpret_cast<entry_t*>(ptr);
void* payload = ptr + payload_off_;
*pool_entry = rocprofiler_pool_entry_t{};
pool_entry->context = reinterpret_cast<rocprofiler_t*>(entry->context);
pool_entry->payload = payload;
return entry;
}
void check_completed() {
if (sync_flag_.test_and_set(std::memory_order_acquire) == false) {
index_t read_index = read_index_.load(std::memory_order_relaxed);
const index_t write_index = write_index_.load(std::memory_order_relaxed);
while(read_index < write_index) {
rocprofiler_pool_entry_t pool_entry{};
entry_t* entry = GetPoolEntry(read_index, &pool_entry);
if (entry->completed.load(std::memory_order_acquire) == true) {
pool_handler_(&pool_entry, pool_handler_arg_);
entry->completed.store(false, std::memory_order_relaxed);
read_index += entry_size_bytes_;
read_index_.store(read_index, std::memory_order_release);
} else {
break;
}
}
sync_flag_.clear(std::memory_order_release);
}
}
const uint32_t payload_off_;
const uint32_t entry_size_bytes_;
const uint32_t array_size_bytes_;
char* array_data_;
char* array_;
volatile std::atomic<index_t> read_index_;
volatile std::atomic<index_t> write_index_;
volatile std::atomic_flag sync_flag_;
const util::AgentInfo* agent_info_;
rocprofiler_feature_t* info_;
const uint32_t info_count_;
rocprofiler_pool_handler_t pool_handler_;
void* pool_handler_arg_;
bool constructed_;
mutex_t mutex_;
};
} // namespace rocprofiler
#endif // SRC_CORE_CONTEXT_POOL_H_
+61 -2
Wyświetl plik
@@ -28,6 +28,7 @@ THE SOFTWARE.
#include <vector>
#include "core/context.h"
#include "core/context_pool.h"
#include "core/hsa_queue.h"
#include "core/intercept_queue.h"
#include "core/proxy_queue.h"
@@ -477,8 +478,8 @@ PUBLIC_API hsa_status_t rocprofiler_open(hsa_agent_t agent, rocprofiler_feature_
}
rocprofiler::Context** context_ret = reinterpret_cast<rocprofiler::Context**>(handle);
rocprofiler::Context::Create(context_ret, agent_info, queue, features, feature_count, properties->handler,
properties->handler_arg);
*context_ret = rocprofiler::Context::Create(agent_info, queue, features, feature_count,
properties->handler, properties->handler_arg);
API_METHOD_SUFFIX
}
@@ -608,6 +609,64 @@ PUBLIC_API hsa_status_t rocprofiler_iterate_trace_data(
API_METHOD_SUFFIX
}
////////////////////////////////////////////////////////////////////////////////
// Open profiling pool
PUBLIC_API hsa_status_t rocprofiler_pool_open(hsa_agent_t agent, // GPU handle
rocprofiler_feature_t* features, // [in] profiling features array
uint32_t feature_count, // profiling info count
rocprofiler_pool_t** pool, // [out] context object
uint32_t mode, // profiling mode mask
rocprofiler_pool_properties_t* properties) // pool properties
{
API_METHOD_PREFIX
rocprofiler::util::HsaRsrcFactory* hsa_rsrc = &rocprofiler::util::HsaRsrcFactory::Instance();
const rocprofiler::util::AgentInfo* agent_info = hsa_rsrc->GetAgentInfo(agent);
if (agent_info == NULL) {
EXC_RAISING(HSA_STATUS_ERROR, "agent is not found");
}
rocprofiler::ContextPool* obj = rocprofiler::ContextPool::Create(
properties->num_entries,
properties->payload_bytes,
agent_info,
features,
feature_count,
properties->handler,
properties->handler_arg
);
*pool = reinterpret_cast<rocprofiler_pool_t*>(obj);
API_METHOD_SUFFIX
}
// Close profiling pool
PUBLIC_API hsa_status_t rocprofiler_pool_close(rocprofiler_pool_t* pool) // profiling pool handle
{
API_METHOD_PREFIX
rocprofiler::ContextPool* obj = reinterpret_cast<rocprofiler::ContextPool*>(pool);
rocprofiler::ContextPool::Destroy(obj);
API_METHOD_SUFFIX
}
// Fetch profiling pool entry
PUBLIC_API hsa_status_t rocprofiler_pool_fetch(rocprofiler_pool_t* pool, // profiling pool handle
rocprofiler_pool_entry_t* entry) // [out] empty profling pool entry
{
API_METHOD_PREFIX
rocprofiler::ContextPool* context_pool = reinterpret_cast<rocprofiler::ContextPool*>(pool);
context_pool->Fetch(entry);
API_METHOD_SUFFIX
}
// Fetch profiling pool entry
PUBLIC_API hsa_status_t rocprofiler_pool_flush(rocprofiler_pool_t* pool) // profiling pool handle
{
API_METHOD_PREFIX
rocprofiler::ContextPool* context_pool = reinterpret_cast<rocprofiler::ContextPool*>(pool);
context_pool->Flush();
API_METHOD_SUFFIX
}
////////////////////////////////////////////////////////////////////////////////
// Return the info for a given info kind
PUBLIC_API hsa_status_t rocprofiler_get_info(
const hsa_agent_t *agent,