Generalized updates (#174)

- include/rocprofiler/agent.h
  - move rocprofiler_dim3_t
- include/rocprofiler/buffer_tracing.h
  - size fields
  - update kernel dispatch record
- include/rocprofiler/callback_tracing.h
  - remove rocprofiler_callback_tracing_code_object_unload_data_t
  - remove rocprofiler_callback_tracing_code_object_register_host_kernel_symbol_data_t
- include/rocprofiler/fwd.h
  - added ROCPROFILER_STATUS_ERROR_CONTEXT_CONFLICT
  - remove ROCPROFILER_CALLBACK_TRACING_CODE_OBJECT_UNLOAD
  - remove ROCPROFILER_CALLBACK_TRACING_CODE_OBJECT_DEVICE_KERNEL_SYMBOL_UNREGISTER
  - add rocprofiler_kernel_id_t typedef
  - add rocprofiler_dim3_t (moved from agent.h)
- lib/common/synchronized.hpp
  - rlock/wlock return decltype(auto)
  - separate prototype from definition
- lib/common/utility.{hpp,cpp}
  - timestamp functions replicating HSA implementation(s)
  - init_public_api_struct for setting size field and ensuring certain type traits
  - simplified static_cleanup_wrapper
  - separate prototype from definition in active_capacity_gate
- lib/rocprofiler/agent.cpp
  - tweak get_rocprofiler_agent impl
- lib/rocprofiler/buffer.cpp
  - fix buffer message log level
- lib/rocprofiler/context.cpp
  - use new paradigm for getting active contexts
- lib/rocprofiler/internal_threading.hpp
  - update to simplified static_cleanup_wrapper implementation
- lib/rocprofiler/registration.cpp
  - fix deactivating contexts
- lib/rocprofiler/rocprofiler.cpp
  - status string for context conflict
- lib/rocprofiler/context/context.*
  - correlation_id struct
  - new get_active_contexts paradigm
- lib/rocprofiler/counters/core.*
  - rocprofiler_packet union
  - tweak start/stop context to accept pointer instead of handle
- lib/rocprofiler/counters/dimensions.cpp
  - update to new get_rocp_agent() return type
- lib/rocprofiler/hsa/hsa.*
  - update to new get_active_contexts paradigm
  - update to new correlation id implementation
  - guard against hsa.def.cpp direct compilation
- lib/rocprofiler/hsa/queue_controller.*
  - update to change in get_rocp_agent return type
  - consistent aliases
  - lookup function for getting queue pointer from hsa queue id
- lib/rocprofiler/hsa/queue.*
  - rocprofiler_packet
  - extend queue_info_session_t
- lib/rocprofiler/tests/registration.cpp
  - improve diagnostic on perf check for rocprofiler_lib.callback_registration_lambda_with_result
This commit is contained in:
Jonathan R. Madsen
2023-11-06 21:59:31 -06:00
zatwierdzone przez GitHub
rodzic 63775f241a
commit 55f2dabbb3
26 zmienionych plików z 828 dodań i 325 usunięć
+74 -44
Wyświetl plik
@@ -75,24 +75,10 @@ public:
Synchronized& operator=(const Synchronized&) = delete;
template <typename FuncT, typename... Args>
auto rlock(FuncT&& lambda, Args&&... args) const
{
static_assert(std::is_invocable<FuncT, const value_type&, Args...>::value,
"function must accept const reference to locked type");
auto lock = std::shared_lock{m_mutex};
return std::forward<FuncT>(lambda)(m_data, std::forward<Args>(args)...);
}
decltype(auto) rlock(FuncT&& lambda, Args&&... args) const;
template <typename FuncT, typename... Args>
auto wlock(FuncT&& lambda, Args&&... args)
{
static_assert(std::is_invocable<FuncT, value_type&, Args...>::value,
"function must accept reference to locked type");
auto lock = std::unique_lock{m_mutex};
return std::forward<FuncT>(lambda)(m_data, std::forward<Args>(args)...);
}
decltype(auto) wlock(FuncT&& lambda, Args&&... args);
// This overload to wlock allows a synchronized map whose keys map to synchronized data to
// use a read lock on the key data and then a write lock on the mapped data.
@@ -100,42 +86,86 @@ public:
typename... Args,
bool EnableForMappedType = IsMappedTypeV,
std::enable_if_t<EnableForMappedType, int> = 0>
auto wlock(FuncT&& lambda, Args&&... args) const
{
return const_cast<this_type*>(this)->wlock(std::forward<FuncT>(lambda),
std::forward<Args>(args)...);
}
decltype(auto) wlock(FuncT&& lambda, Args&&... args) const;
// Upgradable lock. If read returns false, write will be called with a unique_lock.
// Essentially a helper function that does .rlock() followed by .wlock().
template <typename ReadFuncT, typename WriteFuncT, typename... Args>
bool ulock(ReadFuncT&& read, WriteFuncT&& write, Args&&... args)
{
static_assert(std::is_invocable<ReadFuncT, const value_type&, Args...>::value,
"read function must accept const reference to locked type");
static_assert(std::is_invocable<WriteFuncT, value_type&, Args...>::value,
"write function must accept reference to locked type");
using read_return_type = std::invoke_result_t<ReadFuncT, const value_type&, Args...>;
using write_return_type = std::invoke_result_t<WriteFuncT, value_type&, Args...>;
static_assert(std::is_same<read_return_type, write_return_type>::value,
"read and write functions must return same type");
static_assert(std::is_same<read_return_type, bool>::value,
"read/write functions must return bool");
{
auto lock = std::shared_lock{m_mutex};
if(read(m_data, std::forward<Args>(args)...)) return true;
}
auto lock = std::unique_lock{m_mutex};
return write(m_data, std::forward<Args>(args)...);
}
bool ulock(ReadFuncT&& read, WriteFuncT&& write, Args&&... args);
private:
mutable std::shared_mutex m_mutex = {};
value_type m_data = {};
};
//
// member definitions
//
template <typename LockedType, bool IsMappedTypeV>
template <typename FuncT, typename... Args>
decltype(auto)
Synchronized<LockedType, IsMappedTypeV>::rlock(FuncT&& lambda, Args&&... args) const
{
static_assert(std::is_invocable<FuncT, const value_type&, Args...>::value,
"function must accept const reference to locked type");
auto lock = std::shared_lock{m_mutex};
return std::forward<FuncT>(lambda)(m_data, std::forward<Args>(args)...);
}
template <typename LockedType, bool IsMappedTypeV>
template <typename FuncT, typename... Args>
decltype(auto)
Synchronized<LockedType, IsMappedTypeV>::wlock(FuncT&& lambda, Args&&... args)
{
static_assert(std::is_invocable<FuncT, value_type&, Args...>::value,
"function must accept reference to locked type");
auto lock = std::unique_lock{m_mutex};
return std::forward<FuncT>(lambda)(m_data, std::forward<Args>(args)...);
}
// This overload to wlock allows a synchronized map whose keys map to synchronized data to
// use a read lock on the key data and then a write lock on the mapped data.
template <typename LockedType, bool IsMappedTypeV>
template <typename FuncT,
typename... Args,
bool EnableForMappedType,
std::enable_if_t<EnableForMappedType, int>>
decltype(auto)
Synchronized<LockedType, IsMappedTypeV>::wlock(FuncT&& lambda, Args&&... args) const
{
return const_cast<this_type*>(this)->wlock(std::forward<FuncT>(lambda),
std::forward<Args>(args)...);
}
// Upgradable lock. If read returns false, write will be called with a unique_lock.
// Essentially a helper function that does .rlock() followed by .wlock().
template <typename LockedType, bool IsMappedTypeV>
template <typename ReadFuncT, typename WriteFuncT, typename... Args>
bool
Synchronized<LockedType, IsMappedTypeV>::ulock(ReadFuncT&& read, WriteFuncT&& write, Args&&... args)
{
static_assert(std::is_invocable<ReadFuncT, const value_type&, Args...>::value,
"read function must accept const reference to locked type");
static_assert(std::is_invocable<WriteFuncT, value_type&, Args...>::value,
"write function must accept reference to locked type");
using read_return_type = std::invoke_result_t<ReadFuncT, const value_type&, Args...>;
using write_return_type = std::invoke_result_t<WriteFuncT, value_type&, Args...>;
static_assert(std::is_same<read_return_type, write_return_type>::value,
"read and write functions must return same type");
static_assert(std::is_same<read_return_type, bool>::value,
"read/write functions must return bool");
{
auto lock = std::shared_lock{m_mutex};
if(read(m_data, std::forward<Args>(args)...)) return true;
}
auto lock = std::unique_lock{m_mutex};
return write(m_data, std::forward<Args>(args)...);
}
} // namespace common
} // namespace rocprofiler
+84
Wyświetl plik
@@ -21,16 +21,100 @@
#include "lib/common/utility.hpp"
#include <glog/logging.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <ctime>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "lib/common/defines.hpp"
namespace rocprofiler
{
namespace common
{
namespace
{
std::string_view
get_clock_name(clockid_t _id)
{
#define CLOCK_NAME_CASE_STATEMENT(NAME) \
case NAME: return #NAME;
switch(_id)
{
CLOCK_NAME_CASE_STATEMENT(CLOCK_REALTIME)
CLOCK_NAME_CASE_STATEMENT(CLOCK_MONOTONIC)
CLOCK_NAME_CASE_STATEMENT(CLOCK_PROCESS_CPUTIME_ID)
CLOCK_NAME_CASE_STATEMENT(CLOCK_THREAD_CPUTIME_ID)
CLOCK_NAME_CASE_STATEMENT(CLOCK_MONOTONIC_RAW)
CLOCK_NAME_CASE_STATEMENT(CLOCK_REALTIME_COARSE)
CLOCK_NAME_CASE_STATEMENT(CLOCK_MONOTONIC_COARSE)
CLOCK_NAME_CASE_STATEMENT(CLOCK_BOOTTIME)
CLOCK_NAME_CASE_STATEMENT(CLOCK_REALTIME_ALARM)
CLOCK_NAME_CASE_STATEMENT(CLOCK_BOOTTIME_ALARM)
CLOCK_NAME_CASE_STATEMENT(CLOCK_TAI)
default: break;
}
return "CLOCK_UNKNOWN";
}
} // namespace
clockid_t
get_accurate_clock_id_impl()
{
auto clock = CLOCK_MONOTONIC;
utsname kernelInfo;
if(uname(&kernelInfo) == 0)
{
try
{
std::string ver = kernelInfo.release;
size_t idx;
int major = std::stoi(ver, &idx);
int minor = std::stoi(ver.substr(idx + 1));
if(major > 4 || ((major == 4) && (minor >= 4)))
{
clock = CLOCK_MONOTONIC_RAW;
}
} catch(...)
{
// Kernel version string doesn't conform to the standard pattern.
// Keep using the "safe" (non-RAW) clock.
}
}
return clock;
}
uint64_t
get_clock_freq_ns_impl(clockid_t _clk_id)
{
constexpr auto nanosec = std::nano::den;
struct timespec ts;
auto ret = clock_getres(_clk_id, &ts);
if(ROCPROFILER_UNLIKELY(ret != 0))
{
auto _err = errno;
LOG(FATAL) << "error getting clock resolution for " << get_clock_name(_clk_id) << ": "
<< strerror(_err);
}
else if(ROCPROFILER_UNLIKELY(ts.tv_sec != 0 ||
ts.tv_nsec >= std::numeric_limits<uint32_t>::max()))
{
LOG(FATAL) << "clock_getres(" << get_clock_name(_clk_id)
<< ") returned very low frequency (<1Hz)";
}
auto&& _period =
(static_cast<uint64_t>(ts.tv_sec) * nanosec) + static_cast<uint64_t>(ts.tv_nsec);
return nanosec / _period;
}
std::vector<std::string>
read_command_line(pid_t _pid)
{
+164 -47
Wyświetl plik
@@ -22,19 +22,37 @@
#pragma once
#include "lib/common/defines.hpp"
#include <glog/logging.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <functional>
#include <mutex>
#include <ratio>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
namespace rocprofiler
{
namespace common
{
clockid_t
get_accurate_clock_id_impl();
uint64_t
get_clock_freq_ns_impl(clockid_t _clk_id);
inline uint64_t
get_tid()
{
@@ -43,11 +61,51 @@ get_tid()
return _v;
}
inline clockid_t
get_accurate_clock_id()
{
static auto clk_id = get_accurate_clock_id_impl();
return clk_id;
}
inline uint64_t
get_accurate_clock_freq_ns()
{
static auto clk_freq = get_clock_freq_ns_impl(get_accurate_clock_id());
return clk_freq;
}
inline uint64_t
get_ticks(clockid_t clk_id_v) noexcept
{
constexpr auto nanosec = std::nano::den;
auto&& ts = timespec{};
auto ret = clock_gettime(clk_id_v, &ts);
if(ROCPROFILER_UNLIKELY(ret != 0))
{
auto _err = errno;
LOG(FATAL) << "clock_gettime failed: " << strerror(_err);
}
return (static_cast<uint64_t>(ts.tv_sec) * nanosec) + static_cast<uint64_t>(ts.tv_nsec);
}
// this equates to HSA-runtime library implementation of os::ReadAccurateClock()
inline uint64_t
timestamp_ns()
{
// TODO(jrmadsen): this should be updated to the HSA method
return std::chrono::steady_clock::now().time_since_epoch().count();
return get_ticks(get_accurate_clock_id()) * get_accurate_clock_freq_ns();
}
// this equates to HSA-runtime library implementation of os::ReadSystemClock()
inline uint64_t
system_timestamp_ns()
{
constexpr auto boottime_clk = CLOCK_BOOTTIME;
static auto boottime_clk_freq = get_clock_freq_ns_impl(boottime_clk);
return get_ticks(boottime_clk) * boottime_clk_freq;
}
std::vector<std::string>
@@ -69,34 +127,85 @@ get_val(Container& map, const Key& key)
return (pos != map.end() ? &pos->second : nullptr);
}
template <typename Tp>
constexpr void
assert_public_api_struct_properties()
{
static_assert(std::is_class<Tp>::value, "this is not a public API struct");
static_assert(std::is_standard_layout<Tp>::value,
"public API struct should have a standard layout");
static_assert(std::is_trivially_default_constructible<Tp>::value,
"public API struct should be trivially default constructible");
static_assert(std::is_trivially_copy_constructible<Tp>::value,
"public API struct should be trivially copy constructible");
static_assert(std::is_trivially_move_constructible<Tp>::value,
"public API struct should be trivially move constructible");
static_assert(std::is_trivially_copy_assignable<Tp>::value,
"public API struct should be trivially move assignable");
static_assert(std::is_trivially_move_assignable<Tp>::value,
"public API struct should be trivially move assignable");
static_assert(std::is_trivially_copyable<Tp>::value,
"public API struct should be trivially move assignable");
static_assert(std::is_trivial<Tp>::value, "public API struct should be trivial");
static_assert(offsetof(Tp, size) == 0, "public API struct should have a size field first");
static_assert(sizeof(std::declval<Tp>().size) == sizeof(uint64_t),
"public API struct size field should be 64 bits");
}
template <typename Tp>
decltype(auto)
init_public_api_struct(Tp&& val)
{
assert_public_api_struct_properties<Tp>();
::memset(&val, 0, sizeof(Tp));
val.size = sizeof(Tp);
return std::forward<Tp>(val);
}
template <typename Tp>
Tp&
init_public_api_struct(Tp& val)
{
assert_public_api_struct_properties<Tp>();
::memset(&val, 0, sizeof(Tp));
val.size = sizeof(Tp);
return val;
}
/**
* A simple wrapper that will call a function when the
* wrapper is being destroyed. This is primarily useful
* for static variables where we want to run some destruction
* operations when the program exits.
*/
template <typename T, typename L>
template <typename Tp>
class static_cleanup_wrapper
{
public:
static_cleanup_wrapper(T&& data, L&& destroy_func)
: _data(std::move(data))
, _destroy_func(destroy_func)
using data_type = Tp;
using functor_type = std::function<void(Tp&)>;
static_cleanup_wrapper(data_type&& data, functor_type&& destroy_func)
: m_data(std::move(data))
, m_destroy_func(std::move(destroy_func))
{}
static_cleanup_wrapper(L&& destroy_func)
: _destroy_func(destroy_func)
static_cleanup_wrapper(functor_type&& destroy_func)
: m_destroy_func(std::move(destroy_func))
{}
~static_cleanup_wrapper() { _destroy_func(_data); }
~static_cleanup_wrapper() { m_destroy_func(m_data); }
void destroy() { _destroy_func(_data); }
void destroy() { m_destroy_func(m_data); }
T& get() { return _data; }
data_type& get() { return m_data; }
const data_type& get() const { return m_data; }
private:
T _data;
L _destroy_func;
data_type m_data = {};
functor_type m_destroy_func = {};
};
/**
@@ -107,41 +216,10 @@ private:
class active_capacity_gate
{
public:
active_capacity_gate(size_t capacity)
: _capacity(capacity)
{}
void add_active(size_t size)
{
if(size >= _capacity)
{
throw std::runtime_error("Size exceeds gate capacity");
}
active_capacity_gate(size_t capacity);
std::unique_lock lock(_m);
if(_count + size < _capacity)
{
_count += size;
return;
}
_waiters++;
_cv.wait(lock, [&]() { return _count + size < _capacity; });
_waiters--;
_count += size;
}
void remove_active(size_t size)
{
std::unique_lock lock(_m);
if(_count > size)
_count -= size;
else
_count = 0;
if(_waiters > 0)
{
_cv.notify_all();
}
}
void add_active(size_t size);
void remove_active(size_t size);
private:
size_t _count{0};
@@ -151,5 +229,44 @@ private:
std::condition_variable _cv;
};
inline active_capacity_gate::active_capacity_gate(size_t capacity)
: _capacity(capacity)
{}
inline void
active_capacity_gate::add_active(size_t size)
{
if(size >= _capacity)
{
throw std::runtime_error("Size exceeds gate capacity");
}
std::unique_lock lock(_m);
if(_count + size < _capacity)
{
_count += size;
return;
}
_waiters++;
_cv.wait(lock, [&]() { return _count + size < _capacity; });
_waiters--;
_count += size;
}
inline void
active_capacity_gate::remove_active(size_t size)
{
std::unique_lock lock(_m);
if(_count > size)
_count -= size;
else
_count = 0;
if(_waiters > 0)
{
_cv.notify_all();
}
}
} // namespace common
} // namespace rocprofiler