1. 程式人生 > 其它 >systemd service 如何開啟 core dump

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 [email protected]
$ 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-weightdaemon-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