I/O Completion Ports

翻译 2015年07月07日 18:45:53

 

I/O Completion Ports
I/O completion ports provide an efficient threading model for processing multiple asynchronous I/O requests on a multiprocessor system.
When a process creates an I/O completion port, the system creates an associated queue object for requests whose sole purpose is to service these requests.
Processes that handle many concurrent asynchronous I/O requests can do so more quickly and efficiently by using I/O completion ports
in conjunction with a pre-allocated thread pool than by creating threads at the time they receive an I/O request.

I/O完成端口提供在一个多核处理器系统上面处理多个异步I/O请求的高效线程模型.
当创建一个I/O 完成端口时,操作系统创建与I/O完成端口相关联的队列对象来处理这些I/O请求.
I/O完成端口使用和一个预先分配好的线程池相结合,而不是接收I/O请求时才创建线程的方式来高效快速的处理多个并发异步I/O请求.

How I/O Completion Ports Work
The CreateIoCompletionPort function creates an I/O completion port and associates one or more file handles with that port.
When an asynchronous I/O operation on one of these file handles completes, an I/O completion packet is queued in first-in-first-out (FIFO) order to the associated I/O completion port.
One powerful use for this mechanism is to combine the synchronization point for multiple file handles into a single object, although there are also other useful applications.
Please note that while the packets are queued in FIFO order they may be dequeued in a different order.

CreateIoCompletionPort()可以创建一个I/O 完成端口并且可以和一个或者多个Socket相管理.
当这些异步Socket完成时,一个I/O 完成端口数据包将按照先进先出(FIFO)顺序在完成端口上排队.
注意:虽然数据包按照FIFL顺序排队,但是不一定按照FIFL顺序出队列.

When a file handle is associated with a completion port, the status block passed in will not be updated until the packet is removed from the completion port.
The only exception is if the original operation returns synchronously with an error.
A thread (either one created by the main thread or the main thread itself) uses the GetQueuedCompletionStatus function to wait for a completion packet to be queued to the I/O completion port,
rather than waiting directly for the asynchronous I/O to complete.
Threads that block their execution on an I/O completion port are released in last-in-first-out (LIFO) order, and the next completion packet is pulled from the I/O completion port's FIFO queue for that thread.
This means that, when a completion packet is released to a thread, the system releases the last (most recent) thread associated with that port,
passing it the completion information for the oldest I/O completion.

当一个Socket和一个完成端口相关联时,直到the packet 从完成端口移除时,阻塞状态才被更新.
一个线程使用GetQueuedCompletionStatus()函数来等到数据包在完成端口上面排队,而不是等到异步I/O操作的完成.
在I/O完成端口上面的阻塞线程将按照后进先出顺序被释放,然而,数据包是按照FIFL顺序从完成端口上弹出等待线程处理.
这就意味着,当一个数据包从队列中弹出时,将是线程池中最后一个被释放的线程.


Although any number of threads can call GetQueuedCompletionStatus for a specified I/O completion port,
when a specified thread calls GetQueuedCompletionStatus the first time, it becomes associated with the specified I/O completion port until one of three things occurs:
1 The thread exits,
2 specifies a different I/O completion port
3 closes the I/O completion port
In other words, a single thread can be associated with, at most, one I/O completion port.

当指定的线程第一次调用GetQueuedCompletionStatus()时,线程将和指定的完成端口相关联,直到下面任意事件的发生:
1 The thread exits,
2 specifies a different I/O completion port
3 closes the I/O completion port
换言之,一个线程最多和一个完成端口相关联

When a completion packet is queued to an I/O completion port, the system first checks how many threads associated with that port are running.
If the number of threads running is less than the concurrency value , one of the waiting threads (the most recent one) is allowed to process the completion packet. 
When a running thread completes its processing, it typically calls GetQueuedCompletionStatus again, at which point it either returns with the next completion packet or waits if the queue is empty.

当一个Completion Packet 在完成端口排队时,操作系统首先检查在线程池中和完成端口关联的正在运行的线程.
当正在运行的线程数量小于指定的并发值,等待的线程将被允许处理Completion Packet.
当一个运行中的线程处理完Completion Packet后,将再次调用GetQueuedCompletionStatus()处理下一个Completion Packet.如果等待队列为空,GetQueuedCompletionStatus()将阻塞.


Threads can use the PostQueuedCompletionStatus function to place completion packets in an I/O completion port's queue.
By doing so, the completion port can be used to receive communications from other threads of the process, in addition to receiving I/O completion packets from the I/O system.
The PostQueuedCompletionStatus function allows an application to queue its own special-purpose completion packets to the I/O completion port without starting an asynchronous I/O operation
This is useful for notifying worker threads of external events, for example.


Threads and Concurrency

The most important property of an I/O completion port to consider carefully is the concurrency value.
The concurrency value of a completion port is specified when it is created with CreateIoCompletionPort via the NumberOfConcurrentThreads parameter.
This value limits the number of runnable threads associated with the completion port.
When the total number of runnable threads associated with the completion port reaches the concurrency value,
the system blocks the execution of any subsequent threads associated with that completion port until the number of runnable threads drops below the concurrency value.

在完成端口中最重要的属性并且被小心使用的是并发值.
并发值时在使用CreateIoCompletionPort()函数创建IOCP对象时,通过NumberOfConcurrentThreads 参数来设置.
并发值限制可以在完成端口上面同时运行线程最多数量.
当正在运行线程数量大于并发值时,系统将阻塞任何后来的线程.直到正在运行的线程数量小于并发值.


The most efficient scenario occurs when there are completion packets waiting in the queue, but no waits can be satisfied because the port has reached its concurrency limit.
Consider what happens with a concurrency value of one and multiple threads waiting in the GetQueuedCompletionStatus function call.
In this case, if the queue always has completion packets waiting, when the running thread calls GetQueuedCompletionStatus, it will not block execution because, as mentioned earlier, the thread queue is LIFO.
Instead, this thread will immediately pick up the next queued completion packet.
No thread context switches will occur, because the running thread is continually picking up completion packets and the other threads are unable to run.

使用完成端口最高效的方案是处理完成端口数据包的线程达到并发值后,不存在等待的线程.
当到达并发值时,一个线程调用GetQueuedCompletionStatus()将会发生什么?
当一个数据包到达完成端口时,一个运行的线程调用GetQueuedCompletionStatus()函数,该线程不会阻塞立即执行,并继续处理下一个数据包.因为线程是LILO 后进先出顺序.
因为一个运行的线程可以立刻处理完成端口的数据包,所有其他线程将不能运行,因为不存在线程上下文切换.

 

 

理解I/O Completion Port

我将先给出IOCP的定义然后给出它的实现方法,最后剖析一个Echo程序来为您拨开IOCP的谜云,除去你心中对IOCP的烦恼。OK,但我不能保证你明白IOCP的一切,但我会尽我最大的努力。以下是我会在这...
  • l12345678
  • l12345678
  • 2008年02月29日 17:16
  • 2864

Windows Completion Ports( 完成端口) 与 Linux下 epoll技术介绍

WINDOWS完成端口编程 1、基本概念 2、WINDOWS完成端口的特点 3、完成端口(Completion Ports )相关数据结构和创建 4、完成端口线程的工作原理 5、Windows完成端口...
  • wiserstar
  • wiserstar
  • 2014年02月24日 16:29
  • 732

I/O Completion Ports

I/O完成端口提供在一个多核处理器系统上面处理多个异步I/O请求的高效线程模型. 当创建一个I/O 完成端口时,操作系统创建与I/O完成端口相关联的队列对象来处理这些I/O请求. I/O完成端口使用和...
  • sunning9001
  • sunning9001
  • 2015年07月07日 18:45
  • 887

I/O Completion Ports学习

表示还是自己看MSDN最直接,别人的介绍都是嚼剩下,有木有? IO完成端口为在多处理器系统处理多个异步IO请求提供一个高效的线程模型。当一个进程新建一个完成端口,操作系统新建一个目的为服务这...
  • boyhailong
  • boyhailong
  • 2013年09月17日 12:02
  • 1038

I/O Completion Ports

最近想看看有关完成端口的东东,于是就从这里开始了。我试图完美的将其翻译出来,可耐水平有限,有几句我不太有把握。大家帮忙一下?Platform SDK: Storage I/O Completion P...
  • smallfool
  • smallfool
  • 2004年06月15日 15:44
  • 1568

完成端口(Completion Port)详解 配套代码

  • 2012年10月23日 09:52
  • 1.5MB
  • 下载

完成端口(Completion Port)详解

  • 2012年12月08日 14:01
  • 1.58MB
  • 下载

I/O Completion Ports

 最近想看看有关完成端口的东东,于是就从这里开始了。我试图完美的将其翻译出来,可耐水平有限,有几句我不太有把握。大家帮忙一下?Platform SDK: Storage I/O Completion ...
  • Wendysuly
  • Wendysuly
  • 2009年01月01日 20:49
  • 348

I/O Completion ports

I/O  Completion ports是一种非常特殊的核心对象,用来综合一堆线程,让它们为“overlapped请求”服务,其所提供的功能甚至可以跨越多个CPU,Completion port可以...
  • jeromeyu
  • jeromeyu
  • 2012年06月17日 21:16
  • 689

I/O Completion Ports

I/O Completion Ports 点击打开链接 I/O completion ports provide an efficient threading model for proc...
  • yjz1409276
  • yjz1409276
  • 2014年04月20日 17:22
  • 700
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:I/O Completion Ports
举报原因:
原因补充:

(最多只允许输入30个字)