Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Working on trystero

webcpp's rules apply here: read the superproject's
[CODING-GUIDELINES.md](../../CODING-GUIDELINES.md)
(<https://github.com/webcpporg/webcpp/blob/main/CODING-GUIDELINES.md>), whose
chapter 15 gives trystero's design decisions with their reasons, and its
[AGENTS.md](../../AGENTS.md)
(<https://github.com/webcpporg/webcpp/blob/main/AGENTS.md>) first. This file
holds only what is specific to trystero.
Expand Down
2 changes: 2 additions & 0 deletions include/webcpp/trystero/detail/app_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,11 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
};

/** Closes the new connection and returns the live one to `peer`, if any; else adopts it. */
// NOLINTBEGIN(performance-unnecessary-value-param): sinks, moved on
std::shared_ptr<connection> share_or_adopt(std::string_view peer,
std::shared_ptr<backend::peer_connection> pc,
std::shared_ptr<backend::data_channel> channel) {
// NOLINTEND(performance-unnecessary-value-param)
if (auto existing = find(peer)) {
channel->set_handlers({});
pc->set_handlers({});
Expand Down
2 changes: 2 additions & 0 deletions include/webcpp/trystero/room.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,10 @@ class room_impl final : public std::enable_shared_from_this<room_impl>, public r
room_impl& operator=(const room_impl&) = delete;

/** A room of `app`, in its chosen resource, joined: `start` has run. */
// NOLINTBEGIN(performance-unnecessary-value-param): a sink, moved on
static std::shared_ptr<room_impl> make(std::shared_ptr<app_impl> app, room_options options,
std::string_view room_id) {
// NOLINTEND(performance-unnecessary-value-param)
std::pmr::memory_resource* const memory =
options.memory != nullptr ? options.memory : app->memory();
auto impl = std::allocate_shared<room_impl>(std::pmr::polymorphic_allocator<>(memory),
Expand Down
146 changes: 91 additions & 55 deletions test/allocation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <memory_resource>
#include <new>
Expand Down Expand Up @@ -59,6 +60,10 @@ namespace {
// nothing else.
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::size_t global_allocations = 0;
// The sizes of the first global allocations counted, kept without allocating, so that a count
// the test did not expect names what it saw.
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::array<std::size_t, 32> global_sizes{};
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::size_t c_allocations = 0;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Expand Down Expand Up @@ -307,6 +312,9 @@ void release(void* pointer) noexcept {
*/
void* allocate(std::size_t size, std::size_t alignment) {
if (on_counted_thread() && counting) {
if (global_allocations < global_sizes.size()) {
global_sizes.at(global_allocations) = size;
}
++global_allocations;
}
alignment = alignment < sizeof(void*) ? sizeof(void*) : alignment;
Expand All @@ -319,6 +327,26 @@ void* allocate(std::size_t size, std::size_t alignment) {
return pointer;
}

/**
* Writes the size of each global allocation counted to the standard error when they are not
* `expected` in number, and, under MSVC, the iterator debugging level, whose debug runtime gives
* a standard container a proxy it allocates even when empty: a failed count then says what it saw.
*/
void explain_global_allocations(std::size_t expected) {
if (global_allocations == expected) {
return;
}
std::cerr << "global allocations: " << global_allocations << ", where " << expected
<< " were expected; their sizes:";
for (std::size_t i = 0; i < global_allocations && i < global_sizes.size(); ++i) {
std::cerr << ' ' << global_sizes.at(i);
}
#ifdef _ITERATOR_DEBUG_LEVEL
std::cerr << " (_ITERATOR_DEBUG_LEVEL " << _ITERATOR_DEBUG_LEVEL << ')';
#endif
std::cerr << '\n';
}

/**
* A resource that counts what it gives, from a buffer made before the exchange, and what is
* given back: how many bytes are still out, and how many returns name memory it never gave,
Expand Down Expand Up @@ -399,6 +427,7 @@ void the_counts_see_an_allocation() {
void* pointer = ::operator new(40);
counting = false;
::operator delete(pointer);
explain_global_allocations(1U);
BOOST_TEST_EQ(global_allocations, 1U);
#ifdef WEBCPP_TEST_TRYSTERO_COUNTS_C_ALLOCATIONS
// The C library's allocation, on purpose.
Expand Down Expand Up @@ -486,6 +515,7 @@ void exchange_allocates_through_the_rooms_resources(Make make) {
const bool arrived = run_until(io, [&] { return received == 2; });
counting = false;
BOOST_TEST(arrived);
explain_global_allocations(0U);
BOOST_TEST_EQ(global_allocations, 0U);
BOOST_TEST_EQ(c_allocations, 0U);
BOOST_TEST_GT(a_memory.allocations + z_memory.allocations, before);
Expand Down Expand Up @@ -641,6 +671,7 @@ void a_room_allocates_through_its_resource_from_join_to_destruction() {
BOOST_TEST(farewell);
BOOST_TEST_GT(a_lines, 0U);
BOOST_TEST_GT(z_lines, 0U);
explain_global_allocations(2U);
BOOST_TEST_EQ(global_allocations, 2U);
BOOST_TEST_EQ(c_allocations, 0U);
BOOST_TEST_GT(network_memory.allocations, 0U);
Expand All @@ -663,66 +694,71 @@ void rooms_of_one_app_allocate_through_their_resources() {
counting_resource z_memory;
counting_resource z_room_memory;
std::size_t lines = 0;
// Construct configurations in their resources before the counting window.
auto a_config = app_config_in(a_memory, "AAAAAAAAAAAAAAAAAAAA", "wss://relay.test", lines);
auto z_config = app_config_in(z_memory, "zzzzzzzzzzzzzzzzzzzz", "wss://relay.test", lines);
const auto a_own = options_in(a_room_memory);
const auto z_own = options_in(z_room_memory);
// An inline payload of a size no other allocation has: its frame is at most 256 bytes longer.
constexpr std::size_t payload_size = 12'345;
trystero::pmr_bytes to_shared(payload_size, std::uint8_t{1}, &a_memory);
trystero::pmr_bytes to_own(payload_size, std::uint8_t{2}, &a_room_memory);
{
boost::asio::io_context io; // after the resources: destroyed before them
trystero::test::memory_network network{io, 1, &network_memory};
network.relay("wss://relay.test");
warm(io);
counting = true;
// Construct configurations in their resources before the counting window. They, and the
// payloads, are destroyed before the resources are checked: a moved-from object may still
// hold memory of its resource (MSVC's debug containers keep an iterator proxy).
auto a_config = app_config_in(a_memory, "AAAAAAAAAAAAAAAAAAAA", "wss://relay.test", lines);
auto z_config = app_config_in(z_memory, "zzzzzzzzzzzzzzzzzzzz", "wss://relay.test", lines);
const auto a_own = options_in(a_room_memory);
const auto z_own = options_in(z_room_memory);
// An inline payload of a size no other allocation has: its frame is at most 256 bytes
// longer.
constexpr std::size_t payload_size = 12'345;
const trystero::pmr_bytes to_shared(payload_size, std::uint8_t{1}, &a_memory);
const trystero::pmr_bytes to_own(payload_size, std::uint8_t{2}, &a_room_memory);
{
trystero::app a(io.get_executor(), network, std::move(a_config));
trystero::app z(io.get_executor(), network, std::move(z_config));
auto a1 = join(a, "shared", options_in(a_memory));
auto a2 = join(a, "own", a_own);
auto z1 = join(z, "shared", options_in(z_memory));
auto z2 = join(z, "own", z_own);
std::size_t got = 0;
auto in_shared = z1.make_action<trystero::pmr_bytes>("b");
auto in_own = z2.make_action<trystero::pmr_bytes>("b");
in_shared.on_message(
[&](const trystero::pmr_bytes& data, const trystero::message_info&) {
got += data.get_allocator().resource() == &z_memory ? 1 : 0;
});
in_own.on_message([&](const trystero::pmr_bytes& data, const trystero::message_info&) {
got += data.get_allocator().resource() == &z_room_memory ? 1 : 0;
});
BOOST_TEST(run_until(io, [&] {
return a1.peers().size() == 1 && a2.peers().size() == 1 && z1.peers().size() == 1 &&
z2.peers().size() == 1;
}));
for (auto* m : {&z_memory, &z_room_memory}) {
m->sized_from = payload_size;
m->sized_to = payload_size + 256;
boost::asio::io_context io; // after the resources: destroyed before them
trystero::test::memory_network network{io, 1, &network_memory};
network.relay("wss://relay.test");
warm(io);
counting = true;
{
trystero::app a(io.get_executor(), network, std::move(a_config));
trystero::app z(io.get_executor(), network, std::move(z_config));
auto a1 = join(a, "shared", options_in(a_memory));
auto a2 = join(a, "own", a_own);
auto z1 = join(z, "shared", options_in(z_memory));
auto z2 = join(z, "own", z_own);
std::size_t got = 0;
auto in_shared = z1.make_action<trystero::pmr_bytes>("b");
auto in_own = z2.make_action<trystero::pmr_bytes>("b");
in_shared.on_message(
[&](const trystero::pmr_bytes& data, const trystero::message_info&) {
got += data.get_allocator().resource() == &z_memory ? 1 : 0;
});
in_own.on_message(
[&](const trystero::pmr_bytes& data, const trystero::message_info&) {
got += data.get_allocator().resource() == &z_room_memory ? 1 : 0;
});
BOOST_TEST(run_until(io, [&] {
return a1.peers().size() == 1 && a2.peers().size() == 1 &&
z1.peers().size() == 1 && z2.peers().size() == 1;
}));
for (auto* m : {&z_memory, &z_room_memory}) {
m->sized_from = payload_size;
m->sized_to = payload_size + 256;
}
a2.make_action<trystero::pmr_bytes>("b").async_send(
trystero::pmr_bytes(to_own, &a_room_memory),
boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_room_memory),
[](boost::system::error_code) {}));
BOOST_TEST(run_until(io, [&] { return got == 1; }));
BOOST_TEST_EQ(z_room_memory.sized, 1U); // copied once, out of the frame
BOOST_TEST_EQ(z_memory.sized, 1U); // the received frame, and nothing more
z_memory.sized = 0;
a1.make_action<trystero::pmr_bytes>("b").async_send(
trystero::pmr_bytes(to_shared, &a_memory),
boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_memory),
[](boost::system::error_code) {}));
BOOST_TEST(run_until(io, [&] { return got == 2; }));
BOOST_TEST_EQ(z_memory.sized, 2U); // the frame, the payload taken out and moved on
}
a2.make_action<trystero::pmr_bytes>("b").async_send(
trystero::pmr_bytes(to_own, &a_room_memory),
boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_room_memory),
[](boost::system::error_code) {}));
BOOST_TEST(run_until(io, [&] { return got == 1; }));
BOOST_TEST_EQ(z_room_memory.sized, 1U); // copied once, out of the frame
BOOST_TEST_EQ(z_memory.sized, 1U); // the received frame, and nothing more
z_memory.sized = 0;
a1.make_action<trystero::pmr_bytes>("b").async_send(
trystero::pmr_bytes(to_shared, &a_memory),
boost::asio::bind_allocator(std::pmr::polymorphic_allocator<>(&a_memory),
[](boost::system::error_code) {}));
BOOST_TEST(run_until(io, [&] { return got == 2; }));
BOOST_TEST_EQ(z_memory.sized, 2U); // the frame, the payload taken out and moved on
run_for(io, 300); // the io_context runs out of the rooms' work
}
run_for(io, 300); // the io_context runs out of the rooms' work
counting = false;
}
counting = false;
trystero::pmr_bytes(&a_memory).swap(to_shared);
trystero::pmr_bytes(&a_room_memory).swap(to_own);
explain_global_allocations(4U);
BOOST_TEST_EQ(global_allocations, 4U); // the four rooms' JSON storage counters
BOOST_TEST_EQ(c_allocations, 0U);
for (const auto* m : {&a_memory, &a_room_memory, &z_memory, &z_room_memory}) {
Expand Down
39 changes: 36 additions & 3 deletions test/support/memory_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,43 @@ class memory_peer_connection;
*/
using connection_registry = std::pmr::map<std::size_t, memory_peer_connection*>;

/**
* An allocator that takes a container's memory from a resource and constructs its elements as
* std::allocator does: an element keeps the allocator it was made with, where a pmr container would
* copy it into its own resource. MSVC's containers allocate even when empty (a list's sentinel
* node, and, in a debug build, every container's iterator proxy), which this keeps in the resource.
*/
template <class T>
struct node_allocator {
using value_type = T;

explicit node_allocator(std::pmr::memory_resource* memory) noexcept : memory(memory) {}

// Converting, as std::allocator's is: a container rebinds it to its nodes.
template <class U>
node_allocator(const node_allocator<U>& other) noexcept // NOLINT(misc-explicit-constructor)
: memory(other.memory) {}

T* allocate(std::size_t n) {
return static_cast<T*>(memory->allocate(n * sizeof(T), alignof(T)));
}

void deallocate(T* p, std::size_t n) noexcept {
memory->deallocate(p, n * sizeof(T), alignof(T));
}

friend bool operator==(const node_allocator& a, const node_allocator& b) noexcept {
return a.memory == b.memory;
}

std::pmr::memory_resource* memory;
};

class memory_data_channel : public backend::data_channel,
public std::enable_shared_from_this<memory_data_channel> {
public:
memory_data_channel(boost::asio::io_context& io, std::pmr::memory_resource* memory)
: io_(io), memory_(memory) {}
: io_(io), memory_(memory), queue_(node_allocator<backend::bytes>(memory)) {}

void set_handlers(handlers h) override { h_ = std::move(h); }

Expand Down Expand Up @@ -459,8 +491,9 @@ class memory_data_channel : public backend::data_channel,
// Alive exactly while the network is: a channel that outlives it sends nothing.
std::weak_ptr<connection_registry> registry_;
std::size_t pc_ = 0;
// The frames sent and not yet received, and how much of the first one has drained.
std::list<backend::bytes> queue_;
// The frames sent and not yet received, each in the resource it was made in, and how much of
// the first one has drained.
std::list<backend::bytes, node_allocator<backend::bytes>> queue_;
std::size_t head_drained_ = 0;
std::size_t buffered_ = 0;
std::size_t drain_per_post_ = 1;
Expand Down