Linux動態(tài)啟用/禁用超線程技術(shù)的方法詳解
前言
intel的超線程技術(shù)能讓一個物理核上并行執(zhí)行兩個線程,大多數(shù)情況下能提高硬件資源的利用率,增強系統(tǒng)性能。對于cpu密集型的數(shù)值程序,超線程技術(shù)可能會導(dǎo)致整體程序性能下降。鑒于此,執(zhí)行OpenMP或者MPI數(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文件實現(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
備注:
- 腳本需root權(quán)限執(zhí)行;
- 可以通過cat /proc/cpuinfo查看啟用的cpu信息,該命令無需root權(quán)限;
- lscpu命令可查看cpu的狀態(tài)(無需root權(quán)限):超線程狀態(tài)下threads per core數(shù)值為2,禁用時為1.
參考
Disable / Enable HyperThreading cores on runtime – linux
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
詳解如何實現(xiàn)Linux服務(wù)Crash后自動重啟
近期碰到了一個?Linux?Systemd?服務(wù)?Crash,?Crash?后需要人工介入重啟.?那么,?有沒有辦法如何實現(xiàn)?Linux?服務(wù)?Crash?后自動重啟,下面就來和大家分享一下2023-08-08Linux命令學(xué)習(xí)總結(jié):詳解shutdown命令
本篇文章主要介紹了Linux命令學(xué)習(xí)總結(jié):詳解shutdown命令,該命令可以安全關(guān)閉或者重新啟動系統(tǒng)。有興趣的可以了解一下。2016-12-12cloudstack下libvirtd服務(wù)無響應(yīng)問題
這篇文章主要介紹了cloudstack下libvirtd服務(wù)無響應(yīng)問題的相關(guān)資料,需要的朋友可以參考下2016-10-10PHP程序員玩轉(zhuǎn)Linux系列 Nginx中的HTTPS詳解
這篇文章主要為大家詳細介紹了PHP程序員玩轉(zhuǎn)Linux系列文章,MySQL備份還原教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04Linux運維基礎(chǔ)進程管理及環(huán)境組成分析
這篇文章主要為大家介紹了Linux運維基礎(chǔ),對其中進程管理及環(huán)境組成分析作了詳細的分析,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09