Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Swoole optimizes kernel parameter adjustment


May 14, 2021 Swoole


Table of contents


Swoole optimizes kernel parameter adjustment

ulimit settings

ulimit -n to be adjusted to 100,000 or more. U limit -n 100,000 can be modified by executing at the command line. If it cannot be modified, you need to set /etc/security/limits.conf to join

* soft nofile 262140
* hard nofile 262140
root soft nofile 262140
root hard nofile 262140
* soft core unlimited
* hard core unlimited
root soft core unlimited
root hard core unlimited

Kernel settings

net.unix.max_dgram_qlen = 100

Swoole uses unix socket dgram for inter-process communication, which needs to be adjusted if the volume of requests is large. The system defaults to 10 and can be set to 100 or larger.
Or increase the number of worker processes and reduce the number of requests allocated by a single worker process.

net.core.wmem_max

Modify this parameter to increase the memory size of the socket cache

net.ipv4.tcp_mem  =   379008       505344  758016
net.ipv4.tcp_wmem = 4096        16384   4194304
net.ipv4.tcp_rmem = 4096          87380   4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_tw_reuse

Whether it's socket reuse, the purpose of this function is that the listening port can be quickly reused when the server restarts. If this parameter is not set, the server restarts with a failure to start if the port is not released in time

net.ipv4.tcp_tw_recycle

With socket quick recycling, the short connection server needs to turn this parameter on

Message queue settings

This kernel parameter needs to be adjusted when using message queues as a means of communication between processes

  • kernel.msgmnb is 4203520, the maximum number of bytes in the message queue
  • kernel.msgmni is 64, allowing up to a maximum number of message queues to be created
  • kernel.msgmax is 8192, the maximum length of a single data in a message queue

FreeBSD/MacOS

  • sysctl -w net.local.dgram.maxdgram=8192
  • sysctl -w net.local.dgram.recvspace-200000 modify the buffer area size of Unix Socket

Turn on CoreDump

Set kernel parameters

kernel.core_pattern = /data/core_files/core-%e-%p-%t

View the limits of the current coredump file with the ulimit -c command

ulimit -c

If it is 0, you need to modify /etc/security/limits.conf to set the limit.

When core-dump is turned on, the process is exported to the file in the event of an exception to the program. It is of great help to investigate procedural issues

Other important configurations

  • net.ipv4.tcp_syncookies=1
  • net.ipv4.tcp_max_syn_backlog=81920
  • net.ipv4.tcp_synack_retries=3
  • net.ipv4.tcp_syn_retries=3
  • net.ipv4.tcp_fin_timeout = 30
  • net.ipv4.tcp_keepalive_time = 300
  • net.ipv4.tcp_tw_reuse = 1
  • net.ipv4.tcp_tw_recycle = 1
  • net.ipv4.ip_local_port_range = 20000 65000
  • net.ipv4.tcp_max_tw_buckets = 200000
  • net.ipv4.route.max_size = 5242880

See if the configuration is in effect

For example: modify net.unix.max after the use of the net.dgram?qlen

cat /proc/sys/net/unix/max_dgram_qlen

If the modification is successful, here is the value of the new setting.