How many cores I am using on a Linux Server?
Solution 1:
To get the number of CPU cores per CPU:
grep "^core id" /proc/cpuinfo | sort -u | wc -l
Or to get the number of physical CPUs:
grep "^physical id" /proc/cpuinfo | sort -u | wc -l
Solution 2:
I don't know if it helps, but you can use the mpstat
utility to get a breakdown of CPU usage by individual processor (or core). For example:
$ mpstat -P ALL 1
12:49:59 PM CPU %user %nice %sys %iowait %irq %soft %steal %idle intr/s
12:50:00 PM all 7.89 0.00 1.25 0.88 0.00 0.00 0.00 89.97 1359.00
12:50:00 PM 0 14.00 0.00 0.00 0.00 0.00 0.00 0.00 86.00 1043.00
12:50:00 PM 1 15.84 0.00 7.92 3.96 0.00 0.99 0.00 71.29 297.00
12:50:00 PM 2 3.96 0.00 0.00 1.98 0.00 0.99 0.00 93.07 0.00
12:50:00 PM 3 3.96 0.00 0.99 2.97 0.00 0.00 0.00 92.08 0.00
12:50:00 PM 4 4.00 0.00 0.00 0.00 0.00 0.00 0.00 96.00 0.00
12:50:00 PM 5 4.95 0.00 0.99 0.00 0.00 0.00 0.00 94.06 18.00
12:50:00 PM 6 10.89 0.00 0.99 0.00 0.00 0.00 0.00 88.12 0.00
12:50:00 PM 7 5.05 0.00 0.00 0.00 0.00 0.00 0.00 94.95 0.00
In this example, you can see that CPU's 0
, 1
, and 6
are doing more work than the rest of them. Sometimes you will see that a single CPU is near (or at) 100% while others are at zero. This can be an indicator of program (or portion of a program) that is single-threaded and only able to use a single CPU at a time.
To install mpstat
on a Fedora, RHEL, or CentOS system, use yum install sysstat
.
Solution 3:
Unless explicitly configured not to (ie. pinning a process to a specific CPU), all cores can be assumed to be in use at all times. The scheduler will allocate processes the next available core. Case in point, "System Monitor" (part of GNOME) is showing my load almost the same across all 4 cores of my machine.
Solution 4:
So you'll see there are responses here that will tell you how your cores are being utilized.
HOWEVER - this isn't really doing you a service. You've made a basic assumption that just doesn't hold - that your jobs will tend to group themselves onto some subset of the cores.
Instead, your jobs will be spread out across all of the cores, unless you implement something that keeps them "penned in" somehow. (Note: I'm not recommending that; just saying, "unless")
Here's an alternative strategy: Identify for your particular system what the LOAD level is when you feel it's "acceptable" for other users to be adding more jobs. Then, build something that only submits a new background job when the load level is down below that limit.
That way, the solution will be core-count-independent, more portable, more flexible, and easier to "tweak" as well.
Solution 5:
If you want to do this so it works on linux and OS X, you can do:
CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)