資源的限制
Two types of ulimits
Hard limit is the maximum value that is allowed for the soft limit. Any changes to the hard limit require root access.
Soft limit is the value that Linux uses to limit the system resources for running processes. The soft limit cannot be greater than the hard limit.
Hard 設定給 soft 的最大值。任何 hard 改變都需要 root 權限。
Soft 是 linux 限制給每個 process 的上限。Soft 不能超過 hard 上限。
ulimit -aH 顯示 hard 全部設定
ulimit -aS 顯示 soft 全部設定
Soft Hard 區別
ulimit 參數詳解
依據設定的層級分系統級、user級
分系統級、User級。
user級不能超越系統級。
- 可針對 user 做個別設定,在 /etc/security/limits.conf。其中 * 代表所有 user。
- 限制設定落實到 user 啟動的 process。
- 限制設定是繼承性的,子 process 繼承父 process 的設定值。
範例:
User 起動 shell, shell 的設定來自系統預設及 /etc/security/limits.conf。
User 起動 shell, shell 的設定來自系統預設及 /etc/security/limits.conf。
User 在 shell 中啟動 process,此時 process 繼承 shell 的設定。
臨時設定
shell 中使用 ulimit 命令設定,但結束 session就沒有了。
ulimit -s unlimited //設定 stack unlimited
ulimit -n 2048. //open file 數 2048
ulimit -n 2048. //open file 數 2048
永久設定
更改 /etc/security/limits.conf
更改後重新 login 就可套用新設定
查詢當前user / process 設定
ulimit -aH //顯示所有設定 hardulimit -aS //顯示所有設定 softcore file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31204 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 1024 (file descriptor) 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) 31204 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
查詢 pocess 設定
cat /proc/{pid}/limits //example cat /proc/1888/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 31204 31204 processes Max open files 1024 4096 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 31204 31204 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us //Max processes 是可開啟執行緒上限持
查詢 pocess 資源使用量
cat /proc/xxxxx/stat
cat /proc/xxxxx/status
//顯示某 process file descriptor數量 lsof -p 28290 lsof -a -p 28290 or //列出 ls -l /proc/28290/fd | less //計算總數 ls -l /proc/28290/fd | wc -l
系統總上限
sysctl fs.file-nr
fs.file-nr //顯示已經分配的 fd 數量、可分配的 fd 數量、系統總 fd 數量
fs.file-max //系統所有 process 可使用的總 fd 數量
查看系統設定
sysctl fs.file-nr //輸出 fs.file-nr = 1020 0 70000 1. 1020 The number of allocated file handles. 2. 0 The number of unused-but-allocated file handles. 3. 70000 The system-wide maximum number of file handles. cat /proc/sys/fs/file-max 或是看上限設定 sysctl fs.file-max 臨時性修改 sysctl -w fs.file-max=102400 永久修改 sudo vim /etc/sysctl.conf fs.file-max = 70000 //這個數值對應於hard limit sysctl -p 使修改生效
系統設定的user上限
fs.nr_open
系統級user的單一process開檔數限制
查看 cat /proc/sys/fs/nr_open 或是 sysctl fs.nr_open 修改 sudo /etc/sysctl.conf nr_open=1000000
實際真正個人上限
vi /etc/security/limits.conf * hard nofile 102400 //針對所有user,不包括root * soft nofile 102400 //針對所有user,不包括root httpd hard nofile 102400 //針對httpd這個user httpd soft nofile 102400 //針對httpd這個user root hard nofile 102400 //針對root root soft nofile 102400 //針對root
結論
user的每個process開檔上限這些限制1 fs.file-max:系統總上限
2 fs.nr_open:系統設定的個人上限,限制 limits.conf設定的上限
3 /etc/security/limits.conf中nofile設定:實際真正能使用的數量
# /etc/security/limits.conf //設定值 // core - 限制内核文件的大小 date - 最大数据大小 fsize - 最大文件大小 memlock - 最大锁定内存地址空间 nofile - 打开文件的最大数目 rss - 最大持久设置大小 stack - 最大栈大小 cpu - 以分钟为单位的最多CPU时间 noproc - 进程的最大数目 as - 地址空间限制 maxlogins - 此用户允许登录的最大数目 # * 代表针对所有用户 * soft nofile 102400 * hard nofile 102400 * soft stack unlimited //開啟stack 無限制
# /etc/security/limits.conf * soft nproc 100 //不能太大 * hard nproc 100 //不能太大 # /etc/security/limits.conf * - nproc 100 * - nofile 102400
沒有留言:
張貼留言