linux taskset命令

一、简介

taskset命令用于设置进程(或线程)的处理器亲和性(Processor Affinity),可以将进程(或线程)绑定到特定的一个 或 多个CPU上去执行,而不允许将进程(或 线程)调度到其他的CPU上。

CPU亲和性使用位掩码来表示,最低的顺序位是第一个逻辑CPU,最高位对应为最后一个逻辑CPU。

查看cpu的信息可以用 “cat /proc/cpuinfo”命令。

# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 
# 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数

The masks may be specified in hexadecimal (with or without a leading "0x"), or as a CPU list with the --cpu-list option. 

位掩码可以十六进制或者 --cpu-list选项表示,使用十六进制表示的时候可以不带或者带0x前缀。

举例说明

       0x00000001
           is processor #0,

       0x00000003
           is processors #0 and #1,

       0xFFFFFFFF
           is processors #0 through #31,

       32
           is processors #1, #4, and #5,

       --cpu-list 0-2,6
           is processors #0, #1, #2, and #6.

       --cpu-list 0-10:2
           is processors #0, #2, #4, #6, #8 and #10. The suffix ":N"
           specifies stride in the range, for example 0-10:3 is
           interpreted as 0,3,6,9 list.

二、常用参数:

-a, --all-tasks设置或检索所有任务(线程)的CPU相关性对于给定的PID
-c, --cpu-list指定逻辑cpu的序号
-p, --pid在现有PID上操作,不要启动新任务
-V, --version显示版本信息

-h, --help
显示帮助信息

三、常用使用场景

1、查看某一个进程允许运行在那个Cpu上

taskset -p pid
[root@localhost ~]# ps -eLf | grep qemu
root       1389   1339   1389  0    3 14:48 pts/0    00:00:10 /usr/libexec/qemu-kvm -cpu SandyBridge -vnc 0.0.0.0:1 centos1708.img
root       1389   1339   1393  2    3 14:48 pts/0    00:00:36 /usr/libexec/qemu-kvm -cpu SandyBridge -vnc 0.0.0.0:1 centos1708.img
root       1389   1339   1395  0    3 14:48 pts/0    00:00:00 /usr/libexec/qemu-kvm -cpu SandyBridge -vnc 0.0.0.0:1 centos1708.img
root       2638   1409   2638  0    1 15:10 pts/1    00:00:00 grep --color=auto qemu
[root@localhost ~]# taskset -p 1393
pid 1393's current affinity mask: ff
[root@localhost ~]# taskset -p 1389
pid 1389's current affinity mask: ff

输出结构处理器亲和性掩码是ff,表示进程(或 线程)可以在Host上让任何一个CPU运行。

taskset -cp pid
[root@localhost ~]# taskset -cp 1393
pid 1393's current affinity list: 0-7
[root@localhost ~]# taskset -cp 1389
pid 1389's current affinity list: 0-7

查看进程(或 线程)允许允许CPU范围使用-c参数。由于我的Host CPU是4核2线程,因此有8颗逻辑CPU。

2、设置某一进程的cpu的亲和性,也就是设置在某些cpu上运行

taskset -p mask pid
[root@localhost ~]# taskset -p 0x11  1393
pid 1393's current affinity mask: ff
pid 1393's new affinity mask: 11
[root@localhost ~]# taskset -p   1393
pid 1393's current affinity mask: 11
[root@localhost ~]# taskset -cp   1393
pid 1393's current affinity list: 0,4

设置掩码0x11(二进制0001 0001),表示可以在0和4号CPU上允许运行。

taskset -cp cpu_list pid
[root@localhost ~]# taskset -cp 0,3  1393
pid 1393's current affinity list: 0,4
pid 1393's new affinity list: 0,3
[root@localhost ~]# taskset -cp   1393
pid 1393's current affinity list: 0,3

ref:

cat /proc/cpuinfo 查看cpu信息_城南旧梦的博客-CSDN博客_cat cpuinfo

使用taskset命令让进程运行在指定CPU上_弹性云服务器 ECS_故障排除_操作系统类(Linux)_华为云

taskset(1) - Linux manual page

https://www.jianshu.com/p/2627cd875715

Linux命令——taskset - 克拉默与矩阵 - 博客园

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值