Skip to content

Commit 37314c3

Browse files
committed
Fix an issue in Centos 7 where the Runtime.availableProcessors() actually gives you the number usable (not isolated) unlike other linux versions/older JVMs.
1 parent ec7e030 commit 37314c3

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

affinity/src/main/java/net/openhft/affinity/AffinityLock.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,30 @@ public class AffinityLock implements Closeable {
4040
public static final String AFFINITY_RESERVED = "affinity.reserved";
4141
// TODO It seems like on virtualized platforms .availableProcessors() value can change at
4242
// TODO runtime. We should think about how to adopt to such change
43-
public static final int PROCESSORS = Runtime.getRuntime().availableProcessors();
44-
public static final BitSet BASE_AFFINITY = Affinity.getAffinity();
45-
public static final BitSet RESERVED_AFFINITY = getReservedAffinity0();
43+
public static final int PROCESSORS;
44+
45+
public static final BitSet BASE_AFFINITY;
46+
public static final BitSet RESERVED_AFFINITY;
4647
private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLock.class);
47-
private static final LockInventory LOCK_INVENTORY = new LockInventory(new NoCpuLayout(PROCESSORS));
48+
private static final LockInventory LOCK_INVENTORY;
4849

4950
static {
51+
int processors = Runtime.getRuntime().availableProcessors();
52+
VanillaCpuLayout cpuLayout = null;
5053
try {
5154
if (new File("/proc/cpuinfo").exists()) {
52-
cpuLayout(VanillaCpuLayout.fromCpuInfo());
55+
cpuLayout = VanillaCpuLayout.fromCpuInfo();
56+
processors = cpuLayout.cpus();
5357
}
5458
} catch (IOException e) {
5559
LOGGER.warn("Unable to load /proc/cpuinfo", e);
5660
}
61+
PROCESSORS = processors;
62+
BASE_AFFINITY = Affinity.getAffinity();
63+
RESERVED_AFFINITY = getReservedAffinity0();
64+
LOCK_INVENTORY = new LockInventory(new NoCpuLayout(PROCESSORS));
65+
if (cpuLayout != null)
66+
LOCK_INVENTORY.set(cpuLayout);
5767
}
5868

5969
/**

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
<modelVersion>4.0.0</modelVersion>
3030
<artifactId>Java-Thread-Affinity</artifactId>
31-
<version>3.0.6-SNAPSHOT</version>
31+
<version>3.1.1-SNAPSHOT</version>
3232
<packaging>pom</packaging>
3333

3434
<name>OpenHFT/Java-Thread-Affinity Parent</name>

0 commit comments

Comments
 (0)