# 內核參數調優
## ulimit設置
ulimit -n 要調整為100000甚至更大。 命令行下執行 ulimit -n 100000即可修改。如果不能修改,需要設置 /etc/security/limits.conf,加入
~~~
* soft nofile 262140
* hard nofile 262140
root soft nofile 262140
root hard nofile 262140
* soft core unlimited
* hard core unlimited
root soft core unlimited
root hard core unlimited
~~~
注意,修改limits.conf文件后,需要重啟系統生效
## 內核設置
Linux操作系統修改內核參數有3種方式:
* 修改/etc/sysctl.conf文件,加入配置選項,格式為key = value,修改保存后調用sysctl -p加載新配置
* 使用sysctl命令臨時修改,如:sysctl -w net.ipv4.tcp_mem="379008 505344 758016"
* 直接修改/proc/sys/目錄中的文件,如:echo "379008 505344 758016" > /proc/sys/net/ipv4/tcp_mem
>第一種方式在操作系統重啟后會自動生效,第二和第三種方法重啟后失效
**net.unix.max_dgram_qlen = 100**
swoole使用unix socket dgram來做進程間通信,如果請求量很大,需要調整此參數。系統默認為10,可以設置為100或者更大。
或者增加worker進程的數量,減少單個worker進程分配的請求量。
**net.core.wmem_max**
修改此參數增加socket緩存區的內存大小
~~~
net.ipv4.tcp_mem = 379008 505344 758016
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_rmem = 4096 87380 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
~~~
**net.ipv4.tcp_tw_reuse**
是否socket reuse,此函數的作用是Server重啟時可以快速重新使用監聽的端口。如果沒有設置此參數,會導致server重啟時發生端口未及時釋放而啟動失敗
**net.ipv4.tcp_tw_recycle**
使用socket快速回收,短連接Server需要開啟此參數。此參數表示開啟TCP連接中TIME-WAIT sockets的快速回收,Linux系統中默認為0,表示關閉。打開此參數可能會造成NAT用戶連接不穩定,請謹慎測試后再開啟。
**消息隊列設置**
當使用消息隊列作為進程間通信方式時,需要調整此內核參數
* kernel.msgmnb = 4203520,消息隊列的最大字節數
* kernel.msgmni = 64,最多允許創建多少個消息隊列
* kernel.msgmax = 8192,消息隊列單條數據最大的長度
**FreeBSD/MacOS**
* sysctl -w net.local.dgram.maxdgram=8192
* sysctl -w net.local.dgram.recvspace=200000 修改Unix Socket的buffer區尺寸
**開啟CoreDump**
設置內核參數
~~~
kernel.core_pattern = /data/core_files/core-%e-%p-%t
~~~
通過ulimit -c命令查看當前coredump文件的限制
~~~
ulimit -c
~~~
如果為0,需要修改/etc/security/limits.conf,進行limit設置。
>開啟core-dump后,一旦程序發生異常,會將進程導出到文件。對于調查程序問題有很大的幫助
**其他重要配置**
~~~
net.ipv4.tcp_syncookies=1
#對于還未獲得對方確認的連接請求,可保存在隊列中的最大數目
net.ipv4.tcp_max_syn_backlog=81920
net.ipv4.tcp_synack_retries=3
net.ipv4.tcp_syn_retries=3
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 20000 65000
net.ipv4.tcp_max_tw_buckets = 200000
net.ipv4.route.max_size = 5242880
#系統所有進程一共可以打開的文件數量
fs.file-max = 6815744
#防火墻跟蹤表的大小。注意:如果防火墻沒開則會提示error: "net.netfilter.nf_conntrack_max" is an unknown key,忽略即可
net.netfilter.nf_conntrack_max = 2621440
#在每個網絡接口接收數據包的速率比內核處理這些包的速率快時,允許送到隊列的數據包的最大數目
net.core.netdev_max_backlog = 30000
#定義了系統中每一個端口最大的監聽隊列的長度,這是個全局的參數
net.core.somaxconn = 65535
~~~
**查看配置是否生效**
如:修改net.unix.max_dgram_qlen = 100后,通過
~~~
cat /proc/sys/net/unix/max_dgram_qlen
~~~
如果修改成功,這里是新設置的值。
- swoole簡介
- swoole功能概述
- 序章
- 開發必讀
- 1 環境搭建
- 1.1 環境搭建
- 1.2 搭建Echo服務器
- 2 初識Swoole
- 2.1 Worker進程
- 2.2 TaskWorker進程
- 2.3 Timer定時器
- 2.4 Process進程
- 2.5 Table內存表
- 2.6 多端口監聽
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 熱重啟
- 2.10 http_server
- 附錄*server配置
- 附錄*server函數
- 附錄*server屬性
- 附錄*server回調函數
- 附錄*server高級特性
- 心跳檢測
- 3 Swoole協議
- 3.1 EOF協議
- 3.2 固定包頭協議
- 3.3 Http協議
- 3.4 WebSocket協議
- 3.5 MTQQ協議
- 內置http_server
- 內置websocket_server
- Swoole\Redis\Server
- 4 Swoole異步IO
- 4.1 AsyncIO
- 異步文件系統IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole異步客戶端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole協程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定時器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常見問題
- 客戶端鏈接失敗原因
- 如何設置進程數
- 如何實現異步任務
- 如何選擇swoole三種模式
- php中哪些函數是阻塞的
- 是否可以共用1個redis或mysql連接
- 如何在回調函數中訪問外部的變量
- 為什么不要send完后立即close
- 不同的Server程序實例間如何通信
- MySQL的連接池、異步、斷線重連
- 在php-fpm或apache中使用swoole
- 學習Swoole需要掌握哪些基礎知識
- 在phpinfo中有在php-m中沒有
- 同步阻塞與異步非阻塞選擇
- CURL發送POST請求服務器端超時
- 附錄
- 預定義常量
- 內核參數調優
- php四種回調寫法
- 守護進程程序常用數據結構
- swoole生命周期
- swoole_server中內存管理機制
- 使用jemalloc優化swoole內存分配性能
- Reactor、Worker、Task的關系
- Manager進程
- Swoole的實現
- Reactor線程
- 安裝擴展
- swoole-worker手冊
- swoole相關開源項目
- 寫在后面的話
- 版本更新記錄
- 4.0