一つの物理コアが複数スレッドを処理するSMT(ハイパースレッディング)が有効に働く場合と働かない場合を確認する

linux

SMT (Simultaneous Multi-Threading) は一つの物理コアが複数のスレッドの処理を行うことで CPU のスループット向上を目指す技術。Intel はハイパースレッディングと呼んでいる。

物理2コア x 2 = 4 vCPU の m6i.xlarge インスタンスでベンチマークを取ってみる。

$ lscpu --extended
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE
  0    0      0    0 0:0:0:0          yes
  1    0      0    1 1:1:1:0          yes
  2    0      0    0 0:0:0:0          yes
  3    0      0    1 1:1:1:0          yes

/sys/devices/system/cpu/smt/control で SMT を有効/無効化できる。

run_suite () {
  local t; t=$(nproc)
  echo "==== $1  online vCPU=$t ===="
  sysbench cpu --cpu-max-prime=50000 --threads="$t" --time=15 run \
    | grep -E 'events per second|total number of events'
  stress-ng --vm "$t" --vm-bytes 75% --vm-method all \
    --metrics-brief --timeout 15s 2>&1 | grep 'vm '
}

run_suite "SMT ON"
echo off > /sys/devices/system/cpu/smt/control
sleep 2
run_suite "SMT OFF"
echo on > /sys/devices/system/cpu/smt/control

ハイパースレッディングを有効にしても物理コア自体が増えているわけではないため、素数の計算のようなタスクではほとんどスループットが増えないが、待ち時間が発生するメモリの読み書きでは大幅に向上した。

==== SMT ON  online vCPU=4 ====
    events per second:   636.58
    total number of events:              9552
stress-ng: metrc: [2086] vm  689759  16.50  31.22  33.47  41799.76  10663.04

==== SMT OFF  online vCPU=2 ====
    events per second:   620.01
    total number of events:              9302
stress-ng: metrc: [2110] vm  209813  16.50  12.73  19.31  12715.00   6550.20

物理コアが異なる 2 スレッド (-c 0,1) で動かすと 1 スレッド (-c 0)のときと比べてほぼ倍 (3619.18 → 7040.92) になるが、 共有するスレッド (-c 0,2) の場合、1 スレッドあたりのスループットが悪化する (2756.51) ことが行列演算でも確認できる。

$ taskset -c 0   stress-ng --matrix 1 --matrix-method all --metrics-brief --timeout 15s
# stress-ng: metrc: matrix    54290  15.00  15.00  0.00  3619.18  3619.27

$ taskset -c 0,2 stress-ng --matrix 2 --matrix-method all --metrics-brief --timeout 15s
# stress-ng: metrc: matrix    82706  15.00  30.00  0.00  5512.95  2756.51

$ taskset -c 0,1 stress-ng --matrix 2 --matrix-method all --metrics-brief --timeout 15s
# stress-ng: metrc: matrix   105624  15.00  30.00  0.00  7040.92  3520.54