Linux – Advantages and disadvantages of CPU affinity

Advantages and disadvantages of CPU affinity… here is a solution to the problem.

Advantages and disadvantages of CPU affinity

Let’s say I have a multithreaded application (say about 40 threads) running on a multiprocessor system (say 8 cores), using Linux as the operating system, where the different threads are more essentially LWPs (lightweight processes) scheduled by the kernel

What are the advantages/disadvantages of using CPU affinity? Does CPU affinity help by localizing threads to a subset of cores, minimizing cache sharing/misses?

Solution

If you use strict affinity, a particular thread must be running on that processor (or processor group). If you have many threads working completely independently, and they work on larger blocks of memory than a few kilobytes, you are unlikely to benefit much from running on one particular core — because it is likely that other threads running on that particular CPU will discard any L1 cache, and L2 cache will most likely be discarded. Which is more important for performance – cached content or “run faster”? Are some CPUs always idle, or are the CPU loads per core 100%?

However, only you know (until you tell us) what your thread is doing. How big is the “working set” they touch each time they run (how much memory – code and data)? How long does each thread run at runtime? What is the interaction with other threads? Do other threads use data shared with “this” thread? How much to share and what is the mode of sharing?

In the end, the final answer is “What makes it run faster?” ” – Only by having good (realistic) benchmarks and trying different possible options can you find out. Even if you give us every line of code, each thread’s runtime measurement, etc., we can only make more or less complex guesses – until these have been tried and tested (using different usage patterns), it’s almost impossible to know.

In general, I suggest that having a lot of threads either indicates that each thread is not very busy (CPU aspect) or you are “doing it wrong”… More threads are not better, everything goes all out – in this case it is better to use fewer threads, because they will only fight each other.

Related Problems and Solutions