I/O完成端口的优缺点

发布于 2024-10-21 07:12:30 字数 142 浏览 6 评论 0原文

为什么很多人说 I/O 完成端口是一个快速且良好的模型?
I/O完成端口有哪些优点和缺点?

我想知道一些使 I/O 完成端口比其他方法更快的点。

如果能解释一下与其他模型(select、epoll、传统多线程/多进程)的比较就更好了。

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

I want to know some points which make the I/O completion port faster than other approaches.

If you can explain it comparing to other models (select, epoll, traditional multithread/multiprocess), it would be better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飞烟轻若梦 2024-10-28 07:12:30

I/O 完成端口非常棒。没有更好的词来形容他们了。如果 Windows 中的任何事情做得正确的话,那就是完成端口。

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

如果活动线程数已超过最大限制,它将等待其中一个线程完成,然后在进入等待状态后立即将事件传递给该线程。此外,它始终会按 LIFO 顺序唤醒线程,因此缓存很可能仍然处于热状态。

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

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

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

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.

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 timer or asynchronous 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.

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.

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

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.

潇烟暮雨 2024-10-28 07:12:30

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

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

您还可以在这本好书中阅读一些信息 http://www.amazon.com/Windows -系统编程-Johnson-Hart/dp/0321256190

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 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.

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

柒七 2024-10-28 07:12:30

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

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

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

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

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)

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文