Initial skeleton (#1)

* googletest submodule

* cmake folder

* misc root files

- clang-format
- cmake-format
- pyproject.toml
- requirements.txt
- VERSION

* workflows

* RPM files

* external folder

* samples folder

* tests root folder

* source/bin folder

* source/include folder

* source/lib/common folder

* source/lib/plugins folder

* source/lib/tests folder

- for library unit tests

* source/lib/rocprofiler folder

- rocprofiler library implementation

* Remaining cmake files

* lib/common/containers

- ring_buffer
- atomic_ring_buffer
- stable_vector
- static_vector

* Update .gitignore

* Update hsa.hpp

- include cstdint

* cmake formatting (cmake-format) (#2)

Co-authored-by: jrmadsen <jrmadsen@users.noreply.github.com>

* Remove linting.yml

- uses self-hosted runners

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Jonathan R. Madsen
2023-08-08 18:39:01 -05:00
zatwierdzone przez GitHub
rodzic 7d1c7757a8
commit 527aa71f5a
75 zmienionych plików z 20094 dodań i 0 usunięć
@@ -0,0 +1,10 @@
#
set(containers_sources)
set(containers_headers atomic_ring_buffer.hpp c_array.hpp operators.hpp ring_buffer.hpp
stable_vector.hpp static_vector.hpp)
set(containers_sources atomic_ring_buffer.cpp ring_buffer.cpp)
target_sources(rocprofiler-common-library PRIVATE ${containers_sources}
${containers_headers})
@@ -0,0 +1,297 @@
// MIT License
//
// Copyright (c) 2020, The Regents of the University of California,
// through Lawrence Berkeley National Laboratory (subject to receipt of any
// required approvals from the U.S. Dept. of Energy). All rights reserved.
//
// 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.
#include "atomic_ring_buffer.hpp"
#include "lib/common/units.hpp"
#include "lib/common/environment.hpp"
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <cstddef>
#include <sys/mman.h>
namespace rocprofiler
{
namespace common
{
namespace container
{
namespace base
{
atomic_ring_buffer::atomic_ring_buffer(size_t _size, bool _use_mmap)
{
set_use_mmap(_use_mmap);
init(_size);
}
atomic_ring_buffer::~atomic_ring_buffer() { destroy(); }
atomic_ring_buffer::atomic_ring_buffer(const atomic_ring_buffer& rhs)
: m_use_mmap{rhs.m_use_mmap}
, m_use_mmap_explicit{rhs.m_use_mmap_explicit}
{
init(rhs.m_size);
}
atomic_ring_buffer::atomic_ring_buffer(atomic_ring_buffer&& rhs) noexcept
: m_init{rhs.m_init}
, m_use_mmap{rhs.m_use_mmap}
, m_use_mmap_explicit{rhs.m_use_mmap_explicit}
, m_ptr{rhs.m_ptr}
, m_size{rhs.m_size}
, m_read_count{rhs.m_read_count.load()}
, m_write_count{rhs.m_write_count.load()}
{
rhs.reset();
}
atomic_ring_buffer&
atomic_ring_buffer::operator=(const atomic_ring_buffer& rhs)
{
if(this == &rhs) return *this;
destroy();
m_use_mmap = rhs.m_use_mmap;
m_use_mmap_explicit = rhs.m_use_mmap_explicit;
init(rhs.m_size);
return *this;
}
atomic_ring_buffer&
atomic_ring_buffer::operator=(atomic_ring_buffer&& rhs) noexcept
{
if(this == &rhs) return *this;
destroy();
m_init = rhs.m_init;
m_use_mmap = rhs.m_use_mmap;
m_use_mmap_explicit = rhs.m_use_mmap_explicit;
m_ptr = rhs.m_ptr;
m_size = rhs.m_size;
m_read_count = rhs.m_read_count.load();
m_write_count = rhs.m_write_count.load();
rhs.reset();
return *this;
}
void
atomic_ring_buffer::init(size_t _size)
{
if(m_init)
throw std::runtime_error(
"tim::base::atomic_ring_buffer::init(size_t) :: already initialized");
m_init = true;
// Round up to multiple of page size.
_size += units::get_page_size() - ((_size % units::get_page_size() > 0)
? (_size % units::get_page_size())
: units::get_page_size());
if((_size % units::get_page_size()) > 0)
{
std::ostringstream _oss{};
_oss << "Error! size is not a multiple of page size: " << _size << " % "
<< units::get_page_size() << " = " << (_size % units::get_page_size());
throw std::runtime_error(_oss.str());
}
m_size = _size;
m_read_count = 0;
m_write_count = 0;
if(!m_use_mmap_explicit) m_use_mmap = get_env("ROCPROFILER_USE_MMAP", m_use_mmap);
if(!m_use_mmap)
{
m_ptr = malloc(m_size * sizeof(char));
return;
}
// Map twice the buffer size.
if((m_ptr =
mmap(nullptr, m_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) ==
MAP_FAILED)
{
destroy();
auto _err = errno;
// TIMEMORY_PRINTF_FATAL(stderr, "Error using mmap: %s\n", strerror(_err));
throw std::runtime_error(strerror(_err));
}
}
void
atomic_ring_buffer::destroy()
{
if(m_ptr && m_init)
{
if(!m_use_mmap)
{
::free(m_ptr);
}
else
{
// Unmap the mapped virtual memmory.
auto ret = munmap(m_ptr, m_size);
if(ret != 0) perror("munmap");
}
}
m_init = false;
m_size = 0;
m_read_count = 0;
m_write_count = 0;
m_ptr = nullptr;
}
void
atomic_ring_buffer::set_use_mmap(bool _v)
{
if(m_init)
throw std::runtime_error("tim::base::atomic_ring_buffer::set_use_mmap(bool) cannot be "
"called after initialization");
m_use_mmap = _v;
m_use_mmap_explicit = true;
}
std::string
atomic_ring_buffer::as_string() const
{
std::ostringstream ss{};
ss << std::boolalpha << "is_initialized: " << is_initialized() << ", capacity: " << capacity()
<< ", count: " << count() << ", free: " << free() << ", is_empty: " << is_empty()
<< ", is_full: " << is_full() << ", pointer: " << m_ptr << ", read count: " << m_read_count
<< ", write count: " << m_write_count;
return ss.str();
}
//
void*
atomic_ring_buffer::request(size_t _length)
{
if(m_ptr == nullptr || m_size == 0) return nullptr;
if(is_full()) return retrieve(_length);
// if write count is at the tail of buffer, bump to the end of buffer
size_t _write_count = 0;
size_t _offset = 0;
do
{
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
if(_length > free()) return nullptr;
_offset = 0;
_write_count = m_write_count.load();
auto _modulo = m_size - (_write_count % m_size);
if(_modulo < _length) _offset = _modulo;
} while(!m_write_count.compare_exchange_strong(
_write_count, _write_count + _length + _offset, std::memory_order_seq_cst));
// pointer in buffer
void* _out = write_ptr(_write_count);
return _out;
}
//
void*
atomic_ring_buffer::retrieve(size_t _length) const
{
if(m_ptr == nullptr || m_size == 0) return nullptr;
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
// if read count is at the tail of buffer, bump to the end of buffer
size_t _read_count = 0;
size_t _offset = 0;
do
{
if(_length > count()) return nullptr;
_offset = 0;
_read_count = m_read_count.load();
auto _modulo = m_size - (_read_count % m_size);
if(_modulo < _length) _offset = _modulo;
} while(!m_read_count.compare_exchange_strong(
_read_count, _read_count + _length + _offset, std::memory_order_seq_cst));
// pointer in buffer
void* _out = read_ptr(_read_count);
return _out;
}
//
void
atomic_ring_buffer::reset()
{
m_init = false;
m_size = 0;
m_ptr = nullptr;
m_read_count.store(0);
m_write_count.store(0);
}
//
void
atomic_ring_buffer::save(std::fstream& _fs)
{
auto _read_count = m_read_count.load();
auto _write_count = m_write_count.load();
_fs.write(reinterpret_cast<char*>(&m_use_mmap), sizeof(m_use_mmap));
_fs.write(reinterpret_cast<char*>(&m_use_mmap_explicit), sizeof(m_use_mmap_explicit));
_fs.write(reinterpret_cast<char*>(&m_size), sizeof(m_size));
_fs.write(reinterpret_cast<char*>(&_read_count), sizeof(_read_count));
_fs.write(reinterpret_cast<char*>(&_write_count), sizeof(_write_count));
_fs.write(reinterpret_cast<char*>(m_ptr), m_size * sizeof(char));
}
//
void
atomic_ring_buffer::load(std::fstream& _fs)
{
destroy();
size_t _read_count = 0;
size_t _write_count = 0;
_fs.read(reinterpret_cast<char*>(&m_use_mmap), sizeof(m_use_mmap));
_fs.read(reinterpret_cast<char*>(&m_use_mmap_explicit), sizeof(m_use_mmap_explicit));
_fs.read(reinterpret_cast<char*>(&m_size), sizeof(m_size));
init(m_size);
if(!m_ptr) m_ptr = malloc(m_size);
_fs.read(reinterpret_cast<char*>(&_read_count), sizeof(_read_count));
_fs.read(reinterpret_cast<char*>(&_write_count), sizeof(_write_count));
_fs.read(reinterpret_cast<char*>(m_ptr), m_size * sizeof(char));
m_read_count.store(_read_count);
m_write_count.store(_write_count);
}
} // namespace base
} // namespace container
} // namespace common
} // namespace rocprofiler
@@ -0,0 +1,426 @@
// MIT License
//
// Copyright (c) 2020, The Regents of the University of California,
// through Lawrence Berkeley National Laboratory (subject to receipt of any
// required approvals from the U.S. Dept. of Energy). All rights reserved.
//
// 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 "lib/common/units.hpp"
#include "lib/common/environment.hpp"
#include <algorithm>
#include <atomic>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <utility>
#include <vector>
#include <cstddef>
#include <cstdlib>
namespace rocprofiler
{
namespace common
{
namespace container
{
template <typename Tp>
struct atomic_ring_buffer;
//
namespace base
{
/// \struct tim::base::atomic_ring_buffer
/// \brief Ring buffer implementation, with support for mmap as backend (Linux only).
struct atomic_ring_buffer
{
template <typename Tp>
friend struct container::atomic_ring_buffer;
atomic_ring_buffer() = default;
explicit atomic_ring_buffer(bool _use_mmap) { set_use_mmap(_use_mmap); }
explicit atomic_ring_buffer(size_t _size) { init(_size); }
atomic_ring_buffer(size_t _size, bool _use_mmap);
~atomic_ring_buffer();
atomic_ring_buffer(const atomic_ring_buffer&);
atomic_ring_buffer& operator=(const atomic_ring_buffer&);
atomic_ring_buffer(atomic_ring_buffer&&) noexcept;
atomic_ring_buffer& operator=(atomic_ring_buffer&&) noexcept;
/// Returns whether the buffer has been allocated
bool is_initialized() const { return m_init; }
/// Get the total number of bytes supported
size_t capacity() const { return m_size; }
/// Creates new ring buffer.
void init(size_t size);
/// Destroy ring buffer.
void destroy();
/// Request a pointer for writing at least \param n bytes.
void* request(size_t n);
/// Retrieve a pointer for reading at least \param n bytes.
void* retrieve(size_t n) const;
/// Write class-type data to buffer (uses placement new).
template <typename Tp>
std::pair<size_t, Tp*> write(Tp* in, std::enable_if_t<std::is_class<Tp>::value, int> = 0);
/// Write non-class-type data to buffer (uses memcpy).
template <typename Tp>
std::pair<size_t, Tp*> write(Tp* in, std::enable_if_t<!std::is_class<Tp>::value, int> = 0);
/// Request a pointer to an allocation. This is similar to a "write" except the
/// memory is uninitialized. Typically used by allocators. If Tp is a class type,
/// be sure to use a placement new instead of a memcpy.
template <typename Tp>
Tp* request();
/// Read class-type data from buffer (uses placement new).
template <typename Tp>
std::pair<size_t, Tp*> read(Tp* _dest,
std::enable_if_t<std::is_class<Tp>::value, int> = 0) const;
/// Read non-class-type data from buffer (uses memcpy).
template <typename Tp>
std::pair<size_t, Tp*> read(Tp* _dest,
std::enable_if_t<!std::is_class<Tp>::value, int> = 0) const;
/// Retrieve a pointer to the head allocation (read).
template <typename Tp>
Tp* retrieve() const;
/// Returns number of bytes currently held by the buffer.
size_t count() const { return (m_write_count - m_read_count); }
/// Returns how many bytes are availiable in the buffer.
size_t free() const { return (m_size - count()); }
/// Returns if the buffer is empty.
bool is_empty() const { return (count() == 0); }
/// Returns if the buffer is full.
bool is_full() const { return (count() == m_size); }
/// explicitly configure to use mmap if avail
void set_use_mmap(bool);
/// query whether using mmap
bool get_use_mmap() const { return m_use_mmap; }
std::string as_string() const;
void save(std::fstream& _fs);
void load(std::fstream& _fs);
private:
/// Returns the current write pointer.
void* write_ptr(size_t _write_count) const
{
return static_cast<char*>(m_ptr) + (_write_count % m_size);
}
/// Returns the current read pointer.
void* read_ptr(size_t _read_count) const
{
return static_cast<char*>(m_ptr) + (_read_count % m_size);
}
void reset();
private:
bool m_init = false;
bool m_use_mmap = true;
bool m_use_mmap_explicit = false;
void* m_ptr = nullptr;
size_t m_size = 0;
mutable std::atomic<size_t> m_read_count = 0;
std::atomic<size_t> m_write_count = 0;
};
//
template <typename Tp>
std::pair<size_t, Tp*>
atomic_ring_buffer::write(Tp* in, std::enable_if_t<std::is_class<Tp>::value, int>)
{
if(in == nullptr || m_ptr == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
void* _out_p = request(_length);
if(_out_p == nullptr) return {0, nullptr};
// Copy in.
new(_out_p) Tp{std::move(*in)};
// pointer in buffer
Tp* _out = reinterpret_cast<Tp*>(_out_p);
return {_length, _out};
}
//
template <typename Tp>
std::pair<size_t, Tp*>
atomic_ring_buffer::write(Tp* in, std::enable_if_t<!std::is_class<Tp>::value, int>)
{
if(in == nullptr || m_ptr == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
void* _out_p = request(_length);
if(_out_p == nullptr) return {0, nullptr};
// Copy in.
memcpy(_out_p, in, _length);
// pointer in buffer
Tp* _out = reinterpret_cast<Tp*>(_out_p);
return {_length, _out};
}
//
template <typename Tp>
Tp*
atomic_ring_buffer::request()
{
if(m_ptr == nullptr) return nullptr;
return request(sizeof(Tp));
}
//
template <typename Tp>
std::pair<size_t, Tp*>
atomic_ring_buffer::read(Tp* _dest, std::enable_if_t<std::is_class<Tp>::value, int>) const
{
if(is_empty() || _dest == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
void* _out_p = retrieve(_length);
if(_out_p == nullptr) return {0, nullptr};
// pointer in buffer
Tp* in = reinterpret_cast<Tp*>(_out_p);
// Copy out for BYTE, nothing magic here.
*_dest = *in;
return {_length, in};
}
//
template <typename Tp>
std::pair<size_t, Tp*>
atomic_ring_buffer::read(Tp* _dest, std::enable_if_t<!std::is_class<Tp>::value, int>) const
{
if(is_empty() || _dest == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
void* _out_p = retrieve(_length);
if(_out_p == nullptr) return {0, nullptr};
// pointer in buffer
Tp* in = reinterpret_cast<Tp*>(_out_p);
using Up = typename std::remove_const<Tp>::type;
// Copy out for BYTE, nothing magic here.
Up* _out = const_cast<Up*>(_dest);
memcpy(_out, in, _length);
return {_length, in};
}
//
template <typename Tp>
Tp*
atomic_ring_buffer::retrieve() const
{
if(m_ptr == nullptr) return nullptr;
return retrieve(sizeof(Tp));
}
//
} // namespace base
//
/// \struct tim::data_storage::atomic_ring_buffer
/// \brief Ring buffer wrapper around \ref tim::base::atomic_ring_buffer for data of type
/// Tp. If the data object size is larger than the page size (typically 4KB), behavior is
/// undefined. During initialization, one requests a minimum number of objects and the
/// buffer will support that number of object + the remainder of the page, e.g. if a page
/// is 1000 bytes, the object is 1 byte, and the buffer is requested to support 1500
/// objects, then an allocation supporting 2000 objects (i.e. 2 pages) will be created.
template <typename Tp>
struct atomic_ring_buffer : private base::atomic_ring_buffer
{
using base_type = base::atomic_ring_buffer;
static size_t get_items_per_page();
atomic_ring_buffer() = default;
~atomic_ring_buffer() = default;
explicit atomic_ring_buffer(bool _use_mmap)
: base_type{_use_mmap}
{}
explicit atomic_ring_buffer(size_t _size)
: base_type{_size * sizeof(Tp)}
{}
atomic_ring_buffer(size_t _size, bool _use_mmap)
: base_type{_size * sizeof(Tp), _use_mmap}
{}
atomic_ring_buffer(const atomic_ring_buffer&);
atomic_ring_buffer(atomic_ring_buffer&&) noexcept = default;
atomic_ring_buffer& operator=(const atomic_ring_buffer&);
atomic_ring_buffer& operator=(atomic_ring_buffer&&) noexcept = default;
/// Returns whether the buffer has been allocated
bool is_initialized() const { return base_type::is_initialized(); }
/// Get the total number of Tp instances supported
size_t capacity() const { return (base_type::capacity()) / sizeof(Tp); }
/// Creates new ring buffer.
void init(size_t _size) { base_type::init(_size * sizeof(Tp)); }
/// Destroy ring buffer.
void destroy() { base_type::destroy(); }
/// Write data to buffer.
size_t data_size() const { return sizeof(Tp); }
/// Write data to buffer. Return pointer to location of write
Tp* write(Tp* in) { return base_type::write<Tp>(in).second; }
/// Read data from buffer. Return pointer to location of read
Tp* read(Tp* _dest) const { return base_type::read<Tp>(_dest).second; }
/// Get an uninitialized address at tail of buffer.
Tp* request() { return base_type::request<Tp>(); }
/// Read data from head of buffer.
Tp* retrieve() { return base_type::retrieve<Tp>(); }
/// Returns number of Tp instances currently held by the buffer.
size_t count() const { return (base_type::count()) / sizeof(Tp); }
/// Returns how many Tp instances are availiable in the buffer.
size_t free() const { return (base_type::free()) / sizeof(Tp); }
/// Returns if the buffer is empty.
bool is_empty() const { return base_type::is_empty(); }
/// Returns if the buffer is full.
bool is_full() const { return (base_type::free() < sizeof(Tp)); }
template <typename... Args>
auto emplace(Args&&... args)
{
Tp _obj{std::forward<Args>(args)...};
return write(&_obj);
}
using base_type::get_use_mmap;
using base_type::load;
using base_type::save;
using base_type::set_use_mmap;
std::string as_string() const
{
std::ostringstream ss{};
size_t _w = std::log10(base_type::capacity()) + 1;
ss << std::boolalpha << std::right << "data size: " << std::setw(_w) << data_size()
<< " B, is_initialized: " << std::setw(5) << is_initialized()
<< ", is_empty: " << std::setw(5) << is_empty() << ", is_full: " << std::setw(5)
<< is_full() << ", capacity: " << std::setw(_w) << capacity()
<< ", count: " << std::setw(_w) << count() << ", free: " << std::setw(_w) << free()
<< ", raw capacity: " << std::setw(_w) << base_type::capacity()
<< " B, raw count: " << std::setw(_w) << base_type::count()
<< " B, raw free: " << std::setw(_w) << base_type::free()
<< " B, pointer: " << std::setw(15) << base_type::m_ptr
<< ", raw read count: " << std::setw(_w) << base_type::m_read_count
<< ", raw write count: " << std::setw(_w) << base_type::m_write_count;
return ss.str();
}
friend std::ostream& operator<<(std::ostream& os, const atomic_ring_buffer& obj)
{
return os << obj.as_string();
}
};
//
template <typename Tp>
size_t
atomic_ring_buffer<Tp>::get_items_per_page()
{
return std::max<size_t>(units::get_page_size() / sizeof(Tp), 1);
}
//
template <typename Tp>
atomic_ring_buffer<Tp>::atomic_ring_buffer(const atomic_ring_buffer<Tp>& rhs)
: base_type{rhs}
{
size_t _n = rhs.count();
char* _end = static_cast<char*>(rhs.m_ptr) + rhs.m_size;
for(size_t i = 0; i < _n; ++i)
{
char* _addr = static_cast<char*>(rhs.read_ptr(m_read_count)) + (i * sizeof(Tp));
if((_addr + sizeof(Tp)) > _end) _addr = static_cast<char*>(rhs.m_ptr);
Tp* _in = static_cast<Tp*>(static_cast<void*>(_addr));
write(_in);
}
}
//
template <typename Tp>
atomic_ring_buffer<Tp>&
atomic_ring_buffer<Tp>::operator=(const atomic_ring_buffer<Tp>& rhs)
{
if(this == &rhs) return *this;
base_type::operator=(rhs);
size_t _n = rhs.count();
char* _end = static_cast<char*>(rhs.m_ptr) + rhs.m_size;
for(size_t i = 0; i < _n; ++i)
{
char* _addr = static_cast<char*>(rhs.read_ptr(m_read_count)) + (i * sizeof(Tp));
if((_addr + sizeof(Tp)) > _end) _addr = static_cast<char*>(rhs.m_ptr);
Tp* _in = static_cast<Tp*>(static_cast<void*>(_addr));
write(_in);
}
return *this;
}
//
} // namespace container
} // namespace common
} // namespace rocprofiler
@@ -0,0 +1,136 @@
// MIT License
//
// Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
//
// 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 <array>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <vector>
namespace rocprofiler
{
namespace common
{
namespace container
{
template <typename Tp>
struct c_array
{
// Construct an array wrapper from a base pointer and array size
c_array(Tp* _base, size_t _size)
: m_base{_base}
, m_size{_size}
{}
~c_array() = default;
c_array(const c_array&) = default;
c_array& operator=(const c_array&) = default;
c_array& operator=(c_array&&) noexcept = default;
// Get the size of the wrapped array
size_t size() const { return m_size; }
// Access an element by index
Tp& operator[](size_t i) { return m_base[i]; }
// Access an element by index
const Tp& operator[](size_t i) const { return m_base[i]; }
// Access an element by index with bounds check
Tp& at(size_t i)
{
if(i < m_size) return m_base[i];
throw std::out_of_range(std::string{typeid(*this).name()} + std::to_string(i) +
" exceeds size " + std::to_string(m_size));
}
// Access an element by index with bounds check
const Tp& at(size_t i) const
{
if(i < m_size) return m_base[i];
throw std::out_of_range(std::string{typeid(*this).name()} + std::to_string(i) +
" exceeds size " + std::to_string(m_size));
}
// Get a slice of this array, from a start index (inclusive) to end index (exclusive)
c_array<Tp> slice(size_t start, size_t end) { return c_array<Tp>(&m_base[start], end - start); }
void pop_front()
{
++m_base;
--m_size;
}
void pop_back() { --m_size; }
operator Tp*() const { return m_base; }
// Iterator class for convenient range-based for loop support
template <typename Up>
struct iterator
{
// Start the iterator at a given pointer
explicit iterator(Tp* p)
: m_ptr{p}
{}
// Advance to the next element
void operator++() { ++m_ptr; }
void operator++(int) { m_ptr++; }
// Get the current element
Up& operator*() const { return *m_ptr; }
// Compare iterators
bool operator==(const iterator& rhs) const { return m_ptr == rhs.m_ptr; }
bool operator!=(const iterator& rhs) const { return m_ptr != rhs.m_ptr; }
private:
Tp* m_ptr = nullptr;
};
// Get an iterator positioned at the beginning of the wrapped array
iterator<Tp> begin() { return iterator<Tp>{m_base}; }
iterator<const Tp> begin() const { return iterator<const Tp>{m_base}; }
// Get an iterator positioned at the end of the wrapped array
iterator<Tp> end() { return iterator<Tp>{&m_base[m_size]}; }
iterator<const Tp> end() const { return iterator<const Tp>{&m_base[m_size]}; }
private:
Tp* m_base = nullptr;
size_t m_size = 0;
};
// Function for automatic template argument deduction
template <typename Tp>
c_array<Tp>
wrap_c_array(Tp* base, size_t size)
{
return c_array<Tp>(base, size);
}
} // namespace container
} // namespace common
} // namespace rocprofiler
@@ -0,0 +1,239 @@
// MIT License
//
// Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
//
// 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 <iterator>
#include <type_traits>
#define ROCPROFILER_IMPORT_TEMPLATE2(template_name)
#define ROCPROFILER_IMPORT_TEMPLATE1(template_name)
// Import a 2-type-argument operator template into boost (if necessary) and
// provide a specialization of 'is_chained_base<>' for it.
#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>> \
{ \
using value = ::rocprofiler::container::true_t; \
};
// Import a 1-type-argument operator template into boost (if necessary) and
// provide a specialization of 'is_chained_base<>' for it.
#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>> \
{ \
using value = ::rocprofiler::container::true_t; \
};
#define ROCPROFILER_OPERATOR_TEMPLATE(template_name) \
template <typename T, \
typename U = T, \
typename B = empty_base<T>, \
typename O = typename is_chained_base<U>::value> \
struct template_name; \
\
template <typename T, typename U, typename B> \
struct template_name<T, U, B, false_t> : template_name##2 < T \
, U \
, B > \
{}; \
\
template <typename T, typename U> \
struct template_name<T, U, empty_base<T>, true_t> : template_name##1 < T \
, U > \
{}; \
\
template <typename T, typename B> \
struct template_name<T, T, B, false_t> : template_name##1 < T \
, B > \
{}; \
\
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; \
}; \
\
ROCPROFILER_OPERATOR_TEMPLATE2(template_name##2) \
ROCPROFILER_OPERATOR_TEMPLATE1(template_name##1)
#define ROCPROFILER_BINARY_OPERATOR_COMMUTATIVE(NAME, OP) \
template <typename T, typename U, typename B = empty_base<T>> \
struct NAME##2 : B{friend T operator OP(T lhs, const U& rhs){return lhs OP## = rhs; \
} \
friend T operator OP(const U& lhs, T rhs) { return rhs OP## = lhs; } \
} \
; \
\
template <typename T, typename B = empty_base<T>> \
struct NAME##1 : B{friend T operator OP(T lhs, const T& rhs){return lhs OP## = rhs; \
} \
} \
;
#define ROCPROFILER_BINARY_OPERATOR_NON_COMMUTATIVE(NAME, OP) \
template <typename T, typename U, typename B = empty_base<T>> \
struct NAME##2 : B{friend T operator OP(T lhs, const U& rhs){return lhs OP## = rhs; \
} \
} \
;
namespace rocprofiler
{
namespace common
{
namespace container
{
struct true_t
{};
struct false_t
{};
template <typename T>
class empty_base
{};
template <typename T>
struct is_chained_base
{
using value = true_t;
};
ROCPROFILER_BINARY_OPERATOR_COMMUTATIVE(addable, +)
ROCPROFILER_BINARY_OPERATOR_NON_COMMUTATIVE(subtractable, -)
ROCPROFILER_OPERATOR_TEMPLATE(addable)
template <typename T, typename B = empty_base<T>>
struct incrementable : B
{
friend T operator++(T& x, int)
{
incrementable_type nrv(x);
++x;
return nrv;
}
private: // The use of this typedef works around a Borland bug
typedef T incrementable_type;
};
template <typename T, typename B = empty_base<T>>
struct decrementable : B
{
friend T operator--(T& x, int)
{
decrementable_type nrv(x);
--x;
return nrv;
}
private: // The use of this typedef works around a Borland bug
typedef T decrementable_type;
};
template <typename T, typename P, typename B = empty_base<T>>
struct dereferenceable : B
{
P operator->() const { return ::std::addressof(*static_cast<const T&>(*this)); }
};
template <typename T, typename I, typename R, typename B = empty_base<T>>
struct indexable : B
{
R operator[](I n) const { return *(static_cast<const T&>(*this) + n); }
};
template <typename T, typename B = empty_base<T>>
struct equality_comparable1 : B
{
friend bool operator!=(const T& x, const T& y) { return !static_cast<bool>(x == y); }
};
template <typename T, typename P, typename B = empty_base<T>>
struct input_iteratable : equality_comparable1<T, incrementable<T, dereferenceable<T, P, B>>>
{};
template <typename T, typename B = empty_base<T>>
struct output_iteratable : incrementable<T, B>
{};
template <typename T, typename P, typename B = empty_base<T>>
struct forward_iteratable : input_iteratable<T, P, B>
{};
template <typename T, typename P, typename B = empty_base<T>>
struct bidirectional_iteratable : forward_iteratable<T, P, decrementable<T, B>>
{};
// template <typename T, typename U, typename B = empty_base<T>>
// struct subtractable2;
template <typename T, typename U, typename B = empty_base<T>>
struct additive2 : addable2<T, U, subtractable2<T, U, B>>
{};
template <typename T, typename B = empty_base<T>>
struct less_than_comparable1 : B
{
friend bool operator>(const T& x, const T& y) { return y < x; }
friend bool operator<=(const T& x, const T& y) { return !static_cast<bool>(y < x); }
friend bool operator>=(const T& x, const T& y) { return !static_cast<bool>(x < y); }
};
// To avoid repeated derivation from equality_comparable,
// which is an indirect base typename of bidirectional_iterable,
// random_access_iteratable must not be derived from totally_ordered1
// but from less_than_comparable1 only. (Helmut Zeisel, 02-Dec-2001)
template <typename T, typename P, typename D, typename R, typename B = empty_base<T>>
struct random_access_iteratable
: bidirectional_iteratable<T, P, less_than_comparable1<T, additive2<T, D, indexable<T, D, R, B>>>>
{};
template <typename CategoryT,
typename Tp,
typename DistanceT = std::ptrdiff_t,
typename PointerT = Tp*,
typename ReferenceT = Tp&>
struct iterator_helper
{
using iterator_category = CategoryT;
using value_type = Tp;
using difference_type = DistanceT;
using pointer = PointerT;
using reference = ReferenceT;
};
template <typename T, typename V, typename D = std::ptrdiff_t, typename P = V*, typename R = V&>
struct random_access_iterator_helper
: random_access_iteratable<T, P, D, R, iterator_helper<std::random_access_iterator_tag, V, D, P, R>>
{
friend D requires_difference_operator(const T& x, const T& y) { return x - y; }
}; // random_access_iterator_helper
} // namespace container
} // namespace common
} // namespace rocprofiler
@@ -0,0 +1,289 @@
// MIT License
//
// Copyright (c) 2020, The Regents of the University of California,
// through Lawrence Berkeley National Laboratory (subject to receipt of any
// required approvals from the U.S. Dept. of Energy). All rights reserved.
//
// 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.
#include "ring_buffer.hpp"
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/mman.h>
namespace rocprofiler
{
namespace common
{
namespace container
{
namespace base
{
ring_buffer::ring_buffer(size_t _size, bool _use_mmap)
{
set_use_mmap(_use_mmap);
init(_size);
}
ring_buffer::~ring_buffer() { destroy(); }
ring_buffer::ring_buffer(const ring_buffer& rhs)
: m_use_mmap{rhs.m_use_mmap}
, m_use_mmap_explicit{rhs.m_use_mmap_explicit}
{
init(rhs.m_size);
}
ring_buffer::ring_buffer(ring_buffer&& rhs) noexcept
: m_init{rhs.m_init}
, m_use_mmap{rhs.m_use_mmap}
, m_use_mmap_explicit{rhs.m_use_mmap_explicit}
, m_ptr{rhs.m_ptr}
, m_size{rhs.m_size}
, m_read_count{rhs.m_read_count}
, m_write_count{rhs.m_write_count}
{
rhs.reset();
}
ring_buffer&
ring_buffer::operator=(const ring_buffer& rhs)
{
if(this == &rhs) return *this;
destroy();
m_use_mmap = rhs.m_use_mmap;
m_use_mmap_explicit = rhs.m_use_mmap_explicit;
init(rhs.m_size);
return *this;
}
ring_buffer&
ring_buffer::operator=(ring_buffer&& rhs) noexcept
{
if(this == &rhs) return *this;
destroy();
m_init = rhs.m_init;
m_use_mmap = rhs.m_use_mmap;
m_use_mmap_explicit = rhs.m_use_mmap_explicit;
m_ptr = rhs.m_ptr;
m_size = rhs.m_size;
m_read_count = rhs.m_read_count;
m_write_count = rhs.m_write_count;
rhs.reset();
return *this;
}
void
ring_buffer::init(size_t _size)
{
if(m_init)
throw std::runtime_error("tim::base::ring_buffer::init(size_t) :: already initialized");
m_init = true;
// Round up to multiple of page size.
_size += units::get_page_size() - ((_size % units::get_page_size() > 0)
? (_size % units::get_page_size())
: units::get_page_size());
if((_size % units::get_page_size()) > 0)
{
std::ostringstream _oss{};
_oss << "Error! size is not a multiple of page size: " << _size << " % "
<< units::get_page_size() << " = " << (_size % units::get_page_size());
throw std::runtime_error(_oss.str());
}
m_size = _size;
m_read_count = 0;
m_write_count = 0;
if(!m_use_mmap_explicit) m_use_mmap = get_env("ROCPROFILER_USE_MMAP", m_use_mmap);
if(!m_use_mmap)
{
m_ptr = malloc(m_size * sizeof(char));
return;
}
// Map twice the buffer size.
if((m_ptr =
mmap(nullptr, m_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0)) ==
MAP_FAILED)
{
destroy();
auto _err = errno;
// TIMEMORY_PRINTF_FATAL(stderr, "Error using mmap: %s\n", strerror(_err));
throw std::runtime_error(strerror(_err));
}
}
void
ring_buffer::destroy()
{
if(m_ptr && m_init)
{
if(!m_use_mmap)
{
::free(m_ptr);
}
else
{
// Unmap the mapped virtual memmory.
auto ret = munmap(m_ptr, m_size);
if(ret != 0) perror("munmap");
}
}
m_init = false;
m_size = 0;
m_read_count = 0;
m_write_count = 0;
m_ptr = nullptr;
}
void
ring_buffer::set_use_mmap(bool _v)
{
if(!m_init)
{
m_use_mmap = _v;
m_use_mmap_explicit = true;
}
else
{
throw std::runtime_error("tim::base::ring_buffer::set_use_mmap(bool) cannot be "
"called after initialization");
}
}
std::string
ring_buffer::as_string() const
{
std::ostringstream ss{};
ss << std::boolalpha << "is_initialized: " << is_initialized() << ", capacity: " << capacity()
<< ", count: " << count() << ", free: " << free() << ", is_empty: " << is_empty()
<< ", is_full: " << is_full() << ", pointer: " << m_ptr << ", read count: " << m_read_count
<< ", write count: " << m_write_count;
return ss.str();
}
//
void*
ring_buffer::request(size_t _length)
{
if(m_ptr == nullptr) return nullptr;
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
if(_length > free())
throw std::runtime_error("heap-buffer-overflow :: ring buffer is full. read data "
"to avoid data corruption");
// if write count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_write_count % m_size);
if(_modulo < _length) m_write_count += _modulo;
// pointer in buffer
void* _out = write_ptr();
// Update write count
m_write_count += _length;
return _out;
}
//
void*
ring_buffer::retrieve(size_t _length)
{
if(m_ptr == nullptr) return nullptr;
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
if(_length > count()) throw std::runtime_error("ring buffer is empty");
// if read count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_read_count % m_size);
if(_modulo < _length) m_read_count += _modulo;
// pointer in buffer
void* _out = read_ptr();
// Update write count
m_read_count += _length;
return _out;
}
//
size_t
ring_buffer::rewind(size_t n) const
{
if(n > m_read_count) n = m_read_count;
m_read_count -= n;
return n;
}
//
void
ring_buffer::reset()
{
m_init = false;
m_ptr = nullptr;
m_size = 0;
m_read_count = 0;
m_write_count = 0;
}
//
void
ring_buffer::save(std::fstream& _fs)
{
_fs.write(reinterpret_cast<char*>(&m_use_mmap), sizeof(m_use_mmap));
_fs.write(reinterpret_cast<char*>(&m_use_mmap_explicit), sizeof(m_use_mmap_explicit));
_fs.write(reinterpret_cast<char*>(&m_size), sizeof(m_size));
_fs.write(reinterpret_cast<char*>(&m_read_count), sizeof(m_read_count));
_fs.write(reinterpret_cast<char*>(&m_write_count), sizeof(m_write_count));
_fs.write(reinterpret_cast<char*>(m_ptr), m_size * sizeof(char));
}
//
void
ring_buffer::load(std::fstream& _fs)
{
destroy();
_fs.read(reinterpret_cast<char*>(&m_use_mmap), sizeof(m_use_mmap));
_fs.read(reinterpret_cast<char*>(&m_use_mmap_explicit), sizeof(m_use_mmap_explicit));
_fs.read(reinterpret_cast<char*>(&m_size), sizeof(m_size));
init(m_size);
if(!m_ptr) m_ptr = malloc(m_size);
_fs.read(reinterpret_cast<char*>(&m_read_count), sizeof(m_read_count));
_fs.read(reinterpret_cast<char*>(&m_write_count), sizeof(m_write_count));
_fs.read(reinterpret_cast<char*>(m_ptr), m_size * sizeof(char));
}
} // namespace base
} // namespace container
} // namespace common
} // namespace rocprofiler
@@ -0,0 +1,495 @@
// MIT License
//
// Copyright (c) 2020, The Regents of the University of California,
// through Lawrence Berkeley National Laboratory (subject to receipt of any
// required approvals from the U.S. Dept. of Energy). All rights reserved.
//
// 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 "lib/common/environment.hpp"
#include "lib/common/units.hpp"
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <utility>
#include <vector>
namespace rocprofiler
{
namespace common
{
namespace container
{
template <typename Tp>
struct ring_buffer;
//
namespace base
{
/// \struct tim::base::ring_buffer
/// \brief Ring buffer implementation, with support for mmap as backend (Linux only).
struct ring_buffer
{
template <typename Tp>
friend struct container::ring_buffer;
ring_buffer() = default;
explicit ring_buffer(bool _use_mmap) { set_use_mmap(_use_mmap); }
explicit ring_buffer(size_t _size) { init(_size); }
ring_buffer(size_t _size, bool _use_mmap);
~ring_buffer();
ring_buffer(const ring_buffer&);
ring_buffer& operator=(const ring_buffer&);
ring_buffer(ring_buffer&&) noexcept;
ring_buffer& operator=(ring_buffer&&) noexcept;
/// Returns whether the buffer has been allocated
bool is_initialized() const { return m_init; }
/// Get the total number of bytes supported
size_t capacity() const { return m_size; }
/// Creates new ring buffer.
void init(size_t size);
/// Destroy ring buffer.
void destroy();
/// Write class-type data to buffer (uses placement new).
template <typename Tp>
std::pair<size_t, Tp*> write(Tp* in, std::enable_if_t<std::is_class<Tp>::value, int> = 0);
/// Write non-class-type data to buffer (uses memcpy).
template <typename Tp>
std::pair<size_t, Tp*> write(Tp* in, std::enable_if_t<!std::is_class<Tp>::value, int> = 0);
/// Request a pointer to an allocation. This is similar to a "write" except the
/// memory is uninitialized. Typically used by allocators. If Tp is a class type,
/// be sure to use a placement new instead of a memcpy.
template <typename Tp>
Tp* request();
/// Request a pointer to an allocation for at least \param n bytes.
void* request(size_t n);
/// Read class-type data from buffer (uses placement new).
template <typename Tp>
std::pair<size_t, Tp*> read(Tp* out, std::enable_if_t<std::is_class<Tp>::value, int> = 0) const;
/// Read non-class-type data from buffer (uses memcpy).
template <typename Tp>
std::pair<size_t, Tp*> read(Tp* out,
std::enable_if_t<!std::is_class<Tp>::value, int> = 0) const;
/// Retrieve a pointer to the head allocation (read).
template <typename Tp>
Tp* retrieve();
/// Retrieve a pointer to the head allocation of at least \param n bytes (read).
void* retrieve(size_t n);
/// Returns number of bytes currently held by the buffer.
size_t count() const { return (m_write_count - m_read_count); }
/// Returns how many bytes are availiable in the buffer.
size_t free() const { return (m_size - count()); }
/// Returns if the buffer is empty.
bool is_empty() const { return (count() == 0); }
/// Returns if the buffer is full.
bool is_full() const { return (count() == m_size); }
/// Rewind the read position n bytes
size_t rewind(size_t n) const;
/// explicitly configure to use mmap if avail
void set_use_mmap(bool);
/// query whether using mmap
bool get_use_mmap() const { return m_use_mmap; }
std::string as_string() const;
void save(std::fstream& _fs);
void load(std::fstream& _fs);
friend std::ostream& operator<<(std::ostream& os, const ring_buffer& obj)
{
return os << obj.as_string();
}
private:
/// Returns the current write pointer.
void* write_ptr() const { return static_cast<char*>(m_ptr) + (m_write_count % m_size); }
/// Returns the current read pointer.
void* read_ptr() const { return static_cast<char*>(m_ptr) + (m_read_count % m_size); }
void reset();
private:
bool m_init = false;
bool m_use_mmap = true;
bool m_use_mmap_explicit = false;
void* m_ptr = nullptr;
size_t m_size = 0;
mutable size_t m_read_count = 0;
size_t m_write_count = 0;
};
//
template <typename Tp>
std::pair<size_t, Tp*>
ring_buffer::write(Tp* in, std::enable_if_t<std::is_class<Tp>::value, int>)
{
if(in == nullptr || m_ptr == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
if(_length > free())
throw std::runtime_error("heap-buffer-overflow :: ring buffer is full. read data "
"to avoid data corruption");
// if write count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_write_count % m_size);
if(_modulo < _length) m_write_count += _modulo;
// pointer in buffer
Tp* out = reinterpret_cast<Tp*>(write_ptr());
// Copy in.
new((void*) out) Tp{std::move(*in)};
// Update write count
m_write_count += _length;
return {_length, out};
}
//
template <typename Tp>
std::pair<size_t, Tp*>
ring_buffer::write(Tp* in, std::enable_if_t<!std::is_class<Tp>::value, int>)
{
if(in == nullptr || m_ptr == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
if(_length > free())
throw std::runtime_error("heap-buffer-overflow :: ring buffer is full. read data "
"to avoid data corruption");
// if write count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_write_count % m_size);
if(_modulo < _length) m_write_count += _modulo;
// pointer in buffer
Tp* out = reinterpret_cast<Tp*>(write_ptr());
// Copy in.
memcpy((void*) out, in, _length);
// Update write count
m_write_count += _length;
return {_length, out};
}
//
template <typename Tp>
Tp*
ring_buffer::request()
{
if(m_ptr == nullptr) return nullptr;
auto _length = sizeof(Tp);
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
if(_length > free())
throw std::runtime_error("heap-buffer-overflow :: ring buffer is full. read data "
"to avoid data corruption");
// if write count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_write_count % m_size);
if(_modulo < _length) m_write_count += _modulo;
// pointer in buffer
Tp* _out = reinterpret_cast<Tp*>(write_ptr());
// Update write count
m_write_count += _length;
return _out;
}
//
template <typename Tp>
std::pair<size_t, Tp*>
ring_buffer::read(Tp* out, std::enable_if_t<std::is_class<Tp>::value, int>) const
{
if(is_empty() || out == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
// Make sure we do not read out more than there is actually in the buffer.
if(_length > count()) throw std::runtime_error("ring buffer is empty");
// if read count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_read_count % m_size);
if(_modulo < _length) m_read_count += _modulo;
// pointer in buffer
Tp* in = reinterpret_cast<Tp*>(read_ptr());
// Copy out for BYTE, nothing magic here.
*out = *in;
// Update read count.
m_read_count += _length;
return {_length, in};
}
//
template <typename Tp>
std::pair<size_t, Tp*>
ring_buffer::read(Tp* out, std::enable_if_t<!std::is_class<Tp>::value, int>) const
{
if(is_empty() || out == nullptr) return {0, nullptr};
auto _length = sizeof(Tp);
using Up = typename std::remove_const<Tp>::type;
// Make sure we do not read out more than there is actually in the buffer.
if(_length > count()) throw std::runtime_error("ring buffer is empty");
// if read count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_read_count % m_size);
if(_modulo < _length) m_read_count += _modulo;
// pointer in buffer
Tp* in = reinterpret_cast<Tp*>(read_ptr());
// Copy out for BYTE, nothing magic here.
Up* _out = const_cast<Up*>(out);
memcpy(_out, in, _length);
// Update read count.
m_read_count += _length;
return {_length, in};
}
//
template <typename Tp>
Tp*
ring_buffer::retrieve()
{
if(m_ptr == nullptr) return nullptr;
auto _length = sizeof(Tp);
// Make sure we don't put in more than there's room for, by writing no
// more than there is free.
if(_length > count()) throw std::runtime_error("ring buffer is empty");
// if read count is at the tail of buffer, bump to the end of buffer
auto _modulo = m_size - (m_read_count % m_size);
if(_modulo < _length) m_read_count += _modulo;
// pointer in buffer
Tp* _out = reinterpret_cast<Tp*>(read_ptr());
// Update write count
m_read_count += _length;
return _out;
}
//
} // namespace base
///
/// \struct rocprofiler::container::ring_buffer
/// \brief Ring buffer wrapper around \ref tim::base::ring_buffer for data of type Tp. If
/// the data object size is larger than the page size (typically 4KB), behavior is
/// undefined. During initialization, one requests a minimum number of objects and the
/// buffer will support that number of object + the remainder of the page, e.g. if a page
/// is 1000 bytes, the object is 1 byte, and the buffer is requested to support 1500
/// objects, then an allocation supporting 2000 objects (i.e. 2 pages) will be created.
template <typename Tp>
struct ring_buffer : private base::ring_buffer
{
using base_type = base::ring_buffer;
static size_t get_items_per_page();
ring_buffer() = default;
~ring_buffer() = default;
explicit ring_buffer(bool _use_mmap)
: base_type{_use_mmap}
{}
explicit ring_buffer(size_t _size)
: base_type{_size * sizeof(Tp)}
{}
ring_buffer(size_t _size, bool _use_mmap)
: base_type{_size * sizeof(Tp), _use_mmap}
{}
ring_buffer(const ring_buffer&);
ring_buffer(ring_buffer&&) noexcept = default;
ring_buffer& operator=(const ring_buffer&);
ring_buffer& operator=(ring_buffer&&) noexcept = default;
/// Returns whether the buffer has been allocated
bool is_initialized() const { return base_type::is_initialized(); }
/// Get the total number of Tp instances supported
size_t capacity() const { return (base_type::capacity()) / sizeof(Tp); }
/// Creates new ring buffer.
void init(size_t _size) { base_type::init(_size * sizeof(Tp)); }
/// Destroy ring buffer.
void destroy() { base_type::destroy(); }
/// Write data to buffer.
size_t data_size() const { return sizeof(Tp); }
/// Write data to buffer. Return pointer to location of write
Tp* write(Tp* in) { return base_type::write<Tp>(in).second; }
/// Read data from buffer. Return pointer to location of read
Tp* read(Tp* out) const { return base_type::read<Tp>(out).second; }
/// Get an uninitialized address at tail of buffer.
Tp* request() { return base_type::request<Tp>(); }
/// Read data from head of buffer.
Tp* retrieve() { return base_type::retrieve<Tp>(); }
/// Returns number of Tp instances currently held by the buffer.
size_t count() const { return (base_type::count()) / sizeof(Tp); }
/// Returns how many Tp instances are availiable in the buffer.
size_t free() const { return (base_type::free()) / sizeof(Tp); }
/// Returns if the buffer is empty.
bool is_empty() const { return base_type::is_empty(); }
/// Returns if the buffer is full.
bool is_full() const { return (base_type::free() < sizeof(Tp)); }
/// Rewinds the read pointer
size_t rewind(size_t n) const { return base_type::rewind(n); }
template <typename... Args>
auto emplace(Args&&... args)
{
Tp _obj{std::forward<Args>(args)...};
return write(&_obj);
}
using base_type::get_use_mmap;
using base_type::load;
using base_type::save;
using base_type::set_use_mmap;
std::string as_string() const
{
std::ostringstream ss{};
size_t _w = std::log10(base_type::capacity()) + 1;
ss << std::boolalpha << std::right << "data size: " << std::setw(_w) << data_size()
<< " B, is_initialized: " << std::setw(5) << is_initialized()
<< ", is_empty: " << std::setw(5) << is_empty() << ", is_full: " << std::setw(5)
<< is_full() << ", capacity: " << std::setw(_w) << capacity()
<< ", count: " << std::setw(_w) << count() << ", free: " << std::setw(_w) << free()
<< ", raw capacity: " << std::setw(_w) << base_type::capacity()
<< " B, raw count: " << std::setw(_w) << base_type::count()
<< " B, raw free: " << std::setw(_w) << base_type::free()
<< " B, pointer: " << std::setw(15) << base_type::m_ptr
<< ", raw read count: " << std::setw(_w) << base_type::m_read_count
<< ", raw write count: " << std::setw(_w) << base_type::m_write_count;
return ss.str();
}
friend std::ostream& operator<<(std::ostream& os, const ring_buffer& obj)
{
return os << obj.as_string();
}
};
//
template <typename Tp>
size_t
ring_buffer<Tp>::get_items_per_page()
{
return std::max<size_t>(units::get_page_size() / sizeof(Tp), 1);
}
//
template <typename Tp>
ring_buffer<Tp>::ring_buffer(const ring_buffer<Tp>& rhs)
: base_type{rhs}
{
size_t _n = rhs.count();
char* _end = static_cast<char*>(rhs.m_ptr) + rhs.m_size;
for(size_t i = 0; i < _n; ++i)
{
char* _addr = static_cast<char*>(rhs.read_ptr()) + (i * sizeof(Tp));
if((_addr + sizeof(Tp)) > _end) _addr = static_cast<char*>(rhs.m_ptr);
Tp* _in = static_cast<Tp*>(static_cast<void*>(_addr));
write(_in);
}
}
//
template <typename Tp>
ring_buffer<Tp>&
ring_buffer<Tp>::operator=(const ring_buffer<Tp>& rhs)
{
if(this == &rhs) return *this;
base_type::operator=(rhs);
size_t _n = rhs.count();
char* _end = static_cast<char*>(rhs.m_ptr) + rhs.m_size;
for(size_t i = 0; i < _n; ++i)
{
char* _addr = static_cast<char*>(rhs.read_ptr()) + (i * sizeof(Tp));
if((_addr + sizeof(Tp)) > _end) _addr = static_cast<char*>(rhs.m_ptr);
Tp* _in = static_cast<Tp*>(static_cast<void*>(_addr));
write(_in);
}
return *this;
}
//
} // namespace container
} // namespace common
} // namespace rocprofiler
@@ -0,0 +1,389 @@
// MIT License
//
// Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
//
// 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 "lib/common/container/operators.hpp"
#include "lib/common/container/static_vector.hpp"
#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <numeric>
#include <type_traits>
#include <vector>
namespace rocprofiler
{
namespace common
{
namespace container
{
template <typename Tp, size_t ChunkSizeV = 64>
class stable_vector
{
public:
using value_type = Tp;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
static constexpr const size_t chunk_size = ChunkSizeV;
private:
template <size_t N>
struct is_pow2
{
static constexpr bool value = (N & (N - 1)) == 0;
};
static_assert(ChunkSizeV > 0, "ChunkSize needs to be greater than zero");
static_assert(is_pow2<ChunkSizeV>::value, "ChunkSize needs to be a power of 2");
using this_type = stable_vector<Tp, ChunkSizeV>;
using const_this_type = const stable_vector<Tp, ChunkSizeV>;
template <typename ContainerT>
struct iterator_base
{
iterator_base(ContainerT* c = nullptr, size_type i = 0)
: m_container(c)
, m_index(i)
{}
iterator_base& operator+=(size_type i)
{
m_index += i;
return *this;
}
iterator_base& operator-=(size_type i)
{
m_index -= i;
return *this;
}
iterator_base& operator++()
{
++m_index;
return *this;
}
iterator_base& operator--()
{
--m_index;
return *this;
}
difference_type operator-(const iterator_base& it)
{
assert(m_container == it.m_container);
return m_index - it.m_index;
}
bool operator<(const iterator_base& it) const
{
assert(m_container == it.m_container);
return m_index < it.m_index;
}
bool operator==(const iterator_base& it) const
{
return m_container == it.m_container && m_index == it.m_index;
}
protected:
ContainerT* m_container;
size_type m_index;
};
public:
struct const_iterator;
struct iterator
: public iterator_base<this_type>
//, std::iterator<std::random_access_iterator_tag, value_type>
, public random_access_iterator_helper<iterator, value_type>
{
using iterator_base<this_type>::iterator_base;
friend struct const_iterator;
reference operator*() { return (*this->m_container)[this->m_index]; }
};
struct const_iterator
: public iterator_base<const_this_type>
//, std::iterator<std::random_access_iterator_tag, const value_type>
, public random_access_iterator_helper<const_iterator, const value_type>
{
using iterator_base<const_this_type>::iterator_base;
explicit const_iterator(const iterator& it)
: iterator_base<const_this_type>(it.m_container, it.m_index)
{}
const_reference operator*() const { return (*this->m_container)[this->m_index]; }
bool operator==(const const_iterator& it) const
{
return iterator_base<const_this_type>::operator==(it);
}
friend bool operator==(const iterator& l, const const_iterator& r) { return r == l; }
};
stable_vector() = default;
explicit stable_vector(size_type count, const Tp& value);
explicit stable_vector(size_type count);
template <typename InputItrT,
typename = std::enable_if_t<
std::is_convertible<typename std::iterator_traits<InputItrT>::iterator_category,
std::input_iterator_tag>::value>>
stable_vector(InputItrT first, InputItrT last);
explicit stable_vector(std::initializer_list<Tp>);
stable_vector(const stable_vector& other);
stable_vector(stable_vector&& other) noexcept;
stable_vector& operator=(stable_vector v);
iterator begin() noexcept { return {this, 0}; }
const_iterator begin() const noexcept { return {this, 0}; }
const_iterator cbegin() const noexcept { return begin(); }
iterator end() noexcept { return {this, size()}; }
const_iterator end() const noexcept { return {this, size()}; }
const_iterator cend() const noexcept { return end(); }
size_type size() const noexcept
{
return empty() ? 0 : (m_chunks.size() - 1) * ChunkSizeV + m_chunks.back()->size();
}
size_type max_size() const noexcept { return std::numeric_limits<size_type>::max(); }
size_type capacity() const noexcept { return m_chunks.size() * ChunkSizeV; }
bool empty() const noexcept { return m_chunks.size() == 0; }
void reserve(size_type new_capacity);
void shrink_to_fit() noexcept {}
bool operator==(const this_type& c) const
{
return size() == c.size() && std::equal(cbegin(), cend(), c.cbegin());
}
bool operator!=(const this_type& c) const { return !operator==(c); }
void swap(this_type& v) { std::swap(m_chunks, v.m_chunks); }
friend void swap(this_type& l, this_type& r) { l.swap(r); }
reference front() { return m_chunks.front()->front(); }
const_reference front() const { return front(); }
reference back() { return m_chunks.back()->back(); }
const_reference back() const { return back(); }
void push_back(const Tp& t);
void push_back(Tp&& t);
template <typename... Args>
void emplace_back(Args&&... args);
reference operator[](size_type i);
const_reference operator[](size_type i) const;
reference at(size_type i);
const_reference at(size_type i) const;
private:
using chunk_type = container::static_vector<Tp, ChunkSizeV, true>;
using storage_type = std::vector<std::unique_ptr<chunk_type>>;
void add_chunk();
chunk_type& last_chunk();
storage_type m_chunks;
};
template <typename Tp, size_t ChunkSizeV>
stable_vector<Tp, ChunkSizeV>::stable_vector(size_type count, const Tp& value)
{
for(size_type i = 0; i < count; ++i)
{
push_back(value);
}
}
template <typename Tp, size_t ChunkSizeV>
stable_vector<Tp, ChunkSizeV>::stable_vector(size_type count)
{
for(size_type i = 0; i < count; ++i)
{
emplace_back();
}
}
template <typename Tp, size_t ChunkSizeV>
template <typename InputItrT, typename>
stable_vector<Tp, ChunkSizeV>::stable_vector(InputItrT first, InputItrT last)
{
for(; first != last; ++first)
{
push_back(*first);
}
}
template <typename Tp, size_t ChunkSizeV>
stable_vector<Tp, ChunkSizeV>::stable_vector(const stable_vector& other)
{
for(const auto& chunk : other.m_chunks)
{
m_chunks.emplace_back(std::make_unique<chunk_type>(*chunk));
}
}
template <typename Tp, size_t ChunkSizeV>
stable_vector<Tp, ChunkSizeV>::stable_vector(stable_vector&& other) noexcept
: m_chunks(std::move(other.m_chunks))
{}
template <typename Tp, size_t ChunkSizeV>
stable_vector<Tp, ChunkSizeV>::stable_vector(std::initializer_list<Tp> ilist)
{
for(const auto& t : ilist)
{
push_back(t);
}
}
template <typename Tp, size_t ChunkSizeV>
stable_vector<Tp, ChunkSizeV>&
stable_vector<Tp, ChunkSizeV>::operator=(stable_vector v)
{
swap(v);
return *this;
}
template <typename Tp, size_t ChunkSizeV>
void
stable_vector<Tp, ChunkSizeV>::add_chunk()
{
m_chunks.emplace_back(std::make_unique<chunk_type>());
}
template <typename Tp, size_t ChunkSizeV>
typename stable_vector<Tp, ChunkSizeV>::chunk_type&
stable_vector<Tp, ChunkSizeV>::last_chunk()
{
if(ROCPROFILER_UNLIKELY(m_chunks.empty() || m_chunks.back()->size() == ChunkSizeV))
{
add_chunk();
}
return *m_chunks.back();
}
template <typename Tp, size_t ChunkSizeV>
void
stable_vector<Tp, ChunkSizeV>::reserve(size_type new_capacity)
{
const size_t initial_capacity = capacity();
for(difference_type i = new_capacity - initial_capacity; i > 0; i -= ChunkSizeV)
{
add_chunk();
}
}
template <typename Tp, size_t ChunkSizeV>
void
stable_vector<Tp, ChunkSizeV>::push_back(const Tp& t)
{
last_chunk().push_back(t);
}
template <typename Tp, size_t ChunkSizeV>
void
stable_vector<Tp, ChunkSizeV>::push_back(Tp&& t)
{
last_chunk().push_back(std::move(t));
}
template <typename Tp, size_t ChunkSizeV>
template <typename... Args>
void
stable_vector<Tp, ChunkSizeV>::emplace_back(Args&&... args)
{
last_chunk().emplace_back(std::forward<Args>(args)...);
}
template <typename Tp, size_t ChunkSizeV>
typename stable_vector<Tp, ChunkSizeV>::reference
stable_vector<Tp, ChunkSizeV>::operator[](size_type i)
{
return (*m_chunks[i / ChunkSizeV])[i % ChunkSizeV];
}
template <typename Tp, size_t ChunkSizeV>
typename stable_vector<Tp, ChunkSizeV>::const_reference
stable_vector<Tp, ChunkSizeV>::operator[](size_type i) const
{
return const_cast<this_type&>(*this)[i];
}
template <typename Tp, size_t ChunkSizeV>
typename stable_vector<Tp, ChunkSizeV>::reference
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()));
}
return operator[](i);
}
template <typename Tp, size_t ChunkSizeV>
typename stable_vector<Tp, ChunkSizeV>::const_reference
stable_vector<Tp, ChunkSizeV>::at(size_type i) const
{
return const_cast<this_type&>(*this).at(i);
}
template <typename Tp, size_t ChunkSizeV, typename... Args>
auto
resize(stable_vector<Tp, ChunkSizeV>& _v, size_t _n, Args&&... args)
{
if(_n > _v.capacity()) _v.reserve(_n);
while(_v.size() < _n)
_v.emplace_back(std::forward<Args>(args)...);
return _v.size();
}
} // namespace container
} // namespace common
} // namespace rocprofiler
@@ -0,0 +1,221 @@
// MIT License
//
// Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
//
// 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 "lib/common/container/c_array.hpp"
#include <array>
#include <atomic>
#include <cstdlib>
#include <initializer_list>
#include <cstddef>
namespace rocprofiler
{
namespace common
{
namespace container
{
template <typename Tp, size_t N, bool AtomicSizeV = false>
struct static_vector
{
using count_type = std::conditional_t<AtomicSizeV, std::atomic<size_t>, size_t>;
using this_type = static_vector<Tp, N>;
using value_type = Tp;
static_vector() = default;
static_vector(const static_vector&) = default;
static_vector(static_vector&&) noexcept = default;
static_vector& operator=(const static_vector&) = default;
static_vector& operator=(static_vector&&) noexcept = default;
explicit static_vector(size_t _n, Tp _v = {});
explicit static_vector(c_array<Tp>&&);
template <size_t M>
explicit static_vector(std::array<Tp, M>&&);
static_vector& operator=(std::initializer_list<Tp>&& _v);
static_vector& operator=(std::pair<std::array<Tp, N>, size_t>&&);
template <typename... Args>
value_type& emplace_back(Args&&... _v);
template <typename Up>
decltype(auto) push_back(Up&& _v)
{
return emplace_back(Tp{std::forward<Up>(_v)});
}
void pop_back() { --m_size; }
void clear();
void reserve(size_t) noexcept {}
void shrink_to_fit() noexcept {}
auto capacity() noexcept { return N; }
size_t size() const { return m_size; }
bool empty() const { return (size() == 0); }
auto begin() { return m_data.begin(); }
auto begin() const { return m_data.begin(); }
auto cbegin() const { return m_data.cbegin(); }
auto end() { return m_data.begin() + size(); }
auto end() const { return m_data.begin() + size(); }
auto cend() const { return m_data.cbegin() + size(); }
decltype(auto) operator[](size_t _idx) { return m_data[_idx]; }
decltype(auto) operator[](size_t _idx) const { return m_data[_idx]; }
decltype(auto) at(size_t _idx) { return m_data.at(_idx); }
decltype(auto) at(size_t _idx) const { return m_data.at(_idx); }
decltype(auto) front() { return m_data.front(); }
decltype(auto) front() const { return m_data.front(); }
decltype(auto) back() { return *(m_data.begin() + size() - 1); }
decltype(auto) back() const { return *(m_data.begin() + size() - 1); }
auto* data() { return m_data.data(); }
const auto* data() const { return m_data.data(); }
void swap(this_type& _v);
friend void swap(this_type& _lhs, this_type& _rhs) { _lhs.swap(_rhs); }
private:
void update_size(size_t);
private:
count_type m_size = count_type{0};
std::array<Tp, N> m_data = {};
};
template <typename Tp, size_t N, bool AtomicSizeV>
static_vector<Tp, N, AtomicSizeV>::static_vector(size_t _n, Tp _v)
{
m_data.fill(_v);
update_size(_n);
}
template <typename Tp, size_t N, bool AtomicSizeV>
static_vector<Tp, N, AtomicSizeV>::static_vector(c_array<Tp>&& _v)
{
auto _n = std::min<size_t>(N, _v.size());
for(size_t i = 0; i < _n; ++i, ++m_size)
m_data[i] = _v[i];
}
template <typename Tp, size_t N, bool AtomicSizeV>
template <size_t M>
static_vector<Tp, N, AtomicSizeV>::static_vector(std::array<Tp, M>&& _v)
{
auto _n = std::min<size_t>(N, M);
for(size_t i = 0; i < _n; ++i, ++m_size)
m_data[i] = _v[i];
}
template <typename Tp, size_t N, bool AtomicSizeV>
static_vector<Tp, N, AtomicSizeV>&
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));
}
clear();
for(auto&& itr : _v)
m_data[m_size++] = itr;
return *this;
}
template <typename Tp, size_t N, bool AtomicSizeV>
static_vector<Tp, N, AtomicSizeV>&
static_vector<Tp, N, AtomicSizeV>::operator=(std::pair<std::array<Tp, N>, size_t>&& _v)
{
update_size(0);
m_data = std::move(_v.first);
update_size(_v.second);
return *this;
}
template <typename Tp, size_t N, bool AtomicSizeV>
void
static_vector<Tp, N, AtomicSizeV>::clear()
{
update_size(0);
}
template <typename Tp, size_t N, bool AtomicSizeV>
void
static_vector<Tp, N, AtomicSizeV>::swap(this_type& _v)
{
if constexpr(AtomicSizeV)
{
auto _t_size = m_size;
auto _v_size = _v.m_size;
std::swap(m_data, _v.m_data);
update_size(_v_size);
_v.update_size(_t_size);
}
else
{
std::swap(m_size, _v.m_size);
std::swap(m_data, _v.m_data);
}
}
template <typename Tp, size_t N, bool AtomicSizeV>
template <typename... Args>
Tp&
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));
}
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)...};
return m_data[_idx];
}
template <typename Tp, size_t N, bool AtomicSizeV>
void
static_vector<Tp, N, AtomicSizeV>::update_size(size_t _n)
{
if constexpr(AtomicSizeV)
m_size.store(_n);
else
m_size = _n;
}
} // namespace container
} // namespace common
} // namespace rocprofiler