Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Try to ensure there is always a CpuLayout

  • Loading branch information...
commit 2eb1744a127f0f818c01e81fa748b5e54ef61b26 1 parent f750455
@peter-lawrey authored
Showing with 14 additions and 5 deletions.
  1. +14 −5 src/main/java/vanilla/java/affinity/AffinityLock.java
View
19 src/main/java/vanilla/java/affinity/AffinityLock.java
@@ -47,7 +47,7 @@
private static AffinityLock[] LOCKS;
private static NavigableMap<Integer, AffinityLock[]> CORES; // set by cpuLayout()
private static final AffinityLock NONE = new AffinityLock(-1, false, false);
- private static CpuLayout cpuLayout = null;
+ private static CpuLayout cpuLayout = new NoCpuLayout(PROCESSORS);
static {
try {
@@ -55,9 +55,17 @@
cpuLayout(VanillaCpuLayout.fromCpuInfo());
} else {
LOCKS = new AffinityLock[PROCESSORS];
- for (int i = 0; i < PROCESSORS; i++)
- LOCKS[i] = new AffinityLock(i, ((BASE_AFFINITY >> i) & 1) != 0, ((RESERVED_AFFINITY >> i) & 1) != 0);
- cpuLayout(new NoCpuLayout(PROCESSORS));
+ CORES = new TreeMap<Integer, AffinityLock[]>();
+ for (int i = 0; i < PROCESSORS; i++) {
+ AffinityLock al = LOCKS[i] = new AffinityLock(i, ((BASE_AFFINITY >> i) & 1) != 0, ((RESERVED_AFFINITY >> i) & 1) != 0);
+
+ final int layoutId = al.cpuId;
+ int logicalCpuId = coreForId(layoutId);
+ AffinityLock[] als = CORES.get(logicalCpuId);
+ if (als == null)
+ CORES.put(logicalCpuId, als = new AffinityLock[1]);
+ als[cpuLayout.threadId(layoutId)] = al;
+ }
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Unable to load /proc/cpuinfo", e);
@@ -77,6 +85,7 @@ public static void cpuLayout(CpuLayout cpuLayout) {
if (cpuLayout.equals(AffinityLock.cpuLayout))
return;
AffinityLock.cpuLayout = cpuLayout;
+ System.out.println("Locks= " + cpuLayout.cpus());
LOCKS = new AffinityLock[cpuLayout.cpus()];
int threads = cpuLayout.threadsPerCore();
CORES = new TreeMap<Integer, AffinityLock[]>();
@@ -178,7 +187,7 @@ private static AffinityLock acquireLock(boolean bind, int cpuId, AffinityStrateg
for (AffinityStrategy strategy : strategies) {
// consider all processors except cpu 0 which is usually used by the OS.
// if you have only one core, this library is not appropriate in any case.
- for (int i = PROCESSORS - 1; i > 0; i--) {
+ for (int i = LOCKS.length - 1; i > 0; i--) {
AffinityLock al = LOCKS[i];
if (al.canReserve() && (cpuId < 0 || strategy.matches(cpuId, al.cpuId))) {
al.assignCurrentThread(bind, false);
Please sign in to comment.
Something went wrong with that request. Please try again.