Skip to content

Commit a04717e

Browse files
committed
Try 64-bit cpu_set first.
1 parent 14220db commit a04717e

1 file changed

Lines changed: 21 additions & 25 deletions

File tree

affinity/src/main/java/net/openhft/affinity/impl/PosixJNAAffinity.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,10 @@ public enum PosixJNAAffinity implements IAffinity {
4040
public static final boolean LOADED;
4141
private static final Logger LOGGER = Logger.getLogger(PosixJNAAffinity.class.getName());
4242
private static final String LIBRARY_NAME = Platform.isWindows() ? "msvcrt" : "c";
43-
private static final int PROCS = Runtime.getRuntime().availableProcessors();
4443

4544
@Override
4645
public long getAffinity() {
4746
final CLibrary lib = CLibrary.INSTANCE;
48-
if (PROCS <= 32) {
49-
// TODO where are systems with 64+ cores...
50-
final IntByReference cpuset = new IntByReference(0);
51-
try {
52-
final int ret = lib.sched_getaffinity(0, Integer.SIZE / 8, cpuset);
53-
if (ret < 0)
54-
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + cpuset + ") ) return " + ret);
55-
return cpuset.getValue();
56-
} catch (LastErrorException e) {
57-
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + cpuset + ") ) errorNo=" + e.getErrorCode(), e);
58-
}
59-
}
6047
// TODO where are systems with 64+ cores...
6148
final LongByReference cpuset = new LongByReference(0L);
6249
try {
@@ -65,31 +52,40 @@ public long getAffinity() {
6552
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + cpuset + ") ) return " + ret);
6653
return cpuset.getValue();
6754
} catch (LastErrorException e) {
68-
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + cpuset + ") ) errorNo=" + e.getErrorCode(), e);
55+
if (e.getErrorCode() != 22)
56+
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + cpuset + ") ) errorNo=" + e.getErrorCode(), e);
57+
}
58+
final IntByReference cpuset32 = new IntByReference(0);
59+
try {
60+
final int ret = lib.sched_getaffinity(0, Integer.SIZE / 8, cpuset32);
61+
if (ret < 0)
62+
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + cpuset32 + ") ) return " + ret);
63+
return cpuset32.getValue();
64+
} catch (LastErrorException e) {
65+
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + cpuset32 + ") ) errorNo=" + e.getErrorCode(), e);
6966
}
7067
}
7168

7269
@Override
7370
public void setAffinity(final long affinity) {
7471
final CLibrary lib = CLibrary.INSTANCE;
75-
if (PROCS <= 32)
76-
try {
77-
final int ret = lib.sched_setaffinity(0, Integer.SIZE / 8, new IntByReference((int) affinity));
78-
if (ret < 0) {
79-
throw new IllegalStateException("sched_setaffinity((" + Integer.SIZE / 8 + ") , &(" + affinity + ") ) return " + ret);
80-
}
81-
} catch (LastErrorException e) {
82-
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
83-
}
84-
8572
try {
8673
//fixme: where are systems with more then 64 cores...
8774
final int ret = lib.sched_setaffinity(0, Long.SIZE / 8, new LongByReference(affinity));
8875
if (ret < 0) {
8976
throw new IllegalStateException("sched_setaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) return " + ret);
9077
}
9178
} catch (LastErrorException e) {
92-
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
79+
if (e.getErrorCode() != 22)
80+
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
81+
}
82+
try {
83+
final int ret = lib.sched_setaffinity(0, Integer.SIZE / 8, new IntByReference((int) affinity));
84+
if (ret < 0) {
85+
throw new IllegalStateException("sched_setaffinity((" + Integer.SIZE / 8 + ") , &(" + affinity + ") ) return " + ret);
86+
}
87+
} catch (LastErrorException e) {
88+
throw new IllegalStateException("sched_getaffinity((" + Integer.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
9389
}
9490
}
9591

0 commit comments

Comments
 (0)