diff options
| -rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/common/ring_buffer.h | 111 | ||||
| -rw-r--r-- | src/tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/tests/common/ring_buffer.cpp | 130 |
4 files changed, 243 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index f41946cc6..6a3f1fe08 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -71,6 +71,7 @@ add_library(common STATIC | |||
| 71 | param_package.cpp | 71 | param_package.cpp |
| 72 | param_package.h | 72 | param_package.h |
| 73 | quaternion.h | 73 | quaternion.h |
| 74 | ring_buffer.h | ||
| 74 | scm_rev.cpp | 75 | scm_rev.cpp |
| 75 | scm_rev.h | 76 | scm_rev.h |
| 76 | scope_exit.h | 77 | scope_exit.h |
diff --git a/src/common/ring_buffer.h b/src/common/ring_buffer.h new file mode 100644 index 000000000..30d934a38 --- /dev/null +++ b/src/common/ring_buffer.h | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <array> | ||
| 9 | #include <atomic> | ||
| 10 | #include <cstddef> | ||
| 11 | #include <cstring> | ||
| 12 | #include <type_traits> | ||
| 13 | #include <vector> | ||
| 14 | #include "common/common_types.h" | ||
| 15 | |||
| 16 | namespace Common { | ||
| 17 | |||
| 18 | /// SPSC ring buffer | ||
| 19 | /// @tparam T Element type | ||
| 20 | /// @tparam capacity Number of slots in ring buffer | ||
| 21 | /// @tparam granularity Slot size in terms of number of elements | ||
| 22 | template <typename T, size_t capacity, size_t granularity = 1> | ||
| 23 | class RingBuffer { | ||
| 24 | /// A "slot" is made of `granularity` elements of `T`. | ||
| 25 | static constexpr size_t slot_size = granularity * sizeof(T); | ||
| 26 | // T must be safely memcpy-able and have a trivial default constructor. | ||
| 27 | static_assert(std::is_trivial_v<T>); | ||
| 28 | // Ensure capacity is sensible. | ||
| 29 | static_assert(capacity < std::numeric_limits<size_t>::max() / 2 / granularity); | ||
| 30 | static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two"); | ||
| 31 | // Ensure lock-free. | ||
| 32 | static_assert(std::atomic<size_t>::is_always_lock_free); | ||
| 33 | |||
| 34 | public: | ||
| 35 | /// Pushes slots into the ring buffer | ||
| 36 | /// @param new_slots Pointer to the slots to push | ||
| 37 | /// @param slot_count Number of slots to push | ||
| 38 | /// @returns The number of slots actually pushed | ||
| 39 | size_t Push(const void* new_slots, size_t slot_count) { | ||
| 40 | const size_t write_index = m_write_index.load(); | ||
| 41 | const size_t slots_free = capacity + m_read_index.load() - write_index; | ||
| 42 | const size_t push_count = std::min(slot_count, slots_free); | ||
| 43 | |||
| 44 | const size_t pos = write_index % capacity; | ||
| 45 | const size_t first_copy = std::min(capacity - pos, push_count); | ||
| 46 | const size_t second_copy = push_count - first_copy; | ||
| 47 | |||
| 48 | const char* in = static_cast<const char*>(new_slots); | ||
| 49 | std::memcpy(m_data.data() + pos * granularity, in, first_copy * slot_size); | ||
| 50 | in += first_copy * slot_size; | ||
| 51 | std::memcpy(m_data.data(), in, second_copy * slot_size); | ||
| 52 | |||
| 53 | m_write_index.store(write_index + push_count); | ||
| 54 | |||
| 55 | return push_count; | ||
| 56 | } | ||
| 57 | |||
| 58 | size_t Push(const std::vector<T>& input) { | ||
| 59 | return Push(input.data(), input.size()); | ||
| 60 | } | ||
| 61 | |||
| 62 | /// Pops slots from the ring buffer | ||
| 63 | /// @param output Where to store the popped slots | ||
| 64 | /// @param max_slots Maximum number of slots to pop | ||
| 65 | /// @returns The number of slots actually popped | ||
| 66 | size_t Pop(void* output, size_t max_slots = ~size_t(0)) { | ||
| 67 | const size_t read_index = m_read_index.load(); | ||
| 68 | const size_t slots_filled = m_write_index.load() - read_index; | ||
| 69 | const size_t pop_count = std::min(slots_filled, max_slots); | ||
| 70 | |||
| 71 | const size_t pos = read_index % capacity; | ||
| 72 | const size_t first_copy = std::min(capacity - pos, pop_count); | ||
| 73 | const size_t second_copy = pop_count - first_copy; | ||
| 74 | |||
| 75 | char* out = static_cast<char*>(output); | ||
| 76 | std::memcpy(out, m_data.data() + pos * granularity, first_copy * slot_size); | ||
| 77 | out += first_copy * slot_size; | ||
| 78 | std::memcpy(out, m_data.data(), second_copy * slot_size); | ||
| 79 | |||
| 80 | m_read_index.store(read_index + pop_count); | ||
| 81 | |||
| 82 | return pop_count; | ||
| 83 | } | ||
| 84 | |||
| 85 | std::vector<T> Pop(size_t max_slots = ~size_t(0)) { | ||
| 86 | std::vector<T> out(std::min(max_slots, capacity) * granularity); | ||
| 87 | const size_t count = Pop(out.data(), out.size() / granularity); | ||
| 88 | out.resize(count * granularity); | ||
| 89 | return out; | ||
| 90 | } | ||
| 91 | |||
| 92 | /// @returns Number of slots used | ||
| 93 | size_t Size() const { | ||
| 94 | return m_write_index.load() - m_read_index.load(); | ||
| 95 | } | ||
| 96 | |||
| 97 | /// @returns Maximum size of ring buffer | ||
| 98 | constexpr size_t Capacity() const { | ||
| 99 | return capacity; | ||
| 100 | } | ||
| 101 | |||
| 102 | private: | ||
| 103 | // It is important to align the below variables for performance reasons: | ||
| 104 | // Having them on the same cache-line would result in false-sharing between them. | ||
| 105 | alignas(128) std::atomic<size_t> m_read_index{0}; | ||
| 106 | alignas(128) std::atomic<size_t> m_write_index{0}; | ||
| 107 | |||
| 108 | std::array<T, granularity * capacity> m_data; | ||
| 109 | }; | ||
| 110 | |||
| 111 | } // namespace Common | ||
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 4d74bb395..4e75a72ec 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | add_executable(tests | 1 | add_executable(tests |
| 2 | common/param_package.cpp | 2 | common/param_package.cpp |
| 3 | common/ring_buffer.cpp | ||
| 3 | core/arm/arm_test_common.cpp | 4 | core/arm/arm_test_common.cpp |
| 4 | core/arm/arm_test_common.h | 5 | core/arm/arm_test_common.h |
| 5 | core/core_timing.cpp | 6 | core/core_timing.cpp |
diff --git a/src/tests/common/ring_buffer.cpp b/src/tests/common/ring_buffer.cpp new file mode 100644 index 000000000..f3fe57839 --- /dev/null +++ b/src/tests/common/ring_buffer.cpp | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | // Copyright 2018 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <numeric> | ||
| 9 | #include <thread> | ||
| 10 | #include <vector> | ||
| 11 | #include <catch2/catch.hpp> | ||
| 12 | #include "common/ring_buffer.h" | ||
| 13 | |||
| 14 | namespace Common { | ||
| 15 | |||
| 16 | TEST_CASE("RingBuffer: Basic Tests", "[common]") { | ||
| 17 | RingBuffer<char, 4, 1> buf; | ||
| 18 | |||
| 19 | // Pushing values into a ring buffer with space should succeed. | ||
| 20 | for (size_t i = 0; i < 4; i++) { | ||
| 21 | const char elem = static_cast<char>(i); | ||
| 22 | const size_t count = buf.Push(&elem, 1); | ||
| 23 | REQUIRE(count == 1); | ||
| 24 | } | ||
| 25 | |||
| 26 | REQUIRE(buf.Size() == 4); | ||
| 27 | |||
| 28 | // Pushing values into a full ring buffer should fail. | ||
| 29 | { | ||
| 30 | const char elem = static_cast<char>(42); | ||
| 31 | const size_t count = buf.Push(&elem, 1); | ||
| 32 | REQUIRE(count == 0); | ||
| 33 | } | ||
| 34 | |||
| 35 | REQUIRE(buf.Size() == 4); | ||
| 36 | |||
| 37 | // Popping multiple values from a ring buffer with values should succeed. | ||
| 38 | { | ||
| 39 | const std::vector<char> popped = buf.Pop(2); | ||
| 40 | REQUIRE(popped.size() == 2); | ||
| 41 | REQUIRE(popped[0] == 0); | ||
| 42 | REQUIRE(popped[1] == 1); | ||
| 43 | } | ||
| 44 | |||
| 45 | REQUIRE(buf.Size() == 2); | ||
| 46 | |||
| 47 | // Popping a single value from a ring buffer with values should succeed. | ||
| 48 | { | ||
| 49 | const std::vector<char> popped = buf.Pop(1); | ||
| 50 | REQUIRE(popped.size() == 1); | ||
| 51 | REQUIRE(popped[0] == 2); | ||
| 52 | } | ||
| 53 | |||
| 54 | REQUIRE(buf.Size() == 1); | ||
| 55 | |||
| 56 | // Pushing more values than space available should partially suceed. | ||
| 57 | { | ||
| 58 | std::vector<char> to_push(6); | ||
| 59 | std::iota(to_push.begin(), to_push.end(), 88); | ||
| 60 | const size_t count = buf.Push(to_push); | ||
| 61 | REQUIRE(count == 3); | ||
| 62 | } | ||
| 63 | |||
| 64 | REQUIRE(buf.Size() == 4); | ||
| 65 | |||
| 66 | // Doing an unlimited pop should pop all values. | ||
| 67 | { | ||
| 68 | const std::vector<char> popped = buf.Pop(); | ||
| 69 | REQUIRE(popped.size() == 4); | ||
| 70 | REQUIRE(popped[0] == 3); | ||
| 71 | REQUIRE(popped[1] == 88); | ||
| 72 | REQUIRE(popped[2] == 89); | ||
| 73 | REQUIRE(popped[3] == 90); | ||
| 74 | } | ||
| 75 | |||
| 76 | REQUIRE(buf.Size() == 0); | ||
| 77 | } | ||
| 78 | |||
| 79 | TEST_CASE("RingBuffer: Threaded Test", "[common]") { | ||
| 80 | RingBuffer<char, 4, 2> buf; | ||
| 81 | const char seed = 42; | ||
| 82 | const size_t count = 1000000; | ||
| 83 | size_t full = 0; | ||
| 84 | size_t empty = 0; | ||
| 85 | |||
| 86 | const auto next_value = [](std::array<char, 2>& value) { | ||
| 87 | value[0] += 1; | ||
| 88 | value[1] += 2; | ||
| 89 | }; | ||
| 90 | |||
| 91 | std::thread producer{[&] { | ||
| 92 | std::array<char, 2> value = {seed, seed}; | ||
| 93 | size_t i = 0; | ||
| 94 | while (i < count) { | ||
| 95 | if (const size_t c = buf.Push(&value[0], 1); c > 0) { | ||
| 96 | REQUIRE(c == 1); | ||
| 97 | i++; | ||
| 98 | next_value(value); | ||
| 99 | } else { | ||
| 100 | full++; | ||
| 101 | std::this_thread::yield(); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | }}; | ||
| 105 | |||
| 106 | std::thread consumer{[&] { | ||
| 107 | std::array<char, 2> value = {seed, seed}; | ||
| 108 | size_t i = 0; | ||
| 109 | while (i < count) { | ||
| 110 | if (const std::vector<char> v = buf.Pop(1); v.size() > 0) { | ||
| 111 | REQUIRE(v.size() == 2); | ||
| 112 | REQUIRE(v[0] == value[0]); | ||
| 113 | REQUIRE(v[1] == value[1]); | ||
| 114 | i++; | ||
| 115 | next_value(value); | ||
| 116 | } else { | ||
| 117 | empty++; | ||
| 118 | std::this_thread::yield(); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | }}; | ||
| 122 | |||
| 123 | producer.join(); | ||
| 124 | consumer.join(); | ||
| 125 | |||
| 126 | REQUIRE(buf.Size() == 0); | ||
| 127 | printf("RingBuffer: Threaded Test: full: %zu, empty: %zu\n", full, empty); | ||
| 128 | } | ||
| 129 | |||
| 130 | } // namespace Common | ||