0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

numactl and taskset

Last updated at Posted at 2025-07-17

NUMA 架构简介

NUMA:非一致性内存访问架构

  • 多个 CPU 插槽,每个有自己的本地内存
  • 访问本地内存速度快,访问远程内存速度慢
  • 现代多路服务器的标准架构

NUMA 节点是现代多处理器系统中的基本架构单元。

NUMA 节点定义

NUMA 节点(NUMA Node):包含一个或多个 CPU 和本地内存的硬件单元

  • 每个节点内部:CPU 访问本地内存速度最快
  • 节点之间:通过高速互联总线连接
  • 跨节点访问:需要经过总线,延迟更高

典型架构示例

双路服务器(2 个 NUMA 节点)

  节点 0                    节点 1
  ┌─────────────┐         ┌─────────────┐
  │   CPU0      │◄────────┤   CPU1      │
  │  (0-63 核)   │  总线   │  (64-127 核) │
  │             │         │             │
  │  内存 64GB  │         │  内存 64GB  │
  └─────────────┘         └─────────────┘

四路服务器(4 个 NUMA 节点)

  节点 0 (0-31 核)    节点 1 (32-63 核)
       ↓                 ↓
  节点 2 (64-95 核)   节点 3 (96-127 核)

内存访问性能差异

访问类型 延迟 带宽 相对性能
本地内存 ~100ns 100% 1.0x
相邻节点 ~140ns 80% 0.8x
远程节点 ~300ns 50% 0.5x

numactl

numactl 是 Linux 系统中用于控制 NUMA(Non-Uniform Memory Access)的工具。

numactl 主要作用

  1. 查看系统 NUMA 信息
numactl --hardware # 显示 NUMA 拓扑结构

available: 1 nodes (0) # NUMA节点数:1个节点(node 0)
node 0 cpus: 0 1 2 3 4 5 6 7 # CPU核心
node 0 size: 1024 MB
node 0 free: 7651 MB
node distances:
node   0
  0:  10

# 架构类型:UMA(Uniform Memory Access)

##########

numactl --show        # 显示当前进程的 NUMA 策略
policy: default
preferred node: current
physcpubind: 0 1 2 3 4 5 6 7
cpubind: 0
nodebind: 0
membind: 0
preferred:
``

2. 控制进程的 NUMA 绑定

```sh
# 绑定到特定 NUMA 节点
numactl --cpunodebind=0 --membind=0 your_program

# 绑定到特定 CPU 核心
numactl --physcpubind=0-15 your_program

# 内存分配策略
numactl --interleave=all your_program  # 交错分配
numactl --localalloc your_program      # 本地分配

为什么重要?

CPU0 (0-31核) CPU1 (32-63核) CPU2 (64-95核) CPU3 (96-127核)
| | | |
内存块0 内存块1 内存块2 内存块3

性能影响:

  • 本地访问:延迟 ~100ns
  • 跨节点访问:延迟 ~300ns(3倍差距)

lscpu

lscpu --all -e
cat /sys/devices/system/cpu/cpu2/topology/thread_siblings_list

taskset

taskset -c 0,1 your_program  # 将 your_program 绑定到 CPU0 和 CPU1
taskset -p 12345            # 查看 PID 为 12345 的进程的 CPU 亲和性
taskset -pc 0-3 12345       # 将 PID 为 12345 的进程绑定到 CPU0-CPU3
taskset --cpu-list -p 96-127 `pgrep abc` # 设置绑定到最后 32 颗 CPU

renice -19 `abc` # 提高进程优先级

Ref:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?