2 Methods to Get CPU Feature Flags on Linux

2 Methods to Get CPU Feature Flags on Linux

If you're a Linux enthusiast, system administrator, or a developer working on optimizing software for specific hardware, understanding the feature set of the CPU is crucial. Every CPU comes with a set of feature flags that determine its capabilities and supported instructions. These flags are essential for optimizing code, ensuring compatibility, and making informed decisions about software configurations. This tutorial provides 2 methods how to get CPU feature flags on Linux.

Method 1 - /proc/cpuinfo file

The /proc/cpuinfo file holds detailed information about the system's CPU, including its feature flags.

Run the following command in the terminal to get CPU feature flags:

awk -F': ' '/flags/{gsub(" ", "\n", $2);print $2;exit}' /proc/cpuinfo

Output example:

fpu
vme
de
pse
tsc
msr
...

Let's break down the command step by step:

  • awk - is the command line tool, which is used to process text data line by line.
  • -F': ' - this option specifies the field separator. In this case, it's set to a colon followed by a space. This means that the lines will be split into fields based on this separator.
  • /flags/ - this part of the command is a pattern that tells awk to search for lines that contain the word flags. These lines typically contain information about CPU features.
  • gsub(" ", "\n", $2) - it replaces spaces with newline characters in the second field of the matching line. The second field, usually contains a list of CPU feature flags separated by spaces. By replacing spaces with newline characters, each feature flag will be printed on a new line.
  • print $2 - after replacing spaces with newline characters, this command prints the second field of the matching line. It is a list of CPU feature flags, each on a separate line.
  • exit - it tells awk to exit after processing the first line that matches the pattern. The CPU feature flags don't change between CPU cores on a single processor, there's no need to process multiple lines.

Method 2 - lscpu command

The lscpu command provides details regarding the CPU architecture and associated information, encompassing the CPU feature flags.

Open a terminal and just execute:

lscpu | awk -F': *' '/Flags/{gsub(" ", "\n", $2);print $2}'

Let's break down the command step by step:

  • lscpu - this command is used to display detailed information about the CPU on the system.
  • awk - is the command line tool, which is used to process text data line by line.
  • -F': *' - it specifies the field separator, which is set to a colon followed by zero or more spaces. This separator is used to split the lines into fields.
  • /Flags/ - it tells awk to search for lines containing the word Flags.
  • gsub(" ", "\n", $2) - it replaces spaces in the second field. The second field of the matching line contains a list of CPU feature flags separated by spaces. By replacing spaces with newline characters, each feature flag will be printed on a new line.
  • print $2 - it prints the modified second field, which now contains the CPU feature flags, each on a separate line.

Leave a Comment

Cancel reply

Your email address will not be published.