Sigkill caused by insufficient memory: locating and solving the problem of increasing buffer (explaining the cause of sigkill)

Source: Internet
Author: User

New recording versionProgramIt is almost finished. After a new feature "alarm recording" is added, the test starts.

The program will crash after a period of normal operation. Because the program has added a daemon, it will automatically restart after the crash.

Therefore, the test results are as follows: The program runs automatically after a period of time and continues to run. Sometimes, it restarts several hours, sometimes even every minute.

Start to locate the problem:

1. Remove the automatic restart function and run it in GDB. It will crash in over 20 minutes:

17:50:22, 565: info: recved alarm: [10.0.60.2-1-3905], type: 12, starttime: 1319622622, recvtime: 1319622622

2011-10-26 17:50:22, 628: info: setalmstatus CHAN [1] = true

17:50:22, 628: info: recved alarm: [10.0.60.2-1-3905], type: 12, starttime: 1319622622, recvtime: 1319622622

17:50:22, 962: Notice: open file:'/Figure/data/alarmrecord/streamts/1-bitstream _ charm music main path/2011-10-26/20111026175022. Ts'

[Thread 0xa7db70 (lwp 6645) exited]

[Thread 0xa3cb70 (lwp 6644) exited]

[Thread 0x5cdfb70 (lwp 6643) exited]

[Thread 0x45fdb70 (lwp 6642) exited]

[Thread 0x3bfcb70 (lwp 6641) exited]

[Thread 0x240fb70 (lwp 6640) exited]

[Thread 0x52deb70 (lwp 6638) exited]

[Thread 0x1a0eb70 (lwp 6637) exited]

[Thread 0x67b7b70 (lwp 6636) exited]

[Thread 0x31fbb70 (lwp 6635) exited]

[Thread 0x956b70 (lwp 6634) exited]

[Thread 0x935b70 (lwp 6633) exited]

[Thread 0x8f4b70 (lwp 6632) exited]

Program terminatedSignal Sigkill, killed.

The program no longer exists.

The display program exits after receiving sigkill. BT checks the call stack and is empty.

2. view the apue, saying that the sigkill signal cannot be captured. According to the online information, "In fact, the process isn't even made aware of the sigkill signal since the signal goes straight to the kernel init. at that point, init will stop the process. the process never gets the opportunity to catch the signal and act on it ", it seems that the system sends the signal sigkill that cannot be captured to the program, and then the program exits. Therefore, it is suspected that the program occupies too many system resources, causing the program to be killed by the OS.

3. Query System Logs
If you are skeptical, you need to find evidence. The following records are found in/var/log/message:

Oct 26 17:17:30 localhost kernel: Record invoked Oom-killer: gfp_mask = 0x280da, order = 0, oomkilladj = 0

Oct 26 17:17:31 localhost kernel: Record cpuset =/mems_allowed = 0

Oct 26 17:17:31 localhost kernel: PID: 6350, COMM: record not tainted 2.6.31.5-127. fc12.i686. PAE #1

Oct 26 17:17:31 localhost kernel: Call trace:

Oct 26 17:17:31 localhost kernel: [<c049d891>] oom_kill_process + 0x6e/0x1f2

Oct 26 17:17:32 localhost kernel: [<c049dd4f>]? Select_bad_process + 0x87/0 xce

Oct 26 17:17:32 localhost kernel: [<c049de09>] _ out_of_memory + 0x73/0x82

Oct 26 17:17:32 localhost kernel: [<c049de7b>] out_of_memory + 0x63/0x88

Oct 26 17:17:32 localhost kernel: [<c04a00aa>] _ alloc_pages_nodemask + 0x382/0X447

Oct 26 17:17:32 localhost kernel: [<c04af062>] alloc_pages_node.clone.0 + 0x16/0x18

Oct 26 17:17:32 localhost kernel: [<c04afd63>] handle_mm_fault + 0x1d7/0x8f2

Oct 26 17:17:32 localhost kernel: [<c04366e3>]? Finish_task_switch + 0x53/0 xbf

Oct 26 17:17:32 localhost kernel: [<c077a829>] do_page_fault + 0x282/0x298

Oct 26 17:17:32 localhost kernel: [<c077a5a7>]? Do_page_fault + 0x0/0x298

Oct 26 17:17:32 localhost kernel: [<c0778d3b>] error_code + 0x73/0x78

Oct 26 17:17:32 localhost kernel: Mem-Info:

Oct 26 17:17:32 localhost kernel: DMA per-CPU:

Oct 26 17:17:32 localhost kernel: CPU 0: Hi: 0, btch: 1 USD: 0

Oct 26 17:17:32 localhost kernel: CPU 1: Hi: 0, btch: 1 USD: 0

Oct 26 17:17:32 localhost kernel: normal per-CPU:

Oct 26 17:17:32 localhost kernel: CPU 0: Hi: 186, btch: 31 USD: 176

Oct 26 17:17:32 localhost kernel: CPU 1: Hi: 186, btch: 31 USD: 184

Oct 26 17:17:32 localhost kernel: active_anon: 96761 active_file: 752 inactive_anon: 96834

Oct 26 17:17:32 localhost kernel: inactive_file: 1384 unevictable: 0 dirty: 25 writeback: 695 unstable: 0

Oct 26 17:17:32 localhost kernel:Free: 1920 Slab: 5066 mapped: 791 pagetables: 2649 bounce: 0

Oct 26 17:17:32 localhost kernel: DMAFree: 3328kb min: 68kb low: 84kb high: 100kb active_anon: 2296kb inactive_anon: 2420kb active_file: 204kb inactive_file: 724kb vicvictable: 0kb

Present: 15804kb pages_scanned: 0 all_unreclaimable? No

Oct 26 17:17:32 localhost kernel: lowmem_reserve []: 0 809 809

Oct 26 17:17:32 localhost kernel: normalFree: 4352kb min: 3604kb low: 4154kb high: 5404kb active_anon: 384748kb inactive_anon: 384916kb active_file: 2804kb inactive_file: 4812kb

Unevictable: 0kb present: 828992kb pages_scanned: 2496 all_unreclaimable? No

Oct 26 17:17:32 localhost kernel: lowmem_reserve []: 0 0 0 0

Oct 26 17:17:32 localhost kernel: DMA: 14 * 4kb 15 * 8kb 11 * 16kb 7 * 32kb 7 * 64kb 2 * 128kb 0 * 256kb 0 * 512kb 0 * 1024kb 1 * 2048kb 0 * 4096kb = 3328kb

Oct 26 17:17:32 localhost kernel: normal: 598 * 4kb 11 * 8kb 1 * 16kb 3 * 32kb 3 * 64kb 2 * 128kb 1 * 256kb 0 * 512kb 1 * 1024kb 0 * 2048kb 0 * 4096kb = 4320kb

Oct 26 17:17:32 localhost kernel: 2902 total pagecache pages

Oct 26 17:17:32 localhost kernel: 639 pages in SWAp Cache

Oct 26 17:17:32 localhost kernel: swap cache stats: Add 860507,Delete 859868, find 46506/63512

Oct 26 17:17:32 localhost kernel: Free swap = 0kb

Oct 26 17:17:32 localhost kernel: Total swap = 1023992kb

Oct 26 17:17:32 localhost kernel: 212976 pages Ram

Oct 26 17:17:32 localhost kernel: 0 pages highmem

Oct 26 17:17:32 localhost kernel: 4796 pages reserved

Oct 26 17:17:32 localhost kernel: 13599 pages shared

Oct 26 17:17:32 localhost kernel: 203455 pages non-shared

Oct 26 17:17:32 localhost kernel: out of memory: Kill process 6338 (GDB) score 191002 or a child

Oct 26 17:17:32 localhost kernel: killed process 6340 (record)

See the last two rows:

Kernel: out of memory: Kill process 6338 (GDB) score 191002 or a child

Kernel: killed process 6340 (record)

Obviously, the program is killed by the kernel due to insufficient system memory.

For specific memory usage, you can see:
Normal: 598 * 4kb 11 * 8kb 1 * 16kb 3 * 32kb 3 * 64kb 2 * 128kb 1 * 256kb 0 * 512kb 1 * 1024kb 0 * 2048kb 0 * 4096kb = 4320kb, the available memory is only 4 MB,
Free swap = 0kb
Total swap = 1023992kb, swap partition page used up
No memory is available.

"Record invoked Oom-killer", the Oom-killer here is "a layer of protection mechanism to prevent Linux from getting too serious when the memory is insufficient, kill irrelevant processes." For more information, see http://www.dbanotes.net/database/linux_outofmemory_oom_killer.html ".

4. If the problem is fixed, check the cause.

(1) first, reproduce the problem.

Start the program and use the system monitor to view the usage of the system memory:

As the memory used by the Program continues to grow, the swap partition is used up, and the swap partition is used up, the program is killed by the system.

(2) QueryCodeIn the end, locate the problem in the following code:

// Alarm recording Input

Int Cinputts: alarminput ()

{

Int Nret = inittssocket ();

If(Nret <0) | (m_ppreloadbuff = NULL) | (m_pbuff = NULL ))

{

Log (log_type_error,"Channel: [% d-% s]. alarminput (). nret = % d, m_ppreloadbuff = 0x % x, m_pbuff = 0x % x. Exit", M_pchannel-> m_nchannelid, m_pchannel-> m_strname.c_str (), nret, m_ppreloadbuff, m_pbuff );

Return -1;

}

Int Recvtime =Time(Null );

Char Tsdata [ts_data_len] = {0 };

Bool Bstarted =False;

While(! M_nendflag)

{

Recvtime =Time(Null );

Nret = recvfrom (tssock, tsdata, ts_data_len, 0, null, null );

If(Nret> 0)// Receive data

{

// Push the received data into the pre-recorded buffer zone

Cpoolnode * node = m_ppreloadbuff-> allocnode ();

If(Node! = NULL)

{

Node-> m_ndatalen = nret;

Node-> ntime = recvtime;

Memcpy(Node-> m_pdata, tsdata, node-> m_ndatalen );

M_ppreloadbuff-> push (node );

}

Else

{

Log (log_type_error,"Chan [% d-% s], m_ppreloadbuff-> allocnode () failed .", M_pchannel-> m_nchannelid, m_pchannel-> m_strname.c_str ());

}

// Check the current alarm status

If(Bstarted)// Alarm recording in progress

{

Bstarted = m_precordmodule-> determineendoutput (0, recvtime );

}

Else

{

Bstarted = m_precordmodule-> determinestartoutput (recvtime );

If(! Bstarted)// No alert recording is required

{

Continue;

}

Dumppreloadbuff ();

}

// Recording alarms

// Data after the alarm starts is pushed to m_pbuff (during the Alarm duration, data is pushed to m_ppreloadbuff and m_pbuff at the same time)

Cpoolnode * buffnode = m_pbuff-> allocnode ();

If(Buffnode = NULL)

{

Log_period (log_type_error,"Channel: [% d-% s], allocnode failed. Lost Data: % d bytes",

M_pchannel-> m_nchannelid, m_pchannel-> m_strname.c_str (), nret );

Usleep (50 );

Continue;

}

Buffnode-> m_ndatalen = nret;

Buffnode-> ntime = recvtime;

Memcpy(Buffnode-> m_pdata, tsdata, buffnode-> m_ndatalen );

M_pbuff-> push (buffnode );

// Check the pre-recorded Buffer

Checkpreloadbuff (recvtime );

}

Else

{

Log_period (log_type_error,"No input data! Channel: [% d-% s], nrecvsize = % d, % s \ n",

M_pchannel-> m_nchannelid, m_pchannel-> m_strname.c_str (), nret,Strerror(Errno));

}

}

Close (tssock );

Return 0;

}

 

1 void cinputts: checkpreloadbuff (time_t ltime)
2 {
3 if (m_ppreloadbuff = NULL) | (m_pbuff = NULL) | (m_precordmodule = NULL ))
4 {
5 return;
6}
7
8 If (! M_ppreloadbuff-> Empty ())
9 {
10 cpoolnode * node = m_ppreloadbuff-> Front ();
11 if (INT) (ltime-node-> ntime)> = m_nalarmrecordtimebefore) // exceeds the pre-recorded time
12 {
13 cpoolnode * pnode = m_ppreloadbuff-> POP ();
14 m_ppreloadbuff-> freenode (pnode );
15}
16}
17}

Explain the logic first:

(1) alarminput () receives data from the socket. First, it is pushed into the pre-recorded buffer m_ppreloadbuff. The buffer m_ppreloadbuff is actually a queue;

(2) Each time alarminput () receives a packet, it calls checkpreloadbuff () to check the data in the header of the m_ppreloadbuff queue in the pre-recording buffer. If it times out, It is discarded;

(Requirement Description: This section of code requires that only the data with a specified time length be retained in the pre-recorded buffer, and the timeout data will be lost, in this way, the pre-recorded buffer will be maintained at a stable length ).

However, during the test, we found that this code did not work as expected,The queue length of the pre-recorded buffer m_ppreloadbuff keeps increasing, and the system memory is exhausted.!

Here is the culprit!

Analysis logic: After receiving a packet, push it to the queue of m_ppreloadbuff in the pre-recording buffer, and then call checkpreloadbuff () to check the data in the queue header. If the packet times out, It is discarded.

Under normal circumstances, it is okay to push a data in the queue and then pop one; but in this logic:

If(! Bstarted)// No alert recording is required

{

Continue;

}

When the program pushes the data, it directly returns contine and does not execute checkpreloadbuff (). As a result, m_ppreloadbuff accumulates multiple timeout data. When the program calls checkpreloadbuff () next time () check only the data in the team header. In this case, we are likely to push 10 data records and check only three data queues. Therefore, the pre-recorded buffer m_ppreloadbuff will continue to grow.

Locate the problem and solve it: When you execute checkpreloadbuff (), you not only check the data in the queue header, but all the data until no timeout occurs.

In this way, all timeout data is cleared once during each check. As long as checkpreloadbuff () has the opportunity to execute, the buffer will not grow infinitely.

Modify as follows:

1 int cinputts: alarminput ()
2 {
3...
4 If (nret> 0) // receives data
5 {
6 // push the received data into the pre-recorded buffer zone
7 cpoolnode * node = m_ppreloadbuff-> allocnode ();
8 If (node! = NULL)
9 {
10 node-> m_ndatalen = nret;
11 node-> ntime = recvtime;
12 memcpy (node-> m_pdata, tsdata, node-> m_ndatalen );
13 m_ppreloadbuff-> push (node );
14}
15 else
16 {
17 log (log_type_error, "Chan [% d-% s], m_ppreloadbuff-> allocnode () failed.", m_pchannel-> m_nchannelid, m_pchannel-> m_strname.c_str ());
18}
19
20 // check the pre-recorded Buffer
21 checkpreloadbuff (recvtime );
22...
23}

 

1 void cinputts: checkpreloadbuff (time_t ltime)
2 {
3 if (m_ppreloadbuff = NULL) | (m_pbuff = NULL) | (m_precordmodule = NULL ))
4 {
5 return;
6}
7
8 /*
9 * notice: After receiving a packet, call checkpreloadbuff to check the pre-recorded buffer, and clear all expired pre-recorded data at a time.
10 * all time-out pre-recorded data must be cleared, but only the data in the queue header must be checked. Otherwise, once the input and output rates do not match, the pre-recorded buffer will increase rapidly.
11 * a large amount of time-out data cannot be cleared. In the end, the memory used by the Program continues to grow, consuming all the available memory and swap partitions of the system, and finally resulting in the Oom-killer protection mechanism of the OS.
12 * Start the INIT process to send a sigkill signal to the recording program, and then exit the recording program.
13 */
14 // check the oldest data in the pre-recorded Buffer
15 while (! M_ppreloadbuff-> Empty ())
16 {
17 cpoolnode * node = m_ppreloadbuff-> Front (); // clear all time-out data to ensure that only data of the specified time length is retained in the buffer.
18 if (INT) (ltime-node-> ntime)> = m_nalarmrecordtimebefore) // exceeds the pre-recorded time
19 {
20 cpoolnode * pnode = m_ppreloadbuff-> POP ();
21 m_ppreloadbuff-> freenode (pnode );
22}
23 else // will not be processed later
24 {
25 break;
26}
27}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.