Linux 命令行中的处理器/内核数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19619582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Number of processors/cores in command line
提问by Michael
I am running the following command to get the number of processors/cores in Linux:
我正在运行以下命令来获取 Linux 中的处理器/内核数:
cat /proc/cpuinfo | grep processor | wc -l
It works but it does not look elegant. How would you suggest improve it ?
它有效,但看起来并不优雅。您建议如何改进它?
采纳答案by pax162
nproc
is what you are looking for.
nproc
就是你要找的。
More here : http://www.cyberciti.biz/faq/linux-get-number-of-cpus-core-command/
更多信息:http: //www.cyberciti.biz/faq/linux-get-number-of-cpus-core-command/
回答by Digital Trauma
I think the method you give is the most portable on Linux. Instead of spawning unnecessary cat
and wc
processes, you can shorten it a bit:
我认为您提供的方法是 Linux 上最便携的方法。相反产卵不必要的cat
和wc
流程,可以缩短它一下:
$ grep --count ^processor /proc/cpuinfo
2
回答by Digital Trauma
On newer kernels you could also possibly use the the /sys/devices/system/cpu/
interface to get a bit more information:
在较新的内核上,您还可以使用该/sys/devices/system/cpu/
接口来获取更多信息:
$ ls /sys/devices/system/cpu/
cpu0 cpufreq kernel_max offline possible present release
cpu1 cpuidle modalias online power probe uevent
$ cat /sys/devices/system/cpu/kernel_max
255
$ cat /sys/devices/system/cpu/offline
2-63
$ cat /sys/devices/system/cpu/possible
0-63
$ cat /sys/devices/system/cpu/present
0-1
$ cat /sys/devices/system/cpu/online
0-1
See the official docsfor more information on what all these mean.
有关所有这些含义的更多信息,请参阅官方文档。
回答by tim_yates
If you want to do this so it works on linux and OS X, you can do:
如果你想这样做,让它在 linux 和 OS X 上工作,你可以这样做:
CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)
回答by Harald Hoyer
The most simplest tool comes with glibc and is called getconf
:
最简单的工具随 glibc 一起提供,称为getconf
:
$ getconf _NPROCESSORS_ONLN
4
回答by mtk
The lscpu(1)
command provided by the util-linuxproject might also be useful:
util-linux项目lscpu(1)
提供的命令也可能有用:
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 58
Model name: Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
Stepping: 9
CPU MHz: 3406.253
CPU max MHz: 3600.0000
CPU min MHz: 1200.0000
BogoMIPS: 5787.10
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 4096K
NUMA node0 CPU(s): 0-3
回答by gwillie
This is for those who want to a portable way to count cpu cores on *bsd, *nix or solaris (haven't tested on aix and hp-ux but should work). It has always worked for me.
这适用于那些想要以便携式方式计算 *bsd、*nix 或 solaris(尚未在 aix 和 hp-ux 上测试但应该可以工作)上的 cpu 内核数的人。它一直对我有用。
dmesg | \
egrep 'cpu[. ]?[0-9]+' | \
sed 's/^.*\(cpu[. ]*[0-9]*\).*$//g' | \
sort -u | \
wc -l | \
tr -d ' '
solaris grep
& egrep
don't have -o
option so sed
is used instead.
solaris grep
&egrep
没有-o
选项,所以sed
改为使用。
回答by Marco
Another one-liner, withoutcounting hyper-threaded cores:
另一个单线,不计算超线程内核:
lscpu | awk -F ":" '/Core/ { c=; }; /Socket/ { print c* }'
回答by Meitham
If you need an os independent method, works across Windows and Linux. Use python
如果你需要一个独立于操作系统的方法,可以在 Windows 和 Linux 上工作。使用蟒蛇
$ python -c 'import multiprocessing as m; print m.cpu_count()'
16
回答by rabinnh
When someone asks for "the number of processors/cores" there are 2 answers being requested. The number of "processors" would be the physical number installed in sockets on the machine.
当有人询问“处理器/内核的数量”时,需要 2 个答案。“处理器”的数量将是安装在机器上的套接字中的物理数量。
The number of "cores" would be physical cores. Hyperthreaded (virtual) cores would not be included (at least to my mind). As someone who writes a lot of programs with thread pools, you really need to know the count of physical cores vs cores/hyperthreads. That said, you can modify the following script to get the answers that you need.
“核心”的数量将是物理核心。不包括超线程(虚拟)内核(至少在我看来)。作为使用线程池编写大量程序的人,您确实需要知道物理内核与内核/超线程的数量。也就是说,您可以修改以下脚本以获得所需的答案。
#!/bin/bash
MODEL=`cat /cpu/procinfo | grep "model name" | sort | uniq`
ALL=`cat /proc/cpuinfo | grep "bogo" | wc -l`
PHYSICAL=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`
CORES=`cat /proc/cpuinfo | grep "cpu cores" | sort | uniq | cut -d':' -f2`
PHY_CORES=$(($PHYSICAL * $CORES))
echo "Type $MODEL"
echo "Processors $PHYSICAL"
echo "Physical cores $PHY_CORES"
echo "Including hyperthreading cores $ALL"
The result on a machine with 2 model Xeon X5650 physical processors each with 6 physical cores that also support hyperthreading:
在具有 2 个型号 Xeon X5650 物理处理器的机器上的结果,每个处理器具有 6 个物理内核,还支持超线程:
Type model name : Intel(R) Xeon(R) CPU X5650 @ 2.67GHz
Processors 2
Physical cores 12
Including hyperthreading cores 24
On a machine with 2 mdeol Xeon E5472 processors each with 4 physical cores that doesn't support hyperthreading
在具有 2 个 mdeol Xeon E5472 处理器的机器上,每个处理器有 4 个不支持超线程的物理内核
Type model name : Intel(R) Xeon(R) CPU E5472 @ 3.00GHz
Processors 2
Physical cores 8
Including hyperthreading cores 8