diff --git a/projects/roctracer/src/core/memory_pool.h b/projects/roctracer/src/core/memory_pool.h index 27eae906e1..7e5837b486 100644 --- a/projects/roctracer/src/core/memory_pool.h +++ b/projects/roctracer/src/core/memory_pool.h @@ -23,6 +23,7 @@ #include "util/exception.h" +#include #include #include #include @@ -40,7 +41,7 @@ class MemoryPool { const size_t allocation_size = 2 * properties_.buffer_size; pool_begin_ = nullptr; AllocateMemory(&pool_begin_, allocation_size); - if (pool_begin_ == nullptr) EXC_ABORT(ROCTRACER_STATUS_ERROR, "pool allocator failed"); + assert(pool_begin_ != nullptr && "pool allocator failed"); pool_end_ = pool_begin_ + allocation_size; buffer_begin_ = pool_begin_; @@ -80,10 +81,7 @@ class MemoryPool { write_ptr_ = buffer_begin_; next = write_ptr_ + sizeof(record); - if (next > buffer_end_) - EXC_ABORT(ROCTRACER_STATUS_ERROR, - "buffer size(" << properties_.buffer_size << ") is less then the record(" - << sizeof(record) << ")"); + assert(next <= buffer_end_ && "buffer size is less then the record size"); } // Store the record into the buffer, and increment the write pointer. diff --git a/projects/roctracer/src/core/roctracer.cpp b/projects/roctracer/src/core/roctracer.cpp index da23084f63..7e9161244f 100644 --- a/projects/roctracer/src/core/roctracer.cpp +++ b/projects/roctracer/src/core/roctracer.cpp @@ -25,6 +25,7 @@ #define PROF_API_IMPL 1 #include "inc/roctracer_hsa.h" +#include #include #include #include @@ -255,9 +256,9 @@ roctracer_stop_cb_t roctracer_stop_cb = NULL; } // namespace ext_support roctracer_status_t GetExcStatus(const std::exception& e) { - const util::exception* roctracer_exc_ptr = dynamic_cast(&e); - return (roctracer_exc_ptr) ? static_cast(roctracer_exc_ptr->status()) - : ROCTRACER_STATUS_ERROR; + const util::exception* roctracer_exc_ptr = + dynamic_cast*>(&e); + return (roctracer_exc_ptr) ? roctracer_exc_ptr->status() : ROCTRACER_STATUS_ERROR; } class GlobalCounter { @@ -302,8 +303,7 @@ static thread_local std::stack external_id_stack; static inline void CorrelationIdRegistr(const activity_correlation_id_t& correlation_id) { std::lock_guard lck(correlation_id_mutex); const auto ret = correlation_id_map.insert({correlation_id, correlation_id_tls}); - if (ret.second == false) - EXC_ABORT(ROCTRACER_STATUS_ERROR, "HIP activity id is not unique(" << correlation_id << ")"); + assert(ret.second && "HIP activity id is not unique"); DEBUG_TRACE("CorrelationIdRegistr id(%lu) id_tls(%lu)\n", correlation_id, correlation_id_tls); } @@ -312,8 +312,7 @@ static inline activity_correlation_id_t CorrelationIdLookup( const activity_correlation_id_t& correlation_id) { std::lock_guard lck(correlation_id_mutex); auto it = correlation_id_map.find(correlation_id); - if (it == correlation_id_map.end()) - EXC_ABORT(ROCTRACER_STATUS_ERROR, "HIP activity id lookup failed(" << correlation_id << ")"); + assert(it != correlation_id_map.end() && "HIP activity id lookup failed"); const activity_correlation_id_t ret_val = it->second; correlation_id_map.erase(it); @@ -328,27 +327,27 @@ hip_activity_mutex_t hip_activity_mutex; hip_act_cb_tracker_t* hip_act_cb_tracker = NULL; inline uint32_t HipApiActivityEnableCheck(uint32_t op) { - if (hip_act_cb_tracker == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "hip_act_cb_tracker is NULL"); + assert(hip_act_cb_tracker != nullptr && "hip_act_cb_tracker is NULL"); const uint32_t mask = hip_act_cb_tracker->enable_check(op, API_CB_MASK); const uint32_t ret = (mask & ACT_CB_MASK); return ret; } inline uint32_t HipApiActivityDisableCheck(uint32_t op) { - if (hip_act_cb_tracker == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "hip_act_cb_tracker is NULL"); + assert(hip_act_cb_tracker != nullptr && "hip_act_cb_tracker is NULL"); const uint32_t mask = hip_act_cb_tracker->disable_check(op, API_CB_MASK); const uint32_t ret = (mask & ACT_CB_MASK); return ret; } inline uint32_t HipActActivityEnableCheck(uint32_t op) { - if (hip_act_cb_tracker == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "hip_act_cb_tracker is NULL"); + assert(hip_act_cb_tracker != nullptr && "hip_act_cb_tracker is NULL"); hip_act_cb_tracker->enable_check(op, ACT_CB_MASK); return 0; } inline uint32_t HipActActivityDisableCheck(uint32_t op) { - if (hip_act_cb_tracker == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "hip_act_cb_tracker is NULL"); + assert(hip_act_cb_tracker != nullptr && "hip_act_cb_tracker is NULL"); const uint32_t mask = hip_act_cb_tracker->disable_check(op, ACT_CB_MASK); const uint32_t ret = (mask & API_CB_MASK); return ret; @@ -366,7 +365,7 @@ void* HIP_SyncApiDataCallback(uint32_t op_id, roctracer_record_t* record, const int phase = ACTIVITY_API_PHASE_ENTER; if (record != NULL) { - if (data == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "ActivityCallback: data is NULL"); + assert(data != nullptr && "ActivityCallback: data is NULL"); phase = data->phase; } else if (pool != NULL) { phase = ACTIVITY_API_PHASE_EXIT; @@ -375,7 +374,7 @@ void* HIP_SyncApiDataCallback(uint32_t op_id, roctracer_record_t* record, const if (phase == ACTIVITY_API_PHASE_ENTER) { // Allocating a record if NULL passed if (record == NULL) { - if (data != NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "ActivityCallback enter: record is NULL"); + assert(data == nullptr && "ActivityCallback enter: record is NULL"); record_pair_stack->push({}); auto& top = record_pair_stack->top(); data = &(top.data.hip); @@ -426,7 +425,7 @@ void* HIP_SyncActivityCallback(uint32_t op_id, roctracer_record_t* record, int phase = ACTIVITY_API_PHASE_ENTER; if (record != NULL) { - if (data == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "ActivityCallback: data is NULL"); + assert(data != NULL && "ActivityCallback: data is NULL"); phase = data->phase; } else if (pool != NULL) { phase = ACTIVITY_API_PHASE_EXIT; @@ -435,7 +434,7 @@ void* HIP_SyncActivityCallback(uint32_t op_id, roctracer_record_t* record, if (phase == ACTIVITY_API_PHASE_ENTER) { // Allocating a record if NULL passed if (record == NULL) { - if (data != NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "ActivityCallback enter: record is NULL"); + assert(data == nullptr && "ActivityCallback enter: record is NULL"); record_pair_stack->push({}); auto& top = record_pair_stack->top(); record = &(top.record); @@ -463,12 +462,11 @@ void* HIP_SyncActivityCallback(uint32_t op_id, roctracer_record_t* record, ret = data_ptr; } else { - if (pool == NULL) EXC_ABORT(ROCTRACER_STATUS_ERROR, "ActivityCallback exit: pool is NULL"); + assert(pool != nullptr && "ActivityCallback exit: pool is NULL"); // Getting record of stacked if (record == NULL) { - if (record_pair_stack->empty()) - EXC_ABORT(ROCTRACER_STATUS_ERROR, "ActivityCallback exit: record stack is empty"); + assert(!record_pair_stack->empty() && "ActivityCallback exit: record stack is empty"); auto& top = record_pair_stack->top(); record = &(top.record); } @@ -1239,7 +1237,7 @@ PUBLIC_API roctracer_status_t roctracer_set_properties(roctracer_domain_t domain // HSA async-copy tracing hsa_status_t status = hsa_amd_profiling_async_copy_enable(true); - if (status != HSA_STATUS_SUCCESS) EXC_ABORT(status, "hsa_amd_profiling_async_copy_enable"); + assert(status == HSA_STATUS_SUCCESS && "hsa_amd_profiling_async_copy_enable failed"); roctracer::hsa_amd_memory_async_copy_fn = table->amd_ext_->hsa_amd_memory_async_copy_fn; roctracer::hsa_amd_memory_async_copy_rect_fn = table->amd_ext_->hsa_amd_memory_async_copy_rect_fn; diff --git a/projects/roctracer/src/core/tracker.h b/projects/roctracer/src/core/tracker.h index a005f213d8..76ca7d8f66 100644 --- a/projects/roctracer/src/core/tracker.h +++ b/projects/roctracer/src/core/tracker.h @@ -56,10 +56,12 @@ class Tracker { // Creating a proxy signal status = hsa_signal_create(1, 0, NULL, &(entry->signal)); - if (status != HSA_STATUS_SUCCESS) EXC_RAISING(status, "hsa_signal_create"); + if (status != HSA_STATUS_SUCCESS) + EXC_RAISING(ROCTRACER_STATUS_ERROR, "hsa_signal_create failed"); status = hsa_amd_signal_async_handler(entry->signal, HSA_SIGNAL_CONDITION_LT, 1, Handler, entry); - if (status != HSA_STATUS_SUCCESS) EXC_RAISING(status, "hsa_amd_signal_async_handler"); + if (status != HSA_STATUS_SUCCESS) + EXC_RAISING(ROCTRACER_STATUS_ERROR, "hsa_amd_signal_async_handler failed"); } // Delete tracker entry @@ -77,14 +79,15 @@ class Tracker { hsa_amd_profiling_async_copy_time_t async_copy_time{}; hsa_status_t status = hsa_amd_profiling_get_async_copy_time(entry->signal, &async_copy_time); if (status != HSA_STATUS_SUCCESS) - EXC_RAISING(status, "hsa_amd_profiling_get_async_copy_time"); + EXC_RAISING(ROCTRACER_STATUS_ERROR, "hsa_amd_profiling_get_async_copy_time failed"); entry->begin = hsa_rsrc->SysclockToNs(async_copy_time.start); entry->end = hsa_rsrc->SysclockToNs(async_copy_time.end); } else { hsa_amd_profiling_dispatch_time_t dispatch_time{}; hsa_status_t status = hsa_amd_profiling_get_dispatch_time(entry->agent, entry->signal, &dispatch_time); - if (status != HSA_STATUS_SUCCESS) EXC_RAISING(status, "hsa_amd_profiling_get_dispatch_time"); + if (status != HSA_STATUS_SUCCESS) + EXC_RAISING(ROCTRACER_STATUS_ERROR, "hsa_amd_profiling_get_dispatch_time failed"); entry->begin = hsa_rsrc->SysclockToNs(dispatch_time.start); entry->end = hsa_rsrc->SysclockToNs(dispatch_time.end); entry->dev_index = (hsa_rsrc->GetAgentInfo(entry->agent))->dev_index; @@ -105,8 +108,7 @@ class Tracker { orig_signal_ptr->end_ts = prof_signal_ptr->end_ts; const hsa_signal_value_t new_value = hsa_signal_load_relaxed(orig) - 1; - if (signal_value != new_value) - EXC_ABORT(HSA_STATUS_ERROR, "Tracker::Complete bad signal value"); + assert(signal_value == new_value && "Tracker::Complete bad signal value"); hsa_signal_store_screlease(orig, signal_value); } hsa_signal_destroy(signal); diff --git a/projects/roctracer/src/roctx/roctx.cpp b/projects/roctracer/src/roctx/roctx.cpp index c2a71bebe3..780975135d 100644 --- a/projects/roctracer/src/roctx/roctx.cpp +++ b/projects/roctracer/src/roctx/roctx.cpp @@ -92,10 +92,9 @@ thread_map_t thread_map; static thread_local message_stack_t* message_stack = NULL; roctx_status_t GetExcStatus(const std::exception& e) { - const roctracer::util::exception* roctx_exc_ptr = - dynamic_cast(&e); - return (roctx_exc_ptr) ? static_cast(roctx_exc_ptr->status()) - : ROCTX_STATUS_ERROR; + const roctracer::util::exception* roctx_exc_ptr = + dynamic_cast*>(&e); + return (roctx_exc_ptr) ? roctx_exc_ptr->status() : ROCTX_STATUS_ERROR; } void thread_data_init() { @@ -169,11 +168,10 @@ PUBLIC_API int roctxRangePop() { api_callback_fun(ACTIVITY_DOMAIN_ROCTX, ROCTX_API_ID_roctxRangePop, &api_data, api_callback_arg); if (roctx::message_stack->empty()) { - EXC_ABORT(ROCTX_STATUS_ERROR, "Pop from empty stack!"); - } else { - roctx::message_stack->pop(); + EXC_RAISING(ROCTX_STATUS_ERROR, "Pop from empty stack!"); } + roctx::message_stack->pop(); return roctx::message_stack->size(); API_METHOD_CATCH(-1) } diff --git a/projects/roctracer/src/util/exception.h b/projects/roctracer/src/util/exception.h index 4887b19bcd..2a791a184d 100644 --- a/projects/roctracer/src/util/exception.h +++ b/projects/roctracer/src/util/exception.h @@ -21,40 +21,30 @@ #ifndef SRC_UTIL_EXCEPTION_H_ #define SRC_UTIL_EXCEPTION_H_ -#include #include +#include #include -#define EXC_ABORT(error, stream) \ - do { \ - std::ostringstream oss; \ - oss << __FUNCTION__ << "(), " << stream; \ - std::cout << oss.str() << std::endl; \ - abort(); \ - } while (0) - #define EXC_RAISING(error, stream) \ do { \ std::ostringstream oss; \ oss << __FUNCTION__ << "(), " << stream; \ throw roctracer::util::exception(error, oss.str()); \ - } while (0) + } while (false) -namespace roctracer { -namespace util { +namespace roctracer::util { -class exception : public std::exception { +template class exception : public std::runtime_error { public: - explicit exception(const uint32_t& status, const std::string& msg) : status_(status), str_(msg) {} - const char* what() const throw() { return str_.c_str(); } - uint32_t status() const throw() { return status_; } + explicit exception(Status status, const std::string& what_arg) + : std::runtime_error(what_arg), status_(status) {} - protected: - const uint32_t status_; - const std::string str_; + Status status() const noexcept { return status_; } + + private: + const Status status_; }; -} // namespace util -} // namespace roctracer +} // namespace roctracer::util #endif // SRC_UTIL_EXCEPTION_H_