[LINUX] How to make a process thread run only on a specific CPU core

In a multi-core CPU, the CPU core on which the process thread runs depends on the OS scheduler, and the context may move to another CPU core depending on the situation. In Linux, you can set CPU affinity to prevent such movements and allow process threads to run only on specific CPU cores (mainly due to performance issues).

What is CPU affinity?

A collection of logical CPUs that the thread is allowed to execute. CPU affinity is represented by a bitmask.

As an example, CPU0 is 0x00000001, CPU1 is 0x00000002, and the set of CPU0,1,3 is 0x0000000b.

You can check which CPU core CPU0 is actually by using cat / proc / cpuinfo.

How to set CPU affinity by command

You can set the CPU affinity of a process with the command taskset.

# taskset -p mask pid

For mask, specify the CPU affinity, and for pid, specify the process you want to change.

Use the -a option to change for all threads in the process pid.

# taskset -a -p mask pid

How to run only a specific process on a CPU processor

Only that process is allowed to run on that CPU, and all other processes are unallowed.

# ps -e -o pid= | xargs -n 1 taskset -p 0xFFFFFFFE
# taskset -p 0x00000001 PID

How to set CPU affinity programmatically

To set CPU affinity programmatically, use the function sched_setaffinity ().

#define _GNU_SOURCE             /* feature_test_See macros*/
#include <sched.h>

void
func(void)
{
  cpu_set_t mask;
  int ret;

  //Turn off all masks
  CPU_ZERO(&mask);

  // CPU_AFFINITY_Allow running on the core of MASK
  CPU_SET(CPU_AFFINITY_MASK, &mask);

  //Set CPU affinity
  ret = sched_setaffinity(0, sizeof(mask), &mask);
  if (ret != 0) {
    // error
  }
}

Recommended Posts

How to make a process thread run only on a specific CPU core
How to run Django on IIS on a Windows server
How to run a trained transformer model locally on CloudTPU
How to make a multiplayer online action game on Slack
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to run matplotlib on heroku
How to make a deadman's switch
[Blender] How to make a Blender plugin
How to make a crawler --Basic
[C language] How to create, avoid, and make a zombie process
How to make a .dylib library from a .a library on OSX (El Capitan)
[Python] How to make a class iterable
How to make a Backtrader custom indicator
How to test on a Django-authenticated page
How to make a Pelican site map
How to run Cython on OSX Memo
How to run a Maya Python script
How to make only one data register on the Django admin screen
How to make a dialogue system dedicated to beginners
A memorandum to make WebDAV only with nginx
How to make multiple kernels selectable on Jupyter
How to make a dictionary with a hierarchical structure.
How to run MeCab on Ubuntu 18.04 LTS Python
LINUX: How to make arrow keys correspond to 2,4,6,8 on a notebook without a numeric keypad
How to make a QGIS plugin (package generation)
Make slackbot only react on specific channels (Python slackbot)
I read "How to make a hacking lab"
How to live a decent life on 2017 Windows
How to run a Django application on a Docker container (development and production environment)
How to count numbers in a specific range
How to set CPU affinity for process threads
How to apply markers only to specific data in matplotlib
How to deploy a Django application on Alibaba Cloud
How to install Linux on a 32bit UEFI PC
How to make a shooting game with toio (Part 1)
A memorandum on how to use keras.preprocessing.image in Keras
How to build a Django (python) environment on docker
How to run Self bot on Discord.py [Easy vandalism! ]
How to make a Python package using VS Code
How to run setUp only once in python unittest
Basics of PyTorch (2) -How to make a neural network-
How to build a Python environment on amazon linux 2
How to insert a specific process at the start and end of spider with scrapy
How to use GitHub on a multi-person server without a password
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
[Python] How to make a list of character strings character by character
How to build a new python virtual environment on Ubuntu
How to run a Python file at a Windows 10 command prompt
How to run a Python program from within a shell script
Don't lose to Ruby! How to run Python (Django) on Heroku
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit Part 2-
How to transpose a 2D array using only python [Note]
How to mount a Windows 10 directory on Ubuntu-Server 20.04 on VMware Workstation 15
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit edition-
How to make a Python package (written for an intern)
A note on how to load a virtual environment in PyCharm
How to make a simple Flappy Bird game with pygame
How to extract other than a specific index with Numpy
How to register a package on PyPI (as of September 2017)