351d825a8d
* [0/N] git submodules * [1/N] Update cmake, gitignore, external - clang-tidy file - update .gitignore - update main CMakeLists.txt - update external/CMakeLists.txt - update rocprofiler_config_interfaces.cmake - update rocprofiler_formatting.cmake - update rocprofiler_interfaces.cmake - update rocprofiler_linting.cmake - update rocprofiler_options.cmake - update rocprofiler_utilities.cmake * [2/N] Update rocprofiler/config.h - update to work with new rocprofiler.h * [3/N] Update source/lib/rocprofiler/hsa - hsa-types.h: static asserts - hsa.cpp: copyTables scope - hsa.gen.cpp: ACTIVITY_DOMAIN_HSA_API -> ROCPROFILER_TRACER_ACTIVITY_DOMAIN_HSA_API - rename some files - add rocprofiler_ prefix to types and enums - HSA_API_TABLE_LOOKUP_DEFINITION macro - get_saved_table() -> get_table() * [4/N] Update source/lib/common - CMake: change target_link_libraries - defines.hpp: remove ppdefs defined in include/rocprofiler/defines.h * [5/N] Update source/lib/rocprofiler - updates due to changes in rocprofiler.h - rocprofiler_config.cpp: remove unions which are now defined in include/rocprofiler - CMakeLists.txt: rocprofiler.cpp and public hsa-runtime and hip libraries - rocprofiler.cpp: dummy implementations for: - rocprofiler_query_available_agents - rocprofiler_create_context - rocprofiler_start_context - rocprofiler_stop_context - rocprofiler_flush_buffer - rocprofiler_destroy_buffer * [6/N] Update license - replace stale LBNL license * [7/N] CMake format
108 lines
2.7 KiB
C++
108 lines
2.7 KiB
C++
|
|
#pragma once
|
|
|
|
#include "rocprofiler/rocprofiler.h"
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <utility>
|
|
|
|
namespace
|
|
{
|
|
inline size_t // NOLINTNEXTLINE
|
|
get_domain_max_op(rocprofiler_tracer_activity_domain_t _domain)
|
|
{
|
|
switch(_domain)
|
|
{
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_NONE: return -1;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_HSA_API: return 0;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_HIP_API: return 0;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_MARKER_API: return 0;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_KFD_API: return -1;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_EXT_API: return -1;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_HSA_OPS: return 0;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_HIP_OPS: return 0;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_HSA_EVT: return 0;
|
|
case ROCPROFILER_TRACER_ACTIVITY_DOMAIN_LAST: return -1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
template <typename Tp, size_t N = 8>
|
|
struct allocator
|
|
{
|
|
void construct(Tp* const _p, const Tp& _v) const { ::new((void*) _p) Tp{_v}; }
|
|
void construct(Tp* const _p, Tp&& _v) const { ::new((void*) _p) Tp{std::move(_v)}; }
|
|
|
|
void destroy(Tp* const _p) const { _p->~Tp(); }
|
|
|
|
static constexpr auto size = sizeof(Tp);
|
|
using buffer_value_t = char[size];
|
|
|
|
struct buffer_entry
|
|
{
|
|
std::atomic_flag flag = ATOMIC_FLAG_INIT;
|
|
buffer_value_t value = {};
|
|
|
|
void* get()
|
|
{
|
|
if(flag.test_and_set())
|
|
{
|
|
return &value[0];
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bool reset(void* p)
|
|
{
|
|
if(static_cast<void*>(&value[0]) == p)
|
|
{
|
|
flag.clear();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
|
|
static auto& get_buffer()
|
|
{
|
|
static auto _v = std::array<buffer_entry, N>{};
|
|
return _v;
|
|
}
|
|
|
|
Tp* allocate(const size_t n) const
|
|
{
|
|
if(n == 0) return nullptr;
|
|
|
|
if(n == 1)
|
|
{
|
|
// try an find in buffer for data locality
|
|
for(auto& itr : get_buffer())
|
|
{
|
|
auto* _p = itr.get();
|
|
if(_p) return static_cast<Tp*>(_p);
|
|
}
|
|
}
|
|
|
|
auto* _p = new char[n * size];
|
|
return reinterpret_cast<Tp*>(_p);
|
|
}
|
|
|
|
void deallocate(Tp* const ptr, const size_t /*unused*/) const
|
|
{
|
|
for(auto& itr : get_buffer())
|
|
{
|
|
if(itr.reset(ptr)) return;
|
|
}
|
|
|
|
delete ptr;
|
|
}
|
|
|
|
Tp* allocate(const size_t n, const void* const /* hint */) const { return allocate(n); }
|
|
|
|
void reserve(const size_t) {}
|
|
};
|
|
|
|
} // namespace
|