Nginx IO model theory

Was a dollar to buy a video about an organization Nginx found the theory speak good benefit, plan to spend the next few days to learn the way to do study notes

Httpd MPM

  1. the prefork : process model, two-stage structure, the main master process responsible for generating subprocess, each responsible for a respective sub-process a request.
  2. worker : threading models, tertiary structure, the primary master process responsible for generating subprocess, each sub-process responsible for generating a plurality of threads, each thread in response to a request.
  3. Event : threading models, tertiary structure, the primary master process responsible for generating sub-processes, each sub-process in response to a plurality of requests.

Apache encountered on traditional high concurrency problems, to solve the C10K problem, so Nginx came into being.
Apache is used in select models, Nginx using a Epoll model .

I / O process

  1. Http client sends a request to the server.
  2. Through the physical layer up to the application layer that is Nginx, Nginx work in user space, as a process can not access the hardware directly.
  3. Nginx sends a request to the kernel, the kernel to obtain the complete file, xx.html from the disk.
  4. Xx.html kernel will be loaded from disk files into Kernel memory space (buffer), for a long time, because it is read into memory disk.
  5. Copy the data from the kernel buffer to the user process's memory space, time is short, as is the memory swap

I / O models

Network I / O: Socket is essentially read
the disk I / O:

Synchronous / asynchronous: communication mechanism is concerned that the message
synchronization : caller waiting to be returned to the caller information in order to proceed.
Asynchronous : the caller through the state, notification or callback mechanism to notify the operating state of the caller is the caller.

Blocking / non-blocking: the caller's attention in the state it was before waiting for the results returned
blocking : Blocking, refers to the need to complete the operation until after the completion of IO to return to user space, call returns, the caller is suspended.
Non-blocking : Non-Blocking, refers to the IO operation returns immediately after being called a status value to the user, without waiting for IO operations fully completed before the final call returns, the caller will not be suspended.

  • Synchronous blocking: energy consumption, serious waste of resources
  • Non-blocking synchronization: not good use, but also a serious waste of resources
  • Asynchronous blocking: pure waste of resources
  • Asynchronous non-blocking: two things that interfere with each other, the most rational allocation of resources

Five kinds of IO model used in the production of

  • Synchronous blocking IO model :
  1. The easiest IO model, the user thread is blocked IO operations in the kernel.
  2. Thread calls through the system read user initiates an IO read by the kernel space to user space, kernel, and so the packet arrives, the received data copied to the user space, to complete the read operation.
  3. When the user needs to wait for the read data to the read buffer, in order to continue processing the data received, the entire IO request process, user thread is blocked, causing the user to initiate IO requests can not do anything, the low utilization rate of the CPU resources, generated waste.
  • Synchronous non-blocking IO model :
  1. Since the synchronization process repeatedly calling recvfrom polling process, after waiting as long as the kernel packet arrives, repeated calls asking if complete halt.
  2. Nature and did not work the same, but more CPU consumption, because of the constant polling.
  3. Consistent follow-up process and synchronous blocking model, generally do not use this for.
  • IO multiplexing model :
  1. After select the application process as an agent (concurrent), and the request does not deal directly with the kernel, similar groups under a single, then finish loading the kernel buffer data, that returns a message similar to the big screen told to do the business.
  2. Copy data from user space to kernel space, the same continuous process, wherein the model is blocked on select, for a blocking object.
  3. During copy the data to the application buffers, the process is still blocked, is still a synchronization model.
  4. The entire process is equivalent to two obstruction, but the benefits are multiple processes share a select, enhance the efficiency of the overall terms but limited.
  5. Usage scenarios: a TCP server listening socket is necessary to deal with, but also to deal with the connected socket; multi-service multi-protocol; it is necessary to deal with TCP and UDP have to deal with.
  • IO signal drive models :
  1. The first step to achieve non-blocking, establish SIGIO signal handler, when the kernel buffer to get datagram return SIGIO, do not block the process at this time, you can do a lot of other thing.
  2. The second step subsequent kernel buffer datagram assigned to the user space process is still blocked.
  3. Not much used in production.
  • Asynchronous IO model :
  1. Do all to the kernel, the user process do not do anything, all the pressure to the kernel.
  2. Without any obstruction, but this is still a problem of asynchronous non-blocking.
  3. Only Microsoft's windows to achieve a true non-blocking asynchronous, IOCP.

Embodied I / O model

  1. select: cross-platform, I / O multiplexing model.
    select the underlying implementation is an array, using traversal search results back to the process, inefficient, maximum number of connections 1024 handle.
    poll list underlying implementation is, still using the traversal, the maximum number of connections is no upper limit.

  2. Poll: Only linux version, improved version of the select.

  3. ePoll: only linux version, the corresponding I / O multiplexing model, have certain characteristics of the signal driven I / O model,
    epoll underlying implementation is a hash table, using the callback ways to find results back to the process, IO using the event notification method, the kernel copy data buffer space for each user and the like due notified independently of each other, the maximum number of connections is no upper limit.
    epoll also supports memory copy, use mmap (Memory Mapping, memory-mapped) and accelerate messaging kernel space, reduce replication costs, official gives up to 30,000 concurrent consumption issues as well as the port number, process consumes 30,000 is the limit the

mmap concept

  1. Three: Nginx applications, memory, disk
  2. The disk storage blocks in the inode group Block index data and real data, together open up space in the hard disk data, completely mapped into memory, fully one-byte map.
  3. As long as access to the memory space, you will find the inode omitted layers of the process, it reads the required data directly from the disk, thus improving efficiency.
  4. This change in data memory space mapped, then directly written to disk, multiple copies of the process is omitted.

Level trigger and edge trigger

Trigger level: If the report has not been processed fd, fd it will report again when the next poll.
Edge Trigger: Notify only once, epoll unique, relatively higher efficiency and better performance.

Better secondary development of products
Tengine: Taobao special
OpenResty: the pursuit of the ultimate high-performance

Published 49 original articles · won praise 18 · views 4002

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/90247735