windows I/O 完成端口的优缺点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5283032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:10:37  来源:igfitidea点击:

I/O completion port's advantages and disadvantages

windowsnetwork-programmingiodevice-driveriocp

提问by Benjamin

Why do many people say I/O completion port is fast and nice model?
What is the I/O completion port's advantages and disadvantages?

为什么很多人说 I/O 完成端口是快速和漂亮的模型?
I/O 完成端口的优缺点是什么?

I want to know some points which make faster IOCP than other model.

我想知道一些比其他模型更快的 IOCP。

If you can explain it comparing other models(select, epoll, traditional multi thread/process), it would be better.

如果你能比较其他模型(select、epoll、传统的多线程/进程)来解释它,那就更好了。

回答by Damon

I/O completion ports are awesome. There's no better word to describe them. If anything in Windows was done right, it's completion ports.

I/O 完成端口很棒。没有更好的词来形容它们。如果 Windows 中的任何事情都做对了,那就是完成端口。

You can create some number of threads (does not really matter how many) and make them all block on one completion port until an event (either one you post manually, or an event from a timerorasynchronous I/O, or whatever) arrives. Then the completion port will wake one thread to handle the event, up to the limit that you specified. If you didn't specify anything, it will assume "up to number of CPU cores", which is really nice.

您可以创建一定数量的线程(实际上并不重要)并使它们全部阻塞在一个完成端口上,直到事件(您手动发布的一个,或者来自计时器异步 I/O的事件,或其他任何事件)到达. 然后完成端口将唤醒一个线程来处理事件,直到您指定的限制。如果您没有指定任何内容,它将假定“最多 CPU 核心数”,这非常好。

If there are already more threads active than the maximum limit, it will wait until one of them is done and then hand the event to the thread as soon as it goes to wait state. Also, it will always wake threads in a LIFO order, so chances are that caches are still warm.

如果已经有比最大限制更多的线程处于活动状态,它将等待其中一个线程完成,然后在线程进入等待状态时立即将事件交给线程。此外,它总是会以 LIFO 顺序唤醒线程,因此缓存可能仍然是温暖的。

In other words, completion ports are a no-fuss "poll for events" as well as "fill CPU as much as you can" solution.

换句话说,完成端口是一种轻松的“事件轮询”以及“尽可能多地填充 CPU”的解决方案。

You can throw file reads and writes at a completion port, sockets, or anything else that's waitable. And, you can post your own events if you want. Each custom event has at least one integer and one pointer worth of data (if you use the default structure), but you are not really limited to that as the system will happily accept any other structure too.

您可以在完成端口、套接字或其他任何可等待的地方抛出文件读取和写入。而且,您可以根据需要发布自己的活动。每个自定义事件至少有一个整数和一个数据指针(如果您使用默认结构),但您并不仅限于此,因为系统也很乐意接受任何其他结构。

Also, completion ports are fast, really really fast. Once upon a time, I needed to notify one thread from another. As it happened, that thread already had a completion port for file I/O, but it didn't pump messages. So, I wondered if I should just bite the bullet and use the completion port for simplicity, even though posting a thread message would obviously be much more efficient. I was undecided, so I benchmarked. Surprise, it turned out completion ports were about 3 times faster. So... faster and more flexible, the decision was not hard.

此外,完成端口很快,真的非常快。曾几何时,我需要从另一个线程通知一个线程。碰巧的是,该线程已经有一个用于文件 I/O 的完成端口,但它没有泵送消息。所以,我想知道我是否应该咬紧牙关,为了简单起见使用完成端口,即使发布线程消息显然会更有效率。我犹豫不决,所以我进行了基准测试。令人惊讶的是,完成端口的速度大约快了 3 倍。所以……更快更灵活,这个决定并不难。

回答by Sanja Melnichuk

by using IOCP, we can overcome the "one-thread-per-client" problem. It is commonly known that the performance decreases heavily if the software does not run on a true multiprocessor machine. Threads are system resources that are neither unlimited nor cheap.

通过使用 IOCP,我们可以克服“每个客户端一个线程”的问题。众所周知,如果软件不在真正的多处理器机器上运行,则性能会严重下降。线程是既不无限也不便宜的系统资源。

IOCP provides a way to have a few (I/O worker) threads handle multiple clients' input/output "fairly". The threads are suspended, and don't use the CPU cycles until there is something to do.

IOCP 提供了一种让几个(I/O 工作线程)线程“公平地”处理多个客户端的输入/输出的方法。线程被挂起,并且在有事情要做之前不要使用 CPU 周期。

Also you can read some information in this nice book http://www.amazon.com/Windows-System-Programming-Johnson-Hart/dp/0321256190

你也可以在这本好书中阅读一些信息http://www.amazon.com/Windows-System-Programming-Johnson-Hart/dp/0321256190

回答by Stephen Chung

I/O completion ports are provided by the O/S as an asynchronous I/O operation, which means that it occurs in the background (usually in hardware). The system does not waste any resources (e.g. threads) waiting for the I/O to complete. When the I/O is complete, the hardware sends an interrupt to the O/S, which then wakes up the relevant process/thread to handle the result. WRONG: IOCP does NOT require hardware support (see comments below)

I/O 完成端口由 O/S 作为异步 I/O 操作提供,这意味着它发生在后台(通常在硬件中)。系统不会浪费任何等待 I/O 完成的资源(例如线程)。当 I/O 完成时,硬件向 O/S 发送中断,O/S 将唤醒相关进程/线程来处理结果。 错误:IOCP 不需要硬件支持(见下面的评论)

Typically a single thread can wait on a large number of I/O completions while taking up very little resources when the I/O has not returned.

通常,单个线程可以等待大量 I/O 完成,同时在 I/O 尚未返回时占用很少的资源。

Other async models that are not based on I/O completion ports usually employ a thread pool and have threads wait for I/O to complete, thereby using more system resources.

其他不基于 I/O 完成端口的异步模型通常使用线程池并让线程等待 I/O 完成,从而使用更多的系统资源。

The flip side is that I/O completion ports usually require hardware support, and so they are not generally applicable to all async scenarios.

另一方面是 I/O 完成端口通常需要硬件支持,因此它们通常不适用于所有异步场景。