2 #ifndef _HIP_PROF_API_H 3 #define _HIP_PROF_API_H 9 #include "hip/hcc_detail/hip_prof_str.h" 11 template <
typename Record,
typename Fun,
typename Act>
14 typedef std::recursive_mutex mutex_t;
16 typedef Record record_t;
22 volatile std::atomic<bool> sync;
23 volatile std::atomic<uint32_t> sem;
35 memset(&callbacks_table_, 0,
sizeof(callbacks_table_));
38 bool set_activity(uint32_t
id, act_t fun,
void* arg) {
39 std::lock_guard<mutex_t> lock(mutex_);
41 if (
id == HIP_API_ID_ANY) {
42 for (
unsigned i = 0; i < HIP_API_ID_NUMBER; ++i) set_activity(i, fun, arg);
43 }
else if (
id < HIP_API_ID_NUMBER) {
45 callbacks_table_.arr[id].act = fun;
46 callbacks_table_.arr[id].a_arg = arg;
54 bool set_callback(uint32_t
id, fun_t fun,
void* arg) {
55 std::lock_guard<mutex_t> lock(mutex_);
57 if (
id == HIP_API_ID_ANY) {
58 for (
unsigned i = 0; i < HIP_API_ID_NUMBER; ++i) set_callback(i, fun, arg);
59 }
else if (
id < HIP_API_ID_NUMBER) {
61 callbacks_table_.arr[id].fun = fun;
62 callbacks_table_.arr[id].arg = arg;
71 return callbacks_table_.arr[id];
74 inline void sem_sync(
const uint32_t&
id) {
76 if (entry(
id).sync.load() ==
true) sync_wait(
id);
79 inline void sem_release(
const uint32_t&
id) {
84 inline void cb_sync(
const uint32_t&
id) {
85 entry(
id).sync.store(
true);
86 while (entry(
id).sem.load() != 0) {}
89 inline void cb_release(
const uint32_t&
id) {
90 entry(
id).sync.store(
false);
93 inline void sem_increment(
const uint32_t&
id) {
94 const uint32_t prev = entry(
id).sem.fetch_add(1);
95 if (prev == UINT32_MAX) {
96 std::cerr <<
"sem overflow id = " <<
id << std::endl << std::flush;
101 inline void sem_decrement(
const uint32_t&
id) {
102 const uint32_t prev = entry(
id).sem.fetch_sub(1);
104 std::cerr <<
"sem corrupted id = " <<
id << std::endl << std::flush;
109 void sync_wait(
const uint32_t&
id) {
111 while (entry(
id).sync.load() ==
true) {}
121 #include <prof_protocol.h> 123 static const uint32_t HIP_DOMAIN_ID = ACTIVITY_DOMAIN_HIP_API;
124 typedef activity_record_t hip_api_record_t;
125 typedef activity_rtapi_callback_t hip_api_callback_t;
126 typedef activity_sync_callback_t hip_act_callback_t;
129 #define HIP_CB_SPAWNER_OBJECT(CB_ID) \ 130 hip_api_data_t api_data{}; \ 131 INIT_CB_ARGS_DATA(CB_ID, api_data); \ 132 api_callbacks_spawner_t<HIP_API_ID_##CB_ID> __api_tracer(HIP_API_ID_##CB_ID, api_data); 140 class api_callbacks_spawner_t {
142 api_callbacks_spawner_t(
const hip_api_id_t& cid,
hip_api_data_t& api_data) :
146 if (cid_ >= HIP_API_ID_NUMBER) {
147 fprintf(stderr,
"HIP %s bad id %d\n", __FUNCTION__, cid_);
150 callbacks_table.sem_sync(cid_);
152 act = entry(cid_).act;
153 a_arg = entry(cid_).a_arg;
154 fun = entry(cid_).fun;
155 arg = entry(cid_).arg;
158 if (act != NULL) act(cid_, &record_, &api_data_, a_arg);
159 if (fun != NULL) fun(HIP_DOMAIN_ID, cid_, &api_data_, arg);
162 ~api_callbacks_spawner_t() {
164 if (act != NULL) act(cid_, &record_, &api_data_, a_arg);
165 if (fun != NULL) fun(HIP_DOMAIN_ID, cid_, &api_data_, arg);
167 callbacks_table.sem_release(cid_);
171 inline api_callbacks_table_t::hip_cb_table_entry_t& entry(
const uint32_t&
id) {
172 return callbacks_table.entry(
id);
176 hip_api_record_t record_;
178 hip_act_callback_t act;
180 hip_api_callback_t fun;
185 class api_callbacks_spawner_t<HIP_API_ID_NUMBER> {
187 api_callbacks_spawner_t(
const hip_api_id_t& cid,
hip_api_data_t& api_data) {}
192 #define HIP_CB_SPAWNER_OBJECT(x) do {} while(0) 198 bool set_activity(uint32_t
id, act_t fun,
void* arg) {
return false; }
199 bool set_callback(uint32_t
id, fun_t fun,
void* arg) {
return false; }
204 #endif // _HIP_PROF_API_H Definition: hip_prof_api.h:21
Definition: hip_prof_api.h:194
Definition: hip_prof_api.h:30
Definition: hip_prof_api.h:12
Definition: hip_prof_str.h:372