Skip to content

AffinityLock.acquireCore should lock/unlock all cpus ie 2 when hypert… #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: added-tests
Choose a base branch
from

Conversation

peter-lawrey
Copy link
Member

@peter-lawrey peter-lawrey commented May 23, 2025

This change makes AffinityLock.acquireCore() correctly reserve all logical
CPUs that share the same physical core (e.g. both SMT / Hyper-Threads).
It touches the lock inventory, PID-file handling and CPU-layout APIs, plus adds
new verification tests.


✨ Behavioural changes

Area What changed
Whole-core locking AffinityLock.acquireCore() now locks both hyper-threads on the target core, preventing other threads or JVMs from binding to the sibling logical CPU.
AffinityLock • Added secondary field cpuId2 to track the paired logical CPU.
• Hex mask parsing uses Long.parseUnsignedLong (fixes > 64-bit masks).
acquireLock(int) returns noLock (not exception) when CPU id is out-of-range.
acquireLock(int[]) silently skips invalid ids with a log warning.
CPU layout API New method int pair(int cpuId) returns the hyper-thread twin (0 if not HT). Implemented in VanillaCpuLayout; stubbed in NoCpuLayout.
Lock-file plumbing LockChecker & FileLockBasedLockChecker upgraded so obtainLock() can atomically grab two lock files (cpu, cpuId2). PID logging & replace logic updated accordingly.
LockInventory Generates locks with both cpuId and its pair(). Whole-core acquisition path passes both ids to LockCheck.

🛠️ API surface changes

  • CpuLayout – + pair(int cpuId)
  • LockChecker.obtainLock(int cpu1, int cpu2, String meta) – supersedes old 2-arg variant (now removed).
    Internal interfaces only – no public-API break for typical callers.

🧪 Tests

New / updated Purpose
VanillaCpuLayoutPairTest Validates pair() mapping on sample i7 & i3 cpuinfo.
AffinityLockTest Adds checks that acquireCore() reserves all logical CPUs of a core and updates existing expectations to dual-id locks.
FileLockLockCheckTest, LockCheckTest Updated for new updateCpu / replacePid signatures.

Total diff +170 / -47 (mostly tests & lock-plumbing).


✅ Compatibility & Migration

  • No user-facing API is removed; internal service providers updated.
  • Existing code that calls LockChecker.obtainLock() directly must adopt the
    new 3-arg form (very rare outside this repo).

Whole-core reservation eliminates “half-locked” scenarios on hyper-threaded
CPUs, giving applications stronger isolation guarantees.

@peter-lawrey peter-lawrey requested a review from tgd May 23, 2025 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants