<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">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">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" class="gmail_signature"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div></div>