zoukankan      html  css  js  c++  java
  • Linux systemd limits

    https://www.cnblogs.com/IMxY/p/8941022.html

    limits

    关于Centos 7 / RHEL 7 中的limits要了解以下几点:

    1. CentOS 7 / RHEL 7的系统中,使用Systemd替代了之前系统版本中的SysV。
    2. Systemd 中的 /etc/security/limits.conf 文件的配置作用域缩小了,只适用于通过PAM认证登录用户的资源限制,对Systemd的service的资源限制不生效。
    3. 登录用户的限制,通过 /etc/security/limits.conf 和 /etc/security/limits.d/*.conf 来配置。
    4. 系统全局的配置,在文件 /etc/systemd/system.conf 和/etc/systemd/system.conf.d/*.conf 来设定。
    5. 用户全局的配置,在文件 /etc/systemd/user.conf 和/etc/systemd/user.conf.d/*.conf 来设定。
    6. *.conf.d/*.conf 文件会覆盖  *.conf ,一般的sevice,使用system.conf中的配置即可。

     例:编辑 limits.conf 配置文件在文件尾部添加以下内容

      vim /etc/security/limits.conf

    * - nofile 1024000
    * - nproc 1024000

      等于

    * soft nofile 1024000
    * hard nofile 1024000
    * soft nproc 1024000
    * hard nproc 1024000

    两段配置参数为什么相等,在下面《添加格式》中有详细的参数说明。

    参数生效:

    1. working下执行 source /etc/profile 
    2. 退出当前shell重新登录
    3. 临时生效可用ulimit -n number

    查看当前:

    1. 进程:cat /proc/<process_pid>/limits
    2. lsof -p $process_pid 每个文件描述符的具体属性
    3. lsof -p $process_pid | wc -l  当前进程file descriptor table中FD的总量
    4. 用户:ulimit -n -u

        ulimit(选项):

          -a:显示目前资源限制的设定;
          -c <core文件上限>:设定core文件的最大值,单位为区块;
          -d <数据节区大小>:程序数据节区的最大值,单位为KB;
          -f <文件大小>:shell所能建立的最大文件,单位为区块;
          -H:设定资源的硬性限制,也就是管理员所设下的限制;
          -m <内存大小>:指定可使用内存的上限,单位为KB;
          -n <文件数目>:指定同一时间最多可开启的文件数;
          -p <缓冲区大小>:指定管道缓冲区的大小,单位512字节;
          -s <堆叠大小>:指定堆叠的上限,单位为KB;
          -S:设定资源的弹性限制;
          -t <CPU时间>:指定CPU使用时间的上限,单位为秒;
          -u <程序数目>:用户最多可开启的程序数目;
          -v <虚拟内存大小>:指定可使用的虚拟内存上限,单位为KB。

    ulimits文件中的添加格式:

    [username | @groupname]  type  resource [ limit | unlimited ]

      [username | @groupname]:

       username:设置需要被限制的用户名,

       @groupname:组名前面加@和用户名区别。

       也可以用通配符*来做所有用户的限制。

      type:有 soft,hard 和 -,
        soft :指的是当前系统生效的设置值。
        hard:表明系统中所能设定的最大值。
        -      :表明同时设置了 soft 和 hard 的值。
        注意:soft 的限制不能比hard 限制高。

      resource:
        core - 限制内核文件的大小(kb)
        date - 最大数据大小(kb)
        fsize - 最大文件大小(kb)
        memlock - 最大锁定内存地址空间(kb)
        nofile - 打开文件的最大数目
        rss - 最大持久设置大小(kb)
        stack - 最大栈大小(kb)
        cpu - 以分钟为单位的最多 CPU 时间
        noproc - 进程的最大数目
        as - 地址空间限制
        maxlogins - 此用户允许登录的最大数目

      [  limit | unlimited ]:

        limit   :限制所用的数字,具体可根据服务器的硬件配置和服务所需自定义。
        unlimited:无限制


    /etc/systemd/system.conf

    复制代码
    #  This file is part of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU Lesser General Public License as published by
    #  the Free Software Foundation; either version 2.1 of the License, or
    #  (at your option) any later version.
    #
    # Entries in this file show the compile time defaults.
    # You can change settings by editing this file.
    # Defaults can be restored by simply deleting this file.
    #
    # See systemd-system.conf(5) for details.
    
    [Manager]
    #LogLevel=info
    #LogTarget=journal-or-kmsg
    #LogColor=yes
    #LogLocation=no
    #DumpCore=yes
    #CrashShell=no
    #ShowStatus=yes
    #CrashChVT=1
    #CtrlAltDelBurstAction=reboot-force
    #CPUAffinity=1 2
    #JoinControllers=cpu,cpuacct net_cls,net_prio
    #RuntimeWatchdogSec=0
    #ShutdownWatchdogSec=10min
    #CapabilityBoundingSet=
    #SystemCallArchitectures=
    #TimerSlackNSec=
    #DefaultTimerAccuracySec=1min
    #DefaultStandardOutput=journal
    #DefaultStandardError=inherit
    #DefaultTimeoutStartSec=90s
    #DefaultTimeoutStopSec=90s
    #DefaultRestartSec=100ms
    #DefaultStartLimitInterval=10s
    #DefaultStartLimitBurst=5
    #DefaultEnvironment=
    #DefaultCPUAccounting=no
    #DefaultBlockIOAccounting=no
    #DefaultMemoryAccounting=no
    #DefaultTasksAccounting=no
    #DefaultTasksMax=
    #DefaultLimitCPU=
    #DefaultLimitFSIZE=
    #DefaultLimitDATA=
    #DefaultLimitSTACK=
    #DefaultLimitCORE=
    #DefaultLimitRSS=
    #DefaultLimitNOFILE=
    #DefaultLimitAS=
    #DefaultLimitNPROC=
    #DefaultLimitMEMLOCK=
    #DefaultLimitLOCKS=
    #DefaultLimitSIGPENDING=
    #DefaultLimitMSGQUEUE=
    #DefaultLimitNICE=
    #DefaultLimitRTPRIO=
    #DefaultLimitRTTIME=
    复制代码

    默认 system.conf  文件中给很多可限制的参数

    文件和进程的参数如下:

    DefaultLimitNOFILE=1024000
    DefaultLimitNPROC=1024000

    参数生效:

    systemctl daemon-reload
    systemctl restart service_name.service

    File Descriptors的设置

     /proc/sys/fs/file-max决定了当前内核可以打开的最大的文件句柄数

    使用shell计算

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

    设置:

      临时生效

    sysctl -w fs.file-max=100000 

      永久生效

    vim /etc/sysctl.conf + fs.file-max = 100000

      查看

    cat /proc/sys/fs/file-max

    or

    sysctl fs.file-max

    /proc/sys/fs/file-nr中的值由三部分组成,分别为:

    • 已经分配的文件句柄数
    • 已经分配单没有使用的文件句柄数
    • 最大文件句柄数

    第二项的值总为0,这并不是一个错误,它实际上意味着已经分配的文件句柄无一浪费的都已经被使用了
    查找文件句柄问题的时候,还有一个很实用的程序lsof,可以很方便看到某个进程开了那些句柄,也可以看到某个文件/目录被什么进程占用了

    lsof -n |awk '{print $2}'|sort|uniq -c |sort -nr|more
    • file-max是内核级别的,所有的进程总和不能超过这个数
    • ulimit是进程级别的
  • 相关阅读:
    Java实现 LeetCode 50 Pow(x,n)
    Java实现 LeetCode 50 Pow(x,n)
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 47 全排列 II(二)
    Java实现 LeetCode 47 全排列 II(二)
  • 原文地址:https://www.cnblogs.com/shengs/p/10028965.html
Copyright © 2011-2022 走看看