Skip to content

Commit 2ad399f

Browse files
committed
Make affinity optional and not blow up on a Mac.
1 parent a515d2a commit 2ad399f

14 files changed

+351
-388
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,16 @@ public static BitSet getAffinity() {
153153
return AFFINITY_IMPL.getAffinity();
154154
}
155155

156+
public static void setAffinity(final BitSet affinity) {
157+
AFFINITY_IMPL.setAffinity(affinity);
158+
}
159+
156160
public static void setAffinity(int cpu) {
157161
BitSet affinity = new BitSet(Runtime.getRuntime().availableProcessors());
158162
affinity.set(cpu);
159163
setAffinity(affinity);
160164
}
161165

162-
public static void setAffinity(final BitSet affinity) {
163-
AFFINITY_IMPL.setAffinity(affinity);
164-
}
165-
166166
public static int getCpu() {
167167
return AFFINITY_IMPL.getCpu();
168168
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@
3434
* @author peter.lawrey
3535
*/
3636
public class AffinityLock implements Closeable {
37-
private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLock.class);
38-
3937
// Static fields and methods.
4038
public static final String AFFINITY_RESERVED = "affinity.reserved";
41-
4239
// TODO It seems like on virtualized platforms .availableProcessors() value can change at
4340
// TODO runtime. We should think about how to adopt to such change
4441
public static final int PROCESSORS = Runtime.getRuntime().availableProcessors();
4542
public static final BitSet BASE_AFFINITY = Affinity.getAffinity();
4643
public static final BitSet RESERVED_AFFINITY = getReservedAffinity0();
44+
private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLock.class);
4745
private static final LockInventory LOCK_INVENTORY = new LockInventory(new NoCpuLayout(PROCESSORS));
4846

4947
static {
@@ -55,6 +53,7 @@ public class AffinityLock implements Closeable {
5553
LOGGER.warn("Unable to load /proc/cpuinfo", e);
5654
}
5755
}
56+
5857
/**
5958
* Logical ID of the CPU to which this lock belongs to.
6059
*/
@@ -103,16 +102,13 @@ public static CpuLayout cpuLayout() {
103102
return LOCK_INVENTORY.getCpuLayout();
104103
}
105104

106-
private static BitSet getReservedAffinity0()
107-
{
105+
private static BitSet getReservedAffinity0() {
108106
String reservedAffinity = System.getProperty(AFFINITY_RESERVED);
109-
if (reservedAffinity == null || reservedAffinity.trim().isEmpty())
110-
{
107+
if (BASE_AFFINITY != null && (reservedAffinity == null || reservedAffinity.trim().isEmpty())) {
111108
BitSet reserverable = new BitSet(PROCESSORS);
112109
reserverable.set(0, PROCESSORS - 1, true);
113110
reserverable.and(BASE_AFFINITY);
114-
if (reserverable.isEmpty() && PROCESSORS > 1)
115-
{
111+
if (reserverable.isEmpty() && PROCESSORS > 1) {
116112
LoggerFactory.getLogger(AffinityLock.class).info("No isolated CPUs found, so assuming CPUs 1 to {} available.", (PROCESSORS - 1));
117113
reserverable = new BitSet(PROCESSORS);
118114
// make the first CPU unavailable
@@ -188,6 +184,10 @@ public static String dumpLocks() {
188184
return LOCK_INVENTORY.dumpLocks();
189185
}
190186

187+
public static void main(String[] args) {
188+
System.out.println("Test");
189+
}
190+
191191
/**
192192
* Assigning the current thread has a side effect of preventing the lock being used again until it is released.
193193
*
@@ -224,8 +224,7 @@ public void bind(boolean wholeCore) {
224224
assignedThread = Thread.currentThread();
225225
LOGGER.info("Assigning cpu {} to {}", cpuId, assignedThread);
226226
}
227-
if (cpuId >= 0)
228-
{
227+
if (cpuId >= 0) {
229228
BitSet affinity = new BitSet();
230229
affinity.set(cpuId, true);
231230
Affinity.setAffinity(affinity);
@@ -312,8 +311,4 @@ else if (base)
312311
sb.append("CPU not available");
313312
return sb.toString();
314313
}
315-
316-
public static void main(String[] args) {
317-
System.out.println("Test");
318-
}
319314
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class AffinitySupport {
2525
public static int getThreadId() {
2626
return Affinity.getThreadId();
2727
}
28+
2829
public static void setThreadId() {
2930
Affinity.setThreadId();
3031
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private URL[] getBootClassPathURLs() {
3838
Logger LOGGER = LoggerFactory.getLogger(BootClassPath.class);
3939
try {
4040
String bootClassPath = System.getProperty("sun.boot.class.path");
41-
LOGGER.trace("Boot class-path is: {}",bootClassPath);
41+
LOGGER.trace("Boot class-path is: {}", bootClassPath);
4242

4343
String pathSeparator = System.getProperty("path.separator");
4444
LOGGER.trace("Path separator is: '{}'", pathSeparator);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public final synchronized void set(CpuLayout cpuLayout) {
6161
return;
6262
}
6363
reset(cpuLayout);
64-
for (int i = 0; i < cpuLayout.cpus(); i++)
65-
{
64+
for (int i = 0; i < cpuLayout.cpus(); i++) {
6665
final boolean base = AffinityLock.BASE_AFFINITY.get(i);
6766
final boolean reservable = AffinityLock.RESERVED_AFFINITY.get(i);
6867

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ protected AffinityLock newLock(int cpuId, boolean base, boolean reservable) {
3434
}
3535
};
3636

37+
NonForkingAffinityLock(int cpuId, boolean base, boolean reservable, LockInventory lockInventory) {
38+
super(cpuId, base, reservable, lockInventory);
39+
}
40+
3741
/**
3842
* Assign any free cpu to this thread.
3943
*
@@ -114,37 +118,6 @@ public static String dumpLocks() {
114118
return LOCK_INVENTORY.dumpLocks();
115119
}
116120

117-
NonForkingAffinityLock(int cpuId, boolean base, boolean reservable, LockInventory lockInventory) {
118-
super(cpuId, base, reservable, lockInventory);
119-
}
120-
121-
@Override
122-
public void bind(boolean wholeCore) {
123-
super.bind(wholeCore);
124-
Thread thread = Thread.currentThread();
125-
changeGroupOfThread(thread, new ThreadTrackingGroup(thread.getThreadGroup(), this));
126-
}
127-
128-
@Override
129-
public void release() {
130-
Thread thread = Thread.currentThread();
131-
changeGroupOfThread(thread, thread.getThreadGroup().getParent());
132-
super.release();
133-
}
134-
135-
@Override
136-
public void started(Thread t) {
137-
wrapRunnableOfThread(t, this);
138-
}
139-
140-
@Override
141-
public void startFailed(Thread t) {
142-
}
143-
144-
@Override
145-
public void terminated(Thread t) {
146-
}
147-
148121
private static Field makeThreadFieldModifiable(String fieldName) {
149122
try {
150123
Field field = Thread.class.getDeclaredField(fieldName);
@@ -180,4 +153,31 @@ public void run() {
180153
throw new RuntimeException("Failed wrapping " + Thread.class.getName() + "'s '" + TARGET_FIELD.getName() + "' field! Reason: " + e.getMessage());
181154
}
182155
}
156+
157+
@Override
158+
public void bind(boolean wholeCore) {
159+
super.bind(wholeCore);
160+
Thread thread = Thread.currentThread();
161+
changeGroupOfThread(thread, new ThreadTrackingGroup(thread.getThreadGroup(), this));
162+
}
163+
164+
@Override
165+
public void release() {
166+
Thread thread = Thread.currentThread();
167+
changeGroupOfThread(thread, thread.getThreadGroup().getParent());
168+
super.release();
169+
}
170+
171+
@Override
172+
public void started(Thread t) {
173+
wrapRunnableOfThread(t, this);
174+
}
175+
176+
@Override
177+
public void startFailed(Thread t) {
178+
}
179+
180+
@Override
181+
public void terminated(Thread t) {
182+
}
183183
}

0 commit comments

Comments
 (0)