<div dir="ltr"><div class="gmail_default" style="font-size:small">Hi Xinpeng,</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="">I sent to you the case swapping01 solving this(via FILE_LINES_SCANF) already, feel free to take an reference:</div><div class="gmail_default" style=""><a href="https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/mem/swapping/swapping01.c#L85" style="font-size:small">https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/mem/swapping/swapping01.c#L85</a><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 27, 2021 at 2:54 PM <a href="mailto:liuxp11@chinatelecom.cn">liuxp11@chinatelecom.cn</a> <<a href="mailto:liuxp11@chinatelecom.cn">liuxp11@chinatelecom.cn</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<div><span></span>Hi Li, </div><div>Have a question about using macro <span style="font-size:small;line-height:19.5px;background-color:transparent">SAFE_READ_MEMINFO get </span><span style="line-height:1.5;background-color:transparent">MemAvailable value,</span></div><div>Some old kernels maybe not privode "<span style="line-height:1.5;background-color:transparent">MemAvailable</span><span style="line-height:1.5;background-color:transparent">" field, which will broken.</span></div><div><span style="line-height:1.5;background-color:transparent"><br></span></div>
<blockquote style="margin-top:0px;margin-bottom:0px;margin-left:0.5em;margin-right:inherit"><div> </div><div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(181,196,223);padding:3pt 0cm 0cm"><div style="padding:8px;font-size:12px;font-family:tahoma;color:rgb(0,0,0);background:rgb(239,239,239)"><div><b>From:</b> <a href="mailto:liwang@redhat.com" target="_blank">Li Wang</a></div><div><b>Date:</b> 2021-01-27 12:27</div><div><b>To:</b> <a href="mailto:liuxp11@chinatelecom.cn" target="_blank">Xinpeng Liu</a></div><div><b>CC:</b> <a href="mailto:ltp@lists.linux.it" target="_blank">LTP List</a></div><div><b>Subject:</b> Re: [LTP] [PATCH 1/2] syscalls/ioctl: ioctl_sg01.c: ioctl_sg01 invoked oom-killer</div></div></div><div><div class="gmail-m_3715482154841094765FoxDiv20210127144917122140"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small">Hi Xinpeng,</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 27, 2021 at 11:28 AM Xinpeng Liu <<a href="mailto:liuxp11@chinatelecom.cn" target="_blank">liuxp11@chinatelecom.cn</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Kernel version is 5.4.81+,the available RAM is less than free,as follow:<br>
[root@liuxp mywork]# head /proc/meminfo<br>
MemTotal:       198101744 kB<br>
MemFree:        189303148 kB<br>
MemAvailable:   188566732 kB<br>
<br>
So use available RAM to avoid OOM killer.<br>
<br>
Signed-off-by: Xinpeng Liu <<a href="mailto:liuxp11@chinatelecom.cn" target="_blank">liuxp11@chinatelecom.cn</a>><br>
---<br>
 lib/tst_memutils.c | 29 ++++++++++++++++++++++++++---<br>
 1 file changed, 26 insertions(+), 3 deletions(-)<br>
<br>
diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c<br>
index dd09db4..21df9a8 100644<br>
--- a/lib/tst_memutils.c<br>
+++ b/lib/tst_memutils.c<br>
@@ -10,14 +10,33 @@<br>
<br>
 #define TST_NO_DEFAULT_MAIN<br>
 #include "tst_test.h"<br>
+#include "tst_safe_stdio.h"<br>
<br>
 #define BLOCKSIZE (16 * 1024 * 1024)<br>
<br>
+static unsigned long get_available_ram(void)<br>
+{<br>
+       char buf[60]; /* actual lines we expect are ~30 chars or less */<br>
+       unsigned long available_kb = 0;<br>
+       FILE *fp;<br>
+<br>
+       fp = SAFE_FOPEN("/proc/meminfo","r");<br>
+       while (fgets(buf, sizeof(buf), fp)) {<br>
+               if (sscanf(buf, "MemAvailable: %lu %*s\n", &available_kb) == 1){<br>
+                       break;<br>
+               }<br>
+       }<br>
+       SAFE_FCLOSE(fp);<br>
+<br>
+       return 1024 * available_kb;<br>
+}<br>
+<br>
 void tst_pollute_memory(size_t maxsize, int fillchar)<br>
 {<br>
        size_t i, map_count = 0, safety = 0, blocksize = BLOCKSIZE;<br>
        void **map_blocks;<br>
        struct sysinfo info;<br>
+       unsigned long available_ram = get_available_ram();<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">LTP provides SAFE_READ_MEMINFO() macro to be used in /proc/meminfo reading.</div><div class="gmail_default" style="font-size:small">See: <a href="https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/mem/swapping/swapping01.c#L85" target="_blank">https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/mem/swapping/swapping01.c#L85</a></div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
        SAFE_SYSINFO(&info);<br>
        safety = MAX(4096 * SAFE_SYSCONF(_SC_PAGESIZE), 128 * 1024 * 1024);<br>
@@ -26,15 +45,19 @@ void tst_pollute_memory(size_t maxsize, int fillchar)<br>
        if (info.freeswap > safety)<br>
                safety = 0;<br>
<br>
+       /*"MemAvailable" field maybe not exist, or freeram less than available_ram*/<br>
+       if(available_ram == 0 || info.freeram < available_ram)<br>
+               available_ram = info.freeram;<br>
+<br>
        /* Not enough free memory to avoid invoking OOM killer */<br>
-       if (info.freeram <= safety)<br>
+       if (available_ram <= safety)<br>
                return;<br>
<br>
        if (!maxsize)<br>
                maxsize = SIZE_MAX;<br>
<br>
-       if (info.freeram - safety < maxsize / info.mem_unit)<br>
-               maxsize = (info.freeram - safety) * info.mem_unit;<br>
+       if (available_ram - safety < maxsize / info.mem_unit)<br>
+               maxsize = (available_ram - safety) * info.mem_unit;<br>
<br>
        blocksize = MIN(maxsize, blocksize);<br>
        map_count = maxsize / blocksize;<br>
-- <br>
1.8.3.1<br>
<br>
<br>
-- <br>
Mailing list info: <a href="https://lists.linux.it/listinfo/ltp" rel="noreferrer" target="_blank">https://lists.linux.it/listinfo/ltp</a><br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div></div>
</div></div></blockquote>
</div></blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div>