Essential CPU Commands in Linux: A Complete Guide to Monitoring and Tuning

Introduction on CPU Commands

In the world of Linux, understanding how to manage and monitor your CPU is crucial for maintaining optimal performance. This blog post dives deep into the essential CPU commands, helping you view CPU information, monitor usage, and tune performance effectively.

CPU commands

1. Viewing CPU Information

1.1 lscpu

The lscpu command provides detailed information about the CPU architecture, including the number of CPUs, cores, threads, and more.

Syntax:

lscpu [options]

Example:

lscpu

Key Flags:

  • -e : Displays a detailed listing of CPUs in a comma-separated format.
  • -p : Displays CPU information in a parsable format.
  • -J : Outputs information in JSON format.

Explanation:

When you run lscpu, you might see output similar to this:

Architecture:            x86_64
CPU(s):                     8
Thread(s) per core:  2
Core(s) per socket:  4
Socket(s):                 1

This output gives you a quick overview of the CPU’s architecture and capabilities.

1.2 cat /proc/cpuinfo

The /proc/cpuinfo file contains detailed information about each CPU core.

Syntax:

cat /proc/cpuinfo

Example:

cat /proc/cpuinfo | less

Key Flags:

  • | less : Pipes the output to the less command for easy navigation.

Explanation:

The output will include information like:

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model            : 142
model name  : Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz

This command provides extensive details about each CPU core, making it a powerful tool for in-depth analysis.

2. Monitoring CPU Usage

2.1 top

The top command is a dynamic real-time view of running processes, including CPU usage.

Syntax:

top [options]

Example:

top

Key Flags:

  • -d <seconds> : Set the delay time between updates.
  • -u <user> : Show only processes for a specified user.

Explanation:

When you run top, you’ll see an interface that displays:

  • CPU usage percentages
  • Memory usage
  • Running processes

You can press q to quit the command.

2.2 htop

htop is an interactive process viewer for Unix systems, providing a more user-friendly interface compared to top.

Installation:

To install htop, use:

sudo apt install htop   # For Debian/Ubuntu
sudo yum install htop   # For CentOS/RHEL

Syntax:

htop

Explanation:

Running htop gives you a colorful display of CPU usage, memory usage, and running processes. You can easily navigate and manage processes directly from the interface.

2.3 mpstat

The mpstat command provides CPU usage statistics by CPU core.

Installation:

Install the sysstat package if it’s not already installed:

sudo apt install sysstat   # For Debian/Ubuntu
sudo yum install sysstat   # For CentOS/RHEL

Syntax:

mpstat [options] [interval] [count]

Example:

mpstat -P ALL 1 5

Key Flags:

  • -P ALL : Show statistics for all CPUs.
  • 1 : Interval in seconds between each report.
  • 5 : Number of reports to generate.

Explanation:

The output will display CPU usage statistics for each core, helping you identify performance bottlenecks.

3. CPU Performance Tuning

3.1 cpupower

cpupower allows you to manage CPU frequency scaling and settings.

Installation:

To install cpupower:

sudo apt install linux-tools-common linux-tools-generic  # For Debian/Ubuntu
sudo yum install kernel-tools  # For CentOS/RHEL

Syntax:

cpupower [command] [options]

Example:

cpupower frequency-info

Explanation:

This command displays the current CPU frequency, scaling driver, and available frequencies.

3.2 cpufrequtils

cpufrequtils provides utilities for setting CPU frequency scaling.

Installation:

To install cpufrequtils:

sudo apt install cpufrequtils  # For Debian/Ubuntu
sudo yum install cpufrequtils  # For CentOS/RHEL

Syntax:

cpufreq-set [options]

Example:

cpufreq-set -g performance

Key Flags:

  • -g <governor> : Set the CPU frequency governor (e.g., performance, powersave).

Explanation:

This command sets the CPU governor to performance, optimizing it for maximum performance.

Conclusion

Understanding and utilizing CPU commands in Linux is essential for effective system management and performance tuning. By mastering commands like lscpu, top, and cpupower, you can ensure that your system runs efficiently and responds to workload demands. For further reading and advanced topics, explore my website: GeekersHub. Additionally, you can find more resources on CPU performance tuning at Intel’s official site.

FAQs

  1. What is the purpose of the lscpu command?
  • It provides detailed information about the CPU architecture.
  1. How can I monitor real-time CPU usage in Linux?
  • Use the top or htop commands.
  1. What does the mpstat command do?
  • It shows CPU usage statistics for each CPU core.
  1. How can I install htop on my system?
  • Use the package manager for your Linux distribution.
  1. What is CPU frequency scaling?
  • It adjusts the CPU frequency based on the current workload.
  1. How can I change the CPU governor?
  • Use the cpufreq-set command with the appropriate flags.
  1. What does cat /proc/cpuinfo display?
  • It shows detailed information about each CPU core.
  1. What is the difference between top and htop?
  • htop offers a more user-friendly interface than top.
  1. How do I check CPU usage per core?
  • Use the mpstat -P ALL command.
  1. Can I set CPU performance profiles in Linux?
    • Yes, using cpupower or cpufrequtils.
  2. What package provides the mpstat command?
    • It is part of the sysstat package.
  3. How can I check how many CPU cores I have?
    • Run the lscpu command.
  4. What does the cpupower frequency-info command do?
    • It displays information about the current CPU frequency settings.
  5. Can I run htop without installing it?
    • No, htop needs to be installed first.
  6. What are CPU governors?
    • They are algorithms that manage CPU frequency scaling based on workload.

For additional resources and detailed guides, visit GeekersHub.