<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Feb 4, 2021 at 8:12 PM Yang Xu <<a href="mailto:xuyang2018.jy@cn.fujitsu.com" target="_blank">xuyang2018.jy@cn.fujitsu.com</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">According mallinfo man-page, hblkhd member represents<br>
"The number of bytes in blocks currently allocated using mmap(2).".<br>
For allocations greater than or equal to 128K and that can't be satisfied from<br>
the free list, the memory-allocation functions employ mmap(2) instead of increasing<br>
the program break using sbrk(2).<br>
<br>
In this case, we test 20k size to use sbrk and 128k size to use mmap.<br>
<br>
Signed-off-by: Yang Xu <<a href="mailto:xuyang2018.jy@cn.fujitsu.com" target="_blank">xuyang2018.jy@cn.fujitsu.com</a>><br>
---<br>
v1->v2:<br>
1.Use mallopt(M_MMAP_THRESHOLD, 131072) to avoid dynamic mmap threshold<br>
2.Use tst_print_malinfo api <br>
runtest/syscalls | 1 +<br>
testcases/kernel/syscalls/mallinfo/.gitignore | 1 +<br>
.../kernel/syscalls/mallinfo/mallinfo02.c | 64 +++++++++++++++++++<br>
3 files changed, 66 insertions(+)<br>
create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c<br>
<br>
diff --git a/runtest/syscalls b/runtest/syscalls<br>
index 753340068..a8fa3f7bf 100644<br>
--- a/runtest/syscalls<br>
+++ b/runtest/syscalls<br>
@@ -683,6 +683,7 @@ lstat02 lstat02<br>
lstat02_64 lstat02_64<br>
<br>
mallinfo01 mallinfo01<br>
+mallinfo02 mallinfo02<br>
<br>
mallopt01 mallopt01<br>
<br>
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore<br>
index a7e32a637..678ac277e 100644<br>
--- a/testcases/kernel/syscalls/mallinfo/.gitignore<br>
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore<br>
@@ -1 +1,2 @@<br>
/mallinfo01<br>
+/mallinfo02<br>
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo02.c b/testcases/kernel/syscalls/mallinfo/mallinfo02.c<br>
new file mode 100644<br>
index 000000000..d5bed45a8<br>
--- /dev/null<br>
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c<br>
@@ -0,0 +1,64 @@<br>
+// SPDX-License-Identifier: GPL-2.0-or-later<br>
+/*<br>
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.<br>
+ * Author: Yang Xu <<a href="mailto:xuyang2018.jy@cn.fujitsu.com" target="_blank">xuyang2018.jy@cn.fujitsu.com</a>><br>
+ */<br>
+<br>
+/*\<br>
+ * [DESCRIPTION]<br>
+ *<br>
+ * Basic mallinfo() test for malloc() using sbrk or mmap.<br>
+ * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.<br>
+\*/<br>
+<br>
+#include "tst_test.h"<br>
+#include "tst_safe_macros.h"<br>
+#include "tst_mallinfo.h"<br>
+<br>
+#ifdef HAVE_MALLINFO<br>
+void test_mallopt(void)<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">what about renaming to test_mallinfo(void) ?</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>
+ struct mallinfo info;<br>
+ int size;<br>
+ char *buf;<br>
+<br>
+ buf = SAFE_MALLOC(20480);<br>
+<br>
+ info = mallinfo();<br>
+ if (info.uordblks > 20480 && info.hblkhd == 0) {<br>
+ tst_res(TPASS, "malloc() uses sbrk when size < 128k");<br>
+ } else {<br>
+ tst_res(TFAIL, "malloc() use mmap when size < 128k");<br>
+ tst_print_mallinfo("Test malloc(20480)", &info);<br>
+ }<br>
+ free(buf);<br>
+<br>
+ info = mallinfo();<br>
+ size = MAX(info.fordblks, 131072);<br>
+<br>
+ buf = SAFE_MALLOC(size);<br>
+ info = mallinfo();<br>
+ if (info.hblkhd > size && info.hblks > 0) {<br>
+ tst_res(TPASS, "malloc() uses mmap when size >= 128k");<br>
+ } else {<br>
+ tst_res(TFAIL, "malloc uses sbrk when size >= 128k");<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Why not 'TFAIL | TERRNO' ?</div></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">
+ tst_print_mallinfo("Test malloc(1024*128)", &info);<br>
+ }<br>
+<br>
+ free(buf);<br>
+}<br>
+<br>
+static void setup(void)<br>
+{<br>
+ if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)<br>
+ tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Here as well.</div></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>
+<br>
+static struct tst_test test = {<br>
+ .setup = setup,<br>
+ .test_all = test_mallopt,<br>
+};<br>
+<br>
+#else<br>
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");<br>
+#endif<br>
-- <br>
2.23.0<br>
<br>
<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>