从 Linux 中禁用超线程(无法访问 BIOS)

eww*_*ite 34 hp-proliant hyperthreading dell-poweredge

我有一个在远程设施上运行金融交易应用程序的系统。我无权访问 ILO/DRAC,但需要禁用超线程。该系统运行 Intel Westmere 3.33GHz X5680 六核 CPU。我可以重新启动,但要确保系统由于性能问题而未启用超线程。有没有一种干净的方法可以从 Linux 中做到这一点?

编辑:noht添加到内核引导命令行的指令不起作用。RHEL 也一样。

参见:https : //bugzilla.redhat.com/show_bug.cgi?id=440321#c9

ahu*_*us1 23

如果需要,您可以在运行时执行此操作。我在这里找到了一个很好的解决方案:http : //www.absolutelytech.com/2011/08/01/how-to-disable-cpu-cores-in-linux/

步骤 1:确定要关闭的 linux CPU:

cat /proc/cpuinfo
Run Code Online (Sandbox Code Playgroud)

寻找具有相同“核心 ID”的 CPU,您想关闭每对中的一个。

第 2 步:关闭超线程 CPU(在我的例子中是 Linux 看到的总共 8 个“CPU”中的最后四个)

echo 0 > /sys/devices/system/cpu/cpu4/online
echo 0 > /sys/devices/system/cpu/cpu5/online
echo 0 > /sys/devices/system/cpu/cpu6/online
echo 0 > /sys/devices/system/cpu/cpu7/online
Run Code Online (Sandbox Code Playgroud)

您可以为自己设置一个在系统启动后立即运行的脚本。


小智 21

较新的内核提供同步多线程 (SMT) 控制。

您可以检查 SMT 的状态;

cat /sys/devices/system/cpu/smt/active
Run Code Online (Sandbox Code Playgroud)

改变状态

echo off > /sys/devices/system/cpu/smt/control
Run Code Online (Sandbox Code Playgroud)

选项是;

  • 离开
  • 强行关闭

我们已经使用 Linux Kernel 4.4.0 对此进行了测试


小智 14

在机器启动时禁用超线程的脚本...

为了禁用超线程,我在机器 /etc/rc.local 上包含了一个脚本。它不是很干净,但易于安装,独立于 CPU 架构,应该适用于任何现代 linux 发行版。

nano /etc/rc.local

    # place this near the end before the "exit 0"

    for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
        CPUID=$(basename $CPU)
        echo "CPU: $CPUID";
        if test -e $CPU/online; then
                echo "1" > $CPU/online; 
        fi;
        COREID="$(cat $CPU/topology/core_id)";
        eval "COREENABLE=\"\${core${COREID}enable}\"";
        if ${COREENABLE:-true}; then        
                echo "${CPU} core=${CORE} -> enable"
                eval "core${COREID}enable='false'";
        else
                echo "$CPU core=${CORE} -> disable"; 
                echo "0" > "$CPU/online"; 
        fi; 
    done;    
Run Code Online (Sandbox Code Playgroud)

这是如何工作的?

在现代 Linux 发行版上,Linux 内核信息和控件可以作为 /sys 目录中的文件进行访问。例如:

/sys/devices/system/cpu/cpu3 包含逻辑 cpu 3 的内核信息和控制。

cat /sys/devices/system/cpu/cpu3/topology/core_id 将显示此逻辑 CPU 所属的核心编号。

echo "0" > /sys/devices/system/cpu/cpu3/online 允许禁用逻辑 CPU 3。

为什么有效?

我不知道确切的原因......但是系统在关闭超线程后变得更加敏感(在我的 i5 笔记本电脑和具有 60 多个内核的大型 Xeon 服务器上)。我猜这与 per-cpu 缓存、per-cpu 内存分配、cpu 调度程序分配和进程优先级复杂迭代有关。我认为超线程的好处远远超过制作知道如何使用它的 cpu 调度程序的复杂性。

对我来说,超线程的问题是:如果我启动与逻辑内核一样多的 cpu 密集型线程,我将为 cpu 密集型任务提供快速的上下文切换,但对于后台任务而言则代价高昂,因为超线程完全由cpu密集型任务。另一方面,如果我启动与物理内核一样多的 CPU 密集型线程,我将不会有到这些任务的上下文切换和后台任务的快速上下文切换。看起来不错,但后台任务会找到空闲的逻辑处理器,并且几乎会立即运行。就像它们是实时性能(不错 -20)。

在第一个场景中,超线程是有用的,后台任务将使用昂贵的上下文切换,因为我用正常处理最大化了超线程。第二个是不可接受的,因为我的 CPU 功率的 50% 优先于后台任务。

我所说的“cpu密集型”任务是人工智能数据挖掘和授权服务器(我的工作)。廉价计算机和集群中的 Blender 渲染(绘制我未来的房子)。

此外,这是猜测。

我的印象是更好,但可能不是。


rem*_*ems 9

对于非常老的内核(Linux 2.6.9 左右),在引导时将noht参数附加到内核​​。

至少从 Linux 2.6.18 开始,这个内核命令行选项已被删除


来自http://www.faqs.org/docs/Linux-HOWTO/BootPrompt-HOWTO.html

The `noht' Argument

This will disable hyper-threading on intel processors that have this feature. 
Run Code Online (Sandbox Code Playgroud)

如果使用 lilo 编辑你的 /etc/lilo.conf (然后运行 ​​lilo)或者如果使用 grub 然后编辑你的 /boot/grub/menu.lst 。

  • 这是一个 Gentoo 系统。我在 grub 内核命令行中尝试了 `noht` 条目。系统不遵守 `noht` 命令。RHEL 也一样。参见:https://bugzilla.redhat.com/show_bug.cgi?id=440321#c9 (2认同)

Pau*_*l M 9

您可以使用每个内核的“thread_siblings_list”来关闭 HT 对中的第二个内核。

以下命令管道是 hacky,未优化,并希望通过这种方式使其更易于理解。

cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | \
awk -F, '{print $2}' | \
sort -n | \
uniq | \
( while read X ; do echo $X ; echo 0 > /sys/devices/system/cpu/cpu$X/online ; done )
Run Code Online (Sandbox Code Playgroud)

因此,获取所有线程兄弟列表,为每对提取第二个 CPU,获取唯一列表,然后将它们关闭。

这有意义吗?

如果我在运行上述命令后执行“cat /proc/cpuinfo”,则内核数减半。

  • 这是一个很好的答案。我不得不按如下方式修改它以达到我的目的:`echo 0 > /sys/devices/system/cpu/cpu$X/online` 变为 `echo 0 | 须藤三通/sys/devices/system/cpu/cpu$X/online` (2认同)

Eri*_*rik 5

使用内核命令行参数在启动时禁用 SMT/HT nosmt

        nosmt           [KNL,S390] Disable symmetric multithreading (SMT).
                        Equivalent to smt=1.

                        [KNL,x86] Disable symmetric multithreading (SMT).
                        nosmt=force: Force disable SMT, cannot be undone
                                     via the sysfs control file.
Run Code Online (Sandbox Code Playgroud)

使用SMT 控件在运行时禁用 SMT/HT :

   /sys/devices/system/cpu/smt/control:

     This file allows to read out the SMT control state and provides the
     ability to disable or (re)enable SMT. The possible states are:

        ==============  ===================================================
        on              SMT is supported by the CPU and enabled. All
                        logical CPUs can be onlined and offlined without
                        restrictions.

        off             SMT is supported by the CPU and disabled. Only
                        the so called primary SMT threads can be onlined
                        and offlined without restrictions. An attempt to
                        online a non-primary sibling is rejected

        forceoff        Same as 'off' but the state cannot be controlled.
                        Attempts to write to the control file are rejected.

        notsupported    The processor does not support SMT. It's therefore
                        not affected by the SMT implications of L1TF.
                        Attempts to write to the control file are rejected.
        ==============  ===================================================

     The possible states which can be written into this file to control SMT
     state are:

     - on
     - off
     - forceoff

Run Code Online (Sandbox Code Playgroud)

  • 这看起来是一个非常新的选择。你能告诉我们哪个内核版本添加了它吗? (2认同)

eww*_*ite 0

我必须等到能进入 ILO/Drac。内核引导参数不适用于当前的 Linux 发行版。