-
Notifications
You must be signed in to change notification settings - Fork 79
Open
Description
Code part
package net.openhft.affinity;
import java.util.BitSet;
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);
// current cpu bitmap
BitSet currentAffinity = Affinity.getAffinity();
StringBuilder s = new StringBuilder();
for( int i = 0; i < currentAffinity.length(); i++ )
{
s.append( currentAffinity.get( i ) == true ? 1: 0 );
}
System.out.println( s );
//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();
int cpuId_ = Affinity.getCpu();
System.out.println("cpuId of thread: " + cpuId_);
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);
int cpuId = Affinity.getCpu();
System.out.println("cpuId of thread: " + cpuId);
// current cpu bitmap
BitSet currentAffinity = Affinity.getAffinity();
StringBuilder s = new StringBuilder();
for( int i = 0; i < currentAffinity.length(); i++ )
{
s.append( currentAffinity.get( i ) == true ? 1: 0 );
}
System.out.println( s );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
affinityLock.release();
}
}
}
}Output
[main] INFO net.openhft.affinity.AffinityLock - Assigning cpu 7 to Thread[main,5,main]
[reader] INFO net.openhft.affinity.AffinityLock - Assigning cpu 6 to Thread[reader,5,main]
threadId of main 9973760
cpuId of main: -1
cpuId of thread: -1
threadId of one SleepRunnable 14413824
cpuId of thread: -1
[main] INFO net.openhft.affinity.LockInventory - Releasing cpu 7 from Thread[main,5,main]
The assignment of CPUs is
0: CPU not available
1: Reserved for this application
2: Reserved for this application
3: Reserved for this application
4: Reserved for this application
5: Reserved for this application
6: Thread[reader,5,main] alive=true
7: Reserved for this application
[reader] INFO net.openhft.affinity.LockInventory - Releasing cpu 6 from Thread[reader,5,main]
Process finished with exit code 0
Metadata
Metadata
Assignees
Labels
No labels