-
Notifications
You must be signed in to change notification settings - Fork 79
Open
Description
I try to set affinity to one thread, however,
If you want to get/set the affinity directly you can do
long currentAffinity = AffinitySupport.getAffinity();
AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.I get some errors about data type.
Error:(21, 52) java: incompatible types: java.util.BitSet cannot be converted to long
Error:(22, 17) java: no suitable method found for setAffinity(long)
method net.openhft.affinity.Affinity.setAffinity(java.util.BitSet) is not applicable
(argument mismatch; long cannot be converted to java.util.BitSet)
method net.openhft.affinity.Affinity.setAffinity(int) is not applicable
(argument mismatch; possible lossy conversion from long to int)
The code
package net.openhft.affinity;
import static net.openhft.affinity.AffinityStrategies.*;
//import static net.openhft.affinity.Affinity.*;
/**
* @author peter.lawrey
*/
public final class AffinityLockBindMainTest {
private AffinityLockBindMainTest() {
throw new InstantiationError( "Must not instantiate this class" );
}
public static void main(String... args) throws InterruptedException {
AffinityLock al = AffinityLock.acquireLock();
int threadId = AffinitySupport.getThreadId();
System.out.println("threadId of main "+ threadId);
int cpuId = Affinity.getCpu();
System.out.println("cpuId of main: " + cpuId);
long currentAffinity = Affinity.getAffinity();
Affinity.setAffinity(1L << 5); // lock to CPU 5.
try {
// find a cpu on a different socket, otherwise a different core.
AffinityLock readerLock = al.acquireLock(DIFFERENT_SOCKET, DIFFERENT_CORE);
new Thread(new SleepRunnable(readerLock, false), "reader").start();
// find a cpu on the same core, or the same socket, or any free cpu.
//AffinityLock writerLock = readerLock.acquireLock(SAME_CORE, SAME_SOCKET, ANY);
//new Thread(new SleepRunnable(writerLock, false), "writer").start();
Thread.sleep(200);
} finally {
al.release();
}
// allocate a whole core to the engine so it doesn't have to compete for resources.
//al = AffinityLock.acquireCore(false);
//new Thread(new SleepRunnable(al, true), "engine").start();
Thread.sleep(200);
System.out.println("\nThe assignment of CPUs is\n" + AffinityLock.dumpLocks());
}
static class SleepRunnable implements Runnable {
private final AffinityLock affinityLock;
private final boolean wholeCore;
SleepRunnable(AffinityLock affinityLock, boolean wholeCore) {
this.affinityLock = affinityLock;
this.wholeCore = wholeCore;
}
public void run() {
affinityLock.bind(wholeCore);
int threadId = AffinitySupport.getThreadId();
System.out.println("threadId of one SleepRunnable "+ threadId);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
affinityLock.release();
}
}
}
}Metadata
Metadata
Assignees
Labels
No labels