봉황대 in CS

[CPU] What is Hyper-Threading ? 본문

Computer Science & Engineering

[CPU] What is Hyper-Threading ?

등 긁는 봉황대 2024. 7. 15. 17:26

‘Hyper-threading’은 Multi-core system에서 CPU throughput을 높이기 위해 Intel이 2002년에 개발한 기술이다.

 

[참고]

  • CPU : 중앙 처리 장치
    = Processor : 프로그램의 instruction을 수행하기 위한 논리회로 (두 용어를 혼용해서 사용함)
  • Core : 프로그램의 instruction을 읽고 수행하는 독립적인 처리 장치
  • Multi-core system : 두 개 이상의 core를 가진 processor
    → 따라서 컴퓨터는 동일 시간에 서로 다른 task들을 처리할 수 있게 되어 throughput이 증가할 수 있다.

한 컴퓨터에는 CPU가 여러 개 존재할 수 있고, 한 CPU에는 core가 여러 개 존재할 수 있다.

 

 

 

위에서 말한 ‘core’는 ‘physical core’, 즉 물리적으로 존재하는 core를 말한다.

 

 

Hyper-threading 기술은 CPU의 ‘physical core’ 각각을 2개의 ‘logical core’로 나누는데,

(하나의 물리적인 core를 2개의 논리적인 core로 분할)

 

각 logical core들을 ‘thread’라고도 부른다.

 

이는 hardware thread로,

우리가 흔히 아는 software thread(OS 또는 application level에서 생성 및 관리되는 thread)와는 다르다.

 

 

정리하자면,

Hyper-threading은 하나의 physical core가 2개의 thread(logical core) 단위로 동작하도록 하여,

한 physical core가 2개의 독립적인 task들을 동시에 실행할 수 있도록 하는 기술이다.

 

A physical processor acts like two logical or virtual cores.

 


 

[ Traditional Single-core System ]

출처 : https://www.intel.com/content/www/us/en/gaming/resources/hyper-threading.html

 

[ Hyper-threading ]

출처 : https://www.intel.com/content/www/us/en/gaming/resources/hyper-threading.html

 


 

위는 개괄이었고, 더 자세히 들어가자면 ..

 

One physical core works like two “logical cores”
that can handle different software threads.

 

 

Workload(작업)는 software thread들로 나눠질 수 있고,

OS는 이들 각각을 hardware thread(physical core)들에게로 분배하여 동시에 실행시킬 수 있도록 한다.

 

기존의 single-threaded core system에서, 만약 다른 core에게 할당된 task들끼리 의존 관계가 존재해서 순차적으로 진행되어야 하는 경우에는 다른 core의 작업이 완료되기까지 기다리는 시간인 idle time이 존재하게 된다.

 

이 시간 동안은 한 core가 아무 일도 하지 못하고 마냥 기다리고만 있기 때문에 자원이 낭비된다.

 

 

Hyper-threading은 core가 이 idle time에 다른 일을 할 수 있도록 해준다.

 

예를 들자면, logical core 1이 idle time에 도래해서 아무것도 하지 않고 마냥 기다리기만 하는 경우,

이제 logical core 2의 task를 수행시키는 것이다.

 

 

또는, 어느 application이 너무 무거워서 bottleneck이 되는 경우,

다른 logical core에 존재하는 task를 실행시키도록 하여 CPU throughput을 증가시킨다.

 

 

위에서 ‘2개의 독립적인 task들을 동시에 실행’시킨다고 했는데,

엄밀히 말해서는 두 software thread들끼리 실행 전환을 빠르게 하여 ‘동시에’ 실행되는 것으로 보이는 것이다.

 

 

Is Hyper-Threading Always 짱짱 ?


Hyper-threading을 사용하면 언제나 성능이 좋아질까? 아니오.

 

 

Hyper-threading을 통해서 CPU throughput을 얼마나 높일 수 있는지는 실행하는 application의 성격에 따라서 달라지는데,

특히 task들을 ‘parallel 하게 수행시킬 수 있는지’가 관건이다.

 

Task들을 병렬적으로 수행할 수 없다면, 서로 다른 thread를 통해서 동시에 실행시키지 못한다는 것과 동일하기 때문에

hyper-threading을 통한 성능 향상은 거의 없을 것이다.

 

 

또한, 동일한 physical core에 존재하는 logical core들끼리는 자원들(memory caches, functional units 등)을 공유하기 때문에 context switching(보다는 low level일 것 같지만 아무튼 switching) 비용도 고려해야 한다.

 

 

정리하자면, 항상 CPU throughput을 높인다는 보장은 할 수 없고,

실행하려는 application의 특성에 따라 hyper-threading에 대한 사용을 결정지어야 한다는 것이다.

 

 

How to Set Hyper-Threading Enabled || Disabled


Hyper-threading을 지원하는 processor들이 있고, 지원하지 않는 놈들(== one thread per core)도 있기 때문에 먼저 내 컴퓨터가 이를 지원하는지를 알아봐야 한다.

 

 

Default 상태는 ‘enabled’, 즉 Hyper-threading이 켜져 있는 상태이고, (알아보기 전까지는 disabled이 기본 상태인 줄 알았다 ;;)

 

BIOS에서 “Hyper-Threading Technology” → “Enable” 또는 “Disable”로 지정하거나,

아래 명령을 실행시키면 된다는데 (Rocky Linux 9 기준)

# Disabling Hyper-threading
echo off | sudo tee /sys/devices/system/cpu/smt/control
 

tee(1) - Linux manual page

 

man7.org

연구실 서버를 건드리는 건 좀 무서워서 안 해봤다 낄낄…

 

 

Checking Linux System(연구실 서버)’s CPU Architecture


여담으로.. Hyper-threading에 대해서 공부하면서 연구실 서버(Rocky Linux 9)의 CPU와 core들이 어떻게 구성되어 있는지를 확인해 보았다. CPU가 총 몇 개이며, 한 CPU에 core가 몇 개고, hyper-threading 시 어느 core를 어떤 기준으로 사용하는지를 알고 싶었다.

 

 

우선, lscpu 명령을 통해서 CPU architecture 정보를 출력해 볼 수 있다.

 

lscpu(1) - Linux manual page

lscpu(1) — Linux manual page LSCPU(1) User Commands LSCPU(1) NAME         top lscpu - display information about the CPU architecture SYNOPSIS         top lscpu [options] DESCRIPTION         top lscpu gathers CPU architecture information from

man7.org

 

명령어 실행 결과에서 필요한 부분들만 가져오면 ..

Architecture:            x86_64
  ...
  
CPU(s):                  96
  On-line CPU(s) list:   0-95
Vendor ID:               GenuineIntel
  Model name:            Intel(R) Xeon(R) Gold 5318Y CPU @ 2.10GHz
    CPU family:          6
    Model:               106
    Thread(s) per core:  2
    Core(s) per socket:  24
    Socket(s):           2
    ...
    
NUMA:
  NUMA node(s):          2
  NUMA node0 CPU(s):     0-23,48-71
  NUMA node1 CPU(s):     24-47,72-95
  ...

 

  • Socket : Physical processor, 즉 CPU를 말한다. 따라서 이 시스템은 CPU를 2개 가지고 있다.
  • Cores per socket : 한 CPU 당 가지고 있는 physical core의 개수를 말한다.
    따라서 이 시스템은 physical core를 총 48개(2 × 24) 가지고 있다.
  • Threads per core : 한 core 당 실행 가능한 thread의 개수를 말한다.
    즉, 한 physical core 당 2개의 logical core를 가지고 있는, hyper-threading이 가능한 core라는 것이다.

따라서 이 시스템은 logical core(logical processor, logical CPU)는 총 96개(2 × 24 × 2)를 가지고 있다는 것이고,
thread를 총 96개(0 ~ 95)까지 가질 수 있다는 것과 동일한 이야기이다.

 

 

/sys/devices/system/cpu/cpu*/topology 디렉터리에는 CPU architecture 관련 정보들이 저장되어 있다.

예시로, 아래 명령을 통해서 어느 logical CPU가 어느 physical CPU에 binding 되어 있는지를 볼 수 있다.

grep -H . /sys/devices/system/cpu/cpu*/topology/core_id | sort -V
출력 결과
/sys/devices/system/cpu/cpu0/topology/core_id:0
/sys/devices/system/cpu/cpu1/topology/core_id:1
/sys/devices/system/cpu/cpu2/topology/core_id:2
/sys/devices/system/cpu/cpu3/topology/core_id:3
/sys/devices/system/cpu/cpu4/topology/core_id:4
/sys/devices/system/cpu/cpu5/topology/core_id:5
/sys/devices/system/cpu/cpu6/topology/core_id:6
/sys/devices/system/cpu/cpu7/topology/core_id:7
/sys/devices/system/cpu/cpu8/topology/core_id:8
/sys/devices/system/cpu/cpu9/topology/core_id:9
/sys/devices/system/cpu/cpu10/topology/core_id:10
/sys/devices/system/cpu/cpu11/topology/core_id:11
/sys/devices/system/cpu/cpu12/topology/core_id:12
/sys/devices/system/cpu/cpu13/topology/core_id:13
/sys/devices/system/cpu/cpu14/topology/core_id:14
/sys/devices/system/cpu/cpu15/topology/core_id:15
/sys/devices/system/cpu/cpu16/topology/core_id:16
/sys/devices/system/cpu/cpu17/topology/core_id:17
/sys/devices/system/cpu/cpu18/topology/core_id:18
/sys/devices/system/cpu/cpu19/topology/core_id:19
/sys/devices/system/cpu/cpu20/topology/core_id:20
/sys/devices/system/cpu/cpu21/topology/core_id:21
/sys/devices/system/cpu/cpu22/topology/core_id:22
/sys/devices/system/cpu/cpu23/topology/core_id:23
/sys/devices/system/cpu/cpu24/topology/core_id:0
/sys/devices/system/cpu/cpu25/topology/core_id:1
/sys/devices/system/cpu/cpu26/topology/core_id:2
/sys/devices/system/cpu/cpu27/topology/core_id:3
/sys/devices/system/cpu/cpu28/topology/core_id:4
/sys/devices/system/cpu/cpu29/topology/core_id:5
/sys/devices/system/cpu/cpu30/topology/core_id:6
/sys/devices/system/cpu/cpu31/topology/core_id:7
/sys/devices/system/cpu/cpu32/topology/core_id:8
/sys/devices/system/cpu/cpu33/topology/core_id:9
/sys/devices/system/cpu/cpu34/topology/core_id:10
/sys/devices/system/cpu/cpu35/topology/core_id:11
/sys/devices/system/cpu/cpu36/topology/core_id:12
/sys/devices/system/cpu/cpu37/topology/core_id:13
/sys/devices/system/cpu/cpu38/topology/core_id:14
/sys/devices/system/cpu/cpu39/topology/core_id:15
/sys/devices/system/cpu/cpu40/topology/core_id:16
/sys/devices/system/cpu/cpu41/topology/core_id:17
/sys/devices/system/cpu/cpu42/topology/core_id:18
/sys/devices/system/cpu/cpu43/topology/core_id:19
/sys/devices/system/cpu/cpu44/topology/core_id:20
/sys/devices/system/cpu/cpu45/topology/core_id:21
/sys/devices/system/cpu/cpu46/topology/core_id:22
/sys/devices/system/cpu/cpu47/topology/core_id:23
/sys/devices/system/cpu/cpu48/topology/core_id:0
/sys/devices/system/cpu/cpu49/topology/core_id:1
/sys/devices/system/cpu/cpu50/topology/core_id:2
/sys/devices/system/cpu/cpu51/topology/core_id:3
/sys/devices/system/cpu/cpu52/topology/core_id:4
/sys/devices/system/cpu/cpu53/topology/core_id:5
/sys/devices/system/cpu/cpu54/topology/core_id:6
/sys/devices/system/cpu/cpu55/topology/core_id:7
/sys/devices/system/cpu/cpu56/topology/core_id:8
/sys/devices/system/cpu/cpu57/topology/core_id:9
/sys/devices/system/cpu/cpu58/topology/core_id:10
/sys/devices/system/cpu/cpu59/topology/core_id:11
/sys/devices/system/cpu/cpu60/topology/core_id:12
/sys/devices/system/cpu/cpu61/topology/core_id:13
/sys/devices/system/cpu/cpu62/topology/core_id:14
/sys/devices/system/cpu/cpu63/topology/core_id:15
/sys/devices/system/cpu/cpu64/topology/core_id:16
/sys/devices/system/cpu/cpu65/topology/core_id:17
/sys/devices/system/cpu/cpu66/topology/core_id:18
/sys/devices/system/cpu/cpu67/topology/core_id:19
/sys/devices/system/cpu/cpu68/topology/core_id:20
/sys/devices/system/cpu/cpu69/topology/core_id:21
/sys/devices/system/cpu/cpu70/topology/core_id:22
/sys/devices/system/cpu/cpu71/topology/core_id:23
/sys/devices/system/cpu/cpu72/topology/core_id:0
/sys/devices/system/cpu/cpu73/topology/core_id:1
/sys/devices/system/cpu/cpu74/topology/core_id:2
/sys/devices/system/cpu/cpu75/topology/core_id:3
/sys/devices/system/cpu/cpu76/topology/core_id:4
/sys/devices/system/cpu/cpu77/topology/core_id:5
/sys/devices/system/cpu/cpu78/topology/core_id:6
/sys/devices/system/cpu/cpu79/topology/core_id:7
/sys/devices/system/cpu/cpu80/topology/core_id:8
/sys/devices/system/cpu/cpu81/topology/core_id:9
/sys/devices/system/cpu/cpu82/topology/core_id:10
/sys/devices/system/cpu/cpu83/topology/core_id:11
/sys/devices/system/cpu/cpu84/topology/core_id:12
/sys/devices/system/cpu/cpu85/topology/core_id:13
/sys/devices/system/cpu/cpu86/topology/core_id:14
/sys/devices/system/cpu/cpu87/topology/core_id:15
/sys/devices/system/cpu/cpu88/topology/core_id:16
/sys/devices/system/cpu/cpu89/topology/core_id:17
/sys/devices/system/cpu/cpu90/topology/core_id:18
/sys/devices/system/cpu/cpu91/topology/core_id:19
/sys/devices/system/cpu/cpu92/topology/core_id:20
/sys/devices/system/cpu/cpu93/topology/core_id:21
/sys/devices/system/cpu/cpu94/topology/core_id:22
/sys/devices/system/cpu/cpu95/topology/core_id:23

 

 

위의 결과와 lscpu에서 출력된 NUMA 관련 내용들을 조합해서 큰 그림을 그려보자면 다음과 같다.

 

 

 

---

 

Hyper-threading에 대해서 알아보면서 의문이 들었던 부분은, task들이 어떤 기준으로 쪼개져서 어느 core에 할당되는지이다.

이런 정책에 대한 부분도 알아보면 좋을 것 같다.

 

이 글에서는 여기까지만 …

 

 

참고


What is Hyper-Threading?

What Is Hyper-Threading? - Intel

Hyperthreading with vSphere

Disable Hyperthreading From a Running Linux System | Baeldung on Linux

https://iue.tuwien.ac.at/phd/weinbub/dissertationsu16.html

CPU, 프로세서(Processor)와 코어(Core)는 어떤 연관성과 차이점이 있을까?

 

 

 

반응형
Comments