Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I check if hyperthreading is enabled on a Linux machine, using a perl script to check for it?

I'm trying the following way:

dmidecode -t processor | grep HTT

Let me know if I'm on right track.

share|improve this question
    
for dmidecode you have to be root. –  Nils Mar 5 '12 at 20:50
add comment

4 Answers

up vote 4 down vote accepted

You are indeed on the right track :) with

dmidecode -t processor | grep HTT

On Linux, I generally just look for "ht" on the "flags" line of /proc/cpuinfo. See for instance

grep '^flags\b' /proc/cpuinfo | tail -1

or if you want to include the "ht" in the pattern

grep -o '^flags\b.*: .*\bht\b' /proc/cpuinfo | tail -1

(\b matches the word boundaries and helps avoid false positives in cases where "ht" is part of another flag.)

share|improve this answer
    
This will only tell you if the processor is HT capable, not if HT is actually being used. –  Riccardo Murri 14 hours ago
add comment

You can just take a look at the flags of your CPU with:

grep ht /proc/cpuinfo
share|improve this answer
    
This will only tell you if the processor is HT capable, not if HT is actually being used. Nodes whose processor is HT-capable but where HT is not enable will still advertise ht in the CPU flags. –  Riccardo Murri 14 hours ago
add comment

If the number of logical processors is twice the number of cores you have HT. Use to following script to decode /proc/cpuinfo:

#!/bin/sh
CPUFILE=/proc/cpuinfo
test -f $CPUFILE || exit 1
NUMPHY=`grep "physical id" $CPUFILE | sort -u | wc -l`
NUMLOG=`grep "processor" $CPUFILE | wc -l`
if [ $NUMPHY -eq 1 ]
  then
    echo This system has one physical CPU,
  else
    echo This system has $NUMPHY physical CPUs,
fi
if [ $NUMLOG -gt 1 ]
  then
    echo and $NUMLOG logical CPUs.
    NUMCORE=`grep "core id" $CPUFILE | sort -u | wc -l`
    if [ $NUMCORE -gt 1 ]
      then
        echo For every physical CPU there are $NUMCORE cores.
    fi
  else
    echo and one logical CPU.
fi
echo -n The CPU is a `grep "model name" $CPUFILE | sort -u | cut -d : -f 2-`
echo " with`grep "cache size" $CPUFILE | sort -u | cut -d : -f 2-` cache"
share|improve this answer
add comment

The above examples show if the CPU is capable of HT, but not if it is being used. The last method works but not dual socket servers and VMs tested on Xenserver where it doesn’t display Physical CPU, since there are none.

I found this to be the easiest and less code way, which also worked on all my test environments. but requires bc.

echo "testing ################################### "

nproc=$(grep -i "processor" /proc/cpuinfo | sort -u | wc -l)

phycore=$(cat /proc/cpuinfo | egrep "core id|physical id" | tr -d "\n" | sed s/physical/\\nphysical/g | grep -v ^$ | sort | uniq | wc -l)

if [ -z "$(echo "$phycore *2" | bc | grep $nproc)" ]

then

echo "Does not look like you have HT Enabled"

if [ -z "$( dmidecode -t processor | grep HTT)" ]

 then

echo "HT is also not Possible on this server"

 else

echo "This server is HT Capable,  However it is Disabled"

fi

else

   echo "yay  HT Is working"

fi


echo "testing ################################### "

I believe this will work on all platforms, and will tell you if its CPU is capable, and if it is enabled. May be a little messy, I'm a beginner at scripting though. I tested with centos XENSERVER vm, Ubuntu, and Openfiler (rpath)

I have a similar cool script here, would like to see what you think?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.