如何查找物理cpu,cpu核心和逻辑cpu的数量

时间:2021-09-02 10:56:46

环境

  • Red Hat Enterprise Linux 4
  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6
  • Red Hat Enterprise Linux 7

问题

  • x86 / x86_64系统上的物理CPU,CPU内核和逻辑CPU之间有什么区别?
  • 如何从RHEL操作系统获取可用处理器列表?

决议

  • 物理CPU数量

    • 下面的命令将显示一个系统有多少活动的物理处理器。例如:如果这个数字是2,则可以打开系统机箱,用手删除2个物理处理器。

      $ grep physical.id /proc/cpuinfo | sort -u | wc -l
      2
  • 每个CPU的核心数

    • 在具有多核处理器的系统上,下面的命令应该报告每个物理处理器的CPU内核数量(尽管在极少数情况下可能不会)。例如:如果这个数字是4,而物理CPU是2,那么这2个物理处理器中的每个都有4个CPU内核,总共有8个内核。

      $ grep cpu.cores /proc/cpuinfo | sort -u
      cpu cores : 4
  • 逻辑处理器数量

    • 最后这个命令将显示Linux内核所看到的“逻辑”处理器的总数。这个数字通常是三个统计数据中最重要的。它是处理器的有效数量——就操作系统而言,这是能够在任何给定微秒内工作的不同cpu的数量。示例:继续上面的示例场景,下面看到的数字可以是16而不是8。简单地说,如果这个命令显示的数字与CPU内核的总数不同,这是因为在CPU上启用了超线程,从而进一步划分了每个内核(在本例中,分为两个可用的“线程”)。

      $ grep processor /proc/cpuinfo | wc -l
      16

许可?

  • 为了确定RHEL许可原因的cpu数量,上面的第一个命令就足够了;但是,要进行更多的讨论,请咨询 如何确定系统上的CPU插槽数

  • 如果购买的许可根据插槽(已填充或未填充),处理器或内核的数量而变化的第三方软件,请与软件供应商联系,以确切了解它们如何计算CPU数量。

工具

  • 注意,Red Hat Enterprise Linux 6和7附带了lscpu命令,该命令可以打印出系统处理器的简单可读摘要。此外,在RHEL6中,hwloc包是可用的(RHEL7附带它)——hwloc包括lstopo命令,以及各种hwloc-*命令。

  • 在RHEL6之前,直接检查/proc/cpuinfo(如上所述)或使用x86info或dmidecode命令(同名rpm)都能查到相同的信息。

  • 其他第三方选项没有提供担保:xsos(由本文的原始作者开发)和lshw(可在EPEL中获得)。

  • 对于任何版本的Red Hat Enterprise Linux:本文附带的BASH脚本都会解析/proc/cpuinfo,以打印如下所示的易于阅读的摘要。(注意,它没有提供任何保证或官方支持。)
    $ cpu
    128 logical processors (64 CPU cores)
    8 Intel Xeon CPU X7560 @ 2.27GHz (flags: constant_tsc,ht,lm,pae,vmx)
    └─16 threads / 8 cores each
     1 #!/bin/bash
    2 #
    3 # This simple script uses /proc/cpuinfo (or filename of your choosing) to print
    4 # a succinct summary about a system's processors.
    5 # Other useful utilities (some only available in RHEL6 or EPEL):
    6 # x86info, dmidecode, lscpu, cpuid, lshw, lstopo, xsos
    7 #
    8 # Originally uploaded to redhat.com by Ryan Sawhill <rsaw@redhat.com>, Sep 2012; Updated Jan 2013
    9 # This code is from xsos, which can do so much more <http://github.com/ryran/xsos>
    10 #
    11
    12 # Get input
    13 if [[ -r $1 && -f $1 ]]; then
    14 # If passed a readable file, use that
    15 cpuinfo=$1
    16 else
    17 # Otherwise, use /proc/cpuinfo
    18 cpuinfo=/proc/cpuinfo
    19 fi
    20
    21 # Get model of cpu
    22 model_cpu=$(awk -F: '/^model name/{print $2; exit}' <"$cpuinfo")
    23
    24 # If no model detected (e.g. on Itanium), try to use vendor+family
    25 [[ -z $model_cpu ]] && {
    26 vendor=$(awk -F: '/^vendor /{print $2; exit}' <"$cpuinfo")
    27 family=$(awk -F: '/^family /{print $2; exit}' <"$cpuinfo")
    28 model_cpu="$vendor$family"
    29 }
    30
    31 # Clean up cpu model string
    32 model_cpu=$(sed -e 's,(R),,g' -e 's,(TM),,g' -e 's, *, ,g' -e 's,^ ,,' <<<"$model_cpu")
    33
    34 # Get number of logical processors
    35 num_cpu=$(awk '/^processor/{n++} END{print n}' <"$cpuinfo")
    36
    37 # Get number of physical processors
    38 num_cpu_phys=$(grep '^physical id' <"$cpuinfo" | sort -u | wc -l)
    39
    40 # If "physical id" not found, we cannot make any assumptions (Virtualization--)
    41 # But still, multiplying by 0 in some crazy corner case is bad, so set it to 1
    42 # If num of physical *was* detected, add it to the beginning of the model string
    43 [[ $num_cpu_phys == 0 ]] && num_cpu_phys=1 || model_cpu="$num_cpu_phys $model_cpu"
    44
    45 # If number of logical != number of physical, try to get info on cores & threads
    46 if [[ $num_cpu != $num_cpu_phys ]]; then
    47
    48 # Detect number of threads (logical) per cpu
    49 num_threads_per_cpu=$(awk '/^siblings/{print $3; exit}' <"$cpuinfo")
    50
    51 # Two possibile ways to detect number of cores
    52 cpu_cores=$(awk '/^cpu cores/{print $4; exit}' <"$cpuinfo")
    53 core_id=$(grep '^core id' <"$cpuinfo" | sort -u | wc -l)
    54
    55 # The first is the most accurate, if it works
    56 if [[ -n $cpu_cores ]]; then
    57 num_cores_per_cpu=$cpu_cores
    58
    59 # If "cpu cores" doesn't work, "core id" method might (e.g. Itanium)
    60 elif [[ $core_id -gt 0 ]]; then
    61 num_cores_per_cpu=$core_id
    62 fi
    63
    64 # If found info on cores, setup core variables for printing
    65 if [[ -n $num_cores_per_cpu ]]; then
    66 cores1="($((num_cpu_phys*num_cores_per_cpu)) CPU cores)"
    67 cores2=" / $num_cores_per_cpu cores"
    68 # If didn't find info on cores, assume single-core cpu(s)
    69 else
    70 cores2=" / 1 core"
    71 fi
    72
    73 # If found siblings (threads), setup the variable for the final line
    74 [[ -n $num_threads_per_cpu ]] &&
    75 coresNthreads="\n└─$num_threads_per_cpu threads${cores2} each"
    76 fi
    77
    78 # Check important cpu flags
    79 # pae=physical address extensions * lm=64-bit * vmx=Intel hw-virt * svm=AMD hw-virt
    80 # ht=hyper-threading * aes=AES-NI * constant_tsc=Constant Time Stamp Counter
    81 cpu_flags=$(egrep -o "pae|lm|vmx|svm|ht|aes|constant_tsc" <"$cpuinfo" | sort -u | sed ':a;N;$!ba;s/\n/,/g')
    82 [[ -n $cpu_flags ]] && cpu_flags="(flags: $cpu_flags)"
    83
    84 # Check kernel version; print warning if Xen
    85 [[ $(uname -r) =~ xen ]] && {
    86 echo "Warning: kernel for localhost detected as $(uname -r)"
    87 echo "With Xen, CPU layout in /proc/cpuinfo will be inaccurate; consult dmidecode"
    88 }
    89
    90 # Print out the deets
    91 echo -e "${num_cpu} logical processors ${cores1}"
    92 echo -e "${model_cpu} ${cpu_flags} ${coresNthreads}"

    cpu.sh