How to isolate a cpu core from other processes

sivaprakash16

New Member
Joined
Sep 29, 2023
Messages
1
Reaction score
0
Credits
15
I have two threads in my process. According to the requirement, these two threads should run simultaneously without any disturbance. So, I am setting affinity to each thread with 2 different cpus. But they are getting disturbed when I create a new process and thread with same thread priority and affinity.
Tried isolcpus kernel parameter set for particular cpu cores. But it is deprecated.
Not able to use cpuset. I am quiet confused how to use it.
Tried with taskset command. It is not restricting other processes to use the cpu.

How to isolate particular cpu core from other processes, and run only 2 threads from my process?
 


From ChatGPT


Isolating CPU cores from other processes and ensuring that only your two threads run on specific cores requires careful system configuration. Since you mentioned that the isolcpus kernel parameter is deprecated, you can try using the cpuset feature along with cgroups to achieve this. Here are the steps to isolate CPU cores for your threads:

  1. Install and Configure Cgroups: Ensure that the cgroup and cgroupfs-mount packages are installed on your system. You may also need to enable cgroups in your kernel if it's not already enabled.
  2. Create a Cgroup: Create a new cgroup for your threads. For example, you can create a cgroup named "my_threads" using the following command:

    sudo cgcreate -g cpu,cpuacct:/my_threads

  3. Assign CPUs to the Cgroup: Assign the CPU cores you want to isolate to the "my_threads" cgroup. Replace <cpu_list> with the list of CPU cores you want to use (e.g., "0,1" for cores 0 and 1).

    sudo cgset -r cpuset.cpus=<cpu_list> my_threads

  4. Launch Your Process and Threads: Launch your process and threads inside the "my_threads" cgroup. You can do this by specifying the cgroup when running your process or using the cgexec command. For example:

    sudo cgexec -g cpu,cpuacct:/my_threads ./your_program
    This will ensure that your program and its threads run only on the CPU cores specified in <cpu_list>.

  5. Verify Affinity: Within your program, you can also explicitly set CPU affinity for your threads to the CPU cores within your specified list using functions like pthread_setaffinity_np (in the case of POSIX threads) or platform-specific API calls.
By following these steps, you should be able to isolate specific CPU cores for your threads and prevent other processes from using those cores. Please note that cgroups configuration may vary depending on your Linux distribution, so consult your distribution's documentation for specific details. Additionally, ensure that your system's kernel supports cgroups and that the necessary modules are loaded.
 
To isolate a specific CPU core from other processes and ensure that only your two threads run on it, you have a few options. Let's go through them:

  • cpuset (Preferred method):
This allows you to partition the system's CPUs and memory nodes into groups and assign tasks (in your case, threads) to those groups.
To do this:
mkdir /sys/fs/cgroup/cpuset/MyCpuSet echo 2 > /sys/fs/cgroup/cpuset/MyCpuSet/cpuset.cpus echo <your_process_pid> > /sys/fs/cgroup/cpuset/MyCpuSet/tasks
  • This will create a cpuset named MyCpuSet and assign CPU core 2 to it. Replace <your_process_pid> with the PID of your process.
  • Taskset:
    You mentioned that taskset didn't work for you, but it's worth double-checking. You need to ensure that you set the affinity for both threads:

taskset -cp <CPU_ID> <your_process_pid>

  • Replace <CPU_ID> with the ID of the CPU you want to use, and <your_process_pid> with the PID of your process.
  • Numactl:
    Numactl is a tool that allows you to control the NUMA (Non-Uniform Memory Access) policy for processes or shared memory areas. You can use it to bind your process to specific CPUs.
numactl --physcpubind=<CPU_ID> --membind=<NODE_ID> <your_command>
  • Replace <CPU_ID> with the ID of the CPU you want to use and <NODE_ID> with the NUMA node, which should typically be 0 unless you have specific requirements.
  • Isolating CPUs at Boot (Kernel Command Line):
    Although you mentioned that isolcpus is deprecated, it's still a valid option. You can edit your boot loader configuration (usually /etc/default/grub for Grub) and add the isolcpus parameter:
GRUB_CMDLINE_LINUX_DEFAULT="... isolcpus=2,3"
  • This isolates CPUs 2 and 3 from the scheduler.
Remember to replace <CPU_ID> and <your_process_pid> with actual values.

Please ensure that you have the necessary privileges to perform these operations (e.g., you might need to be root or have sudo access). Also, be careful with these settings as they can have a significant impact on system performance and behavior. Always test thoroughly before applying changes in a production environment.
 

Members online


Top