ulimit限制系统资源

简介: limits.conf的工作原理:   limits.conf的后端是这样工作的:limits.conf是 pam_limits.so的配置文件,然后/etc/pam.d/下的应用程序 调用pam_***.so模块。譬如说,当用户访问服务器,服务程序将请求发送到PAM模块,PAM模块根据服务名称在/etc/pam.d目录下选择一个对应的服务文件,然后根据服务文件的内容选择具体的PAM模

limits.conf的工作原理:
  limits.conf的后端是这样工作的:limits.conf是 pam_limits.so的配置文件,然后/etc/pam.d/下的应用程序
调用pam_***.so模块。譬如说,当用户访问服务器,服务程序将请求发送到PAM模块,PAM模块根据服务名称在/etc/pam.d目录下选择一个对应的服务文件,然后根据服务文件的内容选择具体的PAM模块进行处理。[摘抄]


ulimit是一种简单并且有效的实现系统资源限制的方式!

下面分享一个小实验!

 实验:limits实验!关于能打开的最大文件数和能并发的最大进程数:

[研究背景: nginx 与 php 的连接, 以及对其做压力测试的时候! 由于php-cgi是单进程的,影响nginx的效率,  然后便使用产蛋程序 spawn.但是它依然受到限制, 怎么办? 系统最大并发进程数的控制, 也就是下面实验的目的了! ]

[注意, 先将系统的默认全局参数调大 sysctl.conf -->kernel.threads-max = xxxxx .... ]


实验结论:

a. nofile的配置,可以用 * 来通配对所有的用户的设置!

b. noproc的配置,用 *  无法通配, 只能指定特定的用户!

温馨提醒: 最大进程并发数的设置很危险的,如果你的机器性能不是很好的话,用spawn产出5000个cgi进程,然后用ab并发出上万个查询请求进行压力测试!最后你发现,并发数小于5000的时候错误率为零,再高点儿,就有错了! [当然,我使用的是台PC机,不是标配的服务器! 实践很重要!]

另外,当要killall全杀掉服务端开启的cgi进程,你会发现机器卡了,我的实验机器性能不怎么好,崩掉啦~

 

1> limits.conf:
--------------------------->
* soft nofile 1000000
* hard nofile 1000000
root soft nproc  20000
root hard nproc  20000
* soft nproc  20000
* hard nproc  20000
---------------------------->
实验结果:
[root@lin /]# ulimit -u   //20000
[root@lin /]# ulimit -n   //1000000
[root@lin /]# su - test
[test@lin ~]$ ulimit -u   //1024
[test@lin ~]$ ulimit -n   //1000000
####################################
2> limits.conf
--------------------------->
* soft nofile 1000000
* hard nofile 1000000
root soft nproc  20000
root hard nproc  20000
* soft nproc  20000
* hard nproc  20000
test soft nproc  20000
test hard nproc  20000
---------------------------->
实验结果:
[root@lin /]# ulimit -u   //20000
[root@lin /]# ulimit -n   //1000000
[root@lin /]# su - test
[test@lin ~]$ ulimit -u   //20000
[test@lin ~]$ ulimit -n   //1000000
######################################
3> limits.conf
---------------------------->
* soft nofile 1000000
* hard nofile 1000000
root soft nproc  30000
root hard nproc  30000
----------------------------->
实验结果:
[root@lin /]# su - root
[root@lin ~]# ulimit -u  //30000
[root@lin ~]# ulimit -n  //1000000
[root@lin /]# su - test
[test@lin ~]$ ulimit -u  //1024
[test@lin ~]$ ulimit -n  //1000000
#######################################
4> limits.conf
------------------------------>
* soft nofile 1000000
* hard nofile 1000000
* soft nproc  20000
* hard nproc  20000
------------------------------->
实验结果:
[root@lin /]# su - root
[root@lin ~]# ulimit -u  //1024
[root@lin ~]# ulimit -n  //1000000
[root@lin ~]# su - test
[test@lin ~]$ ulimit -u  //1024
[test@lin ~]$ ulimit -n  //1000000
########################################

Appendix:

ulimit是shell内建命令,贴出它的manual手册:

===================================================================================

ulimit [-HSTabcdefilmnpqrstuvx [limit]]
              Provides  control  over  the resources available to the
              shell and to processes started by it, on  systems  that
              allow such control.  The -H and -S options specify that
              the hard or soft limit is set for the  given  resource.
              A  hard  limit  cannot  be increased by a non-root user
              once it is set; a soft limit may be increased up to the
              value of the hard limit.  If neither -H nor -S is spec-
              ified, both the soft and  hard  limits  are  set.   The
              value  of  limit  can be a number in the unit specified
              for the resource or one of  the  special  values  hard,
              soft,  or  unlimited,  which stand for the current hard
              limit, the current soft limit, and  no  limit,  respec-
              tively.   If limit is omitted, the current value of the
              soft limit of the resource is printed,  unless  the  -H
              option is given.  When more than one resource is speci-
              fied, the limit name and unit are  printed  before  the 
              value.  Other options are interpreted as follows:

              -a     All current limits are reported
              -b     The maximum socket buffer size
              -c     The maximum size of core files created
              -d     The maximum size of a process’s data segment
              -e     The maximum scheduling priority ("nice")
              -f     The  maximum  size of files written by the shell
                     and its children
              -i     The maximum number of pending signals
              -l     The maximum size that may be locked into memory
              -m     The maximum resident set size (many  systems  do
                     not honor this limit)
              -n     The  maximum  number  of  open  file descriptors
                     (most systems do not allow this value to be set)
              -p     The  pipe  size in 512-byte blocks (this may not
                     be set)
              -q     The maximum number of  bytes  in  POSIX  message
                     queues
              -r     The maximum real-time scheduling priority
              -s     The maximum stack size
              -t     The maximum amount of cpu time in seconds
              -u     The  maximum  number of processes available to a
                     single user

              -v     The maximum amount of virtual  memory  available
                     to the shell
              -x     The maximum number of file locks
              -T     The maximum number of threads

              If limit is given, it is the new value of the specified
              resource (the -a option is display only).  If no option
              is  given, then -f is assumed.  Values are in 1024-byte
              increments, except for -t, which  is  in  seconds,  -p,
              which  is  in units of 512-byte blocks, and -T, -b, -n,
              and -u, which are unscaled values.  The  return  status
              is  0 unless an invalid option or argument is supplied,
              or an error occurs while setting a new limit.

=====================================================================================


转一个绝对牛掰的过来:(2013年6月加)

ulimit限制之nproc问题

前两天微博上的@王关胜同学问了个问题:

#ulimit问题# 关于nproc设置:centos6,内核版本是2.6.32. 默认情况下,ulimit -u的值为1024,是/etc/security/limits.d/90-nproc.conf的值限制;注释掉这个限制后,值为95044;手工设置90-nproc.conf文件,值为新设置的值。想请 问这个95044是怎么来的?

这个问题挺有意思的,这里面有二个信息点:

1. 为什么limit配置文件是 /etc/security/limits.d/90-nproc.conf 而不是其他?
2. 为什么是nproc的值95044,而不是其他。

之前我也写了些ulimit的问题的解决,参见 这里

我们来简单的做下实验:

$ cat/etc/security/limits.d/90-nproc.conf         
*      soft    nproc   8933
$ ulimit-u
8933
 
$ cat/etc/security/limits.d/90-nproc.conf      #注释掉
#*      soft    nproc   8933
$ ulimit-u
385962

我们可以看出就是说当注释掉限制的话,不同的机器值是不同的。

我们先来回答第一个问题:为什么limit配置文件是 /etc/security/limits.d/90-nproc.conf 而不是其他
这个问题早些时候 杨德华 同学碰到了,也写了篇 博文 来解释redhat6下面如何破解nproc的限制,但是文章没提到这个问题。

我们一步步来看这个问题,首先看下 谁在使用 90-nproc.conf 这个文件:

$ catt.stp
probe syscall.open.return{
  filename = user_string($filename)
  if(!isinstr(filename, "90-nproc.conf")) next;
  printf("%s %d\n", execname(), pid());
}
$ sudostap t.stp
sshd 24844

运行脚本后,开个ssh终端上去,就马上知道sshd在使用这个文件, 同时也验证了配置是即刻生效的。

我们都知道linux下这个limit限制是由pam_limits来执行的。
那么什么是PAM以及它的架构,参考 这里

$ grep-rin pam_limit /etc/pam.d
/etc/pam.d/sudo-i:6:session    required     pam_limits.so
/etc/pam.d/smartcard-auth-ac:16:session     required      pam_limits.so
/etc/pam.d/smartcard-auth:16:session     required      pam_limits.so
/etc/pam.d/system-auth-ac:20:session     required      pam_limits.so
/etc/pam.d/fingerprint-auth:16:session     required      pam_limits.so
/etc/pam.d/sudo:6:session    required     pam_limits.so
/etc/pam.d/runuser:4:session            required        pam_limits.so
/etc/pam.d/password-auth-ac:19:session     required      pam_limits.so
/etc/pam.d/password-auth:19:session     required      pam_limits.so
/etc/pam.d/system-auth:20:session     required      pam_limits.so
/etc/pam.d/fingerprint-auth-ac:16:session     required      pam_limits.so

那很自然,我们就会去找pam_limits的代码来看, 代码在 这里 可以下载,目前的版本是 Linux-PAM-1.1.6。

瞄几下modules/pam_limits/pam_limits.c就知道 限制如何执行的:

/* now the session stuff */
PAM_EXTERN int
pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED,
                     intargc, const char **argv)
{
[...]
   retval = init_limits(pamh, pl, ctrl);
    if(retval != PAM_SUCCESS) {
        pam_syslog(pamh, LOG_WARNING,"cannot initialize");
        returnPAM_ABORT;
    }
 
    retval = parse_config_file(pamh, pwd->pw_name, pwd->pw_uid, pwd->pw_gid, ctrl, pl);
    if(retval == PAM_IGNORE) {
        D(("the configuration file ('%s') has an applicable '<domain> -' entry", CONF_FILE));
        returnPAM_SUCCESS;
    }
    if(retval != PAM_SUCCESS || pl->conf_file != NULL)
        /* skip reading limits.d if config file explicitely specified */
        gotoout;
 
    /* Read subsequent *.conf files, if they exist. */
 
    /* set the LC_COLLATE so the sorting order doesn't depend                                                            
        on system locale */
 
    oldlocale =setlocale(LC_COLLATE,"C");
    glob_rc = glob(LIMITS_CONF_GLOB, GLOB_ERR, NULL, &globbuf);
 
    if(oldlocale != NULL)
        setlocale(LC_COLLATE, oldlocale);
 
    if(!glob_rc) {
        /* Parse the *.conf files. */
        for(i = 0; globbuf.gl_pathv[i] != NULL; i++) {
            pl->conf_file = globbuf.gl_pathv[i];
            retval = parse_config_file(pamh, pwd->pw_name, pwd->pw_uid, pwd->pw_gid, ctrl, pl);
          if(retval == PAM_IGNORE) {
                D(("the configuration file ('%s') has an applicable '<domain> -' entry", pl->conf_file));
                globfree(&globbuf);
                returnPAM_SUCCESS;
            }
            if(retval != PAM_SUCCESS)
                gotoout;
        }
    }
 
out:
[...]
}

分析这段代码可以知道先读/etc/security/limits.conf,如果/etc/security/limits.d/目录下还有配置文件的话,也读进来,一起分析。
这就意味/etc/security/limits.d/里面的文件里面的配置会覆盖/etc/security/limits.conf的配置。

我们看下这行:glob_rc = glob(LIMITS_CONF_GLOB, GLOB_ERR, NULL, &globbuf);
读取/etc/security/limits.d/目录下文件的函数,从名字就可以猜出,是遍历,文件名的数字起到顺序的作用。

到此就解释了文件名90-nproc.conf的作用。

接着看第二个问题: 为什么是nproc的值95044, 而不是其他。
通过阅读process_limit函数只是看到 nproc的最大值限制,没有看到其他的,那我们就很容易联想,如果用户不设置nproc的话,那么这个值应该是由内核自己来决定。

我们看下内核代码 2.6.18:

$ pwd
/home/chuba/linux-2.6.18.x86_64/kernel
$ grep-rin nproc .
./sys.c:896:                            current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
./fork.c:176:   init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
./fork.c:177:   init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
./fork.c:179:           init_task.signal->rlim[RLIMIT_NPROC];
./fork.c:1130:                  p->signal->rlim[RLIMIT_NPROC].rlim_cur) {
./cpuset.c:69:  int cnt;                /* unprocessed events count */
./cpuset.c:1140: * Limit the count of unprocessed events to FM_MAXCNT, so as to avoid
./user.c:181:    * new uid over his NPROC rlimit?  We can check this now

一下子就看出来了 默认值是 max_threads/2. 打开fork.c分析下:

//fork_init(num_physpages);
//void __init fork_init(unsigned long mempages)
/*
* The default maximum number of threads is set to a safe
* value: the thread structures can take up at most half
* of memory.
*/
max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);

其中mempages是机器的物理页面个数, THREAD_SIZE=8K, 也就是说:

default_nproc = max_threads / 2 = (mempages * PAGE_SIZE) / ( 2 * 8 *THREAD_SIZE ) = total_memory/128K;

我们来验证下:

$ cat/proc/meminfo |grep MemTotal
MemTotal:       49421024 kB
$ echo"49421024 / 128"| bc 
386101
$ ulimit-u
385962

算出来default_nproc =386101 是不是和实际的很接近?
因为物理页面会内存用作一些关键数据,所以实际的比计算出来的要小点。

小结: 源码说话!

祝玩的开心!



目录
相关文章
|
人工智能 安全 Apache
QwenPaw:你的私人 AI 助理 —— 数据归你、记忆进化、多端触达的开源个人智能体
QwenPaw 是一款开源、本地优先的AI个人智能体(Apache 2.0),数据归属用户、记忆自主进化、支持钉钉/飞书/微信等多端触达。3行命令即可部署,内置Coding IDE、Persona人格、定时任务、MCP工具生态与多Agent协作,真正属于你的私有AI助理。
QwenPaw:你的私人 AI 助理 —— 数据归你、记忆进化、多端触达的开源个人智能体
|
12月前
|
缓存 前端开发 测试技术
GLM-4.7开源!交出“生产级代码”
智谱今日发布并开源GLM-4.7,专注Coding场景,显著提升编程、推理与工具协同能力,在多项基准测试中位居开源模型前列。已上线z.ai与智谱清言,支持API调用。
3080 3
|
存储 Prometheus 监控
OSS监控体系搭建:Prometheus+Grafana实时监控流量、错误码、存储量(开源方案替代云监控自定义视图)
本方案基于Prometheus构建OSS监控系统,涵盖架构设计、指标采集、可视化、告警及性能优化,助力企业实现高可用、低成本的自建监控体系。
|
存储 前端开发 数据可视化
Grafana Loki,轻量级日志系统
本文介绍了基于Grafana、Loki和Alloy构建的轻量级日志系统。Loki是一个由Grafana Labs开发的日志聚合系统,具备高可用性和多租户支持,专注于日志而非指标,通过标签索引而非内容索引实现高效存储。Alloy则是用于收集和转发日志至Loki的强大工具。文章详细描述了系统的架构、组件及其工作流程,并提供了快速搭建指南,包括准备步骤、部署命令及验证方法。此外,还展示了如何使用Grafana查看日志,以及一些基本的LogQL查询示例。最后,作者探讨了Loki架构的独特之处,提出了“巨型单体模块化”的概念,即一个应用既可单体部署也可分布式部署,整体协同实现全部功能。
7192 189
Grafana Loki,轻量级日志系统
|
存储 Kubernetes 网络安全
关于阿里云 Kubernetes 容器服务(ACK)添加镜像仓库的快速说明
本文介绍了在中国大陆地区因网络限制无法正常拉取 Docker 镜像的解决方案。作者所在的阿里云 Kubernetes 集群使用的是较旧版本的 containerd(1.2x),且无法直接通过 SSH 修改节点配置,因此采用了一种无需更改 Kubernetes 配置文件的方法。通过为 `docker.io` 添加 containerd 的镜像源,并使用脚本自动修改 containerd 配置文件中的路径错误(将错误的 `cert.d` 改为 `certs.d`),最终实现了通过多个镜像站点拉取镜像。作者还提供了一个可重复运行的脚本,用于动态配置镜像源。虽然该方案能缓解镜像拉取问题,
1524 3
|
Ubuntu 数据可视化 Linux
Ubuntu卸载软件:3种卸载方式
只卸载程序。如果你移除程序但保留配置文件,请输入以下命令: sudo apt-get remove <programname>
|
存储 Kubernetes 网络协议
Kubernetes 集群部署 NFS-Subdir-External-Provisioner 存储插件
Kubernetes 对 Pod 进行调度时,以当时集群中各节点的可用资源作为主要依据,自动选择某一个可用的节点,并将 Pod 分配到该节点上。在这种情况下,Pod 中容器数据的持久化如果存储在所在节点的磁盘上,就会产生不可预知的问题,例如,当 Pod 出现故障,Kubernetes 重新调度之后,Pod 所在的新节点上,并不存在上一次 Pod 运行时所在节点上的数
10031 3
Kubernetes 集群部署 NFS-Subdir-External-Provisioner 存储插件
|
存储 机器学习/深度学习 并行计算
Pytorch NCHW/NHWC 的理解
Pytorch NCHW/NHWC 的理解
1805 1
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 6 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 6 月更新)
2163 0
|
Ubuntu
在Ubuntu 20.04 x64系统中增加中文支持
将 Ubuntu 系统语言设置为中文: 1. 更新系统语言包: `sudo apt update` 和 `sudo apt install language-pack-zh-hans`。 2. 修改区域设置:编辑 `/etc/default/locale` 文件,设置 `LANG=zh_CN.UTF-8` 和 `LANGUAGE=zh_CN:zh`。 3. 生成中文 locale: `sudo locale-gen zh_CN.UTF-8` 和 `sudo update-locale`。 4. 重启系统: `sudo reboot`。 5. 验证设置: `locale` 命令检查是否生效。
4254 1