i/o( input output),包括文件i/o、网络i/o。
计算机世界里的速度鄙视:
内存读数据:纳秒级别。千兆网卡读数据:微妙级别。1微秒=1000纳秒,网卡比内存慢了千倍。磁盘读数据:毫秒级别。1毫秒=10万纳秒 ,硬盘比内存慢了10万倍。cpu一个时钟周期1纳秒上下,内存算是比较接近cpu的,其他都等不起。cpu 处理数据的速度远大于i/o准备数据的速度 。
任何编程语言都会遇到这种cpu处理速度和i/o速度不匹配的问题!
在网络编程中如何进行网络i/o优化:怎么高效地利用cpu进行网络数据处理???
从操作系统层面怎么理解网络i/o呢?计算机的世界有一套自己定义的概念。如果不明白这些概念,就无法真正明白技术的设计思路和本质。所以在我看来,这些概念是了解技术和计算机世界的基础。
理解网络i/o避不开的话题:同步与异步,阻塞与非阻塞。
拿山治烧水举例来说,(山治的行为好比用户程序,烧水好比内核提供的系统调用),这两组概念翻译成大白话可以这么理解。
同步/异步关注的是水烧开之后需不需要我来处理。阻塞/非阻塞关注的是在水烧开的这段时间是不是其他事。点火后,傻等,不等到水开坚决不干任何事(阻塞),水开了关火(同步)。
点火后,去看电视(非阻塞),时不时看水开了没有,水开后关火(同步)。
按下开关后,傻等水开(阻塞),水开后自动断电(异步)。
网络编程中不存在的模型。
按下开关后,该干嘛干嘛 (非阻塞),水开后自动断电(异步)。
用户态和内核态的切换耗时,费资源(内存、cpu)
优化建议:
更少的切换。共享空间。网络编程都需要知道fd??? fd是个什么鬼???
linux:万物都是文件,fd就是文件的引用。像不像java中万物都是对象?程序中操作的是对象的引用。java中创建对象的个数有内存的限制,同样fd的个数也是有限制的。
linux在处理文件和网络连接时,都需要打开和关闭fd。
每个进程都会有默认的fd:
0 标准输入 stdin1 标准输出 stdout2 错误输出 stderr怎么优化呢?
对于一次i/o访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间。
所以说,当一个read操作发生时,它会经历两个阶段:
等待数据准备 (waiting for the data to be ready)。将数据从内核拷贝到进程中 (copying the data from the kernel to the process)。正是因为这两个阶段,linux系统升级迭代中出现了下面三种网络模式的解决方案。
简介:最原始的网络i/o模型。进程会一直阻塞,直到数据拷贝完成。
缺点:高并发时,服务端与客户端对等连接,线程多带来的问题:
cpu资源浪费,上下文切换。内存成本几何上升,jvm一个线程的成本约1mb。public static void main(string[] args) throws ioexception { rversocket ss = new rversocket(); 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 rvermsg = " rver sss[ 线程:"+ thread.currentthread().getname() +"]"; socket.getoutputstream().write(rvermsg.getbytes());//阻塞方法 socket.getoutputstream().flush(); } catch (exception e) { e.printstacktrace(); } }
简介:进程反复系统调用,并马上返回结果。
缺点:当进程有1000fds,代表用户进程轮询发生系统调用1000次kernel,来回的用户态和内核态的切换,成本几何上升。
public static void main(string[] args) throws ioexception { rversocketchannel ss = rversocketchannel.open(); ss.bind(new inetsocketaddress(constant.host, constant.port)); system.out.println(" nio rver started ... "); ss.configureblocking(fal); int idx =0; while (true) { final socketchannel socket = ss.accept();//阻塞方法 new thread(() -> { handle(socket); },"线程["+idx+"]" ).start(); } } static void handle(socketchannel socket) { try { socket.configureblocking(fal); 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(); } }
简介:单个线程就可以同时处理多个网络连接。内核负责轮询所有socket,当某个socket有数据到达了,就通知用户进程。多路复用在linux内核代码迭代过程中依次支持了三种调用,即lect、poll、epoll三种多路复用的网络i/o模型。下文将画图结合java代码解释。
简介:有连接请求抵达了再检查处理。
缺点:
句柄上限- 默认打开的fd有限制,1024个。重复初始化-每次调用 lect(),需要把 fd 集合从用户态拷贝到内核态,内核进行遍历。逐个排查所有fd状态效率不高。服务端的lect 就像一块布满插口的插排,client端的连接连上其中一个插口,建立了一个通道,然后再在通道依次注册读写事件。一个就绪、读或写事件处理时一定记得删除,要不下次还能处理。
public static void main(string[] args) throws ioexception { rversocketchannel ssc = rversocketchannel.open();//管道型rversocket ssc.socket().bind(new inetsocketaddress(constant.host, constant.port)); ssc.configureblocking(fal);//设置非阻塞 system.out.println(" nio single rver started, listening on :" + ssc.getlocaladdress()); lector lector = lector.open(); ssc.register(lector, lectionkey.op_accept);//在建立好的管道上,注册关心的事件 就绪 while(true) { lector.lect(); t<lectionkey> keys = lector.lectedkeys(); iterator<lectionkey> it = keys.iterator(); while(it.hasnext()) { lectionkey key = it.next(); it.remove();//处理的事件,必须删除 handle(key); } } } private static void handle(lectionkey key) throws ioexception { if(key.isacceptable()) { rversocketchannel ssc = (rversocketchannel) key.channel(); socketchannel sc = ssc.accept(); sc.configureblocking(fal);//设置非阻塞 sc.register(key.lector(), lectionkey.op_read );//在建立好的管道上,注册关心的事件 可读 } el 在学校想家怎么办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); } }
简介:设计新的数据结构(链表)提供使用效率。
poll和lect相比在本质上变化不大,只是poll没有了lect方式的最大文件描述符数量的限制。
缺点:逐个排查所有fd状态效率不高。
简介:没有fd个数限制,用户态拷贝到内核态只需要一次,使用事件通知机制来触发。通过epoll_ctl注册fd,一旦fd就绪就会通过callback回调机制来激活对应fd,进行相关的i/o操作。
缺点:
跨平台,linux 支持最好。底层实现复杂。同步。 public static void main(string[] args) throws exception { final asynchronousrversocketchannel rverchannel = asynchronousrversocketchannel.open() .bind(new inetsocketaddress(constant.host, constant.port)); rverchannel.accept(null, new completionhandler<asynchronoussocketchannel, object>() { @override public void completed(final asynchronoussocketchannel client, object attachment) { rverchannel.accept(null, this); bytebuffer buffer = bytebuffer.allocate(1024); client.read(buffer, buffer, new completionhandler<integer, bytebuffer>() { @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 nettyrver().rverstart(); system.out.println("netty rver started !"); } public void rverstart() { eventloopgroup bossgroup = new nioeventloopgroup(); eventloopgroup workergroup = new nioeventloopgroup(); rverbootstrap b = new rverbootstrap(); b.group(bossgroup, workergroup) .channel(niorversocketchannel.class) .childhandler(new channelinitializer<socketchannel>() { @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().clofuture().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.clo(); } @override public void exceptioncaught(channelhandlercontext ctx, throwable cau) throws exception { cau.printstacktrace(); ctx.clo(); }}
bossgroup 处理网络请求的大管家(们),网络连接就绪时,交给workgits not overroup干活的工人(们)。
这些技术都是伴随linux内核迭代中提供了高效处理网络请求的系统调用而出现的。了解计算机底层的知识才能更深刻地理解i/o,知其然,更要知其所以然。与君共勉!
本文发布于:2023-04-05 07:11:59,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/bf887f1cd02d401ea1dec470a662a4c4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:linux网络配置命令(linux网络配置的详细过程).doc
本文 PDF 下载地址:linux网络配置命令(linux网络配置的详细过程).pdf
留言与评论(共有 0 条评论) |