A Few Ways To Count CPU Processors Or Cores

So, in today’s article we’re going to learn a few ways to count CPU processors or cores. This is something you might already know about the system in front of you, but may want to verify with a remote system.

To the Linux system, there’s little difference between an individual processor or a core, or even a thread. Each core will appear as its own processor – and that’s exactly how it should be. Linux will treat each as its own processor and do its best to take advantage of multi-core/multi-CPU systems.

Now… Hmm… I shouldn’t need to explain this, but your computer has a processor called a CPU. It may have more than one, though that’s unlikely in a personal computer. Instead, your CPU may have multiple processors on it – each functioning more or less independently as ‘cores’. Additionally, your CPU cores may have multiple threads and each of those threads will appear as its own CPU.

That’s a very simplistic overview and I have to do things like limit how many characters are in the title of the article – and I don’t really want to try to explain everything. 

For example, you could have a 4 core CPU that’s dual threaded. 4 x 2 = 8, so your system will see 8 CPUs. There’s only one physical processor, but you’re effectively working with 8 of them. (By the way, you can *sometimes* compile software optimized by the number of cores available.)

Count CPU Processors Or Cores:

(or threads, I suppose)

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Now, there are so many ways that you can count the CPU processors or cores. We’re just going to cover a few of them. Feel free to leave other solutions as a comment. I’ll approve the comments as quickly as I can.

Solution #1

We just recently used the ‘cpuinfo‘ file to learn about your CPU. So, we’ll start with that one first. We’ll process it with ‘grep’ and then do some counting:

It might look something like this:

output showing there are 8 CPUs available
You’ll have to use your imagination for the rest. I’m only making one screenshot.

Now, those are individual threads that it’s counting – on the same physical CPU. But, to the system they appear to be individual CPUs. I can assure you, I do not have 8 physical CPUs in my laptop.

Solution #2:

Now, your computer does technically know if it’s a physical CPU, CPU core, or CPU core thread. It just normally doesn’t much care. If your CPU and OEM vendor did things properly, you can learn that information using ‘dmidecode‘.

They do not always implement this properly and you will sometimes find mistakes when using ‘dmidecode‘. Let’s hope for the best when we run this more convoluted command:

That one uses egrep and picks out multiple words to include. If you run that command, you should find out that your computer really can tell the difference between cores and threads – and physical CPU count (though not with this command).

Solution #3:

This final solution is nice and easy. If you’re gonna memorize one of these, this would be the one to memorize. In fact, if you’re going to need to count CPU processors or cores, you might just as well use this one. It’s easy and effective.

That one will output the total number of CPUs, cores, or threads. That’s all it will output, just a single number. That number is how many CPUs (effectively) your computer sees – and that’s it. It’s pretty much perfect.

Closure:

There you go. You have another article, this one explaining how to count CPU processors or cores (or threads, really). It’s handy information to know, though you probably already know it. Like I mentioned above, you can optimize C/C++ code to run on the specific number of cores – and there are other reasons you might want to know this. 

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

Subscribe To Our Newsletter
Get notified when new articles are published! It's free and I won't send you any spam.
Linux Tips
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.