Detailed explanation of how to dynamically enable/disable hyperthreading technology in Linux

Detailed explanation of how to dynamically enable/disable hyperthreading technology in Linux

Preface

Intel's hyper-threading technology allows two threads to be executed in parallel on one physical core, which in most cases can improve the utilization of hardware resources and enhance system performance. For CPU-intensive numerical programs, hyperthreading technology may cause overall program performance to degrade. For this reason, it is recommended to disable hyperthreading technology when executing OpenMP or MPI numerical programs.

The following is a script found on github that dynamically turns on and off hyper-threading technology. The principle is to find the relationship between logical cores according to the /sys/devices/system/cpu/cpuX/topology/thread_siblings_list file, and then edit the /sys/devices/system/cpu/cpuX/online file to dynamically turn on and off hyper-threading technology.

#!/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

Remark:

  1. The script needs root privileges to execute;
  2. You can view the enabled CPU information through cat /proc/cpuinfo. This command does not require root privileges.
  3. The lscpu command can be used to view the status of the CPU (no root privileges are required): the threads per core value is 2 when hyperthreading is enabled and 1 when disabled.

refer to

Disable / Enable HyperThreading cores on runtime – linux

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Summary of Linux thread exit methods (recommended)
  • Linux thread switching and process switching methods
  • How to cancel (terminate) a linux thread
  • Comparison and Difference between Processes and Threads in Linux

<<:  MySql 8.0.11 installation and configuration tutorial

>>:  Vue detailed introductory notes

Recommend

Automatic line breaks in html pre tags

At this time, you can use overflow:auto; (when the...

Summary of MySQL 8.0 memory-related parameters

Theoretically, the memory used by MySQL = global ...

Hadoop 3.1.1 Fully Distributed Installation Guide under CentOS 6.8 (Recommended)

Foregoing: This document is based on the assumpti...

A brief analysis of the best way to deal with forgotten MySQL 8 passwords

Preface Readers who are familiar with MySQL may f...

An article teaches you how to use js to achieve the barrage effect

Table of contents Create a new html file: Create ...

How to deploy ElasticSearch in Docker

1. What is ElasticSearch? Elasticsearch is also d...

Detailed explanation of nginx configuration file interpretation

The nginx configuration file is mainly divided in...

Commonplace talk about the usage of MYSQL pattern matching REGEXP and like

like LIKE requires the entire data to match, whil...

In-depth understanding of Worker threads in Node.js

Table of contents Overview The history of CPU-bou...

Detailed explanation of nginx's default_server definition and matching rules

The default_server directive of nginx can define ...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

JavaScript Dom implements the principle and example of carousel

If we want to make a carousel, we must first unde...

jQuery implements dynamic tag event

This article shares the specific code of jQuery t...

DOM operation implementation in react

Table of contents Previous words Usage scenarios ...