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
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 4
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)
Solution 5
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.
Related videos on Youtube
baz
Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.
Updated on September 17, 2022Comments
-
baz over 1 year
I am just wondering how I can find out in bash how many CPU cores a user is now using on a Linux Server?
I am submitting a fair amount of background jobs to the server, so I'd like to write a bash script to check before I submit a job if the running jobs (processes) submitted by me are too many, because I have to let other users have enough cores to use for their jobs. I'd like to know what bash command can tell me how many cores my jobs are using now.
Thanks and regards!
-
baz over 14 yearsI am submitting a fair amount of background jobs to the server, so I like to write a bash script to check before I submit a job if the running jobs(processes) submit by me are too many, because I have to let other users have enough cores to use for their jobs. So I like to know what bash command can tell me how many cores my jobs are using now.
-
baz over 14 yearsI like to know a bash command that can tell me the number, since I want to use it in a bash script.
-
Marc Novakowski over 14 years@Tim
top
can run in batch mode with the -b switch. Combine it with the -n switch to get the number of iterations you need. -
baz over 14 yearscan you give an example of code for obtaining the number of cores used by all the jobs (processes) submit by a user?
-
Marc Novakowski over 14 years@Tim Not off the bat. I'd either have to write such a script by parsing the
top
output or google to see if someone's already done it. However, I'm not convinced its worth the effort because I think I'll get different outputs for every run given the speed at which context switching occurs. Isn't the system load a good enough indicator? It effectively tells you how many virtual cores are required to handle the process queue (processes waiting on the CPU). -
baz over 14 years@ nagul: Yes system load is good and I also check that too. However some people keep telling me that I can not use up all the cores because who knows others will run their jobs soon. So I guess I have to limit the cores I am using. By the way, have you seen somewhere online scripts that check the CPU and memory load and dynamically submit jobs based on the usage? I have been looking them for so long. Thanks!
-
baz over 14 yearsThank you! I do check the overall system load. I have been told by one of my colleagues that I can not use up all the cores because who knows others will run their jobs soon and my jobs will last for quite a while. Do you also think I have to limit the cores I am using?
-
dmckee --- ex-moderator kitten over 14 yearsTrying to count cores is the wrong way to allocate CPU. That is the kernel's job. To tweak it, use
[re]nice
, or if that isn't enough get a queue manager likepbs
. -
dmckee --- ex-moderator kitten over 14 yearsRelevant link: superuser.com/questions/49020/linux-queuing-system
-
Marc Novakowski over 14 years@Tim I'd suggest a different approach. Like EmmEff says, both cores will be used as long as system load is above 1.0. Have a script that parses
top
output to get %CPU utilization for your processes, and if the cumulative usage goes above a specified threshold, lowers their priority usingnice
. Or better still, if you absolutely have to make sure you don't hog all cores, bind all processes to one core: cyberciti.biz/tips/… -
baz over 14 years@ nagul: Thanks! I finally get it done by parsing the top
-
pbr over 14 yearsI just now noticed a comment you added to another poster's answer, indicating this is because of something a colleague told you. So, indeed, it appears that the misconception is his, not yours. Sorry about that!
-
baz over 14 yearsThank you! As to "Identify for your particular system what the LOAD level is when you feel it's "acceptable" for other users to be adding more jobs", if the current load is not mainly caused by my running jobs then I would feel more free to use the rest CPUs, but if in the current load my running jobs take a fair amount then I probably have to give up using the rest CPUs. So do you still think I don't have to estimate the CPU usage of my total running jobs?
-
pbr over 14 yearsNo, I really don't think it makes sense to distinguish between how much of the current load is from your jobs vs. other jobs. Consider the simplicity: your job tries to be a good citizen, and only submits itself if the load is below a level which you consider acceptable. The idea is NEVER to utilize all the remaining capacity; that leaves other future users in a bind. Ideally, all of the users would be using the same logic for this.
-
Nav about 13 yearsThanks a million for providing this answer! My colleagues were using the "top" command and making all the wrong conclusions about processor utilization until I showed them the mpstat command.
-
Alix Axel over 11 yearsNot available on Ubuntu:
# aptitude search mpstat #
. -
Bad Boy over 11 years@AlixAxel try
aptitude search systat
instead. -
Alix Axel over 11 years@MattSolnit: Still no luck.
-
user almost 8 years
-
itoctopus almost 8 yearsThis is the best answer - as it shows what each core is doing. It works right out of the box on a Centos 6 server.
-
seralouk over 4 yearshow to check this for a given user?