Skip to content

Commit 455f959

Browse files
committed
Support both approaches
1 parent 046fb19 commit 455f959

8 files changed

Lines changed: 285 additions & 104 deletions

cpp/include/raft/core/memory_tracking_resources.hpp

Lines changed: 244 additions & 72 deletions
Large diffs are not rendered by default.

cpp/include/raft/mr/host_memory_resource.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION.
2+
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
#pragma once
@@ -69,7 +69,8 @@ RAFT_EXPORT inline auto get_default_host_resource() -> raft::mr::host_resource_r
6969
* @param res The resource to install.
7070
* @return The previous default host resource.
7171
*/
72-
RAFT_EXPORT inline auto set_default_host_resource(raft::mr::host_resource res) -> raft::mr::host_resource
72+
RAFT_EXPORT inline auto set_default_host_resource(raft::mr::host_resource res)
73+
-> raft::mr::host_resource
7374
{
7475
return detail::default_host_resource_holder_.set(res);
7576
}

cpp/include/raft/mr/notifying_adaptor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class notifier {
6767
* or deallocation.
6868
*
6969
* Forwards all calls to the upstream resource, then calls notifier::notify().
70-
* A separate consumer (e.g. resource_monitor) can call notifier::wait() to
70+
* A separate consumer (e.g. sampling_monitor) can call notifier::wait() to
7171
* block until activity occurs.
7272
*
7373
* @tparam Upstream Stored by value. Use a concrete resource type for owning

cpp/include/raft/mr/recording_adaptor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <raft/core/detail/macros.hpp>
88
#include <raft/core/detail/nvtx_range_stack.hpp> // thread_local_current_range
9-
#include <raft/mr/allocation_event_monitor.hpp> // allocation_event, allocation_event_queue
9+
#include <raft/mr/recording_monitor.hpp> // allocation_event, allocation_event_queue
1010
#include <raft/mr/statistics_adaptor.hpp> // resource_stats (atomic counters, reused)
1111

1212
#include <cuda/memory_resource>

cpp/include/raft/mr/allocation_event_monitor.hpp renamed to cpp/include/raft/mr/recording_monitor.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ class allocation_event_queue {
8989
* @brief Consumes allocation_events from a queue and writes one CSV row per
9090
* event from a background thread.
9191
*/
92-
class allocation_event_monitor {
92+
class recording_monitor {
9393
public:
94-
explicit allocation_event_monitor(std::ostream& out) : out_(out) {}
94+
explicit recording_monitor(std::ostream& out) : out_(out) {}
9595

96-
~allocation_event_monitor() { stop(); }
96+
~recording_monitor() { stop(); }
9797

98-
allocation_event_monitor(allocation_event_monitor const&) = delete;
99-
allocation_event_monitor& operator=(allocation_event_monitor const&) = delete;
98+
recording_monitor(recording_monitor const&) = delete;
99+
recording_monitor& operator=(recording_monitor const&) = delete;
100100

101101
[[nodiscard]] auto get_queue() const noexcept -> std::shared_ptr<allocation_event_queue>
102102
{
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace mr {
3939
*
4040
* start() and stop() are idempotent.
4141
*/
42-
class resource_monitor {
42+
class sampling_monitor {
4343
std::ostream& out_;
4444
std::chrono::steady_clock::duration sample_interval_;
4545
std::shared_ptr<notifier> notifier_;
@@ -55,18 +55,18 @@ class resource_monitor {
5555
* @param out Output stream for CSV rows.
5656
* @param sample_interval Minimum time between successive samples.
5757
*/
58-
explicit resource_monitor(std::ostream& out, std::chrono::steady_clock::duration sample_interval)
58+
explicit sampling_monitor(std::ostream& out, std::chrono::steady_clock::duration sample_interval)
5959
: out_(out),
6060
sample_interval_(sample_interval),
6161
notifier_(std::make_shared<notifier>()),
6262
nvtx_range_(raft::common::nvtx::thread_local_current_range())
6363
{
6464
}
6565

66-
~resource_monitor() { stop(); }
66+
~sampling_monitor() { stop(); }
6767

68-
resource_monitor(resource_monitor const&) = delete;
69-
resource_monitor& operator=(resource_monitor const&) = delete;
68+
sampling_monitor(sampling_monitor const&) = delete;
69+
sampling_monitor& operator=(sampling_monitor const&) = delete;
7070

7171
/**
7272
* @brief Shared notifier for notifying_adaptor instances.

cpp/tests/core/allocation_tracking.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <raft/mr/host_memory_resource.hpp>
77
#include <raft/mr/notifying_adaptor.hpp>
8-
#include <raft/mr/resource_monitor.hpp>
8+
#include <raft/mr/sampling_monitor.hpp>
99
#include <raft/mr/statistics_adaptor.hpp>
1010

1111
#include <gtest/gtest.h>
@@ -69,7 +69,7 @@ TEST(AllocationReport, WritesCSVOnDirty)
6969
using namespace std::chrono_literals;
7070

7171
std::ostringstream oss;
72-
raft::mr::resource_monitor report(oss, 1ms);
72+
raft::mr::sampling_monitor report(oss, 1ms);
7373

7474
auto host_stats = std::make_shared<raft::mr::resource_stats>();
7575
auto pinned_stats = std::make_shared<raft::mr::resource_stats>();
@@ -98,7 +98,7 @@ TEST(AllocationReport, StartStopIdempotent)
9898
using namespace std::chrono_literals;
9999

100100
std::ostringstream oss;
101-
raft::mr::resource_monitor report(oss, 1ms);
101+
raft::mr::sampling_monitor report(oss, 1ms);
102102

103103
auto stats = std::make_shared<raft::mr::resource_stats>();
104104
report.register_source("test", stats);
@@ -124,7 +124,7 @@ TEST(AllocationReport, DestructorCallsStop)
124124
std::ostringstream oss;
125125
{
126126
auto stats = std::make_shared<raft::mr::resource_stats>();
127-
raft::mr::resource_monitor report(oss, 1ms);
127+
raft::mr::sampling_monitor report(oss, 1ms);
128128
report.register_source("test", stats);
129129

130130
stats->record_allocate(256);

cpp/tests/core/monitor_resources.cu

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ namespace nvtx = raft::common::nvtx;
2828
using namespace std::chrono_literals;
2929
constexpr std::size_t MiB = std::size_t{1024} * 1024;
3030

31-
TEST(MemoryTrackingResources, TracksDeviceAllocations)
31+
// TODO improve tests (coverage + multiple allocating threads)
32+
33+
TEST(MemoryTrackingResources, Sampling)
3234
{
3335
std::ostringstream oss;
3436
{
@@ -58,34 +60,40 @@ TEST(MemoryTrackingResources, TracksDeviceAllocations)
5860
<< output;
5961
}
6062

61-
TEST(MemoryTrackingResources, MismatchedRangeLabeling)
63+
TEST(MemoryTrackingResources, Recording)
6264
{
63-
const std::string csv_path = "mismatch_range_label.csv";
64-
65+
std::ostringstream oss;
6566
{
6667
raft::resources res;
67-
68-
raft::memory_tracking_resources tracked(res, csv_path, 1ms);
68+
raft::memory_tracking_resources tracked(res, oss);
6969
{
7070
nvtx::range r{"1. expect 10 KB"};
7171
auto matrix = raft::make_host_vector<uint8_t>(tracked, 10 * 1024);
7272
}
7373
{
74-
// Deliberately huge & slow: allocating/freeing 10 GiB of host memory takes
75-
// several ms, which makes the background sampler lag past this range's end.
76-
// As a result this allocation's peak is mis-attributed to the NEXT range in
77-
// the CSV (the range-labeling race discussed in the file header). Source
78-
// attribution (host) stays correct; only the nvtx_range label is wrong.
79-
nvtx::range r{"2. expect 10 GiB"};
80-
auto vector = raft::make_host_vector<uint8_t>(tracked, 10 * 1024 * MiB);
74+
// Deliberately large allocation to test that the memory tracking
75+
// resources can handle a large allocation and labels it correctly
76+
nvtx::range r{"2. expect 100 MiB"};
77+
auto vector = raft::make_host_vector<uint8_t>(tracked, 100 * MiB);
8178
}
8279
{
8380
nvtx::range r{"3. expect 4 MiB"};
8481
auto matrix = raft::make_host_vector<uint8_t>(tracked, 4 * MiB);
8582
}
8683
} // tracked destroyed here: stops the sampler and flushes the file
8784

88-
std::cout << "Wrote allocation statistics to " << csv_path << "\n";
85+
auto output = oss.str();
86+
auto num_lines = std::count(output.begin(), output.end(), '\n');
87+
constexpr size_t NUM_ALLOCS = 3;
88+
constexpr size_t NUM_DEALLOCS = NUM_ALLOCS;
89+
constexpr size_t NUM_HEADER_LINES = 1;
90+
constexpr size_t NUM_LINES_EXPECTED = NUM_ALLOCS + NUM_DEALLOCS + NUM_HEADER_LINES;
91+
EXPECT_GE(num_lines, NUM_LINES_EXPECTED)
92+
<< "Expected at least " << NUM_LINES_EXPECTED
93+
<< " data records (allocation + deallocation + header); got " << num_lines << " lines"
94+
<< std::endl
95+
<< "content: " << std::endl
96+
<< output;
8997
}
9098

9199
} // namespace

0 commit comments

Comments
 (0)