peter-lawrey / Java-Thread-Affinity

Control thread affinity for Java
379 stars 77 forks source link

Thread Migration Behaviour #36

Closed rteshnizi closed 4 years ago

rteshnizi commented 11 years ago

Hi!

i wanted to use thread affinity in order to force Java threads migrate actually migrate from one core to another. Does this code capable of such thing? I mean when I acquire a lock for a thread, does it really make the tread to move (migrate) from one core to another? or when a thread have a lock, the migration will happen at the discretion of VM (migration will happen when VM decides to)?

I'm looking forward to your answer :)

peter-lawrey commented 11 years ago

Hi,

You could use the library to force the thread to change cores. This could be used to trigger the lag and slow down which occurs when you switch cores. Apart from deliberately adding delays, I can't think of a good reason to do this.

Peter.

rteshnizi commented 11 years ago

Thank you for the reply Peter.

Actually I'm working on my Final Bachelor project. As a part of my project I must show and verify the result that are shown in the following IEEE paper: Understanding the cost of thread migration for multi-threaded Java applications running on a multicore platform

In this paper it has been mentioned that by using JNI, the Java program will use the system call sched_setaffinity() to make a thread migrate from one core to another. But when I used your library for this means, I did not get the results which were shown in the paper.

I wanted to make sure that by calling AffinityLock.acquireLock(), a thread will actually go from one core to another. Is it possible for me check the running threads on each cpu core?

BTW, I really appreciate your helps and supports and I will cite your project in my report.

peter-lawrey commented 11 years ago

If you do getThreadAffinity() you should be able to check what it is set to. There is another posix call to check which the cpu id is, I can't remember if the JNA version supports it. The cost of context switching between cores depends heavily on CPU and the program involved. If you switch a heavy thread between sockets the cost will be much higher than a trivial thread between cores on the same socket.

Note: just sleeping for 10 ms (even if you don't switch cores) is enough to slow down a process significantly (2-5x) so perhaps you can try this to see if you measure a slow down.