Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Nginx thread pool model

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to understand the Nginx thread pool model". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the Nginx thread pool model.

Nginx's IO model should be known to everyone. To put it simply, it is a master process and multiple worker processes (the number of processes is determined by the configuration); the master process is responsible for accept requests and queuing, and finally forwards to the worker process for request processing and response.

Nginx runs in multi-process mode. Nginx provides multithreading (multi-threading) in version 1.7.11, but this multithreading is only used for local file operations in the aio model, starting with non-blocking mode to improve the efficiency and concurrency of file IO.

So instead of nginx dealing with proxy request in a multi-threaded way (partly through epoll mode), this multithreading is used to process some local static files.

Here are a few basic instructions: sendfile, aio, and directio, all of which are related to the operation of local files, so let's take a look at their meaning.

Sendfile

This instruction has the same semantics as the system function sendfile (), and the purpose of sendfile is to improve the efficiency of sending local files through socket. The official blog describes how to take advantage of the nginx thread pool aio to achieve 9x performance.

It also has a more memorable name, called Zero copy. What's the difference between that and traditional files that are read and sent to the network?

Disk, network drive, and memory are three different transmission media. If you read a file locally and send it through socket, you usually go through the following steps:

1) the disk drive reads a certain length (chunk) of byte data from the disk according to the schedule of CPU.

2) Byte data copy to kernel memory

3) copy the data in kernel memory to the process workspace memory

4) the process copy the data to the network drive cache through socket and send it out through the corresponding transport protocol.

As you can see, the process of sending data involves multiple copy, which is limited by the design of the computer system.

The main starting point of sendfile is to reduce the copy of data to improve the efficiency of sending. Sendfile is a call at the system level of linux. Socket can directly access file data through DMA (direct memory access) and send it through the transfer protocol, reducing two times of data copy (disk to kernel, kernel to workspace).

The sendfile_max_chunk parameter is used to limit the maximum big data size sent by each sendfile () call. If the size is not limited, the entire worker process will be monopolized. The default is "unlimited". This is so bossy.

For nginx, agent static local static file resources (usually small files) will be very efficient, it is recommended to enable this parameter for some static files such as html, images, etc.

Location / video {sendfile on; sendfile_max_chunk 128k; aio on;}

Directio

This directive is used to turn on the use of the O_DIRECT tag (BSD,linux), corresponding to the system call directio ().

This parameter is set for large files and sendfile for small files. The limited size can be specified through directio, and for files that exceed this size, directio will be used (instead of sendfile).

According to the original design intention of directio, it has the basic principles of sendfile, but does not use the kernel cache, but directly uses DMA, and the memory cache (page alignment part) will be released after use.

Therefore, directio is usually suitable for reading large files, and the frequency of reading is usually very low. Because it doesn't improve efficiency for high-frequency reads (because it doesn't reuse cache, it DMA every time). Due to performance tradeoffs, this parameter defaults to off.

Location / video {sendfile on; directio 8m; aio on;}

Aio

When it comes to the aio model, the semantics are basically the same, except that the asynchronous file IO,nginx turns off this feature by default and needs to be supported on a higher version of the linux platform (2.6.22 +).

On linux, directio can only read blocks based on 512 byte boundary alignment, and unaligned block at the end of the file will be read in blocking mode.

Similarly, if the file is not aligned at the beginning, the entire file will be blocked to read. The so-called alignment here is the cache situation in the memory page of the file data.

When both aio and sendfile are enabled, the aio mechanism will be used for files whose size is greater than the directio setting: that is, files less than the directio setting will directly use sendfile (aio does not participate).

Aio, simply put, uses multithreaded asynchronous mode to read large files to improve IO efficiency, but in fact it may not improve at all. Because the reading of large files can not use cache, and itself is time-consuming, even if it is multi-threaded, the waiting time for request is unpredictable, especially when the concurrent requests are high. But it is certain that aio can improve the concurrency ability of IO.

By default, multithreading mode is off, and we need to enable it through-- with-threads configuration. This feature is compatible on platforms that support epoll and kqueue. For thread pool settings, we can declare them through thread_pool and specify them in the aio directive.

Our profile has been enriched a little bit further.

Thread_pool default_pool threads=16;##main context... Location / video {sendfile on; sendfile_max_chunk 128k; directio 8M; aio threads=default_pool;}

When all threads in the thread pool are in the busy state, new task requests will be added to the waiting queue. We can use the max_queue parameter in thread_pool to specify the queue size. The default queue size is 65536. Subsequent requests will be thrown error when the queue is full.

At this point, I believe you have a deeper understanding of "how to understand the Nginx thread pool mode". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report