2021-07-02 16:46:49 -07:00
|
|
|
/* Copyright (c) 2019 - 2021 Advanced Micro Devices, Inc.
|
2019-10-07 11:55:30 -04:00
|
|
|
|
2020-02-04 08:45:01 -08:00
|
|
|
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:
|
2019-10-07 11:55:30 -04:00
|
|
|
|
2020-02-04 08:45:01 -08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
2019-10-07 11:55:30 -04:00
|
|
|
|
2020-02-04 08:45:01 -08:00
|
|
|
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. */
|
2019-10-07 11:55:30 -04:00
|
|
|
|
|
|
|
|
#ifndef HIP_SRC_HIP_PROF_API_H
|
|
|
|
|
#define HIP_SRC_HIP_PROF_API_H
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
2022-07-29 14:44:49 -07:00
|
|
|
#include <cassert>
|
2019-10-07 11:55:30 -04:00
|
|
|
#include <iostream>
|
2022-08-10 00:36:49 -07:00
|
|
|
#include <shared_mutex>
|
2022-07-29 14:44:49 -07:00
|
|
|
#include <utility>
|
2019-10-07 11:55:30 -04:00
|
|
|
|
2019-10-08 12:46:05 -04:00
|
|
|
#if USE_PROF_API
|
2020-12-15 17:38:08 -05:00
|
|
|
#include "hip/amd_detail/hip_prof_str.h"
|
2019-10-22 12:38:12 -04:00
|
|
|
#include "platform/prof_protocol.h"
|
2019-10-08 12:46:05 -04:00
|
|
|
|
2022-08-30 17:55:43 -07:00
|
|
|
struct hip_api_trace_data_t {
|
|
|
|
|
hip_api_data_t api_data;
|
|
|
|
|
uint64_t phase_enter_timestamp;
|
|
|
|
|
uint64_t phase_data;
|
2019-10-10 20:23:33 -04:00
|
|
|
|
2022-08-30 17:55:43 -07:00
|
|
|
void (*phase_enter)(hip_api_id_t operation_id, hip_api_trace_data_t* data);
|
|
|
|
|
void (*phase_exit)(hip_api_id_t operation_id, hip_api_trace_data_t* data);
|
2019-10-07 11:55:30 -04:00
|
|
|
};
|
|
|
|
|
|
2022-08-30 17:55:43 -07:00
|
|
|
// HIP API callbacks spawner object macro
|
|
|
|
|
#define HIP_CB_SPAWNER_OBJECT(operation_id) \
|
|
|
|
|
api_callbacks_spawner_t<HIP_API_ID_##operation_id> __api_tracer( \
|
|
|
|
|
[=](auto& api_data) { INIT_CB_ARGS_DATA(operation_id, api_data); });
|
2019-10-07 11:55:30 -04:00
|
|
|
|
2022-08-30 17:55:43 -07:00
|
|
|
template <hip_api_id_t operation_id> class api_callbacks_spawner_t {
|
2019-10-07 11:55:30 -04:00
|
|
|
public:
|
2022-08-30 17:55:43 -07:00
|
|
|
template <typename Functor> api_callbacks_spawner_t(Functor init_cb_args_data) {
|
|
|
|
|
static_assert(operation_id >= HIP_API_ID_FIRST && operation_id <= HIP_API_ID_LAST,
|
|
|
|
|
"invalid HIP_API operation id");
|
|
|
|
|
|
|
|
|
|
if (auto function = activity_prof::report_activity.load(std::memory_order_relaxed); function &&
|
|
|
|
|
(enabled_ = function(ACTIVITY_DOMAIN_HIP_API, operation_id, &trace_data_) == 0)) {
|
|
|
|
|
activity_prof::correlation_id = trace_data_.api_data.correlation_id;
|
|
|
|
|
|
|
|
|
|
if (trace_data_.phase_enter != nullptr) {
|
|
|
|
|
init_cb_args_data(trace_data_.api_data);
|
|
|
|
|
trace_data_.phase_enter(operation_id, &trace_data_);
|
|
|
|
|
}
|
2022-08-04 10:19:31 -07:00
|
|
|
}
|
2019-10-07 11:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~api_callbacks_spawner_t() {
|
2022-08-30 17:55:43 -07:00
|
|
|
if (enabled_) {
|
|
|
|
|
if (trace_data_.phase_exit != nullptr) trace_data_.phase_exit(operation_id, &trace_data_);
|
|
|
|
|
activity_prof::correlation_id = 0;
|
|
|
|
|
}
|
2019-10-07 11:55:30 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-29 14:44:49 -07:00
|
|
|
private:
|
2022-08-30 17:55:43 -07:00
|
|
|
bool enabled_{false};
|
2022-08-04 10:19:31 -07:00
|
|
|
union {
|
2022-08-30 17:55:43 -07:00
|
|
|
hip_api_trace_data_t trace_data_;
|
2022-08-04 10:19:31 -07:00
|
|
|
};
|
2019-10-07 11:55:30 -04:00
|
|
|
};
|
|
|
|
|
|
2022-08-30 17:55:43 -07:00
|
|
|
template <> class api_callbacks_spawner_t<HIP_API_ID_NONE> {
|
2019-10-07 11:55:30 -04:00
|
|
|
public:
|
2022-08-30 17:55:43 -07:00
|
|
|
template <typename Functor> api_callbacks_spawner_t(Functor) {}
|
2019-10-07 11:55:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2022-08-30 17:55:43 -07:00
|
|
|
#define HIP_CB_SPAWNER_OBJECT(x) \
|
|
|
|
|
do { \
|
|
|
|
|
} while (false)
|
2019-10-07 11:55:30 -04:00
|
|
|
|
|
|
|
|
class api_callbacks_table_t {
|
|
|
|
|
public:
|
2022-07-29 14:44:49 -07:00
|
|
|
bool set_activity(hip_api_id_t, activity_sync_callback_t, void*) { return false; }
|
|
|
|
|
bool set_callback(hip_api_id_t, activity_rtapi_callback_t, void*) { return false; }
|
2019-10-07 11:55:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif // HIP_SRC_HIP_PROF_API_H
|