# 异步I/O模型
异步I/O模型是我个人觉得所有程序员都必需要学习的一门技术或是编程方法,这其中的设计模式或是解决方法可以借鉴到分布式架构上来。再说一遍,学习这些模型,是非常非常重要的,你千万要认真学习。
史蒂文斯(Stevens)在《[UNIX网络编程](https://book.douban.com/subject/4859464/)》一书6.2 I/O Models中介绍了五种I/O模型。
- 阻塞I/O
- 非阻塞I/O
- I/O的多路复用(select和poll)
- 信号驱动的I/O(SIGIO)
- 异步I/O(POSIX的aio_functions)
然后,在前面我们也阅读过了 - [C10K Problem](https://en.wikipedia.org/wiki/C10k_problem) 。相信你对I/O模型也有了一定的了解。 这里,我们需要更为深入地学习I/O模型,尤其是其中的异步I/O模型。
首先,我们看一篇和Java相关的I/O模型的文章来复习一下之前的内容。[Thousands of Threads and Blocking I/O: The Old Way to Write Java Servers Is New Again (and Way Better)](https://www.slideshare.net/e456/tyma-paulmultithreaded1) ,这个PPT中不仅回顾和比较了各种I/O模型,而且还有各种比较细节的方案和说明,是一篇非常不错的文章。
然后,你可以看一篇Java相关的PPT - 道格·莱亚(Doug Lea)的 [Scalable IO in Java](http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf),这样你会对一些概念有个了解。
接下来,我们需要了解一下各种异步I/O的实现和设计方式。
[IBM - Boost application performance using asynchronous I/O](https://developer.ibm.com/technologies/linux/articles/l-async/) ,这是一篇关于AIO的文章。
[Lazy Asynchronous I/O For Event-Driven Servers](https://www.usenix.org/legacy/event/usenix04/tech/general/full_papers/elmeleegy/elmeleegy_html/html.html) ,这篇文章也很不错。
[The Secret To 10 Million Concurrent Connections -The Kernel Is The Problem, Not The Solution](http://highscalability.com/blog/2013/5/13/the-secret-to-10-million-concurrent-connections-the-kernel-i.html) - C10M问题来了……
还有几篇可能有争议的文章,让你从不同的角度思考。
- [Select is fundamentally broken](https://idea.popcount.org/2017-01-06-select-is-fundamentally-broken/)
- [Epoll is fundamentally broken 1/2](https://idea.popcount.org/2017-02-20-epoll-is-fundamentally-broken-12/)
- [Epoll is fundamentally broken 2/2](https://idea.popcount.org/2017-03-20-epoll-is-fundamentally-broken-22/)
# Lock-Free编程相关
Lock-Free - 无锁技术越来越被开发人员重视,因为锁对于性能的影响实在是太大了,所以如果想开发出一个高性能的程序,你就非常有必要学习 Lock-Free的编程方式。
关于无锁的数据结构,有几篇教程你可以看一下。
[Dr.Dobb’s: Lock-Free Data Structures](http://www.drdobbs.com/lock-free-data-structures/184401865)
[Andrei Alexandrescu: Lock-Free Data Structures](https://erdani.com/publications/cuj-2004-10.pdf)
然后强烈推荐一本免费的电子书:[Is Parallel Programming Hard, And, If So, What Can You Do About It?](https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html) ,这是大牛 [保罗·麦肯尼(Paul E. McKenney)](https://www.linkedin.com/in/paulmckenney/) 写的书。这本书堪称并行编程的经典书,必看。
此时,Wikipedia上有三个词条你要看一下,以此了解并发编程中的一些概念:[Non-blocking algorithm](https://en.wikipedia.org/wiki/Non-blocking_algorithm) 、[Read-copy-update](https://en.wikipedia.org/wiki/Read-copy-update) 和 [Seqlock](https://en.wikipedia.org/wiki/Seqlock)。
接下来,读一下以下两篇论文 。
[Mechanical Sympathy](http://mechanical-sympathy.blogspot.com/) - 博主是马丁·汤普森(Martin Thompson),他是一名英国的技术极客,探索现代硬件的功能,并提供开发、培训、性能调优和咨询服务。他的博客主题是Hardware and software working together in harmony,里面探讨了如何设计和编写软件使得它在硬件上能高性能地运行。非常值得一看。
关于64位系统编程,只要去一个地方就行了: [All about 64-bit programming in one place](https://software.intel.com/en-us/blogs/2011/07/07/all-about-64-bit-programming-in-one-place/),这是一个关于64位编程相关的收集页面,其中包括相关的文章、28节课程,还有知识库和相关的blog。
[What Scalable Programs Need from Transactional Memory](https://dl.acm.org/citation.cfm?id=3037750) ,事务性内存(TM)一直是许多研究的重点,它在诸如IBM Blue Gene/Q和Intel Haswell等处理器中得到了支持。许多研究都使用STAMP基准测试套件来评估其设计。然而,我们所知的所有TM系统上的STAMP基准测试所获得的加速比较有限。
例如,在IBM Blue Gene/Q上有64个线程,我们观察到使用Blue Gene/Q硬件事务内存(HTM)的中值加速比为1.4倍,使用软件事务内存(STM)的中值加速比为4.1倍。什么限制了这些TM基准的性能?在本论文中,作者认为问题在于用于编写它们的编程模型和数据结构上,只要使用合适的模型和数据结构,程序的性能可以有10多倍的提升。
[How eBay’s Shopping Cart used compression techniques to solve network I/O bottlenecks](https://www.ebayinc.com/stories/blogs/tech/how-ebays-shopping-cart-used-compression-techniques-to-solve-network-io-bottlenecks/) ,这是一篇很好的文章,讲述了eBay是如何通过压缩数据来提高整体服务性能的,其中有几个比较好的压缩算法。除了可以让你学到相关的技术知识,还可以让你看到一种比较严谨的工程师文化。
[Linkedin: Boosting Site Speed Using Brotli Compression](https://engineering.linkedin.com/blog/2017/05/boosting-site-speed-using-brotli-compression) ,LinkedIn在2017年早些时候开始使用 [Brotli](https://en.wikipedia.org/wiki/Brotli) 来替换 gzip,以此带来更快的访问,这篇文章讲述了什么是Brotli以及与其它压缩程序的比较和所带来的性能提升。
这里有两篇关于SSD硬盘性能测试的文章。[Performance Testing with SSDs, Part 1](https://devs.mailchimp.com/blog/performance-testing-with-ssds-part-1/) 和 [Performance Testing with SSDs Part 2](https://devs.mailchimp.com/blog/performance-testing-with-ssds-pt-2/) ,这两篇文章介绍了测试SSD硬盘性能以及相关的操作系统调优方法。
[Secure Programming HOWTO - Creating Secure Software](https://www.dwheeler.com/secure-programs/) ,这是一本电子书,其中有繁体中文的翻译,这本电子书讲了Linux/Unix下的一些安全编程方面的知识。
# 相关论文
[Hints for Computer System Design](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/acrobat-17.pdf) ,计算机设计的忠告,这是ACM图灵奖得主 [Butler Lampson](https://en.wikipedia.org/wiki/Butler_Lampson) 在Xerox PARC工作时的一篇论文。这篇论文简明扼要地总结了他在做系统设计时的一些想法,非常值得一读。(用他的话来说,“Studying the design and implementation of a number of computer has led to some general hints for system design. They are described here and illustrated by many examples, ranging from hardware such as the Alto and the Dorado to application programs such as Bravo and Star“。)
[The 5 minute rule for trading memory for disc accesses and the 5 byte rule for trading memory for CPU time](http://www.hpl.hp.com/techreports/tandem/TR-86.1.pdf) ,根据文章名称也可以看出,5分钟法则是用来衡量内存与磁盘的,而5字节法则则是在内存和CPU之间的权衡。这两个法则是Jim Gray和Franco Putzolu在1986年的文章。
在该论文发表10年后的1997年,Jim Gray和Goetz Graefe 又在 [The Five-Minute Rule Ten Years Later and Other Computer Storage Rules of Thumb](http://research.microsoft.com/en-us/um/people/gray/5_min_rule_SIGMOD.pdf) 中对该法则进行了重新审视。2007年,也就是该论文发表20年后,这年的1月28日,Jim Gray驾驶一艘40英尺长的船从旧金山港出海,目的是航行到附近的费拉隆岛,在那里撒下母亲的骨灰。出海之后,他就同朋友和亲属失去了联系。为了纪念和向大师致敬,时隔10多年后的2009年Goetz Graefe又发表了 [The Five-Minute Rule 20 Years Later (and How Falsh Memory Changes the Rules)](http://cacm.acm.org/magazines/2009/7/32091-the-five-minute-rule-20-years-later/fulltext)。
注明一下,Jim Gray是关系型数据库领域的大师。因在数据库和事务处理研究和实现方面的开创性贡献而获得1998年图灵奖。美国科学院、工程院两院院士,ACM和IEEE两会会士。他25岁成为加州大学伯克利分校计算机科学学院第一位博士。在IBM工作期间参与和主持了IMS、System R、SQL/DS、DB2等项目的开发。后任职于微软研究院,主要关注应用数据库技术来处理各学科的海量信息。