The essential
The key to high-performance network servers is concurrency, how to use multi-core servers efficiently, and allow multiple threads to process programs concurrently.
concurrency mode
- Time-based parallelism
- Space-based parallelism,
- Other references,
Within the computer, the most essential is the time (CPU) and Space (memory) of the two resources, the various parallel boundaries are not so obvious. The so-called time-based parallelism, there are two cats, co-guarding a large warehouse, but one work, the other sleeps, both do not work at the same time. The so-called space-based parallel, the warehouse is divided into two parts, so that two cats can be guarded by different parts, two cats can sleep when the work is not tense.
Network Concurrency Program Optimization
For the network concurrent program, can be optimized from various angles, first talk about a single UDP server, then talk about a single TCP server, and finally talk about the server cluster.
UDP server
A typical UDP server, such as a DNS server, has a disadvantage that it can only be published to clients through its well-known service port, where all clients ' traffic is concentrated on a single port, the data with a single thread reads the port is the most efficient, and any form of concurrency in reading sockets can result in degraded performance. For some versions of the kernel, multi-threaded receiving the same set of sockets will produce a surprise group phenomenon, which is mainly caused by the operating system process scheduling mode. Thus, a better pattern is a single thread, and then the data is distributed to different processing threads in the background, but the data passing between the receiving thread and the processing thread may need to be locked in such a way that the performance of the program is degraded (however, in the context of a split-master scenario, single-producer and single-Consumer, Can be implemented in a lock-free queue, which can greatly improve the performance of the program, if some form of lock, in order to avoid frequent locking unlock operation, a better improvement is the batch of data transfer, which can be hundreds of times times the reduction of the call to unlock, also can greatly reduce the thread's context switching overhead.
In the return phase of the data, the typical practice is to pass the data to the sending thread after the processing is finished, and then return the data through the original receive socket. So in the data transmission section, and encountered a single point bottleneck. As with read sockets, when sending data on a single socket, any concurrent writes can also cause a drop in the efficiency of the send, which appears to have to be sent using a single thread. But if we had more than one processing thread, they pooled the data into a single send thread queue, and inevitably encountered a lock-unlock overhead. Although reading and writing on the same socket is a different buffer for the system, transmit and receive performance can not affect each other, but in order to greatly improve the performance of the program, we must solve the transmission bottleneck. This is the use of address reuse, through multiple sockets to send data concurrently. If the client has requirements for the server's send port, we must bind multiple sets of words to the same address and port multiple times, resulting in a timing problem. Since the kernel is implemented, it can receive data in a nested word, only the last time the binding can be received. Therefore, the sending socket must be bound before the receive socket is bound, or the application will not receive any data. Through such processing, and then some of the optimization of hot spots, the UDP program can do to receive and send the Gigabit network card to run full. Of course, if you have a faster network card, you can read and write memory speed to process the data.
TCP Server
The TCP server is divided into two types
- A short connection server, such as a Web server,
- Some special long-connected servers.
For a single server, there is not a strong group of servers, which involves the deployment and load balancing strategy of the cluster , and finally, if there are too many servers, you need to resolve the dynamic join and exit of the server, as well as server security issues.