systemd service 如何開啟 core dump


如何查看 core dump 是否處於開啟狀態

Core dump 中文翻譯為“核心轉儲”,它是進程運行時突然崩潰的那一刻的內存快照。操作系統在程序發生異常而異常在進程內部又沒有被捕獲的情況下,會把進程此刻內存、寄存器狀態、運行堆棧等信息轉儲保存在一個文件里,可使用 gdb 工具來分析。core dump 生產環境一般處於禁用狀態,對於內存消耗性的進程,core dump 時會占用很多系統資源,磁盤空間也可能被寫滿。

 

使用普通用戶登錄 CentOS 7 系統后,執行以下命令,你會發現 core file size soft limit 默認是 0,即 core dump 處於禁用狀態,hard limit 是 unlimited, 也可以直接通過 ulimit -c 命令查看。

$ ulimit -a core file size (blocks, -c) 0 $ ulimit -a -H core file size (blocks, -c) unlimited

soft 及 hard limit 區別可以看 ulimit 的 -S 和 -H 參數解釋。

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.

以 tikv 實例為例,啟動腳本直接調用 binary 啟動進程。

bin/tikv-server \  --addr "0.0.0.0:20160" \  --advertise-addr "172.16.10.72:20160" \  --pd "172.16.10.72:2379,172.16.10.73:2379,172.16.10.74:2379" \  --data-dir "/data1/louis/deploy/data" \  --config conf/tikv.toml \  --log-file "/data1/louis/deploy/log/tikv.log" 2>> "/data1/louis/deploy/log/tikv_stderr.log"

可通過 PID 查看該進程的 resource limit, 包括 max core file size。

$ pgrep -f tikv-server
11352 $ cat /proc/11352/limits Limit Soft Limit Hard Limit Units Max core file size 0 unlimited bytes

如何開啟 core dump

編輯 /etc/security/limits.conf 文件,設置 tidb 用戶 core file size soft 和 hard limit 為 unlimited。

 $ sudo vi /etc/security/limits.conf
tidb        soft        core          unlimited
tidb        hard        core          unlimited

如果希望對所有用戶生效,tidb 替換為 * 即可。

 $ sudo vi /etc/security/limits.conf
*        soft        core          unlimited
*        hard        core          unlimited

退出當前用戶登錄,再重新登錄,你會發現,core dump 已經開啟。

$ exit ~ ssh tidb@172.16.10.72 $ ulimit -c unlimited $ cat /proc/11352/limits Limit Soft Limit Hard Limit Units Max core file size unlimited unlimited bytes

如果使用 systemd 管理 tikv 服務,啟動后發現該進程 max core file size 仍為 0。這是由於 limits.conf 中的配置對 systemd service 的資源限制並不生效,需要在 service 文件中添加 LimitCORE=infinity, 按以下命令操作后,你會發現 core dump file size 變為 unlimited。

$ sudo vi /etc/systemd/system/tikv-20160.service
[Unit] Description=tikv-20160 service After=syslog.target network.target remote-fs.target nss-lookup.target [Service] LimitNOFILE=1000000 LimitCORE=infinity User=tidb ExecStart=/data1/louis/deploy/scripts/run_tikv.sh Restart=always RestartSec=15s [Install] WantedBy=multi-user.target $ sudo systemctl daemon-reload $ sudo systemctl restart tikv-20160.service

 

如果希望對系統 systemd service 全局設置,可修改 /etc/systemd/system.conf 文件。

$ sudo vi /etc/systemd/system.conf
DefaultLimitCORE=infinity

不過該操作生效重啟系統。測試中發現執行以下命令也可以讓 systemd 加載最新配置,不過官方文檔說明該命令很少用,主要在 debugging 和 package upgrades 時使用。

$ sudo systemctl daemon-reexec

daemon-reexec

Reexecute the systemd manager. This will serialize the manager state, reexecute the process and deserialize the state again. This command is of little use except for debugging and package upgrades. Sometimes, it might be helpful as a heavy-weight daemon-reload. While the daemon is being reexecuted, all sockets systemd listening on behalf of user configuration will stay accessible.

 

除了 LimitCORE,還可以在 service 文件中配置 LimitNOFILE、LimitSTACK、LimitNPROC 等資源限制,配置時請注意單位,詳見:

https://www.freedesktop.org/software/systemd/man/systemd.exec.htmlsystemd.execsystemd.exec

 

生成 core dump 文件測試

通過給 tikv 進程發 11 信號(SIGSEGV: Invalid memory reference),默認會在進程工作目錄下生成 core dump 文件, core dump 文件位置與 /proc/sys/kernel/core_pattern 有關。該步驟僅用於測試,不要在線上服務測試。

$ pgrep -f tikv-server
12162 $ pwdx 12162 12162: /data1/louis/deploy $ kill -11 12162 $ cd /data1/louis/deploy $ ls core.12162


總結:
systemd管理的服務進程設置Max core file size的Soft Limit為unlimited
需要在配置文件(/usr/lib/systemd/system/xxx.service )的[Service]配置段下加上
LimitCORE=infinity
然后執行
systemctl daemon-reload
再重啟systemd管理的服務
systemctl restart supervisord

轉自:https://zhuanlan.zhihu.com/p/41153588


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM