您尚未登录。

楼主 # 2024-04-19 06:49:16

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,348
积分: 9202

OOM炸了,DRAM不够,SWAP来凑

刚刚开机,没有swap:

|_   _||_| ___  _ _   |  |   |_| ___  _ _  _ _
  | |   _ |   ||   |  |  |__ | ||   || | ||_'_|
  | |  | || | || _ |  |_____||_||_|_||___||_,_|
  |_|  |_||_|_||_|_|  Tina is Based on OpenWrt!
 ----------------------------------------------
 Tina Linux (5.0, r0-6ada511)
 ----------------------------------------------
nodev   debugfs
root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/# free
              total        used        free      shared  buff/cache   available
Mem:         233780       15596      210488           0        7696      211804
Swap:             0           0           0
root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/#

编译一个没有护栏的测试程序,每次动态申请1M内存,第二个参数是申请次数:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

void main(int argc, char* argv[])
{
        static int count = 0;
        int times = atoi(argv[1]);

        for(int i =0; i<times; i++)
        {
                void* ptr = malloc(1*1024*1024);

                if(ptr == NULL)
                {
                        printf("malloc error");
                }
                else
                {
                        memset(ptr, 0, 1*1024*1024);
                        printf("malloc ok, count=%3d\n", count++);
                }
        }

        while (1)
        {
                printf("sleep\n");
                usleep(1000*1000);
        }
}



申请大约200M内存的时候挂了:

root@TinaLinux:/#
root@TinaLinux:/# free
              total        used        free      shared  buff/cache   available
Mem:         233780       15788      204344        5948       13648      205664
Swap:             0           0           0
root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/# chmod +x /tmp/test && /tmp/test 206
malloc ok, count=0
malloc ok, count=1
... ...
malloc ok, count=197
malloc ok, count=198
malloc ok, count=199
malloc ok, count=200
[  167.228684] test invoked oom-killer: gfp_mask=0x1100cca(GFP_HIGHUSER_MOVABLE|0x1000000), order=0, oom_score_adj=0
[  167.240249] CPU: 1 PID: 1296 Comm: test Not tainted 5.4.61 #1
[  167.246694] Hardware name: Generic DT based system
[  167.252092] [<c010e318>] (unwind_backtrace) from [<c010a8dc>] (show_stack+0x10/0x14)
[  167.260798] [<c010a8dc>] (show_stack) from [<c0785f90>] (dump_stack+0x7c/0x98)
[  167.260815] [<c0785f90>] (dump_stack) from [<c0195914>] (dump_header+0x54/0x270)
[  167.277204] [<c0195914>] (dump_header) from [<c0195d88>] (oom_kill_process+0x78/0x2bc)
[  167.286088] [<c0195d88>] (oom_kill_process) from [<c019676c>] (out_of_memory+0x318/0x384)
[  167.295270] [<c019676c>] (out_of_memory) from [<c01c82e4>] (__alloc_pages_nodemask+0x758/0x9b0)
[  167.305037] [<c01c82e4>] (__alloc_pages_nodemask) from [<c01b37ac>] (alloc_zeroed_user_highpage_movable+0x14/0x3c)
[  167.316657] [<c01b37ac>] (alloc_zeroed_user_highpage_movable) from [<c01b6264>] (handle_mm_fault+0x214/0x7c0)
[  167.327787] [<c01b6264>] (handle_mm_fault) from [<c010f1ac>] (do_page_fault+0x13c/0x29c)
[  167.336872] [<c010f1ac>] (do_page_fault) from [<c010f468>] (do_DataAbort+0x34/0xb4)
[  167.345468] [<c010f468>] (do_DataAbort) from [<c010255c>] (__dabt_usr+0x3c/0x40)
[  167.353768] Exception stack(0xce1fffb0 to 0xce1ffff8)
[  167.359436] ffa0:                                     aa452008 00000000 00006000 aa54c000
[  167.368612] ffc0: 00010bd4 00010c68 00000000 00010134 00000000 00010134 00079470 bea9cb84
[  167.377793] ffe0: 00000000 bea9cb68 000104fc 00027908 20070010 ffffffff
[  167.385280] Mem-Info:
[  167.387840] active_anon:52056 inactive_anon:1486 isolated_anon:0
[  167.387840]  active_file:9 inactive_file:9 isolated_file:0
[  167.387840]  unevictable:0 dirty:0 writeback:0 unstable:0
[  167.387840]  slab_reclaimable:562 slab_unreclaimable:2552
[  167.387840]  mapped:88 shmem:1487 pagetables:139 bounce:0
[  167.387840]  free:478 free_pcp:103 free_cma:0
[  167.423747] Node 0 active_anon:208224kB inactive_anon:5944kB active_file:36kB inactive_file:36kB unevictable:0kB isolated(anon):0kB isolated(file):0kB mapped:352kB dirty:0kB writeback:0kB shmem:5948kB writeback_tmp:0kB unstable:0kB all_unreclaimable? yes
[  167.423767] Normal free:1912kB min:1916kB low:2392kB high:2868kB active_anon:208224kB inactive_anon:5944kB active_file:36kB inactive_file:36kB unevictable:0kB writepending:0kB present:254400kB managed:233780kB mlocked:0kB kernel_stack:560kB pagetables:556kB bounce:0kB free_pcp:412kB local_pcp:112kB free_cma:0kB
[  167.479940] lowmem_reserve[]: 0 0
[  167.479952] Normal: 14*4kB (UM) 12*8kB (UM) 2*16kB (UE) 0*32kB 1*64kB (M) 1*128kB (M) 2*256kB (ME) 0*512kB 1*1024kB (M) 0*2048kB 0*4096kB = 1912kB
[  167.498408] Free pages count per migrate typeat order:     0      1      2      3      4      5      6      7      8      9     10
[  167.511744] zone   Normal, type    Unmovable     13     11      1      0      0      0      0      0      0      0      0
[  167.524168] zone   Normal, type      Movable      1      1      0      0      1      1      1      0      1      0      0
[  167.536584] zone   Normal, type  Reclaimable      0      0      1      0      0      0      1      0      0      0      0
[  167.549007] zone   Normal, type          CMA      0      0      0      0      0      0      0      0      0      0      0
[  167.561421] zone   Normal, type   HighAtomic      0      0      0      0      0      0      0      0      0      0      0
[  167.573839] zone   Normal, type      Isolate      0      0      0      0      0      0      0      0      0      0      0
[  167.586255] 1501 total pagecache pages
[  167.590458] 0 pages in swap cache
[  167.594173] Swap cache stats: add 0, delete 0, find 0/0
[  167.600066] Free swap  = 0kB
[  167.603293] Total swap = 0kB
[  167.606533] 63600 pages RAM
[  167.609661] 0 pages HighMem/MovableOnly
[  167.613957] 5155 pages reserved
[  167.617487] 2048 pages cma reserved
[  167.621396] Tasks state (memory values in pages):
[  167.626678] [  pid  ]   uid  tgid total_vm      rss pgtables_bytes swapents oom_score_adj name
[  167.636364] [   1211]     0  1211     9461       49    18432        0             0 adbd
[  167.645462] [   1254]     0  1254      782       31    10240        0             0 sh
[  167.645472] [   1255]     0  1255      751       27    10240        0             0 init
[  167.663433] [   1256]     0  1256      751       27    10240        0             0 init
[  167.672521] [   1257]     0  1257      751       27    10240        0             0 init
[  167.681611] [   1296]     0  1296    52073    51958   215040        0             0 test
[  167.690698] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),task=test,pid=1296,uid=0
[  167.699814] Out of memory: Killed process 1296 (test) total-vm:208292kB, anon-rss:207480kB, file-rss:0kB, shmem-rss:352kB, UID:0 pgtables:210kB oom_score_adj:0
[  167.766642] oom_reaper: reaped process 1296 (test), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB
Killed
root@TinaLinux:/#

下面开启交换分区:

mkswap /dev/by-name/swapper && swapon /dev/by-name/swapper 

开启完成:

root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/# mkswap /dev/by-name/swapper && swapon /dev/by-name/swapper
Setting up swapspace version 1, size = 536866816 bytes
[  317.944647] Adding 524284k swap on /dev/by-name/swapper.  Priority:-2 extents:1 across:524284k SS
root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/#

测试申请700M,系统正常:

malloc ok, count=0
malloc ok, count=1
... ...
malloc ok, count=691
malloc ok, count=692
malloc ok, count=693
malloc ok, count=694
malloc ok, count=695
malloc ok, count=696
malloc ok, count=697
malloc ok, count=698
malloc ok, count=699
sleep
sleep
sleep

free命令看下内存占用:

root@TinaLinux:/# free
              total        used        free      shared  buff/cache   available
Mem:         233780      221724        6532          92        5524        6400
Swap:        524284      524284           0

申请703M的时候,OOM炸了:

root@TinaLinux:/#
root@TinaLinux:/# chmod +x /tmp/test && /tmp/test 720
malloc ok, count=0
malloc ok, count=1
... ...
malloc ok, count=701
malloc ok, count=702
[24017.015074] test invoked oom-killer: gfp_mask=0x1100cca(GFP_HIGHUSER_MOVABLE|0x1000000), order=0, oom_score_adj=0
[24017.026721] CPU: 1 PID: 1412 Comm: test Not tainted 5.4.61 #1
[24017.033176] Hardware name: Generic DT based system
[24017.033206] [<c010e318>] (unwind_backtrace) from [<c010a8dc>] (show_stack+0x10/0x14)
[24017.033221] [<c010a8dc>] (show_stack) from [<c0785f90>] (dump_stack+0x7c/0x98)
[24017.033237] [<c0785f90>] (dump_stack) from [<c0195914>] (dump_header+0x54/0x270)
[24017.033250] [<c0195914>] (dump_header) from [<c0195d88>] (oom_kill_process+0x78/0x2bc)
[24017.033262] [<c0195d88>] (oom_kill_process) from [<c019676c>] (out_of_memory+0x318/0x384)
[24017.033277] [<c019676c>] (out_of_memory) from [<c01c82e4>] (__alloc_pages_nodemask+0x758/0x9b0)
[24017.033292] [<c01c82e4>] (__alloc_pages_nodemask) from [<c01b37ac>] (alloc_zeroed_user_highpage_movable+0x14/0x3c)
[24017.033305] [<c01b37ac>] (alloc_zeroed_user_highpage_movable) from [<c01b6264>] (handle_mm_fault+0x214/0x7c0)
[24017.033318] [<c01b6264>] (handle_mm_fault) from [<c010f1ac>] (do_page_fault+0x13c/0x29c)
[24017.033332] [<c010f1ac>] (do_page_fault) from [<c010f468>] (do_DataAbort+0x34/0xb4)
[24017.033344] [<c010f468>] (do_DataAbort) from [<c010255c>] (__dabt_usr+0x3c/0x40)
[24017.033349] Exception stack(0xce22ffb0 to 0xce22fff8)
[24017.033358] ffa0:                                     8ac74008 00000000 00007000 8ad6d000
[24017.033368] ffc0: 00010bd4 00010c68 00000000 00010134 00000000 00010134 00079470 beef1b84
[24017.033377] ffe0: 00000000 beef1b68 000104fc 00027908 20070010 ffffffff
[24017.033381] Mem-Info:
[24017.033398] active_anon:25811 inactive_anon:25853 isolated_anon:0
[24017.033398]  active_file:6 inactive_file:4 isolated_file:0
[24017.033398]  unevictable:0 dirty:0 writeback:127 unstable:0
[24017.033398]  slab_reclaimable:628 slab_unreclaimable:3241
[24017.033398]  mapped:19 shmem:19 pagetables:399 bounce:0
[24017.033398]  free:1366 free_pcp:0 free_cma:0
[24017.033412] Node 0 active_anon:103244kB inactive_anon:103412kB active_file:24kB inactive_file:16kB unevictable:0kB isolated(anon):0kB isolated(file):0kB map                                                                    ped:76kB dirty:0kB writeback:508kB shmem:76kB writeback_tmp:0kB unstable:0kB all_unreclaimable? no
[24017.033428] Normal free:5464kB min:1916kB low:2392kB high:2868kB active_anon:103300kB inactive_anon:103328kB active_file:24kB inactive_file:16kB unevictable                                                                    :0kB writepending:488kB present:254400kB managed:233780kB mlocked:0kB kernel_stack:536kB pagetables:1596kB bounce:0kB free_pcp:0kB local_pcp:0kB free_cma:0kB
[24017.033431] lowmem_reserve[]: 0 0
[24017.033438] Normal: 199*4kB (UME) 141*8kB (UME) 85*16kB (UME) 37*32kB (ME) 16*64kB (ME) 1*128kB (M) 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 5620kB
[24017.033474] Free pages count per migrate typeat order:     0      1      2      3      4      5      6      7      8      9     10
[24017.033496] zone   Normal, type    Unmovable     61     34      8      0      0      0      0      0      0      0      0
[24017.033531] zone   Normal, type      Movable     98     82     60     25     13      1      0      0      0      0      0
[24017.033580] zone   Normal, type  Reclaimable     40     25     17     12      3      0      0      0      0      0      0
[24017.033612] zone   Normal, type          CMA      0      0      0      0      0      0      0      0      0      0      0
[24017.033632] zone   Normal, type   HighAtomic      0      0      0      0      0      0      0      0      0      0      0
[24017.033652] zone   Normal, type      Isolate      0      0      0      0      0      0      0      0      0      0      0
[24017.033671] 178 total pagecache pages
[24017.033678] 146 pages in swap cache
[24017.033683] Swap cache stats: add 424365, delete 424256, find 343/587
[24017.033686] Free swap  = 0kB
[24017.033689] Total swap = 524284kB
[24017.033693] 63600 pages RAM
[24017.033696] 0 pages HighMem/MovableOnly
[24017.033699] 5155 pages reserved
[24017.033702] 2048 pages cma reserved
[24017.033705] Tasks state (memory values in pages):
[24017.033708] [  pid  ]   uid  tgid total_vm      rss pgtables_bytes swapents oom_score_adj name
[24017.033730] [   1211]     0  1211    11510        0    24576       77             0 adbd
[24017.033738] [   1254]     0  1254      782        0    10240       36             0 sh
[24017.033747] [   1255]     0  1255      751        0    10240       27             0 init
[24017.033755] [   1256]     0  1256      751        0    10240       27             0 init
[24017.033764] [   1257]     0  1257      751        0    10240       27             0 init
[24017.033772] [   1277]     0  1277      782        0    10240       40             0 sh
[24017.033781] [   1412]     0  1412   181087    51492   731136   129420             0 test
[24017.033787] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),task=test,pid=1412,uid=0
[24017.033813] Out of memory: Killed process 1412 (test) total-vm:724348kB, anon-rss:205892kB, file-rss:0kB, shmem-rss:76kB, UID:0 pgtables:714kB oom_score_adj                                                                    :0
[24017.260905] oom_reaper: reaped process 1412 (test), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB
Killed
root@TinaLinux:/#
root@TinaLinux:/#
root@TinaLinux:/#




离线

#1 2024-04-19 11:09:09

armstrong
会员
注册时间: 2019-04-10
已发帖子: 287
积分: 196.5

Re: OOM炸了,DRAM不够,SWAP来凑

请教晕哥,你演示的无护栏测试代码中,已经判断了指针是否为NULL,这个做法是应用程序普遍的处理方式;即便这样应用还是会被杀。
那如何编写有护栏的应用呢?要求即便内存oom发生,应用程序也不会被杀。是通过signal()函数实现吗?请教晕哥。

离线

楼主 #2 2024-04-19 11:19:04

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,348
积分: 9202

Re: OOM炸了,DRAM不够,SWAP来凑

已经判断了指针是否为NULL,这个做法是应用程序普遍的处理方式

内存分配器返回非空才是成功,否则就是操作野指针了

即便这样应用还是会被杀。

系统物理内存不够了,或交换内存也不够了。

那如何编写有护栏的应用呢?

护栏是指没有判断参数个数,随便写段测试代码。


要求即便内存oom发生,应用程序也不会被杀。

恐怕不行吧,这不成了无源之水了?

请教晕哥。

不敢当,边学边卖弄。





离线

楼主 #3 2024-04-19 17:43:18

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,348
积分: 9202

Re: OOM炸了,DRAM不够,SWAP来凑

5O@X4S@HO$9~ETD[77K`I)O.png

malloc ok, count=146
malloc ok, count=147
malloc ok, count=148
malloc ok, count=149
malloc ok, count=150
malloc ok, count=151
malloc ok, count=152
malloc ok, count=153
[  216.137338] mem_test invoked oom-killer: gfp_mask=0x24200ca(GFP_HIGHUSER_MOVABLE), nodemask=0, order=0, oom_score_adj=0
[  216.150768] CPU: 0 PID: 1703 Comm: mem_test Not tainted 4.9.118 #183
[  216.164815] Hardware name: sun8iw8
[  216.168650] [<c010c7cc>] (unwind_backtrace) from [<c010a6f4>] (show_stack+0x10/0x14)
[  216.179140] [<c010a6f4>] (show_stack) from [<c01da458>] (dump_header.constprop.3+0x64/0x1ac)
[  216.190381] [<c01da458>] (dump_header.constprop.3) from [<c01975a4>] (oom_kill_process+0xec/0x4a8)
[  216.208704] [<c01975a4>] (oom_kill_process) from [<c0197e28>] (out_of_memory+0x348/0x3f0)
[  216.217901] [<c0197e28>] (out_of_memory) from [<c019c0ec>] (__alloc_pages_nodemask+0x950/0xa28)
[  216.234830] [<c019c0ec>] (__alloc_pages_nodemask) from [<c01ba94c>] (handle_mm_fault+0x140/0xb70)
[  216.245960] [<c01ba94c>] (handle_mm_fault) from [<c010ec34>] (do_page_fault+0x210/0x2a8)
[  216.256745] [<c010ec34>] (do_page_fault) from [<c01012d4>] (do_DataAbort+0x38/0xb8)
[  216.271863] [<c01012d4>] (do_DataAbort) from [<c010b39c>] (__dabt_usr+0x3c/0x40)
[  216.281845] Exception stack(0xc2393fb0 to 0xc2393ff8)
[  216.294763] 3fa0:                                     ad371010 00006b7f ad448028 00007fff
[  216.304956] 3fc0: 00000000 00000000 00000000 00000000 00000001 00000000 00000020 beda4e34
[  216.316019] 3fe0: ad37101c beda4df0 00000000 b6f72774 20000010 ffffffff
[  216.330038] [ pid ]   uid  tgid total_vm      rss nr_ptes nr_pmds swapents oom_score_adj name
[  216.341363] [  720]     0   720      330        1       4       0        6             0 syslogd
[  216.359483] [  724]     0   724      329        1       4       0        6             0 klogd
[  216.369134] [  736]     0   736      333        1       5       0       13             0 mdev
[  216.383525] [  780]     0   780     1657       11       7       0      754             0 haveged
[  216.395798] [  786]  1001   786      331        0       4       0       18             0 dbus-daemon
[  216.407732] [  792]     0   792     1110       27       6       0       81             0 ntpd
[  216.424769] [  805]     0   805      223        1       4       0        9             0 dropbear
[  216.435459] [  808]     0   808   168315       19       8       0      211             0 allxbee
[  216.446949] [  812]     0   812   167906       32       7       0      313             0 iotWatchdog
[  216.464755] [  952]     0   952      332        1       4       0       10             0 sh
[  216.474740] [ 1616]     0  1616      329        1       3       0        6             0 udhcpc
[  216.486136] [ 1703]     0  1703    40009     8503      82       0    31313             0 mem_test
[  216.502559] Out of memory: Kill process 1703 (mem_test) score 836 or sacrifice child
[  216.512924] Killed process 1703 (mem_test) total-vm:160036kB, anon-rss:33956kB, file-rss:56kB, shmem-rss:0kB
[  216.602368] oom_reaper: reaped process 1703 (mem_test), now anon-rss:4kB, file-rss:0kB, shmem-rss:0kB
Killed
# bg
-sh: bg: No current job
#
#
# free
              total        used        free      shared  buff/cache   available
Mem:          53960       12648       21936           8       19376       37272
Swap:        131068        5320      125748
#

V3s可以申请到153M 内存





离线

#4 2024-04-20 11:33:28

armstrong
会员
注册时间: 2019-04-10
已发帖子: 287
积分: 196.5

Re: OOM炸了,DRAM不够,SWAP来凑

@晕哥
原来如此,那我理解错了。
我以为“护栏”指的是:某种防范措施,能实现发生OOM时应用不被杀,而仅仅是malloc返回NULL。

离线

#5 2024-04-24 09:39:49

dgtg
会员
注册时间: 2017-11-08
已发帖子: 257
积分: 216.5

Re: OOM炸了,DRAM不够,SWAP来凑

请教晕哥,在C100外挂8MB nor flash的平台上也可以开启SWAP吗?

离线

楼主 #6 2024-04-24 09:52:49

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,348
积分: 9202

Re: OOM炸了,DRAM不够,SWAP来凑

dgtg 说:

请教晕哥,在C100外挂8MB nor flash的平台上也可以开启SWAP吗?

应该不行,除了速度特慢外,swap应该不支持这种分区类型。





离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn