苏苏的博客

简约至极

Linux系统优化的其他配置

Linux 性能优化

ulimit

确保

/proc/sys/fs/nr_open > /proc/sys/fs/file-max > ulimit -Hn > ulimit -Sn > 10000

ulimit -a 查看配置

可根据pid查看某一进程目前的限制

cat /proc/16223/limits

查看每个进程所允许的最大opend files ulimit -n

查看目前系统已打开的open files lsof | wc -l 或者 ls -l /proc/*/fd | wc -l

lsof会列出重复的, 使用 lsof|awk '{print $9}'|sort|uniq|wc -l 可以去除重复

cat /proc/sys/fs/file-nr 会在系统内核层面统计,比较正确

查看系统所允许的最大open files cat /proc/sys/fs/file-max

查看一个进程目前已打开的 lsof -p 28290 或者 lsof -a -p 28290

或者

ls -l /proc/28290/fd | wc -l

查看 hard limit ulimit -Hn

查看 soft limit ulimit -Sn

临时修改

通过ulimit -Hn设置最Hard limit

ulimit -Hn 990000

通过ulimit -Sn设置最soft limit

ulimit -Sn 990000

同时设置soft limit和hard limit。对于非root用户只能设置比原来小的hard limit。

ulimit -n 81920 修改每个进程所允许的最大

修改系统的 echo 819200 > /proc/sys/fs/file-max

  • 所有进程打开的文件描述符数不能超过/proc/sys/fs/file-max
  • 单个进程打开的文件描述符数不能超过user limit中nofile的soft limit
  • nofile的soft limit不能超过其hard limit
  • nofile的hard limit不能超过/proc/sys/fs/nr_open

动态配置一个进程的

prlimit -n81920 -p pid_of_process

查看当前值

grep ‘open files’ /proc/$( cat /var/run/nginx.pid )/limits

永久修改

只有root用户才有权限修改/etc/security/limits.conf

如果limits.conf没有做设定,则默认值是 soft limit 1024 , hard limit 4096

修改

nginx       soft    nofile  10000
nginx       hard    nofile  30000

或者

* soft nofile 250000
* hard nofile 350000

sysctl -p 使其生效

修改系统的

vi /etc/sysctl.conf

设置

fs.file-max = 70000

在nginx层面配置

worker_rlimit_nofile 350000;

user root;
worker_processes  2;
worker_rlimit_nofile 350000;

events {
    worker_connections  81920;
}


测试工具

https://github.com/ideawu/c1000k

overcommit_memory内存分配

sysctl vm.overcommit_memory=1

somaxconn

sysctl net.core.somaxconn=1024

swap

随机数

随机数

/dev/random

/dev/random会根据噪音产生随机数,如果噪音不够它就会阻塞

提高生成速度

cat /proc/sys/kernel/random/entropy_avail 查看熵池大小

如果你的CPU带有DRNG特性,可以充分利用硬件来提高熵池产生的速度 。

如果你的硬件不支持,也没有关系,我们可以让/dev/unrandom来做“熵源”

cat /proc/cpuinfo | grep rdrand 查看是否支持

安装rngd服务(熵服务)

yum install rngd-tools 或者yum install rng-tools

如果你的CPU不支持DRNG特性或者像我一样使用虚拟机,可以使用/dev/unrandom来模拟。

编辑/etc/systemd/system/rngd.service service小结,ExecStart=/sbin/rngd -f -r /dev/urandom

/dev/urandom

伪随机数, 伪随机数的生成速度要快很多

inotify

tail: 无法使用 inotify 机制,回归为 polling 机制: 打开的文件过多

可能的问题是inotify被恶意消耗

查看当前系统的配置

sysctl fs.inotify

查看已在用的

lsof | grep inotify | wc -l

改大一些

sysctl -n -w fs.inotify.max_user_watches=16384

sysctl -n -w fs.inotify.max_user_instances=512

查看谁在消耗

for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | sort -nr

https://unix.stackexchange.com/questions/15509/whos-consuming-my-inotify-resources