taskset 查看或设置 Linux 进程的CPU关联

发布时间: 更新时间: 总字数:366 阅读时间:1m 作者: IP属地: 分享 复制网址

Linux taskset 命令查看或设置进程的CPU关联

help

taskset -h
Usage: taskset [options] [mask | cpu-list] [pid|cmd [args...]]

Options:
 -a, --all-tasks         operate on all the tasks (threads) for a given pid
 -p, --pid               operate on existing given pid
 -c, --cpu-list          display and specify cpus in list format
 -h, --help              display this help
 -V, --version           output version information

The default behavior is to run a new command:
    taskset 03 sshd -b 1024
You can retrieve the mask of an existing task:
    taskset -p 700
Or set it:
    taskset -p 03 700
List format uses a comma-separated list instead of a mask:
    taskset -pc 0,3,7-11 700
Ranges in list format can take a stride argument:
    e.g. 0-31:2 is equivalent to mask 0x55555555

For more information see taskset(1).

示例

已知 sshd 进程的pid是 944

  • 检查cpu_affinity
$ taskset -p 944
pid 944's current affinity mask: 3  # 十六进制:0x3 -> 二进制:11,表示 CPU 0,1 这两个核
$ taskset -pc 944
pid 944's current affinity list: 0,1  # 表示 CPU 0,1 这两个核

说明:

  • -a 操作给定pid的所有任务(线程),taskset -pac 944 2

  • -c 以列表形式显示和指定cpu

  • -p 对给定pid进行操作

  • 设置 cpu_affinity 到二进制 10,即第二个cpu,也就是cpu-1,转化为十六进制:0x2

$ taskset -p 2 944
pid 944's current affinity mask: 3
pid 944's new affinity mask: 2
$ taskset -pc 944
pid 944's current affinity list: 1

类似的工具

  • Redhat TuneD 应用程序来针对各种用例优化系统的性能配置集
    • tuna

参考

  1. Linux 使用 isolcpu 参数隔离 cpu
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数