Apache Performance Optimization Analysis (4)

Source: Internet
Author: User
Tags continue mutex return socket
apache| Performance | Optimization But this realization can cause serious hunger problems. Because multiple child processes execute this loop at the same time, they will block in the select. When a request appears on any socket, all blocked processes are recovered and returned from select (the number of wake processes depends on the operating system and time). They will continue to execute and attempt to accept the connection, but only one process will succeed (assuming there is still only one connection), and the rest of the process will block in accept. This locks all failed processes so that they serve only the requests on one socket. They will always be blocked until there is enough request on that socket to wake them up. This hunger problem was first presented in pr#467. There are at least two ways to solve it.

One scenario is to use a non-blocking socket. In this case, accept will not block the child processes and they will return immediately. But this kind of scheme can cause the waste of CPU time. Suppose there are 10 idle processes in the Select, and then a connection request arrives. Nine processes will wake up, attempt to accept connections, fail, and return to select, which do not actually do anything. And if there is a request on the other socket during this time, no process will serve it. All in all, this is not very effective unless you have a cpu--with the number of free child processes.

Another option was adopted by Apache. This scheme is serialized (serialize) The call to the inner loop. The code looks like this (the improved part is shown in bold):

for (;;) {
Accept_mutex_on ();
for (;;) {
Fd_set Accept_fds;

Fd_zero (&accept_fds);
for (i = First_socket i <= last_socket; ++i) {
Fd_set (i, &accept_fds);
}
rc = Select (last_socket+1, &accept_fds, NULL, NULL, NULL);
if (RC < 1) continue;
New_connection =-1;
for (i = First_socket i <= last_socket; ++i) {
if (Fd_isset (i, &accept_fds)) {
New_connection = Accept (I, NULL, NULL);
if (new_connection!=-1) break;
}
}
if (new_connection!=-1) break;
}
Accept_mutex_off ();
Process the new_connection;
}
 
The accept_mutex_on and Accept_mutex_off two functions implement mutexes (mutual exclusion semaphore), and only one child process can have a mutex at any given time. There are several ways to achieve mutual exclusion. Src/conf.h (prior to 1.3) or src/include/ap_config.h (version 1.3 and beyond) may make the following selections. Some systems do not provide any mutex methods. It is not safe to use multiple listen commands on these systems.

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.