= Get CPU information on Linux

Sometimes, you need to get information about the CPU on a machine, whether it's because you just can't remember what kind of CPU your computer has in it, or whether you need a specific detail, such as the number of cores, or whether it's 32 or 64 bit.
On Linux, there are X ways to get that information.

== GNOME Settings

If you use the GNOME desktop, you can see what kind of CPU you're using in the *Settings* application.

First, click on the *Activities* button in the top left of the GNOME desktop, and type *Settings*.
In the *Settings* window that appears, click *About* in the left column.

image:gnome-settings-about.jpg[GNOME Settings displays your OS and CPU information in the About panel.]

== KDE Kinfocenter

If you use the KDE desktop, find all the CPU information you could ever need in the *KInfocenter* application.

First, click the *K Menu* in the bottom left corner of the KDE Plasma Desktop, and select *Info Center*.
In the *Info Center* window, click *Devices* in the left column, and then *CPU*.

image:kinfocenter-devices-cpu.jpg[KDE Info Center displays detailed CPU specs.]

== Look at /proc/cpuinfo

While we often use the term "Linux" (or sometimes GNU+Linux) to refer to the operating system, it's a kernel first and foremost.
As the binary responsible for bootstrapping your system, Linux has information about everything it's managing, and that includes the CPU.
This data is stored in the `/proc` virtual file system, in a file named `cpuinfo`.
It's this file that applications like *KInfocenter* parses to deliver data about your CPU, but you can parse it yourself, too.

[source,bash]
----
$ cat /proc/cpuinfo | less
processor       : 0
vendor_id       : AuthenticAMD
cpu family      : 25
model           : 33
model name      : AMD Ryzen 5 5600X 6-Core Processor
stepping        : 0
microcode       : 0xa201009
cpu MHz         : 2200.000
cache size      : 512 KB
physical id     : 0
siblings        : 12
core id         : 0
cpu cores       : 6
[...]
----

== arch

If all you need to know is the architecture of your CPU, you can use the `arch` command:

[source,bash]
----
$ arch
x86_64
----

== uname

The `uname` command is usually used to print information about your kernel, but because your kernel is highly dependent on your CPU, there's a lot of CPU data you can extract from its output.
Specific to the CPU, you can get the vendor of your CPU with the `--hardware-platform` (`-i` for short) option:

[source,bash]
----
$ uname --hardware-platform
authenticAMD
----

You can also get the model of your CPU with the `--processor` (`-p` for short) option:

[source,bash]
----
$ uname --processor
AMD Ryzen 5 5600X 6-Core Processor
----

And finally, the architecture with `--machine` (`-m`):

[source,bash]
----
$ uname --machine
x86_64
----

== Programming libraries

Sometimes you need CPU information within a non-interactive interface.
On Linux, any command you type into an interactive terminal session can be scripted so that it's non-interactive, but sometimes you're working with  more than just a simple script.
If you're using a compiled programming language and you need to determine what architecture you're on, there are usually libraries for obtaining that information.
However, you may also be able to do some rudimentary tests to determine architecture without importing a special library.

There are too many programming languages for me to give an example on all of them, but here's a simple example in C++ of the kind of logic you might use:

[source,cpp]
----
#include <iostream>
using namespace std;

int main() {
    if ( SIZE_MAX == 0xFFFFFFFF ) {
      cout << "32 bit";
    } else if ( SIZE_MAX == 0xFFFFFFFFFFFFFFFF ) {
      cout << "64 bit";
    } else {
      cout << "Unknown";
    }
    return 0;
}
----

This code checks `SIZE_MAX` to determine the potential maximum size of a variable.
If it's set to 64-bit, then the CPU that compiled the code is capable of 64-bit.

That's not the only way to extract that kind of data, and if you have libraries that make it even easier then you can use those.

== Knowledge is power

Getting insight into what CPU you're running is a useful trick to know.
It may not come up often, but it's annoying when it does come up and you're caught out for the information you need.
I've covered all the ways I could think of to get CPU information in this article, but there are lots of other ways I haven't thought of.
How do you get CPU information on your systems?
