$ show OpenHFT/Java-Thread-Affinity
This is a mature, cross-platform Java library that provides thread-to-CPU-core binding (affinity) to reduce context switching and improve cache locality for low-latency and high-performance applications. It primarily uses JNA to invoke OS-specific system calls (sched_setaffinity on Linux, SetThreadAffinityMask on Windows, etc.) and offers a fallback to a no-op implementation. The library includes CPU topology detection via /proc/cpuinfo parsing, a file-based inter-process lock mechanism (AffinityLock) to reserve cores across JVMs, a high-resolution timer based on the RDTSC instruction via JNI, and a ThreadFactory for automatic affinity assignment. The architecture is a classic Java library with a clean public API (Affinity, AffinityLock, AffinityThreadFactory) and a set of platform-specific implementations hidden behind the IAffinity interface. The codebase is well-structured, extensively tested (including multi-process and OSGi tests), and actively maintained by the OpenHFT/Chronicle Software team. It is not a data_research project.
The LLM identified 2 files worth highlighting for reuse:
Parses /proc/cpuinfo (or a properties file) to derive CPU layout (sockets, cores, threads per core). This is a self-contained utility that could be reused in any Java project needing to understand the system's CPU topology. It handles missing fields, hyper-threading, and multiple sockets. The parsing logic is robust and demonstrates a pattern for reading and interpreting structured OS configuration files.
Implements LockChecker using Java NIO FileChannel and FileLock to coordinate CPU reservations across multiple JVM processes. It handles lock file creation, exclusive locking, meta-information storage (PID), retry logic on concurrent deletion, and cleanup. This is a production-quality pattern for distributed resource locking using the filesystem, suitable for extraction and reuse in any scenario requiring simple inter-process synchronization.