Common library fixes (#57)

* Update common/container

- fix namespace issue in operators.hpp
- fix exceptions in stable_vector
- fix exceptions in static_vector
- fix emplace_back construction with no args in static_vector

* Add lib/common/utility.hpp

- get_tid function

* Update lib/common/utility.hpp

- add timestamp_ns function

[ROCm/rocprofiler-sdk commit: 06f7b780f9]
This commit is contained in:
Jonathan R. Madsen
2023-09-14 14:24:11 -05:00
committed by GitHub
parent 769e1de7ab
commit ddefa6424d
5 changed files with 74 additions and 15 deletions
@@ -2,7 +2,8 @@
# Builds common utilities into a static library
#
set(common_sources config.cpp environment.cpp demangle.cpp)
set(common_headers config.hpp defines.hpp environment.hpp demangle.hpp mpl.hpp)
set(common_headers config.hpp defines.hpp environment.hpp demangle.hpp mpl.hpp
utility.hpp)
add_library(rocprofiler-common-library STATIC)
add_library(rocprofiler::rocprofiler-common-library ALIAS rocprofiler-common-library)
@@ -33,9 +33,9 @@
#define ROCPROFILER_OPERATOR_TEMPLATE2(template_name2) \
ROCPROFILER_IMPORT_TEMPLATE2(template_name2) \
template <typename T, typename U, typename B> \
struct is_chained_base<::rocprofiler::container::template_name2<T, U, B>> \
struct is_chained_base<::rocprofiler::common::container::template_name2<T, U, B>> \
{ \
using value = ::rocprofiler::container::true_t; \
using value = ::rocprofiler::common::container::true_t; \
};
// Import a 1-type-argument operator template into boost (if necessary) and
@@ -43,9 +43,9 @@
#define ROCPROFILER_OPERATOR_TEMPLATE1(template_name1) \
ROCPROFILER_IMPORT_TEMPLATE1(template_name1) \
template <typename T, typename B> \
struct is_chained_base<::rocprofiler::container::template_name1<T, B>> \
struct is_chained_base<::rocprofiler::common::container::template_name1<T, B>> \
{ \
using value = ::rocprofiler::container::true_t; \
using value = ::rocprofiler::common::container::true_t; \
};
#define ROCPROFILER_OPERATOR_TEMPLATE(template_name) \
@@ -74,7 +74,7 @@
template <typename T, typename U, typename B, typename O> \
struct is_chained_base<template_name<T, U, B, O>> \
{ \
using value = ::rocprofiler::container::true_t; \
using value = ::rocprofiler::common::container::true_t; \
}; \
\
ROCPROFILER_OPERATOR_TEMPLATE2(template_name##2) \
@@ -24,6 +24,7 @@
#include "lib/common/container/operators.hpp"
#include "lib/common/container/static_vector.hpp"
#include "lib/common/defines.hpp"
#include <algorithm>
#include <initializer_list>
@@ -359,8 +360,8 @@ stable_vector<Tp, ChunkSizeV>::at(size_type i)
{
if(ROCPROFILER_UNLIKELY(i >= size()))
{
throw ::rocprofiler::exception<std::out_of_range>("stable_vector::at(" + std::to_string(i) +
"). size is " + std::to_string(size()));
throw std::out_of_range("stable_vector::at(" + std::to_string(i) + "). size is " +
std::to_string(size()));
}
return operator[](i);
@@ -23,6 +23,7 @@
#pragma once
#include "lib/common/container/c_array.hpp"
#include "lib/common/defines.hpp"
#include <array>
#include <atomic>
@@ -141,8 +142,8 @@ static_vector<Tp, N, AtomicSizeV>::operator=(std::initializer_list<Tp>&& _v)
{
if(ROCPROFILER_UNLIKELY(_v.size() > N))
{
throw exception<std::out_of_range>(
std::string{"static_vector::operator=(initializer_list) size > "} + std::to_string(N));
throw std::out_of_range(std::string{"static_vector::operator=(initializer_list) size > "} +
std::to_string(N));
}
clear();
@@ -196,14 +197,21 @@ static_vector<Tp, N, AtomicSizeV>::emplace_back(Args&&... _v)
auto _idx = m_size++;
if(_idx >= N)
{
throw exception<std::out_of_range>(
std::string{"static_vector::emplace_back - reached capacity "} + std::to_string(N));
throw std::out_of_range(std::string{"static_vector::emplace_back - reached capacity "} +
std::to_string(N));
}
if constexpr(std::is_assignable<Tp, decltype(std::forward<Args>(_v))...>::value)
m_data[_idx] = {std::forward<Args>(_v)...};
if constexpr(sizeof...(Args) > 0)
{
if constexpr(std::is_assignable<Tp, decltype(std::forward<Args>(_v))...>::value)
m_data[_idx] = {std::forward<Args>(_v)...};
else
m_data[_idx] = Tp{std::forward<Args>(_v)...};
}
else
m_data[_idx] = Tp{std::forward<Args>(_v)...};
{
m_data[_idx] = {};
}
return m_data[_idx];
}
@@ -0,0 +1,49 @@
// MIT License
//
// Copyright (c) 2023 ROCm Developer Tools
//
// 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:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// 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.
#pragma once
#include <sys/syscall.h>
#include <unistd.h>
#include <chrono>
#include <cstdint>
namespace rocprofiler
{
namespace common
{
inline uint64_t
get_tid()
{
// system calls are expensive so store this in a thread-local
static thread_local uint64_t _v = ::syscall(__NR_gettid);
return _v;
}
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();
}
} // namespace common
} // namespace rocprofiler