如何将一个进程(线程)绑定到一个固定的CPU核上?——004

来源:互联网 发布:4k电视直播软件 编辑:程序博客网 时间:2024/05/20 18:50

http://blog.csdn.net/sangyongjia/article/details/50725734


As multi-core CPUs become increasingly popular on server-grade hardware as well as end-user desktop PCs or laptops, there have been growing efforts in the community (e.g., in terms of programming models, compiler or operating system support) towards developing applications optimized for multi-core architecture.

One operating system (OS) support often exploited to run performance-critical applications on multi-core processors is so-called "processor affinity" or "CPU pinning". This is an OS-specific feature that "binds" a running process or program to particular CPU core(s).

Binding a program to specific CPU cores can be beneficial in several scenarios. For example, when an application with highly cache-bound workload runs together with other CPU-intensive jobs, pinning the application to a specific CPU would reduce CPU cache misses. Also, when two processes communicate via shared memory intensively, scheduling both processes on the cores in the same NUMA domain would speed up their performance.

In this tutorial, I will describe how to run a program or process on specific CPU cores on Linux.


-------------------------------------------------分割线-----------------------------------------------------------------------------------------

google一圈,能够实现的方式有两种:第一种:linux的shell命令行方式,命令名字为taskset。第二种就是代码实现级别的了,pthread_setaffinity_np和sched_setaffinity函数接口。
        第一种方式我已经验证过了,确实可行。同时验证了我心中的疑问:如果将某个线程绑定到某个物理核上之后,在此线程运行结束前,会不会有别的线程被调度到此物理核上执行?  写了一个死循环验证了下,发现绑定之后是不会调度别的线程在此核上运行的!(肉眼观察的,时不时观察下,没发现别的线程在此核上执行;对比了下没有绑定的情况,会发现过段时间此线程就会被调度到别的核心上执行)
        此种方式有个问题,就是只有线程运行起来后才会被绑定到某个核上,不够及时。
        具体的方式为:
            1.首先根据系统的差别运行如下安装命令:
                  sudo apt-get install util-linux        (Debian,Ubuntu or Linux Mint)
                  sudo yum install util-linux             (Fedora,CentOS or RHEL)
            2.相关命令的使用:
                2.1 使用命令 taskset  -p  <PID> 来获得此Process的 CPU affinity。
                       eg: taskset -p 2915  ------> pid 2915's current affinity mask:ff; ff=="1111  1111",没一个1代表一个核,共8个核,能用的核数也为8个核。
                2.2 使用命令 taskset -cp  <PID> 可获得数字形式的CPU affinity。   
                       eg: taskset -cp 2915 ------> pid 2915's current affinity list: 0--7。
                接下来为将进程pin到某个核上的命令;
                2.3 taskset -p <COREMASK>  <PID>
                       eg:taskset -p 0x11 9030  ------>pid 9030's current affinity mask: ff , pid 9030's new affinity mask: 11 。意思就是将此进程绑定到了CPU core 0 and 4。
                2.4 taskset -cp <CORE-LIST>  <PID>   
                        eg:taskset -cp 0,4  9030  ------>the same as below.
                         With "-c" option, you can specify a list of numeric CPU core IDs separated by commas, or even include ranges (e.g., 0,2,5,6-10).
                2.5 taskset <COREMASK>  <EXECUTABLE>
                        eg: taskset 0x1 xxxx   ----->"xxxx" represented the name of one program.
另外:参考文章最后的位置说到,绑定到此物理核之后,别的进程(线程)还可以调度到此核上执行,但是没说绑定的这个线程没执行完之前是否会被别的线程挤掉。根据我的观察是不会被挤掉,这我在文章的开头也有提到。

第二种方式还有待编码验证。
        “待实践后,再补充此部分

具体参考:http://xmodulo.com/run-program-process-specific-cpu-cores-linux.html 需要使用代理google之,要不然就被墙了,打不开。
也可以在linux下使用命令:man taskset。查看帮助。



阅读全文
0 0