zoukankan      html  css  js  c++  java
  • Linux文件句柄数调整

     首先介绍下Linux系统中"一切都是文件"。

    1. Linux系统文件句柄数概念

    文件句柄(Windows)

    文件描述符(Unix/Linux):file discriptor,fd。对于内核而言,所有打开的文件都是通过文件描述符引用,文件描述符是一个非负整数,变化范围是0~(OPEN_MAX-1)。

    其中,OPEN_MAX解释为

     The maximum number of files that a process can have open at any time. Must not be less than _POSIX_OPEN_MAX(20) 

    实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。

    fd通常是一个非常小的非负整数,内核用于标识一个特定进程正在访问的文件。当打开或者创建一个新文件时,内核向进程返回一个文件描述符。当读、写一个文件时,使用open或者create返回的文件描述符来标识该文件,将其作为参数传给read或者write。每个进程在PCB(Process Control Block)中保存着一份文件描述符表,文件描述符就是这个表的索引,每个表项都有一个指向已打开文件的指针。
    文件指针(FILE*):C 语言中使用文件指针做为 I/O的句柄。文件指针指向进程用户区中的一个被称为 FILE 结构的数据结构。FILE 结构包括一个缓冲区和一个文件描述符。而文件描述符是文件描述符表的一个索引,因此从某种意义上说文件指针就是句柄的句柄(在Windows系统上,文件描述符被称作文件句柄,句柄是一种指向指针的指针)。
    Unix系统shell编程时把文件描述符0与进程的标准输入(standard input)关联,文件描述符1与标准输出(standard out)关联,文件描述符2与标准错误(standard error)关联,这种关联关系是程序使用的管理,与Unix内核无关。POSIX 定义了 STDIN_FILENO、STDOUT_FILENO 和 STDERR_FILENO 来代替 0、1、2。这三个符号常量的定义位于头文件 <unistd.h>。一般来说,每个进程最多可以打开 64 个文件(0 — 63)。对于 FreeBSD 8.0、Linux 3.2.0、Mac OS X 10.6.8 以及 Solaris 10 来说,文件描述符的变化范围几乎是无限的,它只受系统配置的存储器总量、整型的字长以及系统管理员所配置的软限制和硬限制的约束。

    2. 查询Linux系统文件句柄数

    # ulimit -a
    core file size          (blocks, -c) 0
    data seg size           (kbytes, -d) unlimited
    scheduling priority             (-e) 0
    file size               (blocks, -f) unlimited
    pending signals                 (-i) 7179
    max locked memory       (kbytes, -l) 64
    max memory size         (kbytes, -m) unlimited
    open files                      (-n) 65535
    pipe size            (512 bytes, -p) 8
    POSIX message queues     (bytes, -q) 819200
    real-time priority              (-r) 0
    stack size              (kbytes, -s) 8192
    cpu time               (seconds, -t) unlimited
    max user processes              (-u) 7179
    virtual memory          (kbytes, -v) unlimited
    file locks                      (-x) unlimited

    3. 配置文件

     单一程序(进程)级别的限制

    /etc/security/limits.conf 
    
    * soft nofile 2048 
    * hard nofile 32768 
    * soft nofile 2048 
    * hard nofile 32768 
    
    第一列表示域(domain),可以使用用户名(root等),组名(以@开头),通配置*和%,%可以用于%group参数。
    第二列表示类型(type),值可以是soft或者hard
    第三列表示项目(item),值可以是core, data, fsize, memlock, nofile, rss, stack, cpu, nproc, as, maxlogins, maxsyslogins, priority, locks, msgqueue, nie, rtprio.
    第四列表示值. 

     系统级别的限制

    man 5 proc:
     /proc/sys/fs/file-max
                  This  file  defines a system-wide limit on the number of open files for all processes.  (See
                  also  setrlimit(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  num-
                  bers: the number of allocated file handles; the number of free file handles; and the maximum
                  number of file handles.  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.

    4. 如何修改最大句柄数

    单一进程级别

    临时生效: ulimit -n 2048 或者 echo ulimit -n 2048>>/etc/profile  

    永久生效:修改 /etc/security/limits.conf 配置文件

     系统级别

      修改 /proc/sys/fs/file-max 文件内容

      方法一:直接修改sys文件, echo 100000 > /proc/sys/fs/file-max

      方法二:直接修改 /etc/sysctl.conf 文件增加或者修改配置项 fs.file-max = 999999 ,输入 #sysctl -p 使之生效;   

    方法三: sysctl -w fs.nr_open=100000000   

       如果增加/proc/sys/fs/file-max,需要同步调整/proc/sys/fs/inode-max到新file-max值的3-4倍,否则会用尽inode资源。通常,file-max的值设置为内存(KB)大小的10%左右,即

       grep MemTotal /proc/meminfo | awk '{printf("%d\n",$2/10)}' 

    5. 如何查询当前系统中文件句柄数使用情况

    1)统计各进程打开句柄数:lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr

    2)统计各用户打开句柄数:lsof -n|awk '{print $3}'|sort|uniq -c|sort -nr

    3)统计各命令打开句柄数:lsof -n|awk '{print $1}'|sort|uniq -c|sort -nr

  • 相关阅读:
    问题账户需求分析
    需求分析初学理解
    GitHub初步探索-1-使用本地代码管理工具,简化上传的过程
    软件工程概论-个人总结
    第二次冲刺-个人工作总结05
    第二次冲刺-个人工作总结04
    第二次冲刺-个人工作总结03
    第二次冲刺-个人工作总结02
    第二次冲刺-个人工作总结01
    第一次冲刺-个人工作总结10
  • 原文地址:https://www.cnblogs.com/hopkins516/p/10171614.html
Copyright © 2011-2022 走看看