|
19 | 19 | import pytest |
20 | 20 |
|
21 | 21 |
|
| 22 | +# Evaluated in the PARENT interpreter (subprocess bodies cannot express a skip: |
| 23 | +# an in-body ``pytest.skip`` would surface as a non-zero exit and FAIL the outer |
| 24 | +# test). ``test_hook_hits()`` is a read-only counter query with no side effects — |
| 25 | +# it does NOT install the gotter — so it is safe to call at import time. It |
| 26 | +# returns ``None`` unless the loaded cdylib was built with the ``test-support`` |
| 27 | +# cargo feature (Linux 64-bit, ``DD_PROFILING_NATIVE_HEAP_TEST_SUPPORT=1``), |
| 28 | +# which the standard CI wheel is not, so the end-to-end handoff proof below skips |
| 29 | +# everywhere except a dedicated test-support build. |
| 30 | +try: |
| 31 | + from ddtrace.internal.datadog.profiling import heap_gotter as _heap_gotter |
| 32 | + |
| 33 | + _GOTTER_TEST_HOOK_AVAILABLE: bool = _heap_gotter.test_hook_hits() is not None |
| 34 | +except Exception: |
| 35 | + _GOTTER_TEST_HOOK_AVAILABLE = False |
| 36 | + |
| 37 | + |
22 | 38 | @pytest.mark.skipif(sys.platform != "linux", reason="native heap gotter is Linux-only") |
23 | 39 | @pytest.mark.subprocess |
24 | 40 | def test_native_heap_gotter_smoke() -> None: |
@@ -228,3 +244,129 @@ def test_profiler_keeps_managed_heap_when_native_heap_disabled() -> None: |
228 | 244 | set_partition.assert_called_once_with(False) |
229 | 245 | finally: |
230 | 246 | prof.stop(flush=False) |
| 247 | + |
| 248 | + |
| 249 | +# --------------------------------------------------------------------------- |
| 250 | +# End-to-end producer-side ownership handoff (Phase 2 de-dup) |
| 251 | +# |
| 252 | +# The tests above prove the *wiring* (arming turns the partition on) and |
| 253 | +# tests/profiling/collector/test_memalloc.py proves the *in-process* half of the |
| 254 | +# partition (> 512B managed allocations are dropped, <= 512B kept, and with the |
| 255 | +# partition off everything is sampled). What neither can prove without a live |
| 256 | +# eBPF/Full-Host attach is the *other* half of the handoff: that the native |
| 257 | +# gotter actually captures the > 512B raw glibc-malloc tail the in-process |
| 258 | +# sampler drops — i.e. that exactly one producer owns each allocation. |
| 259 | +# |
| 260 | +# The test below closes that gap deterministically in a single process using the |
| 261 | +# gotter's built-in ``test-support`` hook-hit counter, replacing the flaky |
| 262 | +# staging A/B dedup signal with an in-CI assertion. It requires a Linux 64-bit |
| 263 | +# ``test-support`` gotter build (see the module-level skip note); it skips in the |
| 264 | +# standard CI wheel, which ships no gotter at all. |
| 265 | +# --------------------------------------------------------------------------- |
| 266 | + |
| 267 | + |
| 268 | +@pytest.mark.skipif( |
| 269 | + sys.platform != "linux" or not _GOTTER_TEST_HOOK_AVAILABLE, |
| 270 | + reason=( |
| 271 | + "needs a Linux 64-bit test-support gotter build exposing " |
| 272 | + "ddtrace_heap_gotter_test_hook_hits() (build with " |
| 273 | + "DD_PROFILING_NATIVE_HEAP_BUILD=1 DD_PROFILING_NATIVE_HEAP_TEST_SUPPORT=1); " |
| 274 | + "the standard CI wheel ships no gotter" |
| 275 | + ), |
| 276 | +) |
| 277 | +@pytest.mark.subprocess |
| 278 | +def test_native_heap_ownership_handoff_end_to_end() -> None: |
| 279 | + """Deterministic, cluster-independent proof of the Phase 2 ownership handoff. |
| 280 | +
|
| 281 | + With the gotter armed and the producer-side size partition on, a > 512B |
| 282 | + managed OBJ allocation must be owned by *exactly one* producer: |
| 283 | +
|
| 284 | + (a) it is NOT sampled by the in-process ``_memalloc`` heap profiler (the |
| 285 | + partition drops the > 512B tail), AND |
| 286 | + (b) it IS seen by the native gotter — the process-global hook-hit counter |
| 287 | + advances by at least one per large allocation, proving the patched GOT |
| 288 | + captured the raw glibc ``malloc`` the in-process sampler dropped. |
| 289 | +
|
| 290 | + A <= 512B control allocation stays pymalloc-pool-served and is still sampled |
| 291 | + in-process, confirming the partition splits by size rather than dropping |
| 292 | + everything. Runs in a subprocess because ``install()`` patches the process |
| 293 | + GOT permanently and the partition flag is process-global. |
| 294 | + """ |
| 295 | + import os |
| 296 | + import tempfile |
| 297 | + |
| 298 | + from ddtrace.internal.datadog.profiling import ddup |
| 299 | + from ddtrace.internal.datadog.profiling import heap_gotter |
| 300 | + from ddtrace.profiling.collector import memalloc |
| 301 | + from tests.profiling.collector import pprof_utils |
| 302 | + from tests.profiling.collector.test_memalloc import _PARTITION_LARGE_ALLOC_COUNT |
| 303 | + from tests.profiling.collector.test_memalloc import _allocate_large_buffers |
| 304 | + from tests.profiling.collector.test_memalloc import _allocate_small_objects |
| 305 | + from tests.profiling.collector.test_memalloc import _count_heap_samples_with_function |
| 306 | + |
| 307 | + # Defensive: the module-level skipif already gated on these, but assert so a |
| 308 | + # mis-configured skip can never let this test pass vacuously. |
| 309 | + assert heap_gotter.is_available, "test requires the gotter cdylib to be present" |
| 310 | + assert heap_gotter.test_hook_hits() is not None, "test requires a test-support gotter build" |
| 311 | + |
| 312 | + # Arm the native producer (permanent + process-global; hence @subprocess). |
| 313 | + assert heap_gotter.install() is True |
| 314 | + assert heap_gotter.is_installed() is True |
| 315 | + |
| 316 | + prefix = os.path.join(tempfile.mkdtemp(), "handoff") |
| 317 | + output_filename = prefix + "." + str(os.getpid()) |
| 318 | + ddup.config( |
| 319 | + service="test_native_heap_ownership_handoff", |
| 320 | + version="test", |
| 321 | + env="test", |
| 322 | + output_filename=prefix, |
| 323 | + ) |
| 324 | + ddup.start() |
| 325 | + |
| 326 | + store: list[object] = [] |
| 327 | + mc = memalloc.MemoryCollector(heap_sample_size=64 * 1024) |
| 328 | + memalloc.set_native_heap_partition(True) |
| 329 | + try: |
| 330 | + with mc: |
| 331 | + # Measure the native counter strictly around the > 512B allocations. |
| 332 | + # The counter is process-global and increments on EVERY intercepted |
| 333 | + # raw malloc (it is NOT sampling-gated), so background allocations |
| 334 | + # can only inflate the delta — never shrink it below the number of |
| 335 | + # large buffers we deliberately allocate. |
| 336 | + hits_before = heap_gotter.test_hook_hits() |
| 337 | + _allocate_large_buffers(store) |
| 338 | + hits_after = heap_gotter.test_hook_hits() |
| 339 | + |
| 340 | + _allocate_small_objects(store) |
| 341 | + mc.snapshot() |
| 342 | + ddup.upload() |
| 343 | + |
| 344 | + profile = pprof_utils.parse_newest_profile(output_filename) |
| 345 | + heap_samples = pprof_utils.get_samples_with_value_type(profile, "heap-space") |
| 346 | + |
| 347 | + # (a) In-process producer dropped the > 512B tail ... |
| 348 | + large_count = _count_heap_samples_with_function(profile, heap_samples, "_allocate_large_buffers") |
| 349 | + assert large_count == 0, ( |
| 350 | + f"partition ON: > 512B managed allocations must NOT be sampled in-process (got {large_count})" |
| 351 | + ) |
| 352 | + |
| 353 | + # (b) ... and the native producer captured it. Each > 512B bytes object |
| 354 | + # is a single raw malloc routed through the patched GOT, so the hook-hit |
| 355 | + # counter must advance by at least the number of large buffers. |
| 356 | + assert hits_before is not None and hits_after is not None |
| 357 | + delta = hits_after - hits_before |
| 358 | + assert delta >= _PARTITION_LARGE_ALLOC_COUNT, ( |
| 359 | + "native gotter must capture the > 512B raw-malloc tail the in-process sampler dropped " |
| 360 | + f"(hook-hit delta {delta} < {_PARTITION_LARGE_ALLOC_COUNT} large allocations)" |
| 361 | + ) |
| 362 | + |
| 363 | + # Control: <= 512B pool-served allocations are invisible to the gotter |
| 364 | + # and must still be sampled in-process — the partition splits by size. |
| 365 | + small_count = _count_heap_samples_with_function(profile, heap_samples, "_allocate_small_objects") |
| 366 | + assert small_count > 0, "partition ON: <= 512B managed allocations must still be sampled in-process" |
| 367 | + finally: |
| 368 | + # Reset the process-global flag so it cannot bleed into other tests |
| 369 | + # sharing this interpreter (belt-and-braces; the subprocess exits anyway). |
| 370 | + memalloc.set_native_heap_partition(False) |
| 371 | + |
| 372 | + del store |
0 commit comments