Ejabberd 性能调优(草稿)

文章目录
  1. 1. 第一阶段
  2. 2. 进程打开文件数限制
  3. 3. 启动参数
  4. 4. 内核参数调整
  5. 5. 排查
  6. 6. Mnesia表过载
  7. 7. Erlang 虚拟机参数
  8. 8. 内核参数
    1. 8.1. TCP
  9. 9. 参考资料

第一阶段

目标10W并发连接

进程打开文件数限制

  1. 编辑 /etc/pam.d/login,取消pam_limits.so的注释
1
# Sets up user limits according to /etc/security/limits.conf
# (Replaces the use of /etc/limits in old login)
session    required   pam_limits.so
  1. /etc/security/limits.conf
1
root    soft    nofile  320000
root    hard    nofile  320000

我们在root账户下测试, 如何验证该设置是否生效?

exit 退出shell, 重新登录执行ulimit -a查看open files是否为320000

1
root@ci:~# 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) 127413
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 320000
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) 127413
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

启动参数

Ejabberd的启动参数可以在配置文件/etc/ejabberd/ejabberdctl.cfg中进行调整

  • ERL_MAX_PORTS

    每个到客户端(s2c)和服务器(s2s)的连接消耗一个port, ERL_MAX_PORTS定义了Ejabberd可以支持的并发连接数. 其可以在配置文件/etc/ejabberd/ejabberdctl.cfg中指定, 默认值为32000.

    大并发连接场景下的应用需要增大该参数的值.

  • ERL_PROCESSES

    Erlang消耗很多轻量级进程, 如果Ejabberd比较繁忙, 可能会达到进程数上限, 这种情况会导致高延迟. 当消息延迟过高时, 需要判断是否是由于该参数引起的.

内核参数调整

1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.ip_local_port_range = 1025 65000
fs.file-max = 65535000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_mtu_probing = 1

内核网络参数net.ipv4.ip_local_port_range指定了本地程序可以打开的端口范围. 61000-32768=28232可以最多建立28232个到Ejabberd服务器的连接, 一般比这个数字小, 系统还有其他程序需要占用端口.

  • net.ipv4.tcp_tw_recycle = 1 快速回收TCP连接, 避免TIME_WAIT时间过长

修改端口范围

1
echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range

排查

  • 查看ulimit
1
ulimit -a
  • 验证进程的最大可打开文件 Max open files
1
root@scm:~# cat /proc/18938/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             127460               127460               processes
Max open files            60000                60000                files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       127460               127460               signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

Mnesia表过载

编辑配置文件/etc/ejabberd/ejabberdctl.cfg, 设置选项:

1
ERL_OPTIONS="-mnesia dump_log_write_threshold 50000 -mnesia dc_dump_limit 40"

当前版本的SHELL脚本有BUG, 需要使用\转义, 如下:

1
ERL_OPTIONS="-mnesia\ dump_log_write_threshold\ 50000\ -mnesia\ dc_dump_limit\ 40"

Erlang 虚拟机参数

http://www.cnblogs.com/lulu/p/4132278.html

  • beam 启动参数
    • +sbt db
      绑定调度器与CPU的亲缘性
    • +P 2000000
      进程数限制(合适即可)
    • +K true
      启用epoll
    • +sbwt none
      关闭beam 调度器 spinlock,降低CPU
    • +swt low
      提高调度器唤醒灵敏度,避免长时间运行睡死问题

内核参数

TCP

http://blog.csdn.net/russell_tao/article/details/18711023

参考资料

  1. 文件描述符
    http://erlangcentral.org/wiki/index.php?title=Introduction_to_Load_Testing_with_Tsung#Max_open_file_descriptor_limit

  2. 虚拟接口
    http://erlangcentral.org/wiki/index.php?title=Introduction_to_Load_Testing_with_Tsung#Virtual_interfaces

  3. Mnesia过载问题
    http://blog.include.io/archives/106
    http://www.tuicool.com/articles/rIBbqa

  4. Tsung 常见问题
    http://tsung.erlang-projects.org/user_manual/faq.html

  5. http://blog.fnil.net/index.php/archives/276/

  6. 幻灯片-安装过程(需要梯子)
    http://www.slideshare.net/ngocdaothanh/tsung-13985127?related=1

  7. Performance Tuning
    https://www.ejabberd.im/tuning