当前位置: 移动技术网 > 网络运营>服务器>Linux > Linux动态启用/禁用超线程技术的方法详解

Linux动态启用/禁用超线程技术的方法详解

2019年09月07日  | 移动技术网网络运营  | 我要评论

前言

intel的超线程技术能让一个物理核上并行执行两个线程,大多数情况下能提高硬件资源的利用率,增强系统性能。对于cpu密集型的数值程序,超线程技术可能会导致整体程序性能下降。鉴于此,执行openmp或者mpi数值程序时建议关闭超线程技术。

以下是github上找到的动态打开、关闭超线程技术的脚本。其原理是根据/sys/devices/system/cpu/cpux/topology/thread_siblings_list文件找到逻辑核的关系,然后编辑/sys/devices/system/cpu/cpux/online文件实现动态开启和关闭超线程技术。

#!/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权限执行;
  2. 可以通过cat /proc/cpuinfo查看启用的cpu信息,该命令无需root权限;
  3. lscpu命令可查看cpu的状态(无需root权限):超线程状态下threads per core数值为2,禁用时为1.

参考

disable / enable hyperthreading cores on runtime – linux

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网