溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux如何動態(tài)啟用/禁用超線程技術(shù)

發(fā)布時間:2021-07-16 14:53:52 來源:億速云 閱讀:197 作者:小新 欄目:服務(wù)器

這篇文章主要為大家展示了“Linux如何動態(tài)啟用/禁用超線程技術(shù)”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Linux如何動態(tài)啟用/禁用超線程技術(shù)”這篇文章吧。

前言

intel的超線程技術(shù)能讓一個物理核上并行執(zhí)行兩個線程,大多數(shù)情況下能提高硬件資源的利用率,增強(qiáng)系統(tǒng)性能。對于cpu密集型的數(shù)值程序,超線程技術(shù)可能會導(dǎo)致整體程序性能下降。鑒于此,執(zhí)行OpenMP或者M(jìn)PI數(shù)值程序時建議關(guān)閉超線程技術(shù)。

以下是github上找到的動態(tài)打開、關(guān)閉超線程技術(shù)的腳本。其原理是根據(jù)/sys/devices/system/cpu/cpuX/topology/thread_siblings_list文件找到邏輯核的關(guān)系,然后編輯/sys/devices/system/cpu/cpuX/online文件實(shí)現(xiàn)動態(tài)開啟和關(guān)閉超線程技術(shù)。

#!/bin/bash

HYPERTHREADING=1

function toggleHyperThreading() {
 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
   CPUID=`basename $CPU | cut -b4-`
   echo -en "CPU: $CPUID\t"
   [ -e $CPU/online ] && echo "1" > $CPU/online
   THREAD1=`cat $CPU/topology/thread_siblings_list | cut -f1 -d,`
   if [ $CPUID = $THREAD1 ]; then
     echo "-> enable"
     [ -e $CPU/online ] && echo "1" > $CPU/online
   else
    if [ "$HYPERTHREADING" -eq "0" ]; then echo "-> disabled"; else echo "-> enabled"; fi
     echo "$HYPERTHREADING" > $CPU/online
   fi
 done
}

function enabled() {
    echo -en "Enabling HyperThreading\n"
    HYPERTHREADING=1
    toggleHyperThreading
}

function disabled() {
    echo -en "Disabling HyperThreading\n"
    HYPERTHREADING=0
    toggleHyperThreading
}

#
ONLINE=$(cat /sys/devices/system/cpu/online)
OFFLINE=$(cat /sys/devices/system/cpu/offline)
echo "---------------------------------------------------"
echo -en "CPU's online: $ONLINE\t CPU's offline: $OFFLINE\n"
echo "---------------------------------------------------"
while true; do
  read -p "Type in e to enable or d disable hyperThreading or q to quit [e/d/q] ?" ed
  case $ed in
    [Ee]* ) enabled; break;;
    [Dd]* ) disabled;exit;;
    [Qq]* ) exit;;
    * ) echo "Please answer e for enable or d for disable hyperThreading.";;
  esac
done

備注:

  1. 腳本需root權(quán)限執(zhí)行;

  2. 可以通過cat /proc/cpuinfo查看啟用的cpu信息,該命令無需root權(quán)限;

  3. lscpu命令可查看cpu的狀態(tài)(無需root權(quán)限):超線程狀態(tài)下threads per core數(shù)值為2,禁用時為1.

以上是“Linux如何動態(tài)啟用/禁用超線程技術(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI