嵌入式开发交流网论坛

标题: 从操作系统层面理解Linux下的网络IO模型 [打印本页]

作者: 明天过后by    时间: 2020-1-16 02:29
标题: 从操作系统层面理解Linux下的网络IO模型
[attach]36251[/attach]图片来源于网络

I/O( INPUT OUTPUT),包括文件I/O、网络I/O。
计算机世界里的速度鄙视:
CPU 处理数据的速度远大于I/O准备数据的速度 。
任何编程语言都会遇到这种CPU处理速度和I/O速度不匹配的问题!
在网络编程中如何进行网络I/O优化:怎么高效地利用CPU进行网络数据处理???
一、相关概念
从操作系统层面怎么理解网络I/O呢?计算机的世界有一套自己定义的概念。如果不明白这些概念,就无法真正明白技术的设计思路和本质。所以在我看来,这些概念是了解技术和计算机世界的基础。
1.1 同步与异步,阻塞与非阻塞

理解网络I/O避不开的话题:同步与异步,阻塞与非阻塞。
拿山治烧水举例来说,(山治的行为好比用户程序,烧水好比内核提供的系统调用),这两组概念翻译成大白话可以这么理解。
1.1.1 同步阻塞

点火后,傻等,不等到水开坚决不干任何事(阻塞),水开了关火(同步)。
[attach]36252[/attach]

1.1.2 同步非阻塞
点火后,去看电视(非阻塞),时不时看水开了没有,水开后关火(同步)。
[attach]36253[/attach]

1.1.3 异步阻塞
按下开关后,傻等水开(阻塞),水开后自动断电(异步)。
网络编程中不存在的模型。
1.1.4 异步非阻塞

按下开关后,该干嘛干嘛 (非阻塞),水开后自动断电(异步)。
[attach]36254[/attach]

1.2 内核空间 、用户空间
[attach]36255[/attach]

1.2.1 内核态 用户态

[attach]36256[/attach]

1.2.2 线程的切换

[attach]36257[/attach]

用户态和内核态的切换耗时,费资源(内存、CPU)。
优化建议:
1.3 套接字 – socket

[attach]36258[/attach]

1.4 文件描述符 –FD 句柄





[attach]36259[/attach]

网络编程都需要知道FD???FD是个什么鬼???
Linux:万物都是文件,FD就是文件的引用。像不像JAVA中万物都是对象?程序中操作的是对象的引用。JAVA中创建对象的个数有内存的限制,同样FD的个数也是有限制的。
[attach]36260[/attach]

Linux在处理文件和网络连接时,都需要打开和关闭FD。
每个进程都会有默认的FD:
1.5 服务端处理网络请求的过程

[attach]36261[/attach]

怎么优化呢?
对于一次I/O访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间。
所以说,当一个read操作发生时,它会经历两个阶段:
正是因为这两个阶段,Linux系统升级迭代中出现了下面三种网络模式的解决方案。
二、I/O模型

2.1 阻塞 I/O - Blocking I/O
[attach]36262[/attach]

简介:最原始的网络I/O模型。进程会一直阻塞,直到数据拷贝完成。
缺点:高并发时,服务端与客户端对等连接,线程多带来的问题:
public static void main(String args) throws IOException { ServerSocket ss = new ServerSocket; ss.bind(new InetSocketAddress(Constant.HOST, Constant.PORT)); int idx =0; while (true) { final Socket socket = ss.accept;//阻塞方法 new Thread( -> { handle(socket); },"线程["+idx+"]" ).start; } } static void handle(Socket socket) { byte bytes = new byte[1024]; try { String serverMsg = " server sss[ 线程:"+ Thread.currentThread.getName +"]"; socket.getOutputStream.write(serverMsg.getBytes);//阻塞方法 socket.getOutputStream.flush; } catch (Exception e) { e.printStackTrace; } }
2.2 非阻塞 I/O - Non Blocking IO

[attach]36263[/attach]

简介:进程反复系统调用,并马上返回结果。
缺点:当进程有1000fds,代表用户进程轮询发生系统调用1000次kernel,来回的用户态和内核态的切换,成本几何上升。
public static void main(String args) throws IOException { ServerSocketChannel ss = ServerSocketChannel.open; ss.bind(new InetSocketAddress(Constant.HOST, Constant.PORT)); System.out.println(" NIO server started ... "); ss.configureBlocking(false); int idx =0; while (true) { final SocketChannel socket = ss.accept;//阻塞方法 new Thread( -> { handle(socket); },"线程["+idx+"]" ).start; } } static void handle(SocketChannel socket) { try { socket.configureBlocking(false); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); socket.read(byteBuffer); byteBuffer.flip; System.out.println("请求:" + new String(byteBuffer.array)); String resp = "服务器响应"; byteBuffer.get(resp.getBytes); socket.write(byteBuffer); } catch (IOException e) { e.printStackTrace; } }
2.3 I/O 多路复用 - IO multiplexing

[attach]36264[/attach]

简介:单个线程就可以同时处理多个网络连接。内核负责轮询所有socket,当某个socket有数据到达了,就通知用户进程。多路复用在Linux内核代码迭代过程中依次支持了三种调用,即SELECT、POLL、EPOLL三种多路复用的网络I/O模型。下文将画图结合Java代码解释。
2.3.1 I/O 多路复用- select

简介:有连接请求抵达了再检查处理。
缺点:
服务端的select 就像一块布满插口的插排,client端的连接连上其中一个插口,建立了一个通道,然后再在通道依次注册读写事件。一个就绪、读或写事件处理时一定记得删除,要不下次还能处理。
public static void main(String args) throws IOException { ServerSocketChannel ssc = ServerSocketChannel.open;//管道型ServerSocket ssc.socket.bind(new InetSocketAddress(Constant.HOST, Constant.PORT)); ssc.configureBlocking(false);//设置非阻塞 System.out.println(" NIO single server started, listening on :" + ssc.getLocalAddress); Selector selector = Selector.open; ssc.register(selector, SelectionKey.OP_ACCEPT);//在建立好的管道上,注册关心的事件 就绪 while(true) { selector.select; Set keys = selector.selectedKeys; Iterator it = keys.iterator; while(it.hasNext) { SelectionKey key = it.next; it.remove;//处理的事件,必须删除 handle(key); } } } private static void handle(SelectionKey key) throws IOException { if(key.isAcceptable) { ServerSocketChannel ssc = (ServerSocketChannel) key.channel; SocketChannel sc = ssc.accept; sc.configureBlocking(false);//设置非阻塞 sc.register(key.selector, SelectionKey.OP_READ );//在建立好的管道上,注册关心的事件 可读 } else if (key.isReadable) { //flip SocketChannel sc = null; sc = (SocketChannel)key.channel; ByteBuffer buffer = ByteBuffer.allocate(512); buffer.clear; int len = sc.read(buffer); if(len != -1) { System.out.println("[" +Thread.currentThread.getName+"] recv :"+ new String(buffer.array, 0, len)); } ByteBuffer bufferToWrite = ByteBuffer.wrap("HelloClient".getBytes); sc.write(bufferToWrite); } }
2.3.2 I/O 多路复用 – poll

[attach]36265[/attach]

简介:设计新的数据结构(链表)提供使用效率。
poll和select相比在本质上变化不大,只是poll没有了select方式的最大文件描述符数量的限制。
缺点:逐个排查所有FD状态效率不高。
2.3.3 I/O 多路复用- epoll

简介:没有fd个数限制,用户态拷贝到内核态只需要一次,使用事件通知机制来触发。通过epoll_ctl注册fd,一旦fd就绪就会通过callback回调机制来激活对应fd,进行相关的I/O操作。
缺点:
public static void main(String args) throws Exception { final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open .bind(new InetSocketAddress(Constant.HOST, Constant.PORT)); serverChannel.accept(null, new CompletionHandler { @Override public void completed(final AsynchronousSocketChannel client, Object attachment) { serverChannel.accept(null, this); ByteBuffer buffer = ByteBuffer.allocate(1024); client.read(buffer, buffer, new CompletionHandler { @Override public void completed(Integer result, ByteBuffer attachment) { attachment.flip; client.write(ByteBuffer.wrap("HelloClient".getBytes));//业务逻辑 } @Override public void failed(Throwable exc, ByteBuffer attachment) { System.out.println(exc.getMessage);//失败处理 } }); } @Override public void failed(Throwable exc, Object attachment) { exc.printStackTrace;//失败处理 } }); while (true) { //不while true main方法一瞬间结束 } }
当然上面的缺点相比较它优点都可以忽略。JDK提供了异步方式实现,但在实际的Linux环境中底层还是epoll,只不过多了一层循环,不算真正的异步非阻塞。而且就像上图中代码调用,处理网络连接的代码和业务代码解耦得不够好。Netty提供了简洁、解耦、结构清晰的API。
public static void main(String args) { new NettyServer.serverStart; System.out.println("Netty server started !"); } public void serverStart { EventLoopGroup bossGroup = new NioEventLoopGroup; EventLoopGroup workerGroup = new NioEventLoopGroup; ServerBootstrap b = new ServerBootstrap; b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline.addLast(new Handler); } }); try { ChannelFuture f = b.localAddress(Constant.HOST, Constant.PORT).bind.sync; f.channel.closeFuture.sync; } catch (InterruptedException e) { e.printStackTrace; } finally { workerGroup.shutdownGracefully; bossGroup.shutdownGracefully; } } } class Handler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; ctx.writeAndFlush(msg); ctx.close; } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace; ctx.close; } }
bossGroup 处理网络请求的大管家(们),网络连接就绪时,交给workGroup干活的工人(们)。
三、总结
回顾
Redis,Nginx,Netty,Node.js 为什么这么香?

这些技术都是伴随Linux内核迭代中提供了高效处理网络请求的系统调用而出现的。了解计算机底层的知识才能更深刻地理解I/O,知其然,更要知其所以然。与君共勉!
文章来源:宜信技术学院 & 宜信支付结算团队技术分享第8期-宜信支付结算部支付研发团队高级工程师周胜帅《从操作系统层面理解Linux的网络IO模型》
分享者:宜信支付结算部支付研发团队高级工程师周胜帅
原文首发于支付结算团队技术公号“野指针”
代码查看:





欢迎光临 嵌入式开发交流网论坛 (http://www.dianzixuexi.com/bbs/) Powered by Discuz! X3.2