Coder Social home page Coder Social logo

archenroot / java-thread-affinity Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openhft/java-thread-affinity

0.0 1.0 0.0 7.27 MB

Java Thread Affinity library

Home Page: http://chronicle.software/products/thread-affinity/

License: GNU Lesser General Public License v3.0

Java 95.62% Makefile 0.69% C++ 2.48% C 1.21%

java-thread-affinity's Introduction

Java-Affinity

OpenHFT Java Thread Affinity library

See the affinity/src/test/java for working examples of how to use this library.

Versions

V2.2 - Latest build. V2.0.1 - Added getThreadId for the process if of the thread.

How does allocation work?

The library will read your /proc/cpuinfo if you have one or provide one and it will determine your CPU layout. If you don't have one it will assume every CPU is on one Socket.

The library looks for isolated CPUs determined by looking at the CPUs you are not running on by default. i.e. if you have 16 CPUs but 8 of them are not available for general use (as determined by the affinity of the process on startup) it will start assigning to those CPUs.

Note: if you have more than one process using this library you need to specify which CPUs the process can use otherwise it will assign the same CPUs to both processes. To control which CPUs a process can use, add -Daffinity.reserved={cpu-mark-in-hex} to the command line of the process

Note: the CPU 0 is reserved for the Operating System, it has to run somewhere.

Acquiring a CPU lock for a thread

You can acquire a lock for a CPU in the following matter

In Java 6

AffinityLock al = AffinityLock.acquireLock();
try {
     // do some work locked to a CPU.
} finally {
     al.release();
}

In Java 7 or 8

try (AffinityLock al = AffinityLock.acquireLock()) {
    // do some work while locked to a CPU.
}

You have further options such as

Acquiring a CORE lock for a thread

You can reserve a whole core. If you have hyper-threading enabled, this will use one CPU and leave it's twin CPU unused.

try (AffinityLock al = AffinityLock.acquireCore()) {
    // do some work while locked to a CPU.
}

Controlling layout

You can chose a layout relative to an existing lock.

try (final AffinityLock al = AffinityLock.acquireLock()) {
    System.out.println("Main locked");
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            try (AffinityLock al2 = al.acquireLock(AffinityStrategies.SAME_SOCKET,
                                                   AffinityStrategies.ANY)) {
                 System.out.println("Thread-0 locked");
            }
        }
    });
    t.start();
}

In this example, the library will prefer a free CPU on the same Socket as the first thread, otherwise it will pick any free CPU.

Getting the thread id.

You can get the current thread id using

int threadId = AffinitySupport.getThreadId();

determining which CPU you are running on.

You can get the current CPU being used by

int cpuId = AffinitySupport.getCpu();

Controlling the affinity more directly.

The affinity of the process on start up is

long baseAffinity = AffinityLock.BASE_AFFINITY;

The available CPU for reservation is

long reservedAffinity = AffinityLock.RESERVED_AFFINITY;

If you want to get/set the affinity directly you can do

long currentAffinity = AffinitySupport.getAffinity(); AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.

Support Material

Java Thread Affinity support group

For an article on how much difference affinity can make and how to use it http://vanillajava.blogspot.com/2013/07/micro-jitter-busy-waiting-and-binding.html

Questions and Answers

Question

I am currently working on a project related to deadlock detection in multithreaded programs in java. We are trying to run threads on different processors and thus came across your github posts regarding the same. https://github.com/peter-lawrey/Java-Thread-Affinity/wiki/Getting-started Being a beginner, I have little knowledge and thus need your assistance. We need to know how to run threads on specified cpu number and then switch threads when one is waiting.

Answer

Use :

AffinityLock.setAffinity (1L << n);

where n is the cpu you want to run the thread on.

java-thread-affinity's People

Contributors

peter-lawrey avatar hft-team-city avatar lburgazzoli avatar danielshaya avatar jozsefbartokhft avatar zelenij avatar msalvagna avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.