[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.

Welcome to the Slackware Documentation Project

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
howtos:hardware:processor_affinity [2012/09/26 01:18 (UTC)] – moved from the general howtos namespace mfillpothowtos:hardware:processor_affinity [2013/01/13 18:12 (UTC)] (current) – [Taskset] hazel
Line 1: Line 1:
 +<--Reviewed 20130113 by hazel -->
 ====== Multi CPU performance tuning ====== ====== Multi CPU performance tuning ======
  
Line 12: Line 13:
 For the sake of article clarity, let's talk about multi-core CPU's, multi-CPU systems, hyperthreading CPU's and such as "//a multi CPU system//". For the sake of article clarity, let's talk about multi-core CPU's, multi-CPU systems, hyperthreading CPU's and such as "//a multi CPU system//".
  
-In a multi-CPU system, if a process (or a series of process threads) is started multiple times, the kernel process scheduler has a tendency to restart those processes or threads on the same CPU as where the process thread ran before. This is potentially more efficient than selecting a random CPU core, because some of the process's state may have been preserved in that CPU's cache. CPU affinity is a natural behaviour of the kernel process scheduler.+In a multi-CPU system, if a process (or a series of process threads) is started multiple times, the kernel process scheduler has a tendency to restart those processes or threads on the same CPU that ran the process thread before. This is potentially more efficient than selecting a random CPU core, because some of the process's state may have been preserved in that CPU's cache. CPU affinity is a natural behaviour of the kernel process scheduler.
  
-Because the scheduler will do its best to keep processes running on the same CPU (as long as this is beneficial to overall performance) you usually do not have to interfere. However, some cases warrant pinnnig a process to a single (or a pre-defined subset of) CPU. Let's look at two examplesthe second which  will be discussed in more detail.+Because the scheduler will do its best to keep processes running on the same CPU (as long as this is beneficial to overall performance) you usually do not have to interfere. However, some cases warrant pinning a process to a single CPU or a predefined subset. Let's look at two examplesthe second will be discussed in more detail.
  
-  - Commercial database vendors like [[http://www.oracle.com/products/database/overview/|Oracle DB]] or [[http://www-01.ibm.com/software/data/db2/|IBM DB2]] want you to pay per CPU. Typically, database servers have a massive amount of CPU cores and you want to be able to decide how much to pay for your database (more performance would meanmore cost)+  - Commercial database vendors like [[http://www.oracle.com/products/database/overview/|Oracle DB]] or [[http://www-01.ibm.com/software/data/db2/|IBM DB2]] want you to pay per CPU. Typically, database servers have a massive amount of CPU cores and you want to be able to decide how much to pay for your database (more performance would mean more cost)
   - One process on a busy server is eating so many of your multi-CPU processor cycles that other programs' performance starts to suffer. Limiting your runaway process to a single CPU will free up resources for your other programs.   - One process on a busy server is eating so many of your multi-CPU processor cycles that other programs' performance starts to suffer. Limiting your runaway process to a single CPU will free up resources for your other programs.
  
Line 27: Line 28:
  
  
-Slackware has a tool for setting the processor affinity for a certain task or process. It is called "''taskset''" and is part of the ''util-linux'' package. Run "''man 1 taskset''" if you want to know more about its purpose and commandline syntax.+Slackware has a tool for setting the processor affinity for a certain task or process. It is called "''taskset''" and is part of the ''util-linux'' package. Run "''man 1 taskset''" if you want to know more about its purpose and command line syntax.
  
 <note>The ''taskset'' command is used to set or retrieve the CPU affinity of a running process given its PID, or to launch a new COMMAND with a given CPU affinity.</note> <note>The ''taskset'' command is used to set or retrieve the CPU affinity of a running process given its PID, or to launch a new COMMAND with a given CPU affinity.</note>
Line 37: Line 38:
 0xffffffff means all processors (#0 through #31) 0xffffffff means all processors (#0 through #31)
 </code> </code>
-Luckily you can use human-readable numbers instead of a bitmask if you add the "-c" commandline parameter.+Luckily you can use human-readable numbers instead of a bitmask if you add the "-c" command line parameter.
  
 Examples: Examples:
Line 46: Line 47:
 </code> Confused by hexadecimal numbers? Then use the human-readable version: <code> </code> Confused by hexadecimal numbers? Then use the human-readable version: <code>
 $ taskset -cp 1 1337 $ taskset -cp 1 1337
-</code> You may notice the strange order of commandline options and arguments; first come all the options and then follow the argument values in pre-defined order... ''man taskset(1)'' is your friend. +</code> You may notice the strange order of command line options and arguments; first come all the options and then follow the argument values in pre-defined order... ''man taskset(1)'' is your friend. 
-  * To start a new process with a fixed CPU affinity, you omit the "-p" switch and append a commandline, like this real-world example of starting a Minecraft server: <code>+  * To start a new process with a fixed CPU affinity, you omit the "-p" switch and append a command line, like this real-world example of starting a Minecraft server: <code>
 $ taskset -c 3 java -Xmx1024M -jar minecraft_server.jar nogui $ taskset -c 3 java -Xmx1024M -jar minecraft_server.jar nogui
 </code> which ties the Java process to the 4th CPU in your computer. The effect of this will be //immediately// obvious. </code> which ties the Java process to the 4th CPU in your computer. The effect of this will be //immediately// obvious.
  
-While you can pin a running process task to a sub-set of your available CPU's, that does not mean that the process and its threads will all of a sudden be limited to those CPU's you dictated. Rather, the kernel scheduler will now start preferring your appointed CPU instead of the CPU cores your processes may be currently running on. It can take quite a while (process threads have to stop and be started again) to see an obvious effect of using the ''taskset'' command on a running task. That is why it may be more effective to use ''taskset'' to start your program with the desired processor affinity. +While you can pin a running process task to a sub-set of your available CPU's, that does not mean that the process and its threads will suddenly be limited to those CPU's you dictated. Rather, the kernel scheduler will now start preferring your appointed CPU instead of the CPU cores your processes may be currently running on. It can take quite a while (process threads have to stop and be started again) to see an obvious effect of using the ''taskset'' command on a running task. That is why it may be more effective to use ''taskset'' to start your program with the desired processor affinity. 
  
 Try assigning one of your CPU intensive programs to one CPU, and use ''top'' or ''htop'' to observe the non-uniform CPU load. For the ''top'' program, you have to press <key>1</key> in order to see processor statistics per CPU core. The ''htop'' command allows you to set processor afffinity and show you a process list sorted by CPU. Try assigning one of your CPU intensive programs to one CPU, and use ''top'' or ''htop'' to observe the non-uniform CPU load. For the ''top'' program, you have to press <key>1</key> in order to see processor statistics per CPU core. The ''htop'' command allows you to set processor afffinity and show you a process list sorted by CPU.
 howtos:hardware:processor_affinity ()