nginx优化(二)io优化 直接io与零拷贝

nginx io相关基础配置

  1. debug_points abort|stop; 在main模块,当nginx产生套接字泄漏时,要不要停止,产生core文件,产生core文件会占用io
  2. error_log file|stderr[debug|info|notice|warn|error|crit|alert|emerg],定义错误日志记录的地方以及日志级别,在main,http server location模块中定义,stderr会输出到屏幕,标准错误输出

直接io

顾名思义直接落盘

  1. 直接io,绕开磁盘高速缓存

    • 正常情况下,文件读写都先读写用户存储的缓冲区,然后读写内核缓冲区,我们可以让读写操作绕过内核缓冲区,直接读写用户缓冲去,提高速度,避免磁盘缓存页的拷贝
    • directio size|off,定义多大的文件直接io,如 directio 10m;
    • directioaligiunent 文件偏移量,默认的即可
    • 高速缓冲区作用,某些文件存入高速缓冲区,可以复用,大文件直接读取,小文件缓存

绕过磁盘高速缓存

异步io

  1. 异步io,aio

    • 在发生io阻塞的时候,让其去处理其他任务,
    • aio on|off threads=[pool],server模块中配置
    • nginx的worker进程发生io阻塞以后,把阻塞io的任务放入到一个新的如无队列中,利用线程去处理这些阻塞io的任务,完成后返回给nginx
    • 定义线程池

      • thread_pool name threads=number [max_queue=number]; 在main中定义
      • 默认配置thread_pool default threads=32 max_queue=65535;
      • aio on threads=default; 使用线程
    • 需要nginx有threads和file-aio模块
    • aio和直接io,两个必须同时出现
    • sendfile和直接io是互斥的,两个不能同时存在,直接io不经过磁盘io,0拷贝时从磁盘io直接发送到socket,所以存在互斥性

零拷贝

  1. sendfile零拷贝

    • sendfile on | off;在http server loction模块中定义,
    • sendfile_max_chunk size; 一次sendfile的数据量
    • 正常情况下,读取文件,先从磁盘(内核)拷贝到应用程序缓冲区(用户),然后应用程序缓冲区将内容写入到socket缓冲区(内核),然后发送到网卡,零拷贝,从磁盘直接拷贝到socket缓冲区

零拷贝原理


mafa1993
51 声望1 粉丝