首页 > 运维 > 线上服务器oom killer实战详解
2021
11-21

线上服务器oom killer实战详解

背景


最近一台线上定时任务服务器,经常不定期报错,经常是请求外部接口的时候报错了。


分析


单独测试没有问题,外部接口也正常响应,百思不得其解,于是根据发生报错的时间点查看系统日志,发现触发了系统的oom-killer

vi /var/log/messages

Jun 28 17:30:06 super-task-cron kernel: php invoked oom-killer: gfp_mask=0x201da
, order=0, oom_score_adj=0
Jun 28 17:30:06 super-task-cron kernel: php cpuset=/ mems_allowed=0
Jun 28 17:30:08 super-task-cron kernel: CPU: 3 PID: 2344 Comm: php Kdump: loaded
 Not tainted 3.10.0-1062.9.1.el7.x86_64 #1
Jun 28 17:30:08 super-task-cron kernel: Hardware name: Tencent Cloud CVM, BIOS s
eabios-1.9.1-qemu-project.org 04/01/2014
Jun 28 17:30:08 super-task-cron kernel: Call Trace:
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa837ac23>] dump_stack+0x19/0x1
b
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa8375ce9>] dump_header+0x90/0x
229
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7d06142>] ? ktime_get_ts64+0x
52/0xf0
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dc1714>] oom_kill_process+0x
254/0x3e0
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7d32e71>] ? cpuset_mems_allow
ed_intersects+0x21/0x30
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dc11bd>] ? oom_unkillable_ta
sk+0xcd/0x120
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dc1266>] ? find_lock_task_mm
+0x56/0xc0
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dc1f66>] out_of_memory+0x4b6
/0x4f0
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dc8a6f>] __alloc_pages_nodem
ask+0xacf/0xbe0
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7e16b28>] alloc_pages_current
+0x98/0x110
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dbd617>] __page_cache_alloc+
0x97/0xb0
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dc01d8>] filemap_fault+0x298
/0x490
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffc02cd346>] ext4_filemap_fault+
0x36/0x50 [ext4]
Jun 28 17:30:08 super-task-cron kernel: [<ffffffffa7dec15a>] __do_fault.isra.61+
0x8a/0x100

...

Jun 28 17:30:08 super-task-cron kernel: [ pid ]   uid  tgid total_vm      rss nr
_ptes swapents oom_score_adj name
Jun 28 17:30:08 super-task-cron kernel: [  659]     0   659    31388       97
   64        0             0 systemd-journal
Jun 28 17:30:08 super-task-cron kernel: [  688]     0   688    29157       77
   27        0             0 lvmetad
Jun 28 17:30:08 super-task-cron kernel: [  696]     0   696    11223      239   
   23        0         -1000 systemd-udevd
Jun 28 17:30:08 super-task-cron kernel: [ 1173]     0  1173    13880      111
   26        0         -1000 auditd
Jun 28 17:30:08 super-task-cron kernel: [ 1203]   999  1203   153609     1437
   64        0             0 polkitd
Jun 28 17:30:08 super-task-cron kernel: [ 1204]   998  1204     2144       37
   10        0             0 lsmd
   
...


Jun 28 17:30:08 super-task-cron kernel: Out of memory: Kill process 18649 (YDSer
vice) score 5 or sacrifice child
Jun 28 17:30:08 super-task-cron kernel: Killed process 20537 (sh), UID 0, total-
vm:1100384kB, anon-rss:3956kB, file-rss:0kB, shmem-rss:0kB
Jun 28 17:30:08 super-task-cron kernel: php invoked oom-killer: gfp_mask=0x201da
, order=0, oom_score_adj=0
Jun 28 17:30:08 super-task-cron kernel: php cpuset=/ mems_allowed=0
Jun 28 17:30:08 super-task-cron kernel: CPU: 0 PID: 10912 Comm: php Kdump: loade
d Not tainted 3.10.0-1062.9.1.el7.x86_64 #1
Jun 28 17:30:08 super-task-cron kernel: Hardware name: Tencent Cloud CVM, BIOS s
eabios-1.9.1-qemu-project.org 04/01/2014


通过下面日志分析,是php脚本触发了oom-killer

Jun 28 17:30:06 super-task-cron kernel: php invoked oom-killer: gfp_mask=0x201da
, order=0, oom_score_adj=0

通过下面日志分析,php触发oom-killer  操作系统杀死了 20537这个进程,这个进程占用的物理内存是:anon-rss:39956kB ,换算单位: 39 Mb,看起来占用的也不多,但是为什么会触发oom呢

Jun 28 17:30:08 super-task-cron kernel: Killed process 20537 (sh), UID 0, total-
vm:1100384kB, anon-rss:39956kB, file-rss:0kB, shmem-rss:0kB

于是继续分析发现当时服务器同事跑了几百个php进程,是因为某个服务不正常,导致定时任务没有执行完毕,然后执行次数多了以后,大量的进程变成了僵尸进程,占用了过多的内存


解决方案


1. kill掉没有正常执行完毕的进程

2. 扩大内存

继续观察后发现正常了。


oom-killer详解参考:/?id=3291

本文》有 0 条评论

留下一个回复