Friday, January 27, 2017

How to Find Number of CPU Cores in linux ?


You can use the lscpu or nproc command to display the number of processing units available to the current process, which may be less than the number of online processors (please note that not all server supports hotplug a CPU on a running Linux system).


The proc file system is a pseudo-file system which is used as an interface to kernel data structures. It is commonly mounted at /proc. The /proc/cpuinfo file is nothing but a collection of CPU and system architecture dependent items, for each supported architecture a different list. Two common entries are processor which gives CPU number and bogomips; a system constant that is calculated during kernel initialization.

Method 1:

Using nproc
 nproc

Example:

linux@linuxforfreshers.com:~$ nproc
2

Method 2:


lscpu gathers CPU architecture information form /proc/cpuinfon in human-read-able format:

Using lscpu


Example:

linux@linuxforfreshers.com:~$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 15
Stepping:              2
CPU MHz:               1596.000
BogoMIPS:              4255.98
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              2048K
NUMA node0 CPU(s):     0,1

Method 3:

Using /proc/cpuinfo

The /proc/cpuinfo and sysfs stores info about your CPU architecture ike number of CPUs, threads, cores, sockets, NUMA nodes, information about CPU caches, CPU family, model, bogoMIPS, yte order and much more:

Example 1: less /proc/cpuinfo

Or

Example 2:

linux@linuxforfreshers.com:~$ cat /proc/cpuinfo | grep -i 'core id'
core id             : 0
core id             : 1

Example 3:

linux@linuxforfreshers.com:~$ lscpu | egrep 'Thread|Core|Socket'
Thread(s) per core:    1
Core(s) per socket:    2
Socket(s):             1

Method 4:
Using dmidecode

Example :
linux@linuxforfreshers.com:~$ sudo dmidecode -t 4 | egrep 'Core Count'
            Core Count: 2










how to find cpu frequency in linux ?

Frequency


CPU frequency scaling enables the operating system to scale the CPU frequency up or down in order to save power. CPU frequencies can be scaled automatically depending on the system load, in response to ACPI events, or manually by userspace programs.
CPU frequency scaling is implemented in the Linux kernel, the infrastructure is called cpufreq

The frequency/speed of the processor is reported by both lscpu and /proc/cpuinfo.
Method 1:
lscpu | grep -i mhz

Example :
ravi@linuxforfreshers.com:~$ lscpu | grep -i mhz
CPU MHz:               1596.000

Method 2:

cat /proc/cpuinfo | grep -i mhz | uniq
Example:
ravi@linuxforfreshers.com:~$ cat /proc/cpuinfo | grep -i mhz | uniq
cpu MHz                      : 1596.000

Method 3:
The change of frequency can be seen by monitoring the output of /proc/cpuinfo using watch.
$ watch -n 0.1 "cat /proc/cpuinfo | grep -i mhz"

Method 4:
Using lshw command
Example:
ravi@linuxforfreshers.com:~$ sudo lshw -c cpu | grep capacity
       capacity: 1596MHz

Method 5:
Using dmidecode
Example:
ravi@linuxforfreshers.com:~$ sudo dmidecode -t processor | grep "Speed"
            Max Speed: 6000 MHz
            Current Speed: 2133 MHz



You can also get the current cpu frequency with this command:
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
Note : root access required using this command.
Example :
ravi@linuxforfreshers.com:~$ sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq
2133000