我有这个脚本,但我不知道如何获得打印输出中的最后一个元素:

cat /proc/cpuinfo | awk '/^processor/{print $3}'

最后一个元素应该是cpu数量减1。


当前回答

不是我的网页,但是来自http://www.ixbrian.com/blog/?p=64&cm_mc_uid=89402252817914508279022&cm_mc_sid_50200000=1450827902的这个命令在centos上很适合我。即使启用了超线程,它也会显示实际的cpu。

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

其他回答

简介: 获取物理cpu:

grep 'core id' /proc/cpuinfo | sort -u

获取物理和逻辑cpu:

grep -c ^processor /proc/cpuinfo

/proc <<这是你需要的任何关于进程和

/proc/cpuinfo <<是任何CPU信息的黄金资源。

下面是我用来计算Linux上在线物理内核数量的方法:

lscpu --online --parse=Core,Socket | grep --invert-match '^#' | sort --unique | wc --lines

简而言之:

lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l

示例(1个socket):

> lscpu
...
CPU(s):                28
Thread(s) per core:    2
Core(s) per socket:    14
Socket(s):             1
....
> lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l
14

示例(2个socket):

> lscpu
...
CPU(s):                56
Thread(s) per core:    2
Core(s) per socket:    14
Socket(s):             2
...
> lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l
28

示例(4个socket):

> lscpu
...
CPU(s):                64
Thread(s) per core:    2
Core(s) per socket:    8
Socket(s):             4
...
> lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l
32

如果你可以使用Python,那么numexpr模块有一个函数:

In [5]: import numexpr as ne

In [6]: ne.detect_number_of_cores()
Out[6]: 8

也:

In [7]: ne.ncores
Out[7]: 8

从命令提示符中查询该信息使用:

# runs whatever valid Python code given as a string with `-c` option
$ python -c "import numexpr as ne; print(ne.ncores)"
8

或者简单地说,可以从multiprocessing.cpu_count()函数中获得此信息

$ python -c "import multiprocessing; print(multiprocessing.cpu_count())"

或者干脆使用os.cpu_count()

$ python -c "import os; print(os.cpu_count())"

对于物理核总数:

grep '^core id' /proc/cpuinfo |sort -u|wc -l

在多插座机器(或总是)上,将上面的结果乘以插座的数量:

echo $(($(grep "^physical id" /proc/cpuinfo | awk '{print $4}' | sort -un | tail -1)+1))

@mklement0使用lscpu给出了一个很好的答案。我在评论中写了一个更简洁的版本

前言:

基于/proc/cpuinfo的答案的问题是,它们解析的是供人类使用的信息,因此缺乏为机器解析设计的稳定格式:输出格式可能因平台和运行时条件而不同;在Linux上使用lscpu -p(在macOS上使用sysctl)可以绕过这个问题。 getconf _NPROCESSORS_ONLN / getconf NPROCESSORS_ONLN不区分逻辑cpu和物理cpu。


下面是一个sh (posix兼容)代码片段,用于在Linux和macOS上确定在线逻辑或物理cpu的数量;详见评论。

Linux使用lscpu, macOS使用sysctl。

术语注意:CPU是指操作系统中最小的处理单元。非超线程内核每个对应1个CPU,而超线程内核包含多于1(通常为2)-逻辑- CPU。 Linux使用以下分类法[1],从最小的单位开始: CPU < core < socket < book <节点 每一层包含下一层的1个或多个实例。

#!/bin/sh

# macOS:           Use `sysctl -n hw.*cpu_max`, which returns the values of 
#                  interest directly.
#                  CAVEAT: Using the "_max" key suffixes means that the *maximum*
#                          available number of CPUs is reported, whereas the
#                          current power-management mode could make *fewer* CPUs 
#                          available; dropping the "_max" suffix would report the
#                          number of *currently* available ones; see [1] below.
#
# Linux:           Parse output from `lscpu -p`, where each output line represents
#                  a distinct (logical) CPU.
#                  Note: Newer versions of `lscpu` support more flexible output
#                        formats, but we stick with the parseable legacy format 
#                        generated by `-p` to support older distros, too.
#                        `-p` reports *online* CPUs only - i.e., on hot-pluggable 
#                        systems, currently disabled (offline) CPUs are NOT
#                        reported.

# Number of LOGICAL CPUs (includes those reported by hyper-threading cores)
  # Linux: Simply count the number of (non-comment) output lines from `lscpu -p`, 
  # which tells us the number of *logical* CPUs.
logicalCpuCount=$([ $(uname) = 'Darwin' ] && 
                       sysctl -n hw.logicalcpu_max || 
                       lscpu -p | egrep -v '^#' | wc -l)

# Number of PHYSICAL CPUs (cores).
  # Linux: The 2nd column contains the core ID, with each core ID having 1 or
  #        - in the case of hyperthreading - more logical CPUs.
  #        Counting the *unique* cores across lines tells us the
  #        number of *physical* CPUs (cores).
physicalCpuCount=$([ $(uname) = 'Darwin' ] && 
                       sysctl -n hw.physicalcpu_max ||
                       lscpu -p | egrep -v '^#' | sort -u -t, -k 2,4 | wc -l)

# Print the values.
cat <<EOF
# of logical CPUs:  $logicalCpuCount
# of physical CPUS: $physicalCpuCount
EOF

[1] macOS sysctl(3)文档

注意,除了macOS之外的bsd衍生系统——例如FreeBSD——只支持hw。sysctl的ncpu键,macOS上已弃用;我不清楚是哪个新键hw。Npu对应于:hw.(logical|physical)cpu_[max]。

感谢@teambob帮助纠正physical-CPU-count lscpu命令。

注意:lscpu -p输出不包括“book”列(手册页提到“books”是分类层次结构中套接字和节点之间的实体)。如果在给定的Linux系统上“books”正在使用(有人知道什么时候和如何使用吗?),physical-CPU-count命令可能会报告不足(这是基于lscpu报告的id在更高级别实体中不是唯一的假设;例如:来自2个不同插座的2个不同内核可能具有相同的ID)。


如果你将上面的代码保存为shell脚本cpu,使用chmod +x cpu可执行,并将其放在$PATH文件夹中,你会看到如下输出:

$ cpus
logical  4
physical 4

Xaekai解释了什么是书:“书是一个模块,里面有一个带有CPU插座、RAM插座、沿边缘的IO连接的电路板,还有一个用于冷却系统集成的钩子。它们用于IBM大型机。更多信息:http://ewh.ieee.org/soc/cpmt/presentations/cpmt0810a.pdf”