Java-Thread-Affinity icon indicating copy to clipboard operation
Java-Thread-Affinity copied to clipboard

Affinity.getCpu() doesn't work

Open shizukanaskytree opened this issue 8 years ago • 3 comments

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

shizukanaskytree avatar Sep 15 '17 14:09 shizukanaskytree

This library's behaviour is OS dependant. Which OS and version do you have?

On 15 Sep. 2017 15:21, "Xiaofeng Wu" [email protected] wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/peter-lawrey/Java-Thread-Affinity/issues/48, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBU8d_SydjmUt5x5DDcnclEaInsApYWks5siof2gaJpZM4PZC84 .

peter-lawrey avatar Sep 15 '17 17:09 peter-lawrey

@peter-lawrey I use MacOS, version 10.12.6.

shizukanaskytree avatar Sep 15 '17 18:09 shizukanaskytree

The Mac OSX doesn't support thread affinity. The library should do nothing on that platform.

On 15 Sep. 2017 19:32, "Xiaofeng Wu" [email protected] wrote:

@peter-lawrey https://github.com/peter-lawrey I use MacOS, version 10.12.6.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/peter-lawrey/Java-Thread-Affinity/issues/48#issuecomment-329864896, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBU8YO-QEQZoROmGZQpD_PCHfWjfXvaks5sisK6gaJpZM4PZC84 .

peter-lawrey avatar Sep 17 '17 20:09 peter-lawrey