<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><style>body { line-height: 1.5; }blockquote { margin-top: 0px; margin-bottom: 0px; margin-left: 0.5em; }div.FoxDiv20210127144917122140 { }body { font-size: 14px; font-family: 'Microsoft YaHei UI'; color: rgb(0, 0, 0); line-height: 1.5; }</style></head><body>
<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:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm"><div style="PADDING-RIGHT: 8px; PADDING-LEFT: 8px; FONT-SIZE: 12px;FONT-FAMILY:tahoma;COLOR:#000000; BACKGROUND: #efefef; PADDING-BOTTOM: 8px; PADDING-TOP: 8px"><div><b>From:</b> <a href="mailto:liwang@redhat.com">Li Wang</a></div><div><b>Date:</b> 2021-01-27 12:27</div><div><b>To:</b> <a href="mailto:liuxp11@chinatelecom.cn">Xinpeng Liu</a></div><div><b>CC:</b> <a href="mailto:ltp@lists.linux.it">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="FoxDiv20210127144917122140"><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>
</div></div></blockquote>
</body></html>