kernel开启启动log_systemd service 如何开启 core dump

5a3005154bfc67e07e4d09b23fe5d753.png

如何查看 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          

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 启动进程。

可通过 PID 查看该进程的 resource limit, 包括 max core file size。

11352
$ cat /proc/11352/limits
Limit                     Soft Limit           Hard Limit           Units
Max core file size        

如何开启 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
$ 

如果使用 systemd 管理 tikv 服务,启动后发现该进程 max core file size 仍为 0。这是由于 limits.conf 中的配置对 systemd service 的资源限制并不生效,需要在 service 文件中添加 LimitCORE=infinity, 按以下命令操作后,你会发现 core dump file size 变为 unlimited。

[Unit

如果希望对系统 systemd service 全局设置,可修改 /etc/systemd/system.conf 文件。

DefaultLimitCORE

不过该操作生效重启系统。测试中发现执行以下命令也可以让 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 有关。该步骤仅用于测试,不要在线上服务测试。

12162
$ pwdx 

参考文章

Linux man: core

Linux man: signal

How to enable coredumps for daemon process (services) in RHEL

How to enable core file dumps when an application crashes or segmentation faults

相关文章

systemd.exec manual

ArchWiki Core Dump

有经验的同学欢迎在评论中讨论,如有错误,欢迎指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值