Skip to content

Commit 5903c8a

Browse files
committed
Unify PMR allocation counting with global operator new tracking
1 parent 6e1cb8e commit 5903c8a

3 files changed

Lines changed: 53 additions & 12 deletions

File tree

bench/beman/allocation_tracker.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,42 @@
1313
#include <atomic>
1414
#include <cstddef>
1515
#include <cstdlib>
16+
#include <memory_resource>
1617
#include <new>
1718

1819
static std::atomic<int64_t> g_alloc_count{0};
1920

21+
/// Counts every allocate call, then delegates to upstream.
22+
class counting_memory_resource
23+
: public std::pmr::memory_resource
24+
{
25+
std::pmr::memory_resource* upstream_;
26+
27+
void* do_allocate(
28+
std::size_t n, std::size_t align) override
29+
{
30+
g_alloc_count.fetch_add(1, std::memory_order_relaxed);
31+
return upstream_->allocate(n, align);
32+
}
33+
34+
void do_deallocate(
35+
void* p, std::size_t n, std::size_t align) override
36+
{
37+
upstream_->deallocate(p, n, align);
38+
}
39+
40+
bool do_is_equal(
41+
memory_resource const& other) const noexcept override
42+
{
43+
return this == &other;
44+
}
45+
46+
public:
47+
explicit counting_memory_resource(
48+
std::pmr::memory_resource* upstream) noexcept
49+
: upstream_(upstream) {}
50+
};
51+
2052
void* operator new(std::size_t n)
2153
{
2254
g_alloc_count.fetch_add(1, std::memory_order_relaxed);

bench/beman/main.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
namespace bex = beman::execution;
4848
namespace capy = boost::capy;
4949

50+
static counting_memory_resource g_counting_resource{
51+
capy::get_recycling_memory_resource()};
52+
53+
auto get_counting_resource() -> std::pmr::memory_resource*
54+
{
55+
return &g_counting_resource;
56+
}
57+
5058
// ===================================================================
5159
// result collection
5260
// ===================================================================
@@ -522,7 +530,7 @@ int main()
522530
sender_thread_pool pool(1);
523531
sndr_read_stream stream{&pool};
524532
auto sched = pool.get_scheduler();
525-
auto* mr = capy::get_recycling_memory_resource();
533+
auto* mr = get_counting_resource();
526534
bex::sync_wait(bex::starts_on(sched,
527535
bex_accept(
528536
stream, grid[run][BEMAN_TASK][NATIVE_STREAM][NATIVE_EXEC_MODEL],
@@ -536,7 +544,7 @@ int main()
536544
sender_thread_pool pool(1);
537545
sndr_io_read_stream_impl stream{&pool};
538546
auto sched = pool.get_scheduler();
539-
auto* mr = capy::get_recycling_memory_resource();
547+
auto* mr = get_counting_resource();
540548
bex::sync_wait(bex::starts_on(sched,
541549
bex_accept(
542550
static_cast<sndr_io_read_stream&>(stream),
@@ -551,7 +559,7 @@ int main()
551559
sender_thread_pool pool(1);
552560
sndr_any_read_stream stream(sndr_read_stream{&pool});
553561
auto sched = pool.get_scheduler();
554-
auto* mr = capy::get_recycling_memory_resource();
562+
auto* mr = get_counting_resource();
555563
bex::sync_wait(bex::starts_on(sched,
556564
bex_accept(
557565
stream, grid[run][BEMAN_TASK][TYPE_ERASED_STREAM][NATIVE_EXEC_MODEL],
@@ -565,7 +573,7 @@ int main()
565573
sender_thread_pool pool(1);
566574
sndr_sync_read_stream stream;
567575
auto sched = pool.get_scheduler();
568-
auto* mr = capy::get_recycling_memory_resource();
576+
auto* mr = get_counting_resource();
569577
bex::sync_wait(bex::starts_on(sched,
570578
bex_accept(
571579
stream, grid[run][BEMAN_TASK][SYNC_STREAM][NATIVE_EXEC_MODEL],
@@ -581,7 +589,7 @@ int main()
581589
sender_thread_pool pool(1);
582590
ioaw_read_stream stream;
583591
auto sched = pool.get_scheduler();
584-
auto* mr = capy::get_recycling_memory_resource();
592+
auto* mr = get_counting_resource();
585593
bex::sync_wait(bex::starts_on(sched,
586594
bex_accept_ioaw(
587595
stream, grid[run][BEMAN_TASK][NATIVE_STREAM][BRIDGED_EXEC_MODEL],
@@ -595,7 +603,7 @@ int main()
595603
sender_thread_pool pool(1);
596604
ioaw_io_read_stream_impl stream;
597605
auto sched = pool.get_scheduler();
598-
auto* mr = capy::get_recycling_memory_resource();
606+
auto* mr = get_counting_resource();
599607
bex::sync_wait(bex::starts_on(sched,
600608
bex_accept_ioaw(
601609
static_cast<ioaw_io_read_stream&>(stream),
@@ -611,7 +619,7 @@ int main()
611619
ioaw_read_stream concrete;
612620
capy::any_read_stream stream(&concrete);
613621
auto sched = pool.get_scheduler();
614-
auto* mr = capy::get_recycling_memory_resource();
622+
auto* mr = get_counting_resource();
615623
bex::sync_wait(bex::starts_on(sched,
616624
bex_accept_ioaw(
617625
stream, grid[run][BEMAN_TASK][TYPE_ERASED_STREAM][BRIDGED_EXEC_MODEL],
@@ -625,7 +633,7 @@ int main()
625633
sender_thread_pool pool(1);
626634
ioaw_sync_read_stream stream;
627635
auto sched = pool.get_scheduler();
628-
auto* mr = capy::get_recycling_memory_resource();
636+
auto* mr = get_counting_resource();
629637
bex::sync_wait(bex::starts_on(sched,
630638
bex_accept_ioaw(
631639
stream, grid[run][BEMAN_TASK][SYNC_STREAM][BRIDGED_EXEC_MODEL],

bench/beman/sndr_any_read_sender.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
#ifndef BOOST_CAPY_BENCH_SNDR_ANY_READ_SENDER_HPP
1919
#define BOOST_CAPY_BENCH_SNDR_ANY_READ_SENDER_HPP
2020

21-
#include <boost/capy/ex/recycling_memory_resource.hpp>
22-
2321
#include <beman/execution/execution.hpp>
2422

2523
#include <coroutine>
2624
#include <cstddef>
2725
#include <cstring>
2826
#include <memory>
27+
#include <memory_resource>
2928
#include <utility>
3029

30+
auto get_counting_resource() -> std::pmr::memory_resource*;
31+
3132
namespace ex = beman::execution;
3233

3334
class sndr_any_read_sender
@@ -37,13 +38,13 @@ class sndr_any_read_sender
3738
{
3839
static void* operator new(std::size_t n)
3940
{
40-
return boost::capy::get_recycling_memory_resource()
41+
return get_counting_resource()
4142
->allocate(n, alignof(std::max_align_t));
4243
}
4344

4445
static void operator delete(void* p, std::size_t n) noexcept
4546
{
46-
boost::capy::get_recycling_memory_resource()
47+
get_counting_resource()
4748
->deallocate(p, n, alignof(std::max_align_t));
4849
}
4950

0 commit comments

Comments
 (0)