Skip to content

add gpu free shared region concurrency regression test - #244

Open
iemAnshuman wants to merge 4 commits into
Project-HAMi:mainfrom
iemAnshuman:issue-1662-shrreg-regression
Open

add gpu free shared region concurrency regression test#244
iemAnshuman wants to merge 4 commits into
Project-HAMi:mainfrom
iemAnshuman:issue-1662-shrreg-regression

Conversation

@iemAnshuman

@iemAnshuman iemAnshuman commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

What this PR does:

lock_shrreg() serializes every process that registers a slot in the shared region, and try_create_shrreg() and init_proc_slot_withlock() run behind it on every process's cuInit() path. Nothing in the tree exercises that under concurrency, so a regression in the semaphore protocol or in slot allocation only shows up on a machine with GPUs and a driver.

test/test_shared_region_concurrency.c forks N workers (128 by default, 256 max via SHRREG_TEST_WORKERS), releases them from a pipe barrier into ensure_initialized() together, and keeps them alive while the parent maps the real cache file. The parent then checks that proc_num equals the worker count, that every worker PID sits in exactly one slot, and that each worker's sequence-point timestamps are present and in order, which shows it went through lock_shrreg() rather than skipping it. Non-zero exit on deadlock (20s deadline, 90s ctest timeout outside it), a lost or duplicate slot, or a worker that dies.

It links only -lrt and -lpthread, no -lcuda, -lnvidia-ml or -lcudart, and calls no CUDA or NVML entry point, so it needs no device node, driver or GPU at runtime. multiprocess_memory_limit.h still includes cuda.h, so the CUDA headers the build already requires are still needed to compile. It compiles the production multiprocess_memory_limit.c, not a copy.

Timings reuse the existing SEQ_POINT_MARK hook: one new sequence point before sem_timedwait(), plus a callback form of the macro that is only selected when SHRREG_SEQUENCE_POINT_CALLBACK is defined. The default build is unchanged, SEQ_POINT_MARK still expands to nothing, and libvgpu.so is not affected.

The latencies it prints are not assertions. No wall-time ceiling applies unless SHRREG_TEST_MAX_WALL_MS is set, so a slow runner cannot fail it.

Not a fix. It does not change initialization behavior and does not address the contention in Project-HAMi/HAMi#1662, and it passes before and after any change there. #239 by @om7057 measures that on real hardware; this is the GPU-free half.

Which issue(s) this PR fixes:

Refs Project-HAMi/HAMi#1662

Special notes for your reviewer:

Built and run on Rostam cluster, 128 CPUs, GCC 11.5.0, CUDA 12.9, driver 580.65.06. The node has two A100 80GB cards, which the test does not touch.

make build-in-docker
cd build && ctest -R shared_region_concurrency --output-on-failure
./build/test/test_shared_region_concurrency
SHRREG_TEST_WORKERS=256 ./build/test/test_shared_region_concurrency

ctest found one matching test and passed it, 1/1 in 0.12 s. Ten consecutive 128-worker runs passed, 92.243 ms to 107.744 ms wall and 89.046 ms to 103.318 ms total p99. The 256-worker run passed at 355.600 ms wall, 348.926 ms total p99 and 335.880 ms semaphore-wait p99.

SHRREG_TEST_MAX_WALL_MS=1 exits non-zero and reports the measured wall time, 100.088 ms on that run. Worker counts of 0 and 257 both exit 2. ldd shows only libc and the ELF loader, and a traced 32-worker run touched no /dev/nvidia, libcuda, libnvidia-ml or libcudart.

One linker fix is included: the inherited -rdynamic kept unused GPU-facing functions alive despite section garbage collection, so the test target adds --no-export-dynamic.

I used Claude Code while writing this test.

Does this PR introduce a user-facing change?: none

Summary by CodeRabbit

  • New Features

    • Added optional synchronization-event callbacks for improved observability.
    • Added semaphore-acquisition event markers.
    • Added a configurable shared-region concurrency test with worker controls, timeout settings, cache validation, retry tracking, and JSON latency reporting.
  • Tests

    • Enabled CTest-based test execution.
    • Registered the concurrency test with a 90-second timeout.
    • Improved its build and linking configuration for more reliable test execution.

Signed-off-by: iemAnshuman <asquare567@gmail.com>
@hami-robot
hami-robot Bot requested a review from archlitchi July 30, 2026 16:06
@hami-robot
hami-robot Bot requested a review from chaunceyjiang July 30, 2026 16:06
@hami-robot

hami-robot Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: iemAnshuman
Once this PR has been reviewed and has the lgtm label, please assign archlitchi for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot

hami-robot Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Welcome @iemAnshuman! It looks like this is your first PR to Project-HAMi/HAMi-core 🎉

@hami-robot hami-robot Bot added the size/L label Jul 30, 2026
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds optional semaphore sequence-point callbacks and a fork-based shared-region concurrency test. CMake builds the test with system libraries, registers it with CTest, sets a 90-second timeout, and enables testing.

Changes

Shared-region concurrency testing

Layer / File(s) Summary
Sequence-point instrumentation
src/multiprocess/multiprocess_memory_limit.h, src/multiprocess/multiprocess_memory_limit.c
Adds an optional callback and marks the point before semaphore acquisition.
Concurrent test harness
test/test_shared_region_concurrency.c
Adds worker state, process synchronization, timing capture, configuration parsing, worker control, and latency calculations.
Cache validation and result handling
test/test_shared_region_concurrency.c
Coordinates workers, validates cache slots and timing results, reports JSON metrics, and cleans up processes and temporary resources.
Build and CTest integration
CMakeLists.txt, test/CMakeLists.txt
Adds the dedicated executable, system-library links, CTest registration, and a 90-second timeout. It also adds a duplicate enable_testing() call.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Test as test_shared_region_concurrency
  participant Workers as worker processes
  participant Lock as lock_shrreg
  participant Cache as shared-region cache
  Test->>Workers: fork and release synchronization gates
  Workers->>Lock: register shared region
  Lock->>Workers: record semaphore acquisition sequence point
  Workers->>Cache: write registration entries
  Test->>Cache: validate live PID slots
  Test->>Workers: reap processes and report metrics
Loading

Suggested labels: enhancement

Suggested reviewers: archlitchi, chaunceyjiang

Poem

A rabbit watched the workers share,
With pipes and clocks arranged with care.
CTest checked each waiting gate,
While cache slots recorded state.
“Hop!” said the bunny, “locks now sing!”

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the added GPU-free shared-region concurrency regression test.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
test/CMakeLists.txt (1)

15-37: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Collapse the duplicated test_shared_region_concurrency.c predicate.

The same MATCHES check is evaluated at line 15 and again at line 32. Folding the link step into the first branch keeps the special case in one place.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/CMakeLists.txt` around lines 15 - 37, Consolidate the duplicated
test_shared_region_concurrency.c condition in the test target setup by moving
its special target_link_libraries call into the existing first branch. Remove
the later repeated predicate and retain the current CUDA-library linking in the
else path.
test/test_shared_region_concurrency.c (1)

69-84: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Sequence marks are overwritten on every lock retry, so reported wait latency understates contention.

lock_shrreg emits SEQ_BEFORE_ACQUIRE_SEMLOCK and SEQ_ACQUIRE_SEMLOCK_OK on each loop iteration, including timed-out attempts. With 128+ workers contending, a worker that times out repeatedly reports only the final successful attempt, so wait_*_ms in print_results can look near-zero while the worker actually waited tens of seconds. Consider accumulating instead of overwriting (e.g., keep the first acquire_begin_ms and count attempts).

♻️ Sketch: record first attempt + attempt count
         case SEQ_BEFORE_ACQUIRE_SEMLOCK:
-            result->acquire_begin_ms = timestamp;
+            if (result->acquire_begin_ms <= 0.0) {
+                result->acquire_begin_ms = timestamp;
+            }
+            result->acquire_attempts++;
             break;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/test_shared_region_concurrency.c` around lines 69 - 84, Update the
sequence handling around SEQ_BEFORE_ACQUIRE_SEMLOCK and SEQ_ACQUIRE_SEMLOCK_OK
to preserve the worker’s initial acquisition timestamp across retries instead of
overwriting it; track the number of acquisition attempts if the existing result
structure and print_results reporting support it, so wait latency includes the
full contention period while release and unlock marks retain their current
behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/CMakeLists.txt`:
- Around line 34-37: Update the test CMake configuration around the CUDA link
branch to gate NVIDIA/CUDA libraries on successful CUDA discovery. Ensure
non-CUDA tests can configure without CUDA, either by applying these link options
only to CUDA-capable targets or by skipping the test subdirectory when CUDA is
unavailable, while preserving existing linking for CUDA-enabled tests.
- Around line 46-48: Increase the CTest TIMEOUT for shared_region_concurrency
above the test’s maximum internal wait budget, accounting for both timeout_ms
wait paths under the default SHRREG_TEST_TIMEOUT_MS value, so the harness can
report its diagnostic before CTest terminates it.

---

Nitpick comments:
In `@test/CMakeLists.txt`:
- Around line 15-37: Consolidate the duplicated test_shared_region_concurrency.c
condition in the test target setup by moving its special target_link_libraries
call into the existing first branch. Remove the later repeated predicate and
retain the current CUDA-library linking in the else path.

In `@test/test_shared_region_concurrency.c`:
- Around line 69-84: Update the sequence handling around
SEQ_BEFORE_ACQUIRE_SEMLOCK and SEQ_ACQUIRE_SEMLOCK_OK to preserve the worker’s
initial acquisition timestamp across retries instead of overwriting it; track
the number of acquisition attempts if the existing result structure and
print_results reporting support it, so wait latency includes the full contention
period while release and unlock marks retain their current behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cd9c8f7e-7a3f-4dd2-a82d-3b5380d50ec4

📥 Commits

Reviewing files that changed from the base of the PR and between 52f33fc and 7981a60.

📒 Files selected for processing (5)
  • CMakeLists.txt
  • src/multiprocess/multiprocess_memory_limit.c
  • src/multiprocess/multiprocess_memory_limit.h
  • test/CMakeLists.txt
  • test/test_shared_region_concurrency.c

Comment thread test/CMakeLists.txt
Comment thread test/CMakeLists.txt Outdated
Signed-off-by: iemAnshuman <asquare567@gmail.com>
Signed-off-by: iemAnshuman <asquare567@gmail.com>
@coderabbitai coderabbitai Bot added the enhancement New feature or request label Jul 30, 2026
Signed-off-by: iemAnshuman <asquare567@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant