Skip to content

Commit 0651f45

Browse files
committed
test(cuda.core): count live owned pools and VA reservations in C++
Move the init_cuda teardown gc.collect() gate's instrumentation out of a Python MemoryResource object counter and into the C++ RAII layer, split into two process-wide atomic counts: owned memory pools (cuMemPoolCreate or imported, not borrowed references) and VA reservations (cuMemAddressReserve). These are the resources issue #2381 is about; borrowed pool references (the device default pool) and MemoryResource subclasses that hold no pool or reservation are no longer counted. - Add std::atomic counts incremented at the owning RAII create sites (wrap_mempool_owned in memory.cpp, create_va_reservation_handle in virtual_memory.cpp) and decremented in their deleters, with live_owned_mempool_count / live_va_reservation_count accessors declared in api.hpp and bridged through _rt.pyx / _rt.pxd. - _buffer.pyx: drop _mr_live_count and MemoryResource.__cinit__/__dealloc__; expose _live_owned_mempool_count / _live_va_reservation_count. - conftest.py: snapshot a (owned_pools, va_reservations) tuple; collect when either component differs; warn per-component. Drop the device-default bookkeeping, since the default pool is a borrowed reference and is no longer counted. - Rewrite the counter tests for the two counters (owned pool, borrowed pool not counted, cycle, VA reservation, concurrency); the gate tests script _live_resource_counts for deterministic skip/collect/warning coverage.
1 parent b102d26 commit 0651f45

11 files changed

Lines changed: 265 additions & 203 deletions

File tree

‎cuda_core/cuda/core/_cpp/rt/api.hpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ VaReservationHandle create_va_reservation_handle(size_t size, size_t alignment,
270270
// Size of the reservation.
271271
size_t va_reservation_size(const VaReservationHandle& h) noexcept;
272272

273+
// Test-suite instrumentation: process-wide counts of live *owned* driver
274+
// resources. Owned memory pools (cuMemPoolCreate or imported) and VA
275+
// reservations (cuMemAddressReserve) are each counted at their RAII create
276+
// site and uncounted in their deleter. Borrowed pool references and
277+
// MemoryResource subclasses that hold no pool or reservation are not counted.
278+
// conftest.py reads these to gate the per-test gc.collect().
279+
long long live_owned_mempool_count() noexcept;
280+
long long live_va_reservation_count() noexcept;
281+
273282
// Map the whole allocation at ptr inside the reservation via cuMemMap and
274283
// apply the allocation's access descriptors. The mapping structurally depends
275284
// on both handles. When the last reference is released, cuMemUnmap is called

‎cuda_core/cuda/core/_cpp/rt/memory.cpp‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "driver_api.hpp"
99
#include "error.hpp"
1010
#include "internal.hpp"
11+
#include <atomic>
1112
#include <cstddef>
1213
#include <cstring>
1314
#include <memory>
@@ -30,6 +31,13 @@ namespace {
3031
struct MemoryPoolBox {
3132
CUmemoryPool resource;
3233
};
34+
35+
// Process-wide count of live *owned* memory pools -- those this layer created
36+
// with cuMemPoolCreate or imported (and will cuMemPoolDestroy). Borrowed pool
37+
// references (create_mempool_handle_ref, get_device_mempool) are not counted.
38+
// Read by the test suite to gate the per-test gc.collect(); atomic for
39+
// free-threaded builds.
40+
std::atomic<long long> g_live_owned_mempools{0};
3341
} // namespace
3442

3543
// Helper to clear peer access before destroying a memory pool.
@@ -65,18 +73,28 @@ static void clear_mempool_peer_access(CUmemoryPool pool, int owner_device) noexc
6573
// owner_device is the ordinal of a device-located pool, or -1 when there is no
6674
// device to skip (host pools) or the location is unknown (imported pools).
6775
static MemoryPoolHandle wrap_mempool_owned(CUmemoryPool pool, int owner_device) {
76+
auto* raw = new MemoryPoolBox{pool};
77+
// Count before constructing the shared_ptr: if its control-block allocation
78+
// throws, it runs the deleter on raw, whose decrement then balances this.
79+
g_live_owned_mempools.fetch_add(1, std::memory_order_relaxed);
6880
auto box = std::shared_ptr<const MemoryPoolBox>(
69-
new MemoryPoolBox{pool},
81+
raw,
7082
[owner_device](const MemoryPoolBox* b) {
7183
GILReleaseGuard gil;
7284
clear_mempool_peer_access(b->resource, owner_device);
7385
pw_cuMemPoolDestroy(b->resource);
86+
g_live_owned_mempools.fetch_sub(1, std::memory_order_relaxed);
7487
delete b;
7588
}
7689
);
7790
return MemoryPoolHandle(box, &box->resource);
7891
}
7992

93+
// Test-suite instrumentation; see g_live_owned_mempools.
94+
long long live_owned_mempool_count() noexcept {
95+
return g_live_owned_mempools.load(std::memory_order_relaxed);
96+
}
97+
8098
MemoryPoolHandle create_mempool_handle(const CUmemPoolProps& props) {
8199
GILReleaseGuard gil;
82100
CUmemoryPool pool;

‎cuda_core/cuda/core/_cpp/rt/virtual_memory.cpp‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "driver_api.hpp"
1313
#include "error.hpp"
1414
#include "internal.hpp"
15+
#include <atomic>
1516
#include <cstddef>
1617
#include <memory>
1718
#include <utility>
@@ -38,6 +39,11 @@ struct VaReservationBox {
3839
size_t size; // exact reserved size; cuMemAddressFree needs both
3940
};
4041

42+
// Process-wide count of live VA reservations (cuMemAddressReserve), the leak
43+
// quantity issue #2381 is about; read by the test suite to gate the per-test
44+
// gc.collect(). Atomic for free-threaded builds.
45+
std::atomic<long long> g_live_va_reservations{0};
46+
4147
struct VaMappingBox {
4248
VaMappingValue resource; // mapped address
4349
size_t size; // == the allocation's size
@@ -93,17 +99,27 @@ VaReservationHandle create_va_reservation_handle(size_t size, size_t alignment,
9399
if (CUDA_SUCCESS != (err = DRIVER_CALL(cuMemAddressReserve, &ptr, size, alignment, hint, 0))) {
94100
return {};
95101
}
102+
auto* raw = new VaReservationBox{{ptr}, size};
103+
// Count before constructing the shared_ptr: if its control-block allocation
104+
// throws, it runs the deleter on raw, whose decrement then balances this.
105+
g_live_va_reservations.fetch_add(1, std::memory_order_relaxed);
96106
auto box = std::shared_ptr<const VaReservationBox>(
97-
new VaReservationBox{{ptr}, size},
107+
raw,
98108
[](const VaReservationBox* b) {
99109
GILReleaseGuard gil;
100110
pw_cuMemAddressFree(b->resource.raw, b->size);
111+
g_live_va_reservations.fetch_sub(1, std::memory_order_relaxed);
101112
delete b;
102113
}
103114
);
104115
return VaReservationHandle(box, &box->resource);
105116
}
106117

118+
// Test-suite instrumentation; see g_live_va_reservations.
119+
long long live_va_reservation_count() noexcept {
120+
return g_live_va_reservations.load(std::memory_order_relaxed);
121+
}
122+
107123
size_t va_reservation_size(const VaReservationHandle& h) noexcept {
108124
return h ? box_of<VaReservationBox>(h)->size : 0;
109125
}

‎cuda_core/cuda/core/_memory/_buffer.pyi‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ class MemoryResource:
301301
buffer properties are retrieved simply by looking up the underlying memory
302302
resource's respective property.)
303303
"""
304-
def __init__(self): ...
305-
def __dealloc__(self): ...
306304
def allocate(self, size: int, *, stream: Stream | GraphBuilder) -> Buffer:
307305
"""Allocate a buffer of the requested size.
308306
@@ -359,4 +357,5 @@ class _MemAttrs(TypedDict):
359357
is_host_accessible: bool
360358
is_managed: bool
361359

362-
def _live_memory_resource_count() -> int: ...
360+
def _live_owned_mempool_count() -> int: ...
361+
def _live_va_reservation_count() -> int: ...

‎cuda_core/cuda/core/_memory/_buffer.pyx‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from __future__ import annotations
66

77
cimport cython
88
from libc.stdint cimport uintptr_t
9-
from libcpp.atomic cimport memory_order_acquire, memory_order_release, atomic as std_atomic
9+
from libcpp.atomic cimport memory_order_acquire, memory_order_release
1010

1111
from cuda.bindings cimport cydriver
1212
from cuda.core._memory._device_memory_resource import DeviceMemoryResource
@@ -20,6 +20,8 @@ from cuda.core._rt cimport (
2020
deviceptr_create_with_owner,
2121
deviceptr_create_with_mr,
2222
register_mr_dealloc_callback,
23+
live_owned_mempool_count,
24+
live_va_reservation_count,
2325
as_intptr,
2426
as_cu,
2527
get_current_context,
@@ -34,12 +36,16 @@ from cuda.core._stream cimport Stream, Stream_accept, Stream_is_legacy_default_t
3436
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN, _parse_fill_value
3537

3638

37-
# Live MemoryResource count, read by the test suite; atomic for free-threaded builds.
38-
cdef std_atomic[long long] _mr_live_count
39+
# Process-wide counts of live *owned* driver resources, read by the test suite.
40+
# Counting lives in the C++ RAII layer (owned CUmemoryPool and VA reservations),
41+
# so borrowed pool references and MemoryResource subclasses that hold no pool or
42+
# reservation are excluded. conftest.py reads these to gate the per-test collect.
43+
def _live_owned_mempool_count() -> int:
44+
return live_owned_mempool_count()
3945

4046

41-
def _live_memory_resource_count() -> int:
42-
return _mr_live_count.load()
47+
def _live_va_reservation_count() -> int:
48+
return live_va_reservation_count()
4349

4450
import warnings
4551
from collections.abc import Sequence
@@ -784,12 +790,6 @@ cdef class MemoryResource:
784790
resource's respective property.)
785791
"""
786792

787-
def __cinit__(self):
788-
_mr_live_count.fetch_add(1)
789-
790-
def __dealloc__(self):
791-
_mr_live_count.fetch_sub(1)
792-
793793
def allocate(self, size_t size, *, stream: Stream | GraphBuilder) -> Buffer:
794794
"""Allocate a buffer of the requested size.
795795

‎cuda_core/cuda/core/_rt.pxd‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ cdef size_t mem_allocation_size(const MemAllocationHandle& h) noexcept nogil
302302
cdef VaReservationHandle create_va_reservation_handle(
303303
size_t size, size_t alignment, cydriver.CUdeviceptr hint) except+ nogil
304304
cdef size_t va_reservation_size(const VaReservationHandle& h) noexcept nogil
305+
306+
# Test-suite instrumentation: process-wide counts of live owned pools and VA
307+
# reservations (see api.hpp). Read by conftest.py to gate the per-test collect.
308+
cdef long long live_owned_mempool_count() noexcept nogil
309+
cdef long long live_va_reservation_count() noexcept nogil
305310
cdef VaMappingHandle create_va_mapping_handle(
306311
cydriver.CUdeviceptr ptr, const MemAllocationHandle& h_alloc,
307312
const VaReservationHandle& h_res) except+ nogil

‎cuda_core/cuda/core/_rt.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ cdef extern from "_cpp/rt/rt.hpp" namespace "cuda_core::rt":
184184
size_t size, size_t alignment, cydriver.CUdeviceptr hint) except+ nogil
185185
size_t va_reservation_size "cuda_core::rt::va_reservation_size" (
186186
const VaReservationHandle& h) noexcept nogil
187+
long long live_owned_mempool_count "cuda_core::rt::live_owned_mempool_count" () noexcept nogil
188+
long long live_va_reservation_count "cuda_core::rt::live_va_reservation_count" () noexcept nogil
187189
VaMappingHandle create_va_mapping_handle "cuda_core::rt::create_va_mapping_handle" (
188190
cydriver.CUdeviceptr ptr, const MemAllocationHandle& h_alloc,
189191
const VaReservationHandle& h_res) except+ nogil

‎cuda_core/tests/AGENTS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ with pytest.raises(RuntimeError, match="IPC is not available"):
6262

6363
The `init_cuda` fixture in `conftest.py` runs `gc.collect()` and
6464
`cuCtxSynchronize()` before popping the context. The collect runs only when the
65-
live `MemoryResource` count has changed since setup, so do not rely on it as a
65+
count of live owned driver resources (owned memory pools or VA reservations,
66+
tracked in the C++ layer) has changed since setup, so do not rely on it as a
6667
substitute for cleaning up explicitly: prefer context managers for resources
6768
whose lifetime fits a single scope, and keep pool lifetimes inside the test
6869
that creates them. The collected items and imported modules are frozen after

‎cuda_core/tests/conftest.py‎

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
PinnedMemoryResourceOptions,
4949
_device,
5050
)
51-
from cuda.core._memory._buffer import _live_memory_resource_count
51+
from cuda.core._memory._buffer import _live_owned_mempool_count, _live_va_reservation_count
5252
from cuda.core._utils.cuda_utils import handle_return
5353

5454

@@ -131,38 +131,30 @@ def _item_uses_init_cuda(item):
131131
return "init_cuda" in getattr(item, "fixturenames", ())
132132

133133

134-
# init_cuda collects at teardown only if the process-wide live MemoryResource
135-
# count differs from its value at setup. That is sound only if no older
136-
# resource is released during a test (it could cancel a new leak), so tests
137-
# that construct resources must use init_cuda, and the leftover check below
138-
# reports resources that outlive a test without it.
134+
# init_cuda collects at teardown only if the process-wide count of live *owned*
135+
# driver resources (owned memory pools and VA reservations) differs from its
136+
# value at setup. The counts come from the C++ RAII layer, so a borrowed pool
137+
# reference (the device default pool) and MemoryResource subclasses that hold no
138+
# pool or reservation do not move them. That makes the gate sound only if no
139+
# older owned resource is released during a test (it could cancel a new leak),
140+
# so tests that construct owned resources must use init_cuda, and the leftover
141+
# check below reports owned resources that outlive a test without it.
139142

140-
# Live count after the last init_cuda test; None until one has run.
141-
_mr_expected_live = None
142-
# Live count at the start of the current item, before its fixtures set up.
143-
_mr_live_at_item_start = 0
144-
_mr_last_init_cuda_nodeid = None
145143

144+
def _live_resource_counts():
145+
"""(owned memory pools, VA reservations) live process-wide, as a tuple.
146146
147-
def _device_default_memory_resource_count():
148-
"""Number of main-thread Devices that currently cache a default MR.
149-
150-
Reaches into Device thread-local state (``_device._tls``) on purpose; update
151-
this and :func:`_release_device_default_memory_resources` if that moves.
147+
Only *owned* driver resources are counted (see the C++ accessors), so this
148+
excludes borrowed pool references and non-owning MemoryResource subclasses.
152149
"""
153-
return sum(d._memory_resource is not None for d in getattr(_device._tls, "devices", ()))
154-
150+
return (_live_owned_mempool_count(), _live_va_reservation_count())
155151

156-
def _release_device_default_memory_resources():
157-
"""Drop each main-thread Device's lazily created default memory resource.
158152

159-
Otherwise they are still counted when init_cuda compares counts at
160-
teardown and force a collect in every test that allocates through the
161-
device default. The setter rejects None,
162-
hence the private access.
163-
"""
164-
for d in getattr(_device._tls, "devices", ()):
165-
d._memory_resource = None
153+
# Counts after the last init_cuda test; None until one has run.
154+
_mr_expected_live = None
155+
# Counts at the start of the current item, before its fixtures set up.
156+
_mr_live_at_item_start = (0, 0)
157+
_mr_last_init_cuda_nodeid = None
166158

167159

168160
@pytest.hookimpl(wrapper=True)
@@ -171,17 +163,17 @@ def pytest_runtest_protocol(item, nextitem):
171163
# not the session start, because the count legitimately rises over a run
172164
# (interned GraphMemoryResource, pytest-retained class instances).
173165
global _mr_expected_live, _mr_live_at_item_start, _mr_last_init_cuda_nodeid
174-
_mr_live_at_item_start = _live_memory_resource_count()
166+
_mr_live_at_item_start = _live_resource_counts()
175167
try:
176168
return (yield)
177169
finally:
178-
live = _live_memory_resource_count()
170+
live = _live_resource_counts()
179171
if _item_uses_init_cuda(item):
180172
_mr_expected_live = live
181173
_mr_last_init_cuda_nodeid = item.nodeid
182174
elif _mr_expected_live is not None:
183-
# Only lower it: a released resource must not hide a later leak.
184-
_mr_expected_live = min(_mr_expected_live, live)
175+
# Only lower each component: a released resource must not hide a later leak.
176+
_mr_expected_live = tuple(min(a, b) for a, b in zip(_mr_expected_live, live))
185177

186178

187179
class _CudaCoreParallelPlugin:
@@ -243,25 +235,27 @@ def init_cuda(request):
243235
)
244236

245237
# Leftover check: growth since the previous init_cuda test means something
246-
# in between left a resource alive. Collect first to sweep a cyclic
247-
# leftover, and before the count at setup is recorded so it is reflected there.
248-
if _mr_expected_live is not None and _mr_live_at_item_start > _mr_expected_live:
238+
# in between left an owned resource alive. Collect first to sweep a cyclic
239+
# leftover, and before the counts at setup are recorded so they are reflected there.
240+
if _mr_expected_live is not None and any(now > exp for now, exp in zip(_mr_live_at_item_start, _mr_expected_live)):
249241
gc.collect()
250-
held = _live_memory_resource_count() - _mr_expected_live
242+
now_counts = _live_resource_counts()
243+
held = tuple(max(n - e, 0) for n, e in zip(now_counts, _mr_expected_live))
244+
excess = tuple(s - e for s, e in zip(_mr_live_at_item_start, _mr_expected_live))
251245
seed = getattr(request.config.option, "randomly_seed", None)
252246
seed_msg = f" (pytest-randomly seed: {seed})" if seed is not None else ""
253247
warnings.warn(
254-
f"{_mr_live_at_item_start - _mr_expected_live} memory resource(s) were left alive by "
255-
f"tests that ran since {_mr_last_init_cuda_nodeid} ({max(held, 0)} still alive after a "
248+
f"{excess} (owned_pools, va_reservations) memory resource(s) were left alive by "
249+
f"tests that ran since {_mr_last_init_cuda_nodeid} ({held} still alive after a "
256250
f"collect). A test or fixture that constructs a memory resource must request "
257251
f"init_cuda.{seed_msg}",
258252
UnreleasedMemoryResourceWarning,
259253
stacklevel=2,
260254
)
261255

262-
# Live count at setup, excluding cached Device default resources (they are
263-
# released at teardown before the count is compared).
264-
live_at_setup = _live_memory_resource_count() - _device_default_memory_resource_count()
256+
# Counts at setup. The device default pool is a borrowed reference and is
257+
# not counted, so no cached-default bookkeeping is needed.
258+
live_at_setup = _live_resource_counts()
265259
try:
266260
yield device
267261
finally:
@@ -270,13 +264,12 @@ def init_cuda(request):
270264
# torn-down fixtures are not counted below. Revisit if pytest changes
271265
# how it retains fixture values.
272266
request.node.funcargs.clear()
273-
# Only a resource in a reference cycle outlives the test frame, so
274-
# collect only if the count differs from its value at setup. A lower
267+
# Only an owned resource in a reference cycle outlives the test frame, so
268+
# collect only if the counts differ from their values at setup. A lower
275269
# count means an older resource was released, which could mask a leak
276270
# in this test: collect in that case too.
277-
_release_device_default_memory_resources()
278-
live_after_release = _live_memory_resource_count()
279-
if live_after_release != live_at_setup:
271+
live_after = _live_resource_counts()
272+
if live_after != live_at_setup:
280273
gc.collect()
281274
# Force any pool/allocation whose only remaining reference was a local
282275
# in this test's frame to actually get destroyed now, then drain the
@@ -288,12 +281,13 @@ def init_cuda(request):
288281
driver.cuCtxSynchronize()
289282
_ = _device_unset_current()
290283
# After the cleanup, so an escalated warning cannot skip it.
291-
if live_after_release < live_at_setup:
284+
if any(now < snap for now, snap in zip(live_after, live_at_setup)):
285+
released = tuple(s - n for s, n in zip(live_at_setup, live_after))
292286
warnings.warn(
293-
f"{request.node.nodeid} released {live_at_setup - live_after_release} memory resource(s) "
294-
f"that existed before it started (count {live_after_release}, snapshot {live_at_setup}). "
295-
"A leak in this test could have been masked by that release. Find what held the "
296-
"released resource.",
287+
f"{request.node.nodeid} released {released} (owned_pools, va_reservations) memory "
288+
f"resource(s) that existed before it started (counts {live_after}, snapshot "
289+
f"{live_at_setup}). A leak in this test could have been masked by that release. "
290+
"Find what held the released resource.",
297291
BaselineMemoryResourceReleasedWarning,
298292
stacklevel=2,
299293
)

0 commit comments

Comments
 (0)