Files
rocm-systems/src/core/roctracer.cpp
T

494 lines
18 KiB
C++
Raw Normal View History

2018-05-10 13:19:10 -05:00
#include "inc/roctracer.h"
2018-08-07 03:25:55 -05:00
#include "inc/roctracer_hcc.h"
//#include "inc/roctracer_hip.h"
2018-05-10 13:19:10 -05:00
#include <atomic>
#include <hip/hip_runtime.h>
#include <mutex>
#include <string.h>
#include <pthread.h>
2018-08-07 03:25:55 -05:00
#include "inc/roctracer/hsa_rt_utils.hpp"
2018-05-10 13:19:10 -05:00
#include "util/exception.h"
#include "util/hsa_rsrc_factory.h"
#include "util/logger.h"
#define PUBLIC_API __attribute__((visibility("default")))
#define CONSTRUCTOR_API __attribute__((constructor))
#define DESTRUCTOR_API __attribute__((destructor))
#define PTHREAD_CALL(call) \
do { \
int err = call; \
if (err != 0) { \
errno = err; \
perror(#call); \
abort(); \
} \
} while (0)
#define HIPAPI_CALL(call) \
do { \
hipError_t err = call; \
if (err != hipSuccess) \
HIP_EXC_RAISING(ROCTRACER_STATUS_HIP_API_ERR, #call " error(" << err << ")"); \
} while (0)
2018-05-10 13:19:10 -05:00
#define API_METHOD_PREFIX \
roctracer_status_t err = ROCTRACER_STATUS_SUCCESS; \
2018-05-10 13:19:10 -05:00
try {
#define API_METHOD_SUFFIX \
} \
catch (std::exception & e) { \
ERR_LOGGING(__FUNCTION__ << "(), " << e.what()); \
err = roctracer::GetExcStatus(e); \
} \
return err;
2018-05-25 22:39:22 -05:00
#define API_METHOD_CATCH(X) \
} \
catch (std::exception & e) { \
ERR_LOGGING(__FUNCTION__ << "(), " << e.what()); \
} \
(void)err; \
return X;
2018-05-10 13:19:10 -05:00
///////////////////////////////////////////////////////////////////////////////////////////////////
// Internal library methods
//
namespace roctracer {
roctracer_status_t GetExcStatus(const std::exception& e) {
2018-05-10 13:19:10 -05:00
const util::exception* roctracer_exc_ptr = dynamic_cast<const util::exception*>(&e);
return (roctracer_exc_ptr) ? static_cast<roctracer_status_t>(roctracer_exc_ptr->status()) : ROCTRACER_STATUS_ERROR;
2018-05-10 13:19:10 -05:00
}
class GlobalCounter {
public:
typedef std::mutex mutex_t;
typedef uint64_t counter_t;
static counter_t Increment() {
std::lock_guard<mutex_t> lock(mutex_);
return ++counter_;
}
private:
static mutex_t mutex_;
static counter_t counter_;
};
GlobalCounter::mutex_t GlobalCounter::mutex_;
GlobalCounter::counter_t GlobalCounter::counter_ = 0;
class MemoryPool {
public:
typedef std::mutex mutex_t;
static void allocator_default(char** ptr, size_t size, void* arg) {
(void)arg;
if (*ptr == NULL) {
*ptr = reinterpret_cast<char*>(malloc(size));
} else if (size != 0) {
*ptr = reinterpret_cast<char*>(realloc(ptr, size));
} else {
free(*ptr);
*ptr = NULL;
}
}
MemoryPool(const roctracer_properties_t& properties) {
// Assigning pool allocator
alloc_fun_ = allocator_default;
alloc_arg_ = NULL;
if (properties.alloc_fun != NULL) {
alloc_fun_ = properties.alloc_fun;
alloc_arg_ = properties.alloc_arg;
}
// Pool definition
2018-05-25 01:26:36 -05:00
buffer_size_shift_ = properties.buffer_size;
buffer_size_ = 1 << buffer_size_shift_;
2018-05-10 13:19:10 -05:00
const size_t pool_size = 2 * buffer_size_;
pool_begin_ = NULL;
alloc_fun_(&pool_begin_, pool_size, alloc_arg_);
if (pool_begin_ == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "pool allocator failed");
pool_end_ = pool_begin_ + pool_size;
buffer_begin_ = pool_begin_;
buffer_end_ = buffer_begin_ + buffer_size_;
write_ptr_ = buffer_begin_;
// Consuming read thread
read_callback_fun_ = properties.buffer_callback_fun;
read_callback_arg_ = properties.buffer_callback_arg;
2018-06-22 19:02:42 +00:00
consumer_arg_.set(this, NULL, NULL, true);
2018-05-10 13:19:10 -05:00
PTHREAD_CALL(pthread_mutex_init(&read_mutex_, NULL));
PTHREAD_CALL(pthread_cond_init(&read_cond_, NULL));
PTHREAD_CALL(pthread_create(&consumer_thread_, NULL, reader_fun, &consumer_arg_));
}
~MemoryPool() {
Flush();
PTHREAD_CALL(pthread_cancel(consumer_thread_));
void *res;
PTHREAD_CALL(pthread_join(consumer_thread_, &res));
if (res != PTHREAD_CANCELED) EXC_ABORT(ROCTRACER_STATUS_ERROR, "consumer thread wasn't stopped correctly");
2018-05-10 13:19:10 -05:00
allocator_default(&pool_begin_, 0, alloc_arg_);
}
template <typename Record>
2018-06-22 19:02:42 +00:00
void Write(const Record& record) {
std::lock_guard<mutex_t> lock(write_mutex_);
getRecord<Record>(record);
}
void Flush() {
2018-05-10 13:19:10 -05:00
std::lock_guard<mutex_t> lock(write_mutex_);
2018-06-22 19:02:42 +00:00
if (write_ptr_ > buffer_begin_) {
spawn_reader(buffer_begin_, write_ptr_);
sync_reader(&consumer_arg_);
buffer_begin_ = (buffer_end_ == pool_end_) ? pool_begin_ : buffer_end_;
buffer_end_ = buffer_begin_ + buffer_size_;
write_ptr_ = buffer_begin_;
}
}
private:
struct consumer_arg_t {
MemoryPool* obj;
const char* begin;
const char* end;
2018-07-17 00:08:41 +00:00
volatile std::atomic<bool> valid;
2018-06-22 19:02:42 +00:00
void set(MemoryPool* obj_p, const char* begin_p, const char* end_p, bool valid_p) {
obj = obj_p;
begin = begin_p;
end = end_p;
valid.store(valid_p);
}
};
2018-05-25 01:26:36 -05:00
2018-06-22 19:02:42 +00:00
template <typename Record>
Record* getRecord(const Record& init) {
2018-05-10 13:19:10 -05:00
char* next = write_ptr_ + sizeof(Record);
if (next > buffer_end_) {
if (write_ptr_ == buffer_begin_) EXC_ABORT(ROCTRACER_STATUS_ERROR, "buffer size(" << buffer_size_ << ") is less then the record(" << sizeof(Record) << ")");
spawn_reader(buffer_begin_, write_ptr_);
2018-05-10 13:19:10 -05:00
buffer_begin_ = (buffer_end_ == pool_end_) ? pool_begin_ : buffer_end_;
buffer_end_ = buffer_begin_ + buffer_size_;
write_ptr_ = buffer_begin_;
next = write_ptr_ + sizeof(Record);
}
2018-05-10 13:19:10 -05:00
Record* ptr = reinterpret_cast<Record*>(write_ptr_);
write_ptr_ = next;
2018-05-25 01:26:36 -05:00
2018-06-22 19:02:42 +00:00
*ptr = init;
2018-05-25 01:26:36 -05:00
return ptr;
}
2018-05-10 13:19:10 -05:00
static void reset_reader(consumer_arg_t* arg) {
2018-06-22 19:02:42 +00:00
arg->valid.store(false);
2018-05-10 13:19:10 -05:00
}
static void sync_reader(const consumer_arg_t* arg) {
2018-06-22 19:02:42 +00:00
while(arg->valid.load() == true) PTHREAD_CALL(pthread_yield());
2018-05-10 13:19:10 -05:00
}
static void* reader_fun(void* consumer_arg) {
consumer_arg_t* arg = reinterpret_cast<consumer_arg_t*>(consumer_arg);
roctracer::MemoryPool* obj = arg->obj;
reset_reader(arg);
while (1) {
PTHREAD_CALL(pthread_mutex_lock(&(obj->read_mutex_)));
2018-06-22 19:02:42 +00:00
while (arg->valid.load() == false) {
2018-05-10 13:19:10 -05:00
PTHREAD_CALL(pthread_cond_wait(&(obj->read_cond_), &(obj->read_mutex_)));
}
2018-05-25 01:26:36 -05:00
2018-05-10 13:19:10 -05:00
obj->read_callback_fun_(arg->begin, arg->end, obj->read_callback_arg_);
reset_reader(arg);
PTHREAD_CALL(pthread_mutex_unlock(&(obj->read_mutex_)));
}
return NULL;
}
void spawn_reader(const char* data_begin, const char* data_end) {
sync_reader(&consumer_arg_);
PTHREAD_CALL(pthread_mutex_lock(&read_mutex_));
2018-06-22 19:02:42 +00:00
consumer_arg_.set(this, data_begin, data_end, true);
2018-05-10 13:19:10 -05:00
PTHREAD_CALL(pthread_cond_signal(&read_cond_));
PTHREAD_CALL(pthread_mutex_unlock(&read_mutex_));
}
// pool allocator
roctracer_allocator_t alloc_fun_;
void* alloc_arg_;
// Pool definition
2018-05-25 01:26:36 -05:00
size_t buffer_size_shift_;
2018-05-10 13:19:10 -05:00
size_t buffer_size_;
char* pool_begin_;
char* pool_end_;
char* buffer_begin_;
char* buffer_end_;
char* write_ptr_;
mutex_t write_mutex_;
// Consuming read thread
roctracer_buffer_callback_t read_callback_fun_;
void* read_callback_arg_;
consumer_arg_t consumer_arg_;
pthread_t consumer_thread_;
pthread_mutex_t read_mutex_;
pthread_cond_t read_cond_;
};
CONSTRUCTOR_API void constructor() {
util::Logger::Create();
}
DESTRUCTOR_API void destructor() {
util::HsaRsrcFactory::Destroy();
util::Logger::Destroy();
}
2018-08-07 03:25:55 -05:00
roctracer_record_t* SyncActivityCallback(
2018-05-10 13:19:10 -05:00
uint32_t activity_kind,
2018-06-22 19:02:42 +00:00
roctracer_record_t* record,
2018-05-10 13:19:10 -05:00
const void* callback_data,
void* arg)
{
2018-08-07 03:25:55 -05:00
static hsa_rt_utils::Timer timer;
2018-05-10 13:19:10 -05:00
2018-08-07 03:25:55 -05:00
const hip_api_data_t* data = reinterpret_cast<const hip_api_data_t*>(callback_data);
2018-05-10 13:19:10 -05:00
MemoryPool* pool = reinterpret_cast<MemoryPool*>(arg);
if (pool == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "ActivityCallback pool is NULL");
2018-08-07 03:25:55 -05:00
if (data->phase == ACTIVITY_API_PHASE_ENTER) {
record->domain = ACTIVITY_DOMAIN_HIP_API;
2018-06-22 19:02:42 +00:00
record->activity_kind = activity_kind;
record->begin_ns = timer.timestamp_ns();
2018-05-10 13:19:10 -05:00
// Correlation ID generating
2018-05-25 01:26:36 -05:00
uint64_t correlation_id = data->correlation_id;
if (correlation_id == 0) {
correlation_id = GlobalCounter::Increment();
2018-08-07 03:25:55 -05:00
const_cast<hip_api_data_t*>(data)->correlation_id = correlation_id;
2018-05-25 01:26:36 -05:00
}
2018-06-22 19:02:42 +00:00
record->correlation_id = correlation_id;
2018-05-25 01:26:36 -05:00
// Passing record to HCC
2018-08-07 03:25:55 -05:00
Kalmar::CLAMP::SetActivityRecord(correlation_id);
2018-06-22 19:02:42 +00:00
return record;
2018-05-10 13:19:10 -05:00
} else {
2018-06-22 19:02:42 +00:00
record->end_ns = timer.timestamp_ns();
2018-08-07 03:25:55 -05:00
Kalmar::CLAMP::GetActivityCoord(&(record->device_id), &(record->stream_id));
2018-06-22 19:02:42 +00:00
pool->Write(*record);
2018-05-25 01:26:36 -05:00
// Clearing record in HCC
2018-08-07 03:25:55 -05:00
Kalmar::CLAMP::SetActivityRecord(0);
2018-06-22 19:02:42 +00:00
return NULL;
2018-05-25 01:26:36 -05:00
}
}
2018-08-07 03:25:55 -05:00
void AsyncActivityCallback(
2018-05-25 01:26:36 -05:00
uint32_t op_id,
void* record,
void* arg)
{
2018-08-07 03:25:55 -05:00
static hsa_rt_utils::Timer timer;
2018-07-17 00:08:41 +00:00
MemoryPool* pool = reinterpret_cast<MemoryPool*>(arg);
2018-08-07 03:25:55 -05:00
roctracer_record_t* record_ptr = reinterpret_cast<roctracer_record_t*>(record);
record_ptr->domain = ACTIVITY_DOMAIN_HCC_OPS;
2018-06-22 19:02:42 +00:00
pool->Write(*record_ptr);
2018-05-10 13:19:10 -05:00
}
util::Logger::mutex_t util::Logger::mutex_;
util::Logger* util::Logger::instance_ = NULL;
MemoryPool* memory_pool = NULL;
2018-07-17 00:08:41 +00:00
typedef std::recursive_mutex memory_pool_mutex_t;
memory_pool_mutex_t memory_pool_mutex;
2018-05-10 13:19:10 -05:00
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Public library methods
//
extern "C" {
// Returns library vesrion
PUBLIC_API uint32_t roctracer_version_major() { return ROCTRACER_VERSION_MAJOR; }
PUBLIC_API uint32_t roctracer_version_minor() { return ROCTRACER_VERSION_MINOR; }
// Returns the last error
PUBLIC_API const char* roctracer_error_string() {
return strdup(roctracer::util::Logger::LastMessage().c_str());
}
// Return ID string by given domain and activity/API ID
2018-05-25 22:39:22 -05:00
// NULL returned on the error and the library errno is set
PUBLIC_API const char* roctracer_id_string(const uint32_t& domain, const uint32_t& id) {
2018-05-25 22:39:22 -05:00
API_METHOD_PREFIX
switch (domain) {
2018-08-07 03:25:55 -05:00
case ACTIVITY_DOMAIN_HCC_OPS: {
return Kalmar::CLAMP::GetCmdName(id);
break;
}
2018-08-07 03:25:55 -05:00
case ACTIVITY_DOMAIN_HIP_API: {
return hipApiName(id);
2018-05-25 22:39:22 -05:00
break;
}
default:
EXC_RAISING(ROCTRACER_STATUS_BAD_DOMAIN, "invalid domain ID(" << domain << ")");
}
API_METHOD_CATCH(NULL)
2018-05-10 13:19:10 -05:00
}
// Enable runtime API callbacks
PUBLIC_API roctracer_status_t roctracer_enable_api_callback(
roctracer_domain_t domain,
2018-05-10 13:19:10 -05:00
uint32_t cid,
2018-08-07 03:25:55 -05:00
roctracer_rtapi_callback_t callback,
2018-05-10 13:19:10 -05:00
void* user_data)
{
API_METHOD_PREFIX
switch (domain) {
2018-08-07 03:25:55 -05:00
case ACTIVITY_DOMAIN_ANY:
if (cid != 0) HIP_EXC_RAISING(ROCTRACER_STATUS_BAD_PARAMETER, "DOMAIN_ANY and cid != 0");
cid = HIP_API_ID_ANY;
case ACTIVITY_DOMAIN_HIP_API: {
hipError_t hip_err = hipRegisterApiCallback(cid, (void*)callback, user_data);
2018-05-10 13:19:10 -05:00
if (hip_err != hipSuccess) HIP_EXC_RAISING(ROCTRACER_STATUS_HIP_API_ERR, "hipRegisterApiCallback error(" << hip_err << ")");
break;
}
default:
EXC_RAISING(ROCTRACER_STATUS_BAD_DOMAIN, "invalid domain ID(" << domain << ")");
}
API_METHOD_SUFFIX
}
// Enable runtime API callbacks
PUBLIC_API roctracer_status_t roctracer_disable_api_callback(
roctracer_domain_t domain,
2018-05-10 13:19:10 -05:00
uint32_t cid)
{
API_METHOD_PREFIX
switch (domain) {
2018-08-07 03:25:55 -05:00
case ACTIVITY_DOMAIN_ANY:
if (cid != 0) HIP_EXC_RAISING(ROCTRACER_STATUS_BAD_PARAMETER, "DOMAIN_ANY and cid != 0");
cid = HIP_API_ID_ANY;
case ACTIVITY_DOMAIN_HIP_API: {
2018-05-10 13:19:10 -05:00
hipError_t hip_err = hipRemoveApiCallback(cid);
if (hip_err != hipSuccess) HIP_EXC_RAISING(ROCTRACER_STATUS_HIP_API_ERR, "hipRemoveApiCallback error(" << hip_err << ")");
break;
}
default:
EXC_RAISING(ROCTRACER_STATUS_BAD_DOMAIN, "invalid domain ID(" << domain << ")");
}
API_METHOD_SUFFIX
}
// Return default pool and set new one if parameter pool is not NULL.
2018-06-19 11:22:32 -05:00
PUBLIC_API roctracer_pool_t* roctracer_default_pool(roctracer_pool_t* pool) {
2018-07-17 00:08:41 +00:00
std::lock_guard<roctracer::memory_pool_mutex_t> lock(roctracer::memory_pool_mutex);
2018-05-10 13:19:10 -05:00
roctracer_pool_t* p = reinterpret_cast<roctracer_pool_t*>(roctracer::memory_pool);
if (pool != NULL) roctracer::memory_pool = reinterpret_cast<roctracer::MemoryPool*>(pool);
2018-06-22 19:02:42 +00:00
//if (p == NULL) EXC_RAISING(ROCTRACER_STATUS_UNINIT, "default pool is not initialized");
2018-05-10 13:19:10 -05:00
return p;
}
// Open memory pool
PUBLIC_API roctracer_status_t roctracer_open_pool(
2018-05-10 13:19:10 -05:00
const roctracer_properties_t* properties,
roctracer_pool_t** pool)
{
API_METHOD_PREFIX
2018-07-17 00:08:41 +00:00
std::lock_guard<roctracer::memory_pool_mutex_t> lock(roctracer::memory_pool_mutex);
2018-05-10 13:19:10 -05:00
if ((pool == NULL) && (roctracer::memory_pool != NULL)) {
EXC_RAISING(ROCTRACER_STATUS_ERROR, "default pool already set");
}
roctracer::MemoryPool* p = new roctracer::MemoryPool(*properties);
if (p == NULL) EXC_RAISING(ROCTRACER_STATUS_ERROR, "MemoryPool() error");
if (pool != NULL) *pool = p;
else roctracer::memory_pool = p;
API_METHOD_SUFFIX
}
// Close memory pool
PUBLIC_API roctracer_status_t roctracer_close_pool(roctracer_pool_t* pool) {
2018-05-10 13:19:10 -05:00
API_METHOD_PREFIX
2018-07-17 00:08:41 +00:00
std::lock_guard<roctracer::memory_pool_mutex_t> lock(roctracer::memory_pool_mutex);
2018-05-10 13:19:10 -05:00
roctracer_pool_t* ptr = (pool == NULL) ? roctracer_default_pool() : pool;
roctracer::MemoryPool* memory_pool = reinterpret_cast<roctracer::MemoryPool*>(ptr);
delete(memory_pool);
if (pool == NULL) roctracer::memory_pool = NULL;
API_METHOD_SUFFIX
}
// Enable activity records logging
PUBLIC_API roctracer_status_t roctracer_enable_api_activity(
roctracer_domain_t domain,
2018-08-07 03:25:55 -05:00
uint32_t activity_id,
2018-05-10 13:19:10 -05:00
roctracer_pool_t* pool)
{
API_METHOD_PREFIX
if (pool == NULL) pool = roctracer_default_pool();
switch (domain) {
2018-08-07 03:25:55 -05:00
case ACTIVITY_DOMAIN_ANY:
if (activity_id != 0) HIP_EXC_RAISING(ROCTRACER_STATUS_BAD_PARAMETER, "DOMAIN_ANY and activity_id != 0");
roctracer_enable_api_activity(ACTIVITY_DOMAIN_HCC_OPS, hc::HSA_OP_ID_ANY, pool);
roctracer_enable_api_activity(ACTIVITY_DOMAIN_HIP_API, HIP_API_ID_ANY, pool);
break;
case ACTIVITY_DOMAIN_HCC_OPS: {
const bool err = Kalmar::CLAMP::SetActivityCallback(activity_id, (void*)roctracer::AsyncActivityCallback, (void*)pool);
if (err == true) HCC_EXC_RAISING(ROCTRACER_STATUS_HCC_OPS_ERR, "Kalmar::CLAMP::SetActivityCallback error");
break;
}
case ACTIVITY_DOMAIN_HIP_API: {
const hipError_t hip_err = hipRegisterActivityCallback(activity_id, (void*)roctracer::SyncActivityCallback, (void*)pool);
2018-05-10 13:19:10 -05:00
if (hip_err != hipSuccess) HIP_EXC_RAISING(ROCTRACER_STATUS_HIP_API_ERR, "hipRegisterActivityCallback error(" << hip_err << ")");
break;
}
default:
EXC_RAISING(ROCTRACER_STATUS_BAD_DOMAIN, "invalid domain ID(" << domain << ")");
}
API_METHOD_SUFFIX
}
// Disable activity records logging
PUBLIC_API roctracer_status_t roctracer_disable_api_activity(
roctracer_domain_t domain,
2018-08-07 03:25:55 -05:00
uint32_t activity_id)
2018-05-10 13:19:10 -05:00
{
API_METHOD_PREFIX
switch (domain) {
2018-08-07 03:25:55 -05:00
case ACTIVITY_DOMAIN_ANY:
if (activity_id != 0) HIP_EXC_RAISING(ROCTRACER_STATUS_BAD_PARAMETER, "DOMAIN_ANY and activity_id != 0");
roctracer_disable_api_activity(ACTIVITY_DOMAIN_HCC_OPS, hc::HSA_OP_ID_ANY);
roctracer_disable_api_activity(ACTIVITY_DOMAIN_HIP_API, HIP_API_ID_ANY);
break;
case ACTIVITY_DOMAIN_HCC_OPS: {
const bool err = Kalmar::CLAMP::SetActivityCallback(activity_id, NULL, NULL);
if (err == true) HCC_EXC_RAISING(ROCTRACER_STATUS_HCC_OPS_ERR, "Kalmar::CLAMP::SetActivityCallback(NULL) error");
break;
}
case ACTIVITY_DOMAIN_HIP_API: {
const hipError_t hip_err = hipRemoveActivityCallback(activity_id);
2018-05-10 13:19:10 -05:00
if (hip_err != hipSuccess) HIP_EXC_RAISING(ROCTRACER_STATUS_HIP_API_ERR, "hipRemoveActivityCallback error(" << hip_err << ")");
break;
}
default:
EXC_RAISING(ROCTRACER_STATUS_BAD_DOMAIN, "invalid domain ID(" << domain << ")");
}
API_METHOD_SUFFIX
}
// Flush available activity records
PUBLIC_API roctracer_status_t roctracer_flush_api_activity(roctracer_pool_t* pool) {
2018-05-10 13:19:10 -05:00
API_METHOD_PREFIX
if (pool == NULL) pool = roctracer_default_pool();
roctracer::MemoryPool* memory_pool = reinterpret_cast<roctracer::MemoryPool*>(pool);
memory_pool->Flush();
API_METHOD_SUFFIX
}
} // extern "C"