Common Linux optimizations

Swappiness Virtual Memory

Swappiness virtual memory is a kernel parameter for Linux. When swap virtual memory is used by the control system, the relative weight of memory usage can be set between 0 and 100. The lower this parameter value is, the less swap virtual memory partitions will be used and the more memory will be used by the Linux system.

The higher the parameter value, the opposite, which makes the kernel use more swap space, the recommended setting is 10, depending on the server hardware configuration

# View parameter values for swap partitions
cat /proc/sys/vm/swappiness

# Modify the value of the swap partition: temporary
sysctl -w vm.swappiness=10

# Modify the value of the swap partition: Permanent
echo "vm.swappiness=10" >> /etc/sysctl.conf

Turn off dynamic memory allocation (memory page transparency)

Transparent Huge Pages (huge THP transparent pages) have been introduced since version 6 of CentOS. This feature is enabled by default starting with version 7 of CentOS. Transparent HugePages dynamically allocate memory at runtime, while standard HugePages pre-allocate memory at system startup and do not change at runtime

Because Transparent HugePages dynamically allocate memory at run time, it can cause memory allocation delays at run time

Therefore, although THP is meant to improve memory performance, some database vendors recommend that THP be turned off directly (such as Cloudera, ORACLE, MariaDB, MongoDB, etc.) or performance may degrade.

# Provisional Effectiveness
echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo never > /sys/kernel/mm/transparent_hugepage/enabled

# Effective Permanently
echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >> /etc/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local

Maximum File Handles

Large data services can open very large number of file handles. Add similar content by editing/etc/security/limits.conf to increase restrictions

# This line refers to the specified user, preceded by * on behalf of all users
* hard nofile 50000
* soft nofile 50000

Maximum Forked Processes

Configuration allows a large number of threads to be generated. To increase the number allowed on Linux, edit/etc/security/limits.conf

* hard  nproc  10000
* soft  nproc  10000

# Releases need to be added to edit/etc/security/limits.d/20-nproc.conf
* soft nproc 10000

Threads limit for normal user startup

Modify the maximum number of threads an average user can create

# Centos6
sudo vi /etc/security/limits.d/90-nproc.conf
# Centos7
sudo vi /etc/security/limits.d/20-nproc.conf

#  Modify to a specified quantity, for example

* soft nproc 4096

Number of TCP Socket Ports (Increase the number of TCP socket ports available)

Increase the number of TCP socket ports available, which is especially important if your process creates and removes a large number of sockets in a very short time

sudo sysctl -w net.ipv4.ip_local_port_range ="10000 65000"

Reduce Socket idle time

When a socket connection is idle for too long, it affects concurrency. Setting the time for a socket to remain in TIMED_WAIT state can quickly create and destroy new sockets

sudo sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait ="1"

Normal user enlarges virtual memory

Error message description: max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144] (Maximum virtual memory preference is too small)

# First adjustment: temporary adjustment, exit session login will be invalid (configure in test environment)
sudo  sysctl -w vm.max_map_count=262144    

# Second: Permanent Effectiveness (Configuration in Production)
sudo vim /etc/sysctl.conf
# Add a line at the end
vm.max_map_count=262144

Improving IO performance

By default, linux records the time at which a file is accessed. The file system records some time stamps of the file when it is accessed, created, modified, etc. For example, when the file was created, last modified, and last accessed; this is unnecessary in most cases.

Because a large number of files are accessed while the system is running, reducing some actions (such as reducing the number of time stamp records, etc.) will significantly improve disk IO efficiency and file system performance.

If you encounter high machine IO load or high CPU WAIT, try using noatime and nodiratime to disable logging of the last access timestamp, which will improve throughput

# Edit/etc/fstab, that is, change to 0. My Tencent cloud server is already 0 by default
/dev/mapper/centos-root /      xfs     defaults,noatime        0 0
UUID=47f23406-2cda-4601-93b6-09030b30e2dd /boot     xfs     defaults        0 0
/dev/mapper/centos-swap swap     swap    defaults        0 0

# Modified Remount
mount -o remount /
perhaps
mount -o remount /boot

Keywords: Linux Database

Added by bubblybabs on Sun, 19 Sep 2021 03:20:11 +0300