Skip to content

Commit c0959fb

Browse files
author
Stan Shkodnik
committed
Fixed sched_getaffinity/sched_setaffinity to work on RH
1 parent efddeeb commit c0959fb

File tree

10 files changed

+476
-14
lines changed

10 files changed

+476
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
*.iml
99

1010
target/
11+
.idea
1112

affinity-test/src/test/java/net/openhft/osgi/osgi/OSGiBundleTest.java renamed to affinity-test/src/test/java/net/openhft/affinity/osgi/OSGiBundleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
//@Ignore
3737
@RunWith(PaxExam.class)
38-
public class OSGiBundleTest extends OSGiTestBase {
38+
public class OSGiBundleTest extends net.openhft.affinity.osgi.OSGiTestBase {
3939
@Inject
4040
BundleContext context;
4141

@@ -77,7 +77,7 @@ public void checkBundleExports() {
7777
final String exports = bundle.getHeaders().get("Export-Package");
7878
final String[] packages = exports.split(",");
7979

80-
assertEquals(2, packages.length);
80+
assertTrue(packages.length >= 2);
8181
assertTrue(packages[0].startsWith("net.openhft.affinity;")
8282
|| packages[0].startsWith("net.openhft.affinity.impl;"));
8383
assertTrue(packages[1].startsWith("net.openhft.affinity;")

affinity-test/src/test/java/net/openhft/osgi/osgi/OSGiTestBase.java renamed to affinity-test/src/test/java/net/openhft/affinity/osgi/OSGiTestBase.java

File renamed without changes.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
* @author peter.lawrey
3535
*/
3636
public class AffinityLock {
37+
private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLock.class);
38+
3739
// Static fields and methods.
3840
public static final String AFFINITY_RESERVED = "affinity.reserved";
3941

@@ -42,7 +44,6 @@ public class AffinityLock {
4244
public static final int PROCESSORS = Runtime.getRuntime().availableProcessors();
4345
public static final long BASE_AFFINITY = AffinitySupport.getAffinity();
4446
public static final long RESERVED_AFFINITY = getReservedAffinity0();
45-
private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLock.class);
4647
private static final LockInventory LOCK_INVENTORY = new LockInventory(new NoCpuLayout(PROCESSORS));
4748

4849
static {
@@ -289,4 +290,9 @@ else if (base)
289290
sb.append("CPU not available");
290291
return sb.toString();
291292
}
293+
294+
public static void main(String[] args) {
295+
296+
System.out.println("Test");
297+
}
292298
}

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919
package net.openhft.affinity;
2020

21-
import net.openhft.affinity.impl.NullAffinity;
22-
import net.openhft.affinity.impl.OSXJNAAffinity;
23-
import net.openhft.affinity.impl.PosixJNAAffinity;
24-
import net.openhft.affinity.impl.WindowsJNAAffinity;
21+
import net.openhft.affinity.impl.*;
2522
import org.jetbrains.annotations.NotNull;
2623
import org.slf4j.Logger;
2724
import org.slf4j.LoggerFactory;
@@ -47,9 +44,17 @@ public enum AffinitySupport {
4744
if (osName.contains("Win") && isWindowsJNAAffinityUsable()) {
4845
LOGGER.trace("Using Windows JNA-based affinity control implementation");
4946
AFFINITY_IMPL = WindowsJNAAffinity.INSTANCE;
50-
} else if (osName.contains("x") && isPosixJNAAffinityUsable()) {
51-
LOGGER.trace("Using Posix JNA-based affinity control implementation");
52-
AFFINITY_IMPL = PosixJNAAffinity.INSTANCE;
47+
} else if (osName.contains("x")) {
48+
if(osName.startsWith("Linux") && isLinuxJNAAffinityUsable()) {
49+
LOGGER.trace("Using Linux JNA-based affinity control implementation");
50+
AFFINITY_IMPL = LinuxJNAAffinity.INSTANCE;
51+
} else if(isPosixJNAAffinityUsable()) {
52+
LOGGER.trace("Using Posix JNA-based affinity control implementation");
53+
AFFINITY_IMPL = PosixJNAAffinity.INSTANCE;
54+
} else {
55+
LOGGER.info("Using dummy affinity control implementation");
56+
AFFINITY_IMPL = NullAffinity.INSTANCE;
57+
}
5358
} else if (osName.contains("Mac") && isMacJNAAffinityUsable()) {
5459
LOGGER.trace("Using MAC OSX JNA-based thread id implementation");
5560
AFFINITY_IMPL = OSXJNAAffinity.INSTANCE;
@@ -59,6 +64,10 @@ public enum AffinitySupport {
5964
}
6065
}
6166

67+
public static IAffinity getAffinityImpl() {
68+
return AFFINITY_IMPL;
69+
}
70+
6271
private static boolean isWindowsJNAAffinityUsable() {
6372
if (isJNAAvailable()) {
6473
try {
@@ -87,6 +96,20 @@ private static boolean isPosixJNAAffinityUsable() {
8796
}
8897
}
8998

99+
private static boolean isLinuxJNAAffinityUsable() {
100+
if (isJNAAvailable()) {
101+
try {
102+
return LinuxJNAAffinity.LOADED;
103+
} catch (Throwable t) {
104+
logThrowable(t, "Linux JNA-based affinity not usable because it failed to load!");
105+
return false;
106+
}
107+
} else {
108+
LOGGER.warn("Linux JNA-based affinity not usable due to JNA not being available!");
109+
return false;
110+
}
111+
}
112+
90113
private static boolean isMacJNAAffinityUsable() {
91114
if (isJNAAvailable()) {
92115
return true;

0 commit comments

Comments
 (0)