• 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
  • 相关阅读:
    『开源』仿SQLServer山寨一个 跨数据库客户端
    『开源』也顺手写一个 科学计算器:重磅开源
    『转载』还在找字体?收下这份最全可商用免费字体清单!
    『开源协议』Creative Commons Attribution 3.0 License . 协议的个人理解,并 转载分享 4000个 精美可商用小图标
    『卧槽』意外发现了 Hashtable 的 foreach 用法 BUG
    『性能』List 和 HashSet 查找性能比较 (任何数据量的检索 从此只用 HashSet )
    『备注』GDI+ 绘制文本有锯齿,透明背景文本绘制
    『动态』动态JSON万能转换函数 + .Net40 dynamic动态数据绑定
    『随笔』.Net 底层 数组[] 的 基本设计探秘 512 子数组
    『转载』中国芯片差在哪?这篇讲全了
  • 原文地址:https://www.cnblogs.com/xingxiz/p/15366719.html
Copyright © 2020-2023  润新知