Read building a high-performance Web site server concurrency processing power-2

Source: Internet
Author: User
Tags epoll sendfile

System calls

The process has two modes of operation: User state and kernel state. The process is usually in the user state, when the CPU and memory can be used, and when the process needs to operate on the hardware peripherals (such as reading disk files, sending network data), it must switch to the kernel state, when the kernel-state task is completed, the process is then cut back to the user state.

Because the system call involves the process from the user state to the kernel state of the switch, resulting in a certain amount of memory space exchange, which is a certain degree of context switching, so the cost of system calls is usually more expensive.

Reducing unnecessary system calls is also an aspect of Web server performance optimization.

Memory allocation

Apache's memory usage at runtime is staggering, mainly due to its multi-process model, which allows Apache to request large amounts of memory as a memory pool at the start of the run. The Nginx memory allocation policy, which uses multithreading to handle requests, makes it possible to share memory resources between multiple threads, thus greatly reducing its overall memory usage, and Nginx maintains 10,000 inactive HTTP persistent connections requiring only 2.5MB of memory.

    • Persistent connections

Persistent connections (keep-alive) are also known as long connections. http/1.1 has a complete definition of long connections, and the HTTP request data cast contains life on long connections:

Connection:keep-alive


The efficient use of long connections can reduce the overhead of re-establishing connections and effectively accelerate performance. For a multi-process model such as Apache, if the long connection timeout is too long, even if the browser does not have any requests, and Apache still maintain the connected child process, once the number of concurrent users, Apache will maintain a large number of idle processes, serious impact on server performance.

I/O model

Some people say that bit is born to not replicate, the life of the data is the input and output.

In fact, how to make the high-speed CPU and slow I/O devices better coordinated, this is from the birth of modern computers to the current exploration of the topic.

PIO and DMA

Long ago, the data transfer between disk and memory was CPU-controlled, that is, how to read the disk file into memory, the data to be forwarded through the CPU storage, this method is called Pio.

Later, DMA (direct memory access) instead of PIO, it can exchange disk and memory data directly without the CPU. In DMA mode, the CPU only needs to release instructions to DMA, the DMA to handle the transfer of data, DMA through the system bus to transmit data, the transfer is complete notification of the CPU, which reduces the CPU share.

Synchronous blocking i/o650) this.width=650; "Src=" http://misc.zhangsr.cn/resources/img/blog/attachment/201502/1_20150204174710. JPG "alt=" 1_20150204174710.jpg "/>

Blocking refers to the process that is currently initiating I/O operations being blocked, not the CPU being blocked.

For example, for example, you go shopping, hungry, you see the snack city, in a noodle shop bought a bowl of noodles, pay the money, noodles do need time, you know when you can do a good job, have to sit there and so on, and so on noodles finish eating and then continue shopping. -Eating noodles Here is an I/O operation.

Synchronous non-blocking i/o650) this.width=650; "Src=" http://misc.zhangsr.cn/resources/img/blog/attachment/201502/1_20150204180739. JPG "alt=" 1_20150204180739.jpg "/>

In synchronous blocking I/O, the process actually waits for two parts, one to wait for the data to be ready, and the other to wait for the data to be copied (copy data from Kenrel to user).

Calls to synchronize non-blocking I/O do not wait for data to be ready, and if the data is unreadable or not writable, it tells the process immediately.

Back to the story of buying noodles, if you are not willing to wait for the noodles to go shopping, but also worry about noodles do not pick up in time, so you stroll and then run back to see if the noodles do well, and many times, finally although even eat noodles, but tired of panting.

Multi-Channel I/O readiness notification 650) this.width=650; "Src=" http://misc.zhangsr.cn/resources/img/blog/attachment/201502/1_ 20150205125946.jpg "alt=" 1_20150205125946.jpg "/>

The advent of multiple I/O readiness Notifications provides a high-performance scenario for a large number of file descriptor readiness checks, which allows a process to monitor all file descriptors at the same time and quickly get the file descriptors that are ready for you, and then access the data only for those file descriptors.

Back to the story of buying noodles, join you not only bought a face, but also in other snack bars to buy dumplings, porridge, pies, etc., these things need time to make. In the synchronous nonblocking I/O model, you have to turn to the snack bar to ask for progress. Now that you have introduced multiple I/O ready notifications, the snack city has installed an electronic screen in the lobby, which will be displayed on the screen after all the food in the snack bar, so that you just need to look at the big screen at intervals, and maybe you can also visit nearby shops.

It is important to note that I/O readiness notifications are just a quick way to get ready file descriptors, and when we know that we are ready to access the data itself, we need to choose a blocking or non-blocking approach.

There are many different implementations of multiple I/O readiness:

Select

Select was first seen in the 4.3BSD in 1983 and is used by a select () system to monitor an array with multiple file descriptors, and when select () is put back, the ready file descriptor in the array is changed by the kernel to the flag bit. Allows the process to obtain these file descriptors for subsequent read and write operations.

Select is currently supported on all platforms, but one disadvantage of select is that a single process can monitor the maximum number of file descriptors, which is typically 1024 on Linux, but can be improved by modifying the macro definition or even recompiling the kernel.

In addition, the data structure of a large number of file descriptors maintained by select () increases linearly with the number of file descriptors, as well as the cost of replication. At the same time, because the latency of the network response time makes a large number of TCP connections inactive, but calling select () will have a linear scan of all sockets, so this can also waste some overhead.

Poll

Poll was born in 1986 to System V RELEASE3 (UNIX), and it is not substantially different from Select, except that there is no limit on the number of files to monitor, and the select disadvantage applies to poll.

In addition, when select () and poll () file descriptors are ready to tell the process, if the process does not do I/O to it, the file descriptors will be reported again the next time they are called, so there is generally no loss of the ready notification message, which is called a horizontal trigger (level triggered).

SIGIO

Linxu2.4 provides Sigio, which implements the Select/poll notification method through real time Signal, but the difference is that Select/poll tells us which file descriptors are ready until we read and write. Every time Select/poll tells us, and Sigio tells us which file descriptor has just become ready, it only says it again, and if we do not take action, it will not tell us that this is called edge-triggering (edges triggered).

/dev/poll

Sun provides a new implementation in Solaris that uses a virtual/dev/poll device, you can write an array of file descriptors to be monitored, and then wait for a time notification via the IOCTL (), and when the IOCTL () is put back in the Ready file descriptor, you can start from/dev/ An array of all ready file descriptors is read in poll, similar to Sigio.

There are many ways to implement/dev/poll-like devices under Linux, but none provide direct kernel support, which is unstable when the server is under heavy load.

/dev/epoll

Subsequently, the device named/dev/epoll appears in an indefinite form on the Linux2.4, which provides functionality similar to/dev/poll, and adds memory mapping (MMAP) technology to some extent to improve performance.

However,/dev/epoll is still just a patch, and Linux2.4 does not add its implementation to the kernel.

Epoll

Until Linux2.6 came up with a kernel-direct-support implementation, which is epoll, which is recognized as the best-performing multi-I/O readiness notification method under Linxu2.6.

Epoll can support both horizontal and edge start-up, by default, the Epoll takes a horizontal trigger, and if you want to use the edge, you need to increase the Epollet option when the event is registered.

In the Nginx Epoll model generation (SRC/EVENT/MODULES/NGX_EPOLL_MODULE.C), you can see that it uses edge triggering:

ee.events = Epollin | Epollout | Epollet;

Another essential improvement is that Epoll uses the event-based readiness notification method. In Select/poll, the kernel scans all monitored file descriptors only after a certain method is called, and Epoll notifies Epoll_ctl () to register each file descriptor, once a file descriptor is ready, The kernel uses a callback mechanism like callback to quickly activate the file descriptor and be notified when the process calls Epoll_wait ().

Back to the purchase of the story, although the electronic screen, but the display is the status of all food, including the production and has been done, which obviously caused you trouble reading, as if select/poll every time to return all the monitored file descriptor, if you can only display good food, The snack city was then improved, just as/dev/poll just told the ready file descriptor. When displaying a good food, if it is displayed only once, and whether you see it or not, it is the equivalent of starting at the edge, and if it is displayed every time before you receive it, it is equivalent to a level trigger.

But even so, once you're far away, you have to go back to the snack city to see the electronic screen, can you make it easier to get notified? Snack city to take a mobile phone message notification method, you only need to go to the Food City Management Office registration, you can in the meal ready to receive timely SMS notification, which is similar to the Epoll event mechanism.

Memory mapping

The Linux kernel provides a special way to access disk files, which can associate an in-memory block of address space with a specified disk file, which translates the access of this memory into a disk file, a technique known as memory Mapping.

Using memory mapping can improve disk I/O performance, it does not need to use a system call such as read () or write () to access the file, but rather through mmap () system calls to establish an association of memory and disk files, and then access the disk like memory.

Direct I/O

In Linux2.6, memory-mapped and direct-access disk files are not inherently different because the data is replicated two times from the process user-state memory space to the disk, which is "disk-to-kernel buffer" and "kernel buffer--user-state memory space".

The purpose of introducing kernel buffers is to improve the access performance of disk files, because when a process needs to write a disk file, it is actually only the kernel buffer that tells the process that it has succeeded. However, for some complex applications, such as database servers, they want to bypass the kernel buffers and implement and manage I/O buffers themselves in the user-state space in order to fully improve performance.

LINXU provides support for this requirement by adding the parameter option O_direct in the open () system call, and the file opened with it bypasses direct access to the kernel buffer, thus avoiding the overhead of CPU and memory.

In MySQL, for the InnoDB storage engine itself, it is possible to manage the cache of data and indexes, so reliance on kernel buffers is not so important.

Sendfile

In the process of requesting a static file from the Web server, the data of the disk file goes through the kernel buffer, then to the user state memory space, because it is not necessary to process the static data, so they are sent to the network card corresponding to the kernel buffer, and then sent to the network card to send.

The data goes out of the kernel and goes back to the kernel without any changes. In the kernel of Linux2.4, a kernel-level Web server program called KHTTPD is introduced, which only handles requests for static files. The purpose of the introduction is that the kernel wants the requested processing to be done in the kernel as much as possible, reducing the switching of the kernel state and the cost of user-state data replication.

Linux provides this mechanism to the developer through system calls, which is the sendfile () system call. It can transfer specific portions of a disk file directly to the socket descriptor that represents the client.

asynchronous i/o650) this.width=650; "Src=" http://misc.zhangsr.cn/resources/img/blog/attachment/201502/1_20150210191402.jpg "alt=" 1_20150210191402.jpg "/>

Blocking and non-blocking refers to whether the process needs to wait if the data accessed by the process is not yet ready.

Synchronous and asynchronous refers to the mechanism to access data, synchronous refers to the request and wait for the completion of the I/O operation, when the data is ready to be blocked while reading and writing, asynchronous refers to the request data can continue to process other tasks, and then wait for the I/O operation is completed, which makes the process not block when the data read and write.

Reference article: analysis of high performance IO model


—————————— This article is published synchronously in ZHANGSR my personal blog ——————————

Read building a high-performance Web site server concurrency processing power-2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.