linux网络配置的详细过程(linux网络配置命令)

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呢?计算机的世界有一套自己定义的概念。如果不明白这些概念,就无法真正明白技术的设计思路和本质。所以在我看来,这些概念是了解技术和计算机世界的基础。

1.1 同步与异步,阻塞与非阻塞

理解网络I/O避不开的话题:同步与异步,阻塞与非阻塞。

拿山治烧水举例来说,(山治的行为好比用户程序,烧水好比内核提供的系统调用),这两组概念翻译成大白话可以这么理解。

  • 同步/异步关注的是水烧开之后需不需要我来处理。
  • 阻塞/非阻塞关注的是在水烧开的这段时间是不是干了其他事。

1.1.1 同步阻塞

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

linux网络配置的详细过程(linux网络配置命令)

按下开关后,傻等水开(阻塞),水开后自动断电(异步)。

linux网络配置的详细过程(linux网络配置命令)

linux网络配置的详细过程(linux网络配置命令)

linux网络配置的详细过程(linux网络配置命令)

linux网络配置的详细过程(linux网络配置命令)

网络编程都需要知道FD??? FD是个什么鬼???

Linux:万物都是文件,FD就是文件的引用。像不像JAVA中万物都是对象?程序中操作的是对象的引用。JAVA中创建对象的个数有内存的限制,同样FD的个数也是有限制的。

linux网络配置的详细过程(linux网络配置命令)

怎么优化呢?

对于一次I/O访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间。

所以说,当一个read操作发生时,它会经历两个阶段:

  • 等待数据准备 (Waiting for the data to be ready)。
  • 将数据从内核拷贝到进程中 (Copying the data from the kernel to the process)。

正是因为这两个阶段,Linux系统升级迭代中出现了下面三种网络模式的解决方案。

二、IO模型介绍

2.1 阻塞 I/O – Blocking I/O

linux网络配置的详细过程(linux网络配置命令)

简介:进程反复系统调用,并马上返回结果。

缺点:当进程有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

linux网络配置的详细过程(linux网络配置命令)

简介:有连接请求抵达了再检查处理。

缺点:

  • 句柄上限- 默认打开的FD有限制,1024个。
  • 重复初始化-每次调用 select(),需要把 fd 集合从用户态拷贝到内核态,内核进行遍历。
  • 逐个排查所有FD状态效率不高。

服务端的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<SelectionKey> keys = selector.selectedKeys();            Iterator<SelectionKey> 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

linux网络配置的详细过程(linux网络配置命令)

简介:设计新的数据结构(链表)提供使用效率。

poll和select相比在本质上变化不大,只是poll没有了select方式的最大文件描述符数量的限制。

缺点:逐个排查所有FD状态效率不高。

2.3.3 I/O 多路复用- epoll

简介:没有fd个数限制,用户态拷贝到内核态只需要一次,使用事件通知机制来触发。通过epoll_ctl注册fd,一旦fd就绪就会通过callback回调机制来激活对应fd,进行相关的I/O操作。

缺点:

  • 跨平台,Linux 支持最好。
  • 底层实现复杂。
  • 同步。
 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<AsynchronousSocketChannel, Object>() {            @Override            public void completed(final AsynchronousSocketChannel client, Object attachment) {                serverChannel.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 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<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().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干活的工人(们)。

三、总结

回顾

  • 同步/异步,连接建立后,用户程序读写时,如果最终还是需要用户程序来调用系统read()来读数据,那就是同步的,反之是异步。Windows实现了真正的异步,内核代码甚为复杂,但对用户程序来说是透明的。
  • 阻塞/非阻塞,连接建立后,用户程序在等待可读可写时,是不是可以干别的事儿。如果可以就是非阻塞,反之阻塞。大多数操作系统都支持的。

Redis,Nginx,Netty,Node.js 为什么这么香?

这些技术都是伴随Linux内核迭代中提供了高效处理网络请求的系统调用而出现的。了解计算机底层的知识才能更深刻地理解I/O,知其然,更要知其所以然。与君共勉!

(0)
小多多的头像小多多创始人

相关推荐

发表回复

登录后才能评论