taskset设置CPU亲和

7520阅读 0评论2013-08-07 zooyo
分类:LINUX

在grup启动时传给内核参数: isolcpus=2-15, 这里表示隔离第3到16个CPU, Linux程序只跑在第一和第二个CPU上, 空闲的CPU我们可以指定跑进程了.
这是内核文档里对内核参数的解释:
  1. isolcpus= [KNL,SMP] Isolate CPUs from the general scheduler.
  2.                         Format:
  3.                         <cpu number>,...,<cpu number>
  4.                         or
  5.                         <cpu number>-<cpu number>
  6.                         (must be a positive range in ascending order)
  7.                         or a mixture
  8.                         <cpu number>,...,<cpu number>-<cpu number>

  9.                         This option can be used to specify one or more CPUs
  10.                         to isolate from the general SMP balancing and scheduling
  11.                         algorithms. You can move a process onto or off an
  12.                         "isolated" CPU via the CPU affinity syscalls or cpuset.
  13.                         <cpu number> begins at 0 and the maximum value is
  14.                         "number of CPUs in system - 1".

  15.                         This option is the preferred way to isolate CPUs. The
  16.                         alternative -- manually setting the CPU mask of all
  17.                         tasks in the system -- can cause problems and
  18.                         suboptimal load balancer performance.

然后我们利用taskset程序来设置运行的程序跑在哪个CPU上, 本例是16个CPU, 用mask表示:
  1. Anshion> ./test.sh &
  2. [1] 684
  3. Anshion> taskset -p 0x8000 684
  4. pid 684's current affinity mask: 3
  5. pid 684's new affinity mask: 8000

这样在top中就可以看到, 这个进程跑在15号CPU上了, top命令运行按f, 再按j, 可以显示命令运行的cpu.然后用W保存配置.


  1. taskset -c 15 ./test.sh &
这样可以直接调用程序时指定跑在哪个CPU上.







上一篇:求一个整数是否是2的n次方
下一篇:使netmap pkt-gen 工作在 FreeBSD 9.1