09 Process Management and Performance Tuning


  • .

A process is an instance of a program running in an operating system. Process management involves monitoring, controlling, and manipulating processes.

Basic Commands for Process Management

ps

ps is used to list running processes.

  1. List all processes:
ps -aux
  • -a: All users.
  • -u: List users and other details.
  • -x: Include processes without terminals.
  1. List specific processes:
ps -u username
  • Lists processes run by a specific user.

top

top is an interactive tool for monitoring processes in real-time.

  1. Run the command:
top

Displayed information:

  • PID: Process ID.
  • USER: Process owner.
  • %CPU: CPU usage.
  • %MEM: Memory usage.

kill

Used to stop a process by sending a signal.

  1. Stopping a process based on PID:
kill PID
  1. Sending a specific signal:
kill -9 PID
  • -9: Signal to stop the process forcibly.

htop

htop is an alternative to top with a friendlier interface.

  • Install htop:
sudo zypper install htop
  • Run htop:
htop

Monitoring CPU and Memory Usage

CPU Monitoring

  • The vmstat command

vmstat provides information about CPU, memory, and I/O.

  • Run:
vmstat 1
Displays statistics every 1 second
  • The mpstat command

mpstat is used to monitor CPU usage per core.

  • Install:
sudo zypper install sysstat
  • Run:
mpstat -P ALL
  • The sar command

sar records CPU usage statistics.

  • Run:
sar -u 1 5

Description:

  • -u: CPU statistics.
  • 1 5: Every 1 second, 5 times.

Memory Monitoring

  • free command

free is used to view memory usage.

  • Run:
free -h

Description:

  • -h: Displays the size in easy-to-read format (GB/MB).

  • vmstat command In addition to CPU, vmstat also provides memory information.

  • Run:

vmstat 1

System Performance Optimization and Tuning

Kernel Tuning with sysctl

sysctl is used to change kernel parameters.

  • View kernel parameters:
sysctl -a
  • Change temporary parameters:
sudo sysctl -w vm.swappiness=10
  • Change permanent parameters: Add to /etc/sysctl.conf:
vm.swappiness=10
  • Apply changes:
sudo sysctl -p

Disk I/O Optimization

  • Use a tool like iotop to monitor disk I/O:
sudo zypper install iotop
iotop
  • Use a filesystem like XFS or Btrfs for high performance.

CPU Optimization

  • Limit CPU usage for a specific process with cpulimit :
sudo cpulimit -l 50 -p PID

Description :

  • -l: CPU limit in percent.

Swap and Memory Management

Swap

Swap is a space on the disk that is used as additional memory when RAM is full.

Viewing Swap Information

  • Use the swapon command:
swapon –show
  • Or use free:
free -h

Adding Swap

  1. Create a swap file:
sudo fallocate -l 1G /swapfile
  1. Change file permissions:
sudo chmod 600 /swapfile
  1. Format as swap:
sudo mkswap /swapfile
  1. Enable swap:
sudo swapon /swapfile
  1. Add to /etc/fstab for automatic activation:
/swapfile swap swap defaults 0 0

Setting Swappiness

Swappiness determines how often the system uses swap.

  1. View the swappiness value:
cat /proc/sys/vm/swappiness
  1. Change the temporary value:
sudo sysctl vm.swappiness=10
  1. Change the permanent value: Add to /etc/sysctl.conf:
vm.swappiness=10