本文讲述IO model(IO模型)。
1.Basic concept
首先讲述一些基本的概念。
1.1. Blocking call vs. Non-blocking call
什么是Blocking call(阻塞调用) Non-blocking call(非阻塞调用)?
Blocking call cause the requesting process to be blocked until the call return.
Non-blocking call does not cause the requesting process to be blocked.
1.2. Synchronous IO vs. Asynchronous IO
1.2.1 首先什么是Synchronous(同步)和Asynchronous(异步)?
Synchronous mean change at the same time and same step between 2 objectives
Asynchronous mean change not at the same time and same step between 2 objectives
同步异步是通用的概念,比如在TCP协议中已有同步的概念。
TCP 3 way handshake:
3 way handshake make client and server change at the same time, so segment for handshake call SYNC
1.2.2 接下来,我们讨论什么是Synchronous IO(同步IO)和Asynchronous IO (异步IO)?
Synchronous IO:
Process and kernel complete work at the same time
Asynchronous IO:
Process and kernel complete work not at the same time
For example:
Process read file A, kernel read file at the same time.
Process read files in the order of A,B,C, kernel also read files in the order of A,B,C, that is at the same step.
In practice, IO operation have request and result, in asynchronous IO, request and result is separate, there are 2 functions, one for request, one for result.
1.3. Multiplexing vs. multiple process/thread
Multiplexing is handle multiple request in one process/thread
Multiple process/thread is handle one request in one process/thread
Level Triggered(水平触发)
Edge Triggered(边缘触发)
Multiplexing is a part of select, poll, epoll, kqueue, IOCP
1.4. Iterative vs. concurrent
Iterative: one after one
Concurrent: work at multiple threads
1.5. Polling triggers notification vs. call back notification
Select, poll, epoll, IOCP is polling triggers notification
Signal is call back notification
1.6. IO Ready Notification vs. IO Complete Notification
IO READY Notification: IO is ready, such as readable, writable
Completed IO Request Notification: IO request is completed
2.各操作系统的支持
上面介绍了一些基本概念,这些基本概念中有一些是操作系统的基本机制,比如IO Ready Notification vs. IO Complete Notification两种机制。下面介绍一下各操作系统的一些IO model要用到的一些机制。
2.1 Linux IO Ready Notification
2.1.1 IO file descriptor Notification
This is not independent mechanism, it is a part of select, poll, epoll mechanism.
2.1.2 Signal Notification (SIGIO)
2.2 Linux Completed IO Request Notification
2.2.1 Linux Native AIO
A completed IO Request Notification mechanism is built in Linux Native AIO
2.3 Windows Completed IO Request Notification
2.3.1. IO Completion Port
A completed IO Request Notification mechanism is built in IO Completion Port
2.3.2. windows Device kernel object
2.3.3. windows event
2.3.4. windows alertable IO
3. Target of IO Model
我们研究IO模型的目的是最大化利用CPU,要最大化利用CPU就要去除无用的操作,那什么是无用的操作那,blocking和线程的CPU上线文切换都是无用的操作。
Maximize CPU utilization -> no useless operation -> no block, no context switch
Make thread keeping working and busy
4. IO Model
我将IO Model总体上分为三类:
- 1.All in one call I/O Model category
- 2.IO Ready Notification I/O Model category
- 3.Completed IO Request Notification I/O Model category
Blocking and Non-blocking in Asynchronous 没有太大的意义
3.1. Synchronous blocking Model category
3.2. Synchronous non-blocking Model category
3.3. IO Ready Model category
3.4. Asynchronous Model (IO complete Model) category
4. Common Synchronous blocking Model Category (3 models)
各种操作系统对这类IO model都有支持,而且采用的形式都一致,因为这总模型基于blocking socket api,所以称这类模型是common。
在这个类别下有几种不同的实现,主要的区别在于使用Iterative还是使用Concurrent。
4.1. Synchronous Blocking Iterative Model
4.2. Synchronous Blocking Concurrent Model
4.2.1. Synchronous Blocking Request Separation Model
4.2.2. Synchronous Blocking Read/Write Separation Model
5. Common Synchronous Non-blocking Model Category (1 models)
各种操作系统对这类IO model也都有支持,而且采用的形式都一致,因为这总模型基于non-blocking socket api,所以称这类模型是common的。
8. Linux IO Ready Notification I/O Model Category (4 Models)
8.1. SIGIO
8.1.1. SIGIO – Blocking Model
8.1.2. SIGIO – Non-Blocking Model
8.2. Select model
8.2.1. Select-Blocking Model
8.2.2. Select-Non-blocking Model
8.3. Poll Model
8.4. Epoll Model
9. Linux Completed IO Request Notification I/O Model Category (4 Models)**
9.1. linux native aio Model (blocking)
9.2. Linux native aio with epoll Model (blocking)
9.3. Epoll with multi-thread Model
9.4. Linux native aio, Epoll, multi-thread Model
11. Windows IO Ready Notification I/O Model
11.1. Windows WSAAsyncSelect Model
11.2. Windows WSAEventSelect Model
12. Windows Completed IO Request Notification I/O Model
12.1. Only Completed IO Request Notification Model
12.1.1. windows Device kernel object Model (overlap IO) (Asynchronous blocking)
12.1.2. windows event Model (overlap IO) (Asynchronous blocking)
12.1.3. Completion Routine Model (overlap IO) (windows alertable IO) (Asynchronous blocking)
12.2. Using Completed IO Request Notification and Multiplexing Model
12.2.1. Windows IO completion port Model (Asynchronous Non-blocking)
13. Solaris Completed IO Request Notification I/O Model
13.1. Event completion Model
14. glibc Completed IO Request Notification
14.1. Signal
Signal in Posix aio
14.2. Callback
Callback in Posix aio
15. Glibc Completed IO Request Notification I/O Model
15.1. Posix aio (signal, call) Model (Asynchronous Non-blocking)