Understand IO Completion Port

xiaoxiao2021-03-06  64

Welcome to this IOCP tutorial. I will give the IOCP definition and give its implementation method, and finally analyze an echo program to open the IOCP puzzle cloud, remove your heart on IOCP. OK, but I can't guarantee that you understand everything IOCP, but I will do my best. The following is the related art I will mention in this article: I / O port synchronization / asynchronous blocked / non-blocking server / client multi-threaded program Design Winsock API 2.0

Prior to this, I have developed a project, one of which needs to support the code, and only use SELECT, CONNECT, Accept, Listen, Send and Recv, plus a few #ifdef Package is used to deal with the incompatibility between Winsock and BSD socket [socket], a network subsystem is written for only a few hours, and I still make me a good aftertaste. It didn't touch it for a long time. A few days ago, we plan to make a online game, I took the initiative to bear this piece, think about this is not a small CASE, I am stealing. Online games, online games provide fun and homologous game experiences for hundreds of players, they fight each other or join the team to overcome the common enemies. I am confident that I am ready to write my network, so I found that the past block synchronous mode mode can't get a huge number of multi-player [MMP] architectures, directly denied it. So, there is an IOCP. If you can easily lose IOCP, there will be this tutorial. Please follow me to enter the topic.

What is IOCP first let's take a look at the I / O completion port for IOCP may be the most complex kernel object provided by Win32. [Advanced Windows 3rd] Jeffrey Richter This is the best way to implement high-capacity web servers. [Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports] Microsoft Corporation completed the port model to provide the best scalability. This model is very suitable to handle hundreds and even thousands of sockets. [Windows Network Programming 2nd] Anthony Jones & Jim Ohlund I / O Completion Ports is particularly important because they are the only technique for high load servers [must maintain many connection lines at the same time. The Completion Ports uses some threads to help balance the load caused by the I / O request. Such architectures are particularly suitable for "Scalable" servers generated in the SMP system. [Win32 multi-threaded program] Jim Beveridge & Robert Wiener

It seems that we have a complete reason to believe that IOCP is the first choice for large network architecture. What is the IOCP? Microsoft introduced this concept of IOCP in Winsock2. IOCP full name I / O Completion port, Chinese translated into I / O completion port. IOCP is an Asynchronous I / O API that delivers the I / O event to the application efficiently. Unlike the use of select () or other asynchronous methods, a socket [socket] is associated with a completion port, and then proceeds to normal Winsock operations. However, when an event occurs, this completion port will be added to a queue by the operating system. The application can then query the core layer to get this complete port. Here I am going to add some of the above concepts. Before explaining [Finish], I want to simply let the synchronization and asynchronous concepts, logically let another after doing one after another. It is to synchronize, while doing two or two things together is asynchronous. You can also take a single thread and multi-threaded metaphor. But we must separate synchronous and blockage, asynchronous, and non-blocking distal distalbsor, so-called clogging functions such as Accept (...), after calling this function, at this time thread will hang until the operating system is notified, "Hey Brothers Some people come in, "that the hangs will continue to work, which is also in line with the" producer-consumer "model. Blocking and synchronizing seems to have two points, but it is a completely different concept. Everyone knows that I / O equipment is a relatively slow device. Regardless of the printer, modem, even hard drives, compared with the CPU, it is uncomfortable, and the completion of I / O is a unwise thing. Sometimes the flow rate of data is very amazing, move the data from your file server with Ethernet speed, speed may be up to one million bytes per second, if you try to read 100KB from the file server, in the user's vision It is almost instantaneous completion, but you have to know that your thread executes this command, has been wasted 10 1 million CPU cycles. Therefore, we generally use another thread to perform I / O. Overlap IO [Overlapped I / O] is a technology of Win32, you can ask the operating system to transfer data for you and inform you when transferred. This is also the meaning of [completion]. This technology allows your program to continue to handle transactions during I / O. In fact, the inside of the operating system is threaded to complete overlapped I / O. You can get all the interests of threads without having to pay for the price of pain. Completing the so-called [port] in the port is not the port mentioned in TCP / IP, it can be said that it is completely no relationship. I didn't want to pass an I / O device [I / O DEVICE] and port [IOCP port]. It is estimated that this port is also confused. IOCP is only used to read and write operations, and it is similar to file I / O. Since it is a read and write device, we can ask for it just to deal with read and write efficient. The third part of the article You will easily discover the true intention of the IOCP design.

What is the relationship between IOCP and the network? int main () {WSAStartup (MAKEWORD (2, 2), & wsaData); ListeningSocket = socket (AF_INET, SOCK_STREAM, 0); bind (ListeningSocket, (SOCKADDR *) & ServerAddr, sizeof (ServerAddr)); listen (ListeningSocket, 5) ; int nlistenAddrLen = sizeof (clientAddr); while (TRUE) {NewConnection = accept (ListeningSocket, (SOCKADDR *) & clientAddr, & nlistenAddrLen); HANDLE hThread = CreateThread (NULL, 0, ThreadFunc, (void *) NewConnection, 0, & dwTreadId) CloseHandle (hthread);} return 0;} I believe that as long as friends who write the network should be familiar with such structures. The rear thread is hanged, waiting for a customer to issue a request, and then create a new thread to handle the request. When the new thread handles the customer request, the initial thread loops goes back to wait for another customer request. The thread processing that handles the customer request is ended. In the above concurrent model, a thread is created for each customer request. It is advantageous that the thread waiting for the request only needs to do very little work. Most time, the thread is in sleep [because the RECV is in a plug state]. But when the concurrent model is applied to the server side [Windows NT], the Windows NT team notes that the performance of these applications is unpredictable. Special, handling a lot of customer requests means that many threads are running concurrently in the system. Because all these threads are running [not hanging and waiting for something], Microsoft realizes that the NT core takes too much time to convert the context of running thread [context], threads have not got a lot of CPU time. Do their work. Everyone may also feel the bottleneck of parallel models in it created a new thread for each customer request. Creating a thread is smaller than the creation process overhead, but it is far from not overhead. We may wish to imagine: If you have a good N thread in advance, let them in the HOLD [Clog], then you can deliver all users' requests to a message queue. Then the N thread takes the message from the message queue one by one. You can avoid the shutdown of each user request. Not only reduces the resources of threads, but also improves the utilization rate of threads. Theoretical is very good, what will I do if I have to think about it? How will Microsoft don't think about it ?! The solution to this problem is a kernel object called I / O completed port, he first Windows NT3.5 is introduced. In fact, our idea should be almost the design mechanism of IOCP. In fact, it is not a message queue! You said this and [port] What are you connected? My understanding is that IOCP is an interface that is communicated with the application and operating system. As for the specific design of IOCP, I am also very difficult to say, after all, I haven't seen the code implemented, but you can simulate it, but you may ..., if you want to understand the IOCP, Jeffrey Ritchter Advanced Windows 3rd in which 13 Chapters and 14th have a lot of valuable content, you can take a look at how the system is all.

Implementation method Microsoft provides corresponding API functions for IOCP, mainly two, let's take a look at: Handle FileHandle, // Handle to File Handle ExistingCompletionPort, // Handle To I / O Completion Port Ulong_ptr CompletionKey , // Completion Key DWORD NUMBEROFCONCURRENTTHREADS / / NUMBER OF Threads to Execute ConcURRENTLY; first pay attention to this function to be used for two distinct purposes before discussing each parameter: 1. Used to create a completed port object 2. Associate a handle [Handle] and complete port together to create a completion of a port, we only need to fill in the parameter of NumberOfConcurrentThreads. It tells the system a maximum number of threads that are allowed to operate simultaneously on the port. By default, the number of wires is the same, but the number of CPUs is the same, but the experience gives us a formula: thread number = CPU number * 2 2 To make the completed port is useful, you must associate it the same or more devices. This is also done by calling Createiocompletionport. You have to pass an existing completion of the port to this function. We have to handle the online event, that is, the customer's socket is passed as a Handle. And a completed button [a 32-bit value for you, it is a pointer, the operating system doesn't care what you are. When you associate a device to the port, the system adds a message to the device list of the completion port. Another API is BOOL GetQueuedCompletionStatus (HANDLE CompletionPort, // handle to completion port LPDWORD lpNumberOfBytes, // bytes transferred PULONG_PTR lpCompletionKey, // file completion key LPOVERLAPPED * lpOverlapped, // buffer DWORD dwMilliseconds // optional timeout value); first The parameter indicates which completion port is to be monitored. Many service applications are just using an I / O completion port, and all I / O requests are completed, and they will be sent to this port. Simply put, getQueuedCompletionStatus causes the calling thread to hang until one of the specified port I / O completes a queue or until the timeout. The third data structure associated with the I / O completion port is to make the thread get information in completing the I / O item: the number of bytes transmitted, the completion keys, and the address of the overlapped structure. This information is returned to thread by passing to the LPDWNumberofByTestransferred, LPDWCompletionKey and LPOVERLAPPED parameters of GetQueuedCompletionsatus.

According to the things that have already been mentioned so far, first build a frame. Let you explain how to develop an Echo server using the completed port. Based on: 1. Initialize Winsock 2. Create a completion port 3. Create a certain amount of threads depending on the server thread 4. Prepare a socket for bind then Listen 5. Enter Cyclic Accept Waiting for the customer request 6. Create a data structure Socket and other related information 7. Connect the Socket associated with the completion port 8. After delivery, you will continue to repeat 5 to 8 processes, we will use the specific code to show the details of the details. WOW, the code of the program is posted here, it is really a scenery, can't Ctrl V can't f7. If you need the source code, you can send it to me o_nono@163.net to this article, there should be a paragraph, I brought it You have made a whirlwind tour, explore the so-called completion port. Many details cannot be detained in detail due to the relationship between the space. But I hope this article will bring you more thinking. If you have any questions, you can send it to o_nono@163.net. This is a post I am in VC version http://expert.9cbs.net/expert/topic/2646/2646592.xml?temp =.8504755 The problem is what the overlapping port overlap is what it means to help everyone. However If you look at the article above, you should do it well, overlap is a bit concept. When the CPU performs your code, you encounter an I / O request [such as read and write files.], The system generates an interrupt, let the CPU complete this I / O request, wait until the completion, the system generates an interrupt again The original program continues to run. It is also said that the synchronization between the two is held by interruption. The terminal can be understood as the amount of signal of hardware. This is the so-called synchronization concept, only in a thread can only process an I / O request at the same time, an I / O operation is very time consuming, when your code hangs, wait for I / O to complete this time Inside, your thread isted to waste N instruction cycles. If you want to read a big file repeatedly, the efficiency of synchronization is very low.

In order to solve this problem, after the CPU encounters an I / O request, the system is the system to open an internal thread to process the I / O request, and your thread does not hang, but you may I will feel that if I / O is still not completed, the subsequent code will make me do it, I don't do it? If the following code is related to this I / O operation, then it will wait, wait until this I / O operation is complete, you can get the I / O completion by calling waitformultiobject () and getoverlappedResult () in a thread. The message is then processed accordingly. But if the subsequent code is independent of this I / O operation, you can go with a faster speed, without waiting for the IO request to complete this is asynchronous.

You want you to have such a request, ie readfile (...) -1writefile (...) -2readfile (...) -3 You use synchronization in the program, then when you complete 1 Will continue, 2 will continue after execution. This is synchronization.

If you use asynchronous, when the system encounters 1, OK, open a thread to complete the IO request, then the system continues to run 2, 3, and open two threads separately. 1-2-3 If it is a time-consuming operation, especially in the network, then 1-2-3 These three IO requests are parallel, that is, overlapping. The overlapping I / O is capable of handling multiple I / O at the same time, in fact, you can do multiple threads yourself can also handle multiple I / O, of course, after the internal optimization of the system is stronger than you, huh, huh.

I just simply said that overlap [overlapped] did not give you an analysis from the point of view. I hope you can understand the overlapping IO. Take a look at the Windows network programming, not a model, and finally mention the shortcomings of the overlapping model, he has opened a thread for each IO request, and when there is 1000 requests, the system processing thread context [context] Switch is also It's very time consuming, so this also triggered the completion of the port model IOCP, using the thread pool to solve this problem, I will not say much. May you succeed!

转载请注明原文地址:https://www.9cbs.com/read-111444.html

New Post(0)