Apache monitoring and tuning (7) MPM related parameters

Common related parameters of the MPM module include: MaxSpareServers, MinSpareServers, ServerLimit, StartServers, ThreadsPerChild, MaxConnectionsPerChild, MaxRequestWorkers, ListenBackLog, ListenCoresBucketsRatio, MaxMemFree and ReceiveBufferSize.

1) MaxSpareThreads

表示最大空闲线程数
语法:MaxSpareThreads number
适合的MPM模块:event,worker,mpm_netware,mpmt_os2

This directive sets the maximum number of idle child processes. The so-called idle child process refers to a child process that is not processing requests. If there are currently more than MaxSpareServers idle child processes, the parent process will kill the excess child processes. This parameter only needs to be adjusted on very busy machines. This parameter usually cannot be set too large until the number of idle threads is less than this number. If you set the value of this directive to be smaller than MinSpareServers , Apache will automatically modify it to "MinSpareServers+1".

For workers and events , the recommended default value is MaxSpareThreads 250.

For mpm_netware the default is MaxSpareThreads 100.

mpmt_os2 works with mpm_netware . For  mpmt_os2 the default value is 10.

Notice:

The range of MaxSpareThreads values ​​is restricted. Apache httpd will automatically correct the given value according to the following rules:

Ø  The value of  mpm_netware needs to be greater than MinSpareThreads .

Ø For workers and events , this value must be greater than or equal to the sum of  MinSpareThreads and  ThreadsPerChild .

2) MinSpareServers

表示可用于处理请求峰值的最小空闲线程数
语法:MinSpareThreads number
适合的MPM模块:event,worker,mpm_netware,mpmt_os2

Represents the minimum number of idle threads to handle peak requests. Different MPMs handle this directive differently.

The default value of this item for worker module and event module is MinSpareThreads 75. If the number of idle threads in the server is less than the set value, child threads will be created until the number of idle threads is greater than the minimum number of idle threads we set.

If there are not enough free threads in the server, child processes will be created until the number of free threads is greater than number . If ListenCoresBucketsRatio  is enabled, additional processes/threads may be created. The default value for the mpm_netware module is MinSpareThreads 10, and for  the mpmt_os2 module the default value is 5.

3) ServerLimit

表示可配置进程数的上限
语法:	ServerLimit number
适合的模块:	event,worker,prefork

For preforkMPM, it is configured through MaxRequestWorkers , because preforkMPM only generates one thread as a child process. For worker and eventMPM, the maximum value will be configured through the two parameters ThreadLimit and MaxRequestWorkers . The MaxRequestWorkers directive can be modified during server restart.

When using this instruction, special attention must be paid to the fact that the value of ServerLimit cannot be set much higher than the actual value used. If the value is set too large, a lot of memory that we do not need to use will be allocated. If both ServerLimit and MaxRequestWorkers are set higher than the system can handle, Apache httpd may fail to start, or the system may become unstable.

Note: Generally speaking, the maximum value of ServerLimit can be set to 20000, which is limited by the server. If you need to set it to a larger value, you need to modify the MAX_SERVER_LIMIT value in the mpm source file.

4) StartServers

表示服务器启动时创建的子进程数
语法:StartServers number
合适的MPM模块:event,worker,prefork,mpmt_os2

The StartServers directive is used to set the number of child server processes created at startup. The value of StartServers is usually dynamically adjusted based on  MinSpareThreadsMaxSpareThreadsMinSpareServers , and MaxSpareServers .

The default value varies from MPM to MPM. The default value of worker and event is StartServers 3, the default value of prefork is 5, and the default value of mpmt_os2  is 2.

5) ThreadsPerChild

表示每个子进程创建的线程数
语法:ThreadsPerChild number
适合的模块:event,worker,mpm_winnt

This instruction means to set the number of threads created by each process. When the server starts, it first creates the number of sub-processes, and then creates threads. If the mpm_winnet module is used, only one sub-process will be generated, so the instruction setting should be greater than the maximum load handled by the server. , if the worker module is used, multiple child processes will be generated, and the total number of threads should be greater than the load of the server.

The default value of mpm_winnt directive is 25. The default value of ThreadsPerChild for other MPMs is 64.

The value set by ThreadsPerChild cannot exceed the value of ThreadLimit . If a higher value is configured, it will automatically decrease and log a warning log message on startup.

6) MaxConnectionsPerChild

表示每个线程最多可以处理的连接数
语法:	MaxConnectionsPerChild number
默认:	MaxConnectionsPerChild 0
模块:	event,worker,prefork,mpm_winnt,mpm_netware,mpmt_os2
兼容性:	可用的Apache HTTP Server 2.3.9和更高版本。老的版本该参数为MaxRequestsPerChild 。

The MaxConnectionsPerChild directive mainly sets the maximum number of connections that a single sub-process can handle. If the number of connections served by the child process reaches this maximum value, the child process will be killed. If the value of MaxConnectionsPerChild is set to 0, it means that the child process can handle an unlimited number of connections. Setting MaxConnectionsPerChild to a non-zero value can limit the problem of a process consuming too much memory due to memory leaks.

7) MaxRequestWorkers

描述:	同时处理的最大连接数
句法:	MaxRequestWorkers number
模块:	event,worker,prefork

The MaxRequestWorkers instruction is mainly used to set the maximum number of connections that the server can handle at the same time. If it exceeds the set value, queuing will occur. The maximum queuing value is set by the ListenBacklog instruction. During the queuing process, only when a request  ends The child process will then be released for use by other connection services.

For this non-threaded service MPM module (such as prefork), the MaxRequestWorkers directive will be converted into the maximum number of child processes of the server, which is the value of ServerLimit. The default value of the MaxRequestWorkers directive is 256.

For MPM modules that generate multi-threaded classes (such as event  or worker ), the MaxRequestWorkers directive will be used to limit the number of server client connections. For mixed MPM, the default ServerLimit value is 16 and the default ThreadsPerChild value is 25. In this case, the value set by the MaxRequestWorkers directive must be greater than the product of 16 times 25.

Before version 2.3.13, the previous version of the MaxRequestWorkers directive was called MaxClients.

8) ListenBacklog

表示挂起连接队列的最大长度,即排队的队列找度
语法: ListenBackLog backlog
默认: ListenBackLog 511
适合的模块: event,worker,prefork,mpm_winnt,mpm_netware,mpmt_os2

The ListenBackLog command is used to set the connection queue length. The default value is 511. Generally, we do not need to set or adjust this command, but if some systems are attacked by TCP SYN, this value can be increased appropriately.

9) ListenCoresBucketsRatio

表示在线CPU核数与监听桶的比率
语法:	 	ListenCoresBucketsRatio ratio
默认值:	ListenCoresBucketsRatio 0 (disabled)
适合的模块:		event, worker, prefork

This option has two core contents that need to be understood. One is the number of online CPU cores; the other is the listening bucket.

First, we introduce what online CPU cores are. The kernel uses 4 bitmaps to save the CPU core in 4 states: possible, present, active and online. Where online represents the number of online CPU cores. There is a file online in the /sys/devices/system/cpu directory that records the number of all online CPU cores currently.

The Linux operating system will call to enable smp multi-core during initialization. cpuhotplug can automatically open cores according to the CPU load to achieve a balance between performance and power consumption. Finally, the idle CPU will enter the cpuidle state. The cpuhotplug principle is shown in the figure.

To study the listening bucket, you must first understand the process of TCP connection and the relationship between TCP connection and socket.

The TCP connection process is shown in the figure.

The listen function is used to listen to the socket that has been bound to addr+port through the bind() function. After listening, the socket changes from the CLOSE state to the LISTEN state, and this socket can provide a TCP connection window to the outside world. The connect() function is used to initiate a connection request to a listening socket, which is to initiate the TCP three-way handshake process.

So what is the relationship between TCP connections and sockets? Each TCP connection, whether client or server, is associated with a socket and the file descriptor pointed to by the socket. When the server receives the ACK message, it means that the three-way handshake has been completed and the TCP connection between the client and the server has been established. After the TCP connection is established, the TCP connection will be placed in the established queue opened by listen() to wait for the accept message. At this time, the socket associated with the TCP connection is the listen socket and the descriptor pointing to the file.

When the TCP in the established queue is accepted by accept(), it will be associated with the socket specified by accept() and allocate a new file descriptor. That is to say, after accept(), this connection and the listen socket The connection has nothing to do with it anymore.

Generally, an addr+port can only be bound to one socket, which means that addr+port cannot be reused, and different sockets can only be bound to different addr+ports.

The threads that monitor the socket are all preemptive monitors. There can only be one listening thread monitoring or using the listening socket at the same time. When this listening thread receives the request, it will give up its listening qualifications. At this time, other monitoring threads will grab the monitoring right, but only one thread can grab the monitoring right at the same time. Other working processes are shown in the figure.

Under normal circumstances, addr+port can only be bound to one socket. If the address and port are reused, the combination is socket reuse. In the current Linux kernel, address reuse is supported. The socket option SO_REUSEADDR supports port reuse. socket option SO_REUSEPORT. After setting the port reuse option, binding the socket is equivalent to binding two or more addr+ports to one instance. For the listening process/thread, the socket that is reused each time is called a listening bucket, that is, each listening socket is a listening bucket. Taking httpd's worker or event model as an example, assume that there are currently N sub-processes, and each sub-process contains a listening thread and N worker threads. Its working process is shown in the figure.

Using address reuse and port reuse technology is equivalent to binding multiple sockets with the same addr+port. As shown in Figure 10-9, three sockets are bound to a listening bucket, and there will be three threads to monitor the three sockets at the same time. However, each socket still has the same logic as the address is not reused and the port is not reused. It is a preemptive way to obtain monitoring rights.

The advantage of address reuse and port reuse is that it can reduce the competition for mutex locks during monitoring, avoid the "starvation problem", improve monitoring efficiency, and achieve better load balancing, but this is also limited by the CPU core. If For a single-core CPU, there is no advantage in address reuse and port reuse because the number of threads is not enough.

Now you can understand the meaning of the ListenCoresBucketsRatio option, which is to set the ratio of online CPU cores to listening buckets.

10) MaxMemFree

表示在不调用free分析内存的情况下允许分配器保留的最大空闲内存数。
语法:	 	MaxMemFree KBytes
默认值:	MaxMemFree 2048
适合的模块:		event,worker,prefork,mpm_winnt,mpm_netware

In MPM threads, each thread has its own allocator. This parameter indicates the maximum amount of free memory that each allocator is allowed to maintain when the free() function is not called to release memory. If set to zero, the threshold is unrestricted.

11) ReceiveBufferSize

表示TCP接收的数据时的缓存大小
语法:	ReceiveBufferSize bytes
默认值:	ReceiveBufferSize 0
适合的模块:	event, worker, prefork, mpm_winnt, mpm_netware, mpmt_os2

Used to set the size of the buffer area when TCP receives data. If set to 0, it means that the value of the operating system shall prevail.

The above are the common command settings involved in the MPM module.

Guess you like

Origin blog.csdn.net/meimeieee/article/details/126736193