• 内核参数详解


    man 5 proc

    http://blog.itpub.net/15480802/viewspace-753819/

    file-max:定义LINUX 系统 所有进程 open files 的限制

    file-nr:显示了当前linux 系统 open files 的状态

    nr-open:单个进程max file-handles的限制

    This denotes the maximum number of file-handles a process can
    allocate. Default value is 1024*1024 (1048576) which should be
    enough for most machines. Actual limit depends on RLIMIT_NOFILE
    resource limit.

     /proc/[pid]/fd/       

    lsof |grep xxx |wc -l:这个才是真正open files= /proc/[pid]/fd/    + .SO(库文件数)+ (目录数)

    1.    系统最大打开文件描述符数:/proc/sys/fs/file-max
    a.    查看
    $ cat /proc/sys/fs/file-max
    186405
    2. 设置
    a.    临时性
    # echo 1000000 > /proc/sys/fs/file-max
    2.    永久性:在/etc/sysctl.conf中设置
    fs.file-max = 1000000
     
    2.    进程最大 打开文件描述符数:user limit中nofile的soft limit
    a.    查看
    $ ulimit -n
    1700000
    2. 设置
    a.    临时性:通过ulimit -Sn设置最大打开文件描述符数的soft limit,注意soft limit不能大于hard limit(ulimit -Hn可查看hard limit),另外ulimit -n默认查看的是soft limit,但是ulimit -n 1800000则是同时设置soft limit和hard limit。对于非root用户只能设置比原来小的hard limit。
    查看hard limit:
    $ ulimit -Hn
    1700000
    设置soft limit,必须小于hard limit:
    $ ulimit -Sn 1600000
    2.    永久性:上面的方法只是临时性的,注销重新登录就失效了,而且不能增大hard limit,只能在hard limit范围内修改soft limit。若要使修改永久有效,则需要在/etc/security/limits.conf中进行设置(需要root权限),可添加如下两行,表示用户chanon最大打开文件描述符数的soft limit为1800000,hard limit为2000000。以下设置需要注销之后重新登录才能生效:
    chanon           soft    nofile          1800000
    chanon           hard   nofile          2000000
    设置nofile的hard limit还有一点要注意的就是hard limit不能大于/proc/sys/fs/nr_open,假如hard limit大于nr_open,注销后无法正常登录。可以修改nr_open的值:
    # echo 2000000 > /proc/sys/fs/nr_open
     
    3.    查看当前系统使用的打开文件描述符数
    [root@localhost bin]# cat /proc/sys/fs/file-nr
    5664        0        186405
    其中第一个数表示当前系统已分配使用的打开文件描述符数,第二个数为分配后已释放的(目前已不再使用),第三个数等于file-max。
     
    4.    总结:
    a.    所有进程打开的文件描述符数不能超过/proc/sys/fs/file-max
    b.    单个进程打开的文件描述符数不能超过user limit中nofile的soft limit
    c.    nofile的soft limit不能超过其hard limit
    d.    nofile的hard limit不能超过/proc/sys/fs/nr_open
    通常对Linux某个用户设置系统资源,我们都已经知道可以用ulimit命令来查看和设置。
    
    表 1. ulimit 参数说明
    
    选项 [options]    含义    例子
    -H    设置硬资源限制,一旦设置不能增加。    ulimit – Hs 64;限制硬资源,线程栈大小为 64K。
    -S    设置软资源限制,设置后可以增加,但是不能超过硬资源设置。    ulimit – Sn 32;限制软资源,32 个文件描述符。
    -a    显示当前所有的 limit 信息。    ulimit – a;显示当前所有的 limit 信息。
    -c    最大的 core 文件的大小, 以 blocks 为单位。    ulimit – c unlimited; 对生成的 core 文件的大小不进行限制。
    -d    进程最大的数据段的大小,以 Kbytes 为单位。    ulimit -d unlimited;对进程的数据段大小不进行限制。
    -f    进程可以创建文件的最大值,以 blocks 为单位。    ulimit – f 2048;限制进程可以创建的最大文件大小为 2048 blocks。
    -l    最大可加锁内存大小,以 Kbytes 为单位。    ulimit – l 32;限制最大可加锁内存大小为 32 Kbytes。
    -m    最大内存大小,以 Kbytes 为单位。    ulimit – m unlimited;对最大内存不进行限制。
    -n    可以打开最大文件描述符的数量。    ulimit – n 128;限制最大可以使用 128 个文件描述符。
    -p    管道缓冲区的大小,以 Kbytes 为单位。    ulimit – p 512;限制管道缓冲区的大小为 512 Kbytes。
    -s    线程栈大小,以 Kbytes 为单位。    ulimit – s 512;限制线程栈的大小为 512 Kbytes。
    -t    最大的 CPU 占用时间,以秒为单位。    ulimit – t unlimited;对最大的 CPU 占用时间不进行限制。
    -u    用户最大可用的进程数。    ulimit – u 64;限制用户最多可以使用 64 个进程。
    -v    进程最大可用的虚拟内存,以 Kbytes 为单位。    ulimit – v 200000;限制最大可用的虚拟内存为 200000 Kbytes。
    当然我们都知道Linux大部分的命令设置都是临时生效,而且ulimit命令只对当前终端生效,如果需要永久生效的话,我们有两种方法,一种是将命令写至profile和bashrc中,在有些网页中说写到rc.local,其实是不对的;还有一种就是在limits.conf中添加记录(需重启生效,并且在/etc/pam.d/中的seesion有使用到limit模块)。接下来讨论的就是在limits.conf用户最大文件打开数限制的相关内容。
    
    针对用户打开最大文件数的限制, 在limits.conf对应的nofile,不管是man手册还是文件中说明都只是一句话“maximum number of open files",它其实对应是单个进程能打开的最大文件数,通常为了省事,我们想取消它的限制,根据man手册中,“values -1, unlimited or infinity indicating no limit”,-1、unlimited、infinity都是表明不做限制,可是当你实际给nofile设置成这个值,等你重启就会发现无法登录系统了。
    
    由此可见,nofile是有一个上限的,同时用ulimit测试:
    
    #ulimit -n unlimited
    
    bash: ulimit: open files: cannot modify limit: 不允许的操作
    
    写一个简单的for循环得出:
    
    #for  V in `seq  100000  10000000`;do ulimit -n $V;[[ $? != 0 ]]&&break;done
    
    再执行ulimit -n ,可以看到1048576就是nofile的最大值了,但为什么是这个值?1048576是1024*1024,当然这并没有什么卵用。。。再跟踪一下我们就会发现这个值其实是由内核参数nr_open定义的:
    
    # cat /proc/sys/fs/nr_open
    
    1048576
    
    到此我们就要说起nr_open,与file-max了,网上在说到设置最大文件数时偶尔有些帖子也说到要修改file-max,字面上看file-max确实像是对应最大文件数,而在Linux内核文档中它们两的解释是:
    
    file-max:
    
    The value in file-max denotes the maximum number of file-
    handles that the Linux kernel will allocate. When you get lots
    of error messages about running out of file handles, you might
    want to increase this limit
    
    执行:grep -r MemTotal /proc/meminfo | awk '{printf("%d",$2/10)}',可以看到与file-max是相近的;
    
    nr_open:
    
    This denotes the maximum number of file-handles a process can
    allocate. Default value is 1024*1024 (1048576) which should be
    enough for most machines. Actual limit depends on RLIMIT_NOFILE
    resource limit.
    
    额,到此笔者有点晕了,file-handles(即文件句柄),然后相比而言在UNIX/LINUX中我们接触更多是file discriptor(FD,即文件描述符),通过百度搜索,似乎file-handle在windows中是一个类似file discrptor的东东,但是我们讨论的是linux,再google一下,我们可以精确到c语言中这两个概念的区别,据他们的讨论file-handle应该是一个高层的对象,使用fopen,fread等函数来调用,而FD是底层的一个对象,可以通过open,read等函数来调用。
    
    到此,我们应该可以下一个大致的结论了,file-max是内核可分配的最大文件数,nr_open是单个进程可分配的最大文件数,所以在我们使用ulimit或limits.conf来设置时,如果要超过默认的1048576值时需要先增大nr_open值(sysctl -w fs.nr_open=100000000或者直接写入sysctl.conf文件)。当然百万级别的单进程最大file-handle打开数应该也够

     

    /proc/sys/fs/file-max
                  This file defines a system-wide limit on the number of open files for all processes.  (See also setr-
                  limit(2),  which  can be used by a process to set the per-process limit, RLIMIT_NOFILE, on the number
                  of files it may open.)  If you get lots of error messages about running  out  of  file  handles,  try
                  increasing this value:
    
                  echo 100000 > /proc/sys/fs/file-max
    
                  The kernel constant NR_OPEN imposes an upper limit on the value that may be placed in file-max.
    
                  If  you  increase  /proc/sys/fs/file-max, be sure to increase /proc/sys/fs/inode-max to 3-4 times the
                  new value of /proc/sys/fs/file-max, or you will run out of inodes.
    
           /proc/sys/fs/file-nr
                  This (read-only) file gives the number of files presently opened.  It  contains  three  numbers:  The
                  number of allocated file handles, the number of free file handles and the maximum number of file han-
                  dles.  The kernel allocates file handles dynamically, but it doesn’t free them again.  If the  number
                  of allocated files is close to the
    
                  maximum,  you should consider increasing the maximum.  When the number of free file handles is large,
                  you’ve encountered a peak in your usage of file handles and you probably don’t need to  increase  the
                  maximum.
    
           /proc/sys/fs/inode-max
                  This  file  contains  the  maximum  number of in-memory inodes.  On some (2.4) systems, it may not be
                  present. This value should be 3-4 times larger than the value in file-max, since  stdin,  stdout  and
                  network  sockets also need an inode to handle them. When you regularly run out of inodes, you need to
                  increase this value.
    
           /proc/sys/fs/inode-nr
                  This file contains the first two values from inode-state.
    
           /proc/sys/fs/inode-state
                  This file contains seven  numbers:  nr_inodes,  nr_free_inodes,  preshrink  and  four  dummy  values.
                  nr_inodes is the number of inodes the system has allocated.  This can be slightly more than inode-max
                  because Linux allocates them one page full at a time.  nr_free_inodes represents the number  of  free
                  inodes.  preshrink is non-zero when the nr_inodes > inode-max and the system needs to prune the inode
                  list instead of allocating more.
    
           /proc/sys/fs/inotify (since Linux 2.6.13)
                  This directory contains files max_queued_events, max_user_instances, and max_user_watches,  that  can
                  be used to limit the amount of kernel memory consumed by the inotify interface.  For further details,
                  see inotify(7).
    
           /proc/sys/fs/lease-break-time
                  This file specifies the grace period that the kernel  grants  to  a  process  holding  a  file  lease
                  (fcntl(2)) after it has sent a signal to that process notifying it that another process is waiting to
                  open the file.  If the lease holder does not remove or downgrade the lease within this grace  period,
                  the kernel forcibly breaks the lease.
    
           /proc/sys/fs/leases-enable
                  This  file  can  be used to enable or disable file leases (fcntl(2)) on a system-wide basis.  If this
                  file contains the value 0, leases are disabled.  A non-zero value enables leases.
    
           /proc/sys/fs/mqueue (since Linux 2.6.6)
                  This directory contains files msg_max, msgsize_max, and queues_max, controlling the resources used by
                  POSIX message queues.  See mq_overview(7) for details.

     


    /proc/sys/net目录

    http://www.cnblogs.com/fczjuever/archive/2013/04/17/3026694.html

      所有的TCP/IP参数都位于/proc/sys/net目录下(请注意,对/proc/sys/net目录下内容的修改都是临时的,任何修改在系统重启后都会丢失),例如下面这些重要的参数:

    参数(路径+文件)

    描述

    默认值

    优化值

    /proc/sys/net/core/rmem_default

    默认的TCP数据接收窗口大小(字节)。

    229376

    256960

    /proc/sys/net/core/rmem_max

    最大的TCP数据接收窗口(字节)。

    131071

    513920

    /proc/sys/net/core/wmem_default

    默认的TCP数据发送窗口大小(字节)。

    229376

    256960

    /proc/sys/net/core/wmem_max

    最大的TCP数据发送窗口(字节)。

    131071

    513920

    /proc/sys/net/core/netdev_max_backlog

    在每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。

    1000

    2000

    /proc/sys/net/core/somaxconn

    定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数。

    128

    2048

    /proc/sys/net/core/optmem_max

    表示每个套接字所允许的最大缓冲区的大小。

    20480

    81920

    /proc/sys/net/ipv4/tcp_mem

    确定TCP栈应该如何反映内存使用,每个值的单位都是内存页(通常是4KB)。第一个值是内存使用的下限;第二个值是内存压力模式开始对缓冲区使用应用压力的上限;第三个值是内存使用的上限。在这个层次上可以将报文丢弃,从而减少对内存的使用。对于较大的BDP可以增大这些值(注意,其单位是内存页而不是字节)。

    94011  125351  188022

    131072  262144  524288

    /proc/sys/net/ipv4/tcp_rmem

    为自动调优定义socket使用的内存。第一个值是为socket接收缓冲区分配的最少字节数;第二个值是默认值(该值会被rmem_default覆盖),缓冲区在系统负载不重的情况下可以增长到这个值;第三个值是接收缓冲区空间的最大字节数(该值会被rmem_max覆盖)。

    4096  87380  4011232

    8760  256960  4088000

    /proc/sys/net/ipv4/tcp_wmem

    为自动调优定义socket使用的内存。第一个值是为socket发送缓冲区分配的最少字节数;第二个值是默认值(该值会被wmem_default覆盖),缓冲区在系统负载不重的情况下可以增长到这个值;第三个值是发送缓冲区空间的最大字节数(该值会被wmem_max覆盖)。

    4096  16384  4011232

    8760  256960  4088000

    /proc/sys/net/ipv4/tcp_keepalive_time

    TCP发送keepalive探测消息的间隔时间(秒),用于确认TCP连接是否有效。

    7200

    1800

    /proc/sys/net/ipv4/tcp_keepalive_intvl

    探测消息未获得响应时,重发该消息的间隔时间(秒)。

    75

    30

    /proc/sys/net/ipv4/tcp_keepalive_probes

    在认定TCP连接失效之前,最多发送多少个keepalive探测消息。

    9

    3

    /proc/sys/net/ipv4/tcp_sack

    启用有选择的应答(1表示启用),通过有选择地应答乱序接收到的报文来提高性能,让发送者只发送丢失的报文段,(对于广域网通信来说)这个选项应该启用,但是会增加对CPU的占用。

    1

    1

    /proc/sys/net/ipv4/tcp_fack

    启用转发应答,可以进行有选择应答(SACK)从而减少拥塞情况的发生,这个选项也应该启用。

    1

    1

    /proc/sys/net/ipv4/tcp_timestamps

    TCP时间戳(会在TCP包头增加12个字节),以一种比重发超时更精确的方法(参考RFC 1323)来启用对RTT 的计算,为实现更好的性能应该启用这个选项。

    1

    1

    /proc/sys/net/ipv4/tcp_window_scaling

    启用RFC 1323定义的window scaling,要支持超过64KB的TCP窗口,必须启用该值(1表示启用),TCP窗口最大至1GB,TCP连接双方都启用时才生效。

    1

    1

    /proc/sys/net/ipv4/tcp_syncookies

    表示是否打开TCP同步标签(syncookie),内核必须打开了CONFIG_SYN_COOKIES项进行编译,同步标签可以防止一个套接字在有过多试图连接到达时引起过载。

    1

    1

    /proc/sys/net/ipv4/tcp_tw_reuse

    表示是否允许将处于TIME-WAIT状态的socket(TIME-WAIT的端口)用于新的TCP连接 。

    0

    1

    /proc/sys/net/ipv4/tcp_tw_recycle

    能够更快地回收TIME-WAIT套接字。

    0

    1

    /proc/sys/net/ipv4/tcp_fin_timeout

    对于本端断开的socket连接,TCP保持在FIN-WAIT-2状态的时间(秒)。对方可能会断开连接或一直不结束连接或不可预料的进程死亡。

    60

    30

    /proc/sys/net/ipv4/ip_local_port_range

    表示TCP/UDP协议允许使用的本地端口号

    32768  61000

    1024  65000

    /proc/sys/net/ipv4/tcp_max_syn_backlog

    对于还未获得对方确认的连接请求,可保存在队列中的最大数目。如果服务器经常出现过载,可以尝试增加这个数字。

    2048

    2048

    /proc/sys/net/ipv4/tcp_low_latency

    允许TCP/IP栈适应在高吞吐量情况下低延时的情况,这个选项应该禁用。

    0

     

    /proc/sys/net/ipv4/tcp_westwood

    启用发送者端的拥塞控制算法,它可以维护对吞吐量的评估,并试图对带宽的整体利用情况进行优化,对于WAN 通信来说应该启用这个选项。

    0

     

    /proc/sys/net/ipv4/tcp_bic

    为快速长距离网络启用Binary Increase Congestion,这样可以更好地利用以GB速度进行操作的链接,对于WAN通信应该启用这个选项。

    1

     


    /etc/sysctl.conf文件

      /etc/sysctl.conf是一个允许你改变正在运行中的Linux系统的接口。它包含一些TCP/IP堆栈和虚拟内存系统的高级选项,可用来控制Linux网络配置,由于/proc/sys/net目录内容的临时性,建议把TCPIP参数的修改添加到/etc/sysctl.conf文件, 然后保存文件,使用命令“/sbin/sysctl –p”使之立即生效。具体修改方案参照上文:

    net.core.rmem_default = 256960

    net.core.rmem_max = 513920

    net.core.wmem_default = 256960

    net.core.wmem_max = 513920

    net.core.netdev_max_backlog = 2000

    net.core.somaxconn = 2048

    net.core.optmem_max = 81920

    net.ipv4.tcp_mem = 131072  262144  524288

    net.ipv4.tcp_rmem = 8760  256960  4088000

    net.ipv4.tcp_wmem = 8760  256960  4088000

    net.ipv4.tcp_keepalive_time = 1800

    net.ipv4.tcp_keepalive_intvl = 30

    net.ipv4.tcp_keepalive_probes = 3

    net.ipv4.tcp_sack = 1

    net.ipv4.tcp_fack = 1

    net.ipv4.tcp_timestamps = 1

    net.ipv4.tcp_window_scaling = 1

    net.ipv4.tcp_syncookies = 1

    net.ipv4.tcp_tw_reuse = 1

    net.ipv4.tcp_tw_recycle = 1

    net.ipv4.tcp_fin_timeout = 30

    net.ipv4.ip_local_port_range = 1024  65000

    net.ipv4.tcp_max_syn_backlog = 2048

  • 相关阅读:
    Linux下JDK安装位置
    Oracle中的User与Schema
    MFC中不同窗口间的切换
    MFC 动态的画线 画当前的线是清除上一次画的线
    VC ADO连接ACCESS步骤及错误处理
    虚继承和虚函数继承
    虚拟机无法连接连接U盘
    未能找到类型或命名空间
    VS2008 重构 封装字段 出错 无法使用
    vs2010 Visual Studio 遇到了异常。这可能是由某个扩展导致的
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5602731.html
Copyright © 2020-2023  润新知