Wednesday 13 August 2014

How to get the number of CPUs on a Linux Box?

=============
1. cat /proc/cpuinfo
=============
/proc/cpuinfo marks down the details of each logical cpu (e.g. cpu core or hyper-thread if hyper-thread is enabled).
By counting the number of lines which contain "processor", we can get the number of logical cpu:
======
root@manoj [~]# cat /proc/cpuinfo | grep "processor" | wc -l
8
======
The above result means that, my server has got 8 logical CPUs.

Any logical cpu shares the same Physical ID is in the same cpu socket, so we can get the number of physical cpu by counting the number of physical id which are unique:
==========
root@manoj [~]# cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
2
==========


For the same physical cpu, the number of cores equals to the number of logical cpus if hyperthread is not enabled;

If two logical cpus share the same core id, hyper-thread is enabled. This is how to get the number of cores in each physical cpu:
=====
cat /proc/cpuinfo | grep "cpu cores" | wc -l
=====
This is how to get the number of logical cpus in each physical cpu:
=======
cat /proc/cpuinfo | grep "siblings"
=======

=======
2) cat /proc/stat
=======
/proc/stat gives the performance of each logical cpu . We can get the number by counting the lines of "cpu[0-9]":
========
cat /proc/stat | grep "^cpu[0-9]" | wc -l
========

Kool :)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.