Skip to contentSkip to conversation

Check available memory to support embedded devices

PATCH New Julio Cruz 3 participants 5 messages Mar 16, 2016 – Apr 7, 2016 4 messages not in the archive

lore
Share & tools
lore

Julio Cruz

author
#0 lore raw

PATCH 1/1 Check available memory to support embedded devices

This patch check the available memory before to perform the different test cases. If the memory is not enough (according with each test case), the test finish as TCONF.
This could be usefull when you are testing on embedded devices with RAM memory limitation (i.e. 512MB)
This patch no changed the test case procedure and is still valid for non-embedded devices. It just verify the available memory.

---
 testcases/kernel/syscalls/getrusage/getrusage03.c | 80 +++++++++++++++++------
 1 file changed, 61 insertions(+), 19 deletions(-)
testcases/kernel/syscalls/getrusage/getrusage03.c +61 -19
index 54cdc83..8967c8e 100644
@@ -45,7 +45,10 @@
 char *TCID = "getrusage03";
 int TST_TOTAL = 1;
 
-#define DELTA_MAX	10240
+#define DELTA_MAX	        10240
+#define DEFAULT_ALLOCATION_MB	100
+
+unsigned long avail_memory_mb;
 
 static struct rusage ru;
 static long maxrss_init;
@@ -65,6 +68,14 @@ static void consume(int mega);
 static void setup(void);
 static void cleanup(void);
 
+unsigned long get_available_memory_mb(void)
+{
+	unsigned long long ps, pn;
+	ps = sysconf(_SC_PAGESIZE);
+	pn = sysconf(_SC_AVPHYS_PAGES);
+	return ps * pn / (1024*1024);
+}
+
 int main(int argc, char *argv[])
 {
 	int lc;
@@ -73,19 +84,26 @@ int main(int argc, char *argv[])
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		tst_resm(TINFO, "allocate 100MB");
-		consume(100);
-
-		inherit_fork();
-		inherit_fork2();
-		fork_malloc();
-		grandchild_maxrss();
-		zombie();
-		sig_ign();
-		exec_without_fork();
+	avail_memory_mb = get_available_memory_mb();
+
+	tst_resm(TINFO, "Available memory: %ldMB\n", avail_memory_mb);
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+	} else {
+		for (lc = 0; TEST_LOOPING(lc); lc++) {
+			tst_count = 0;
+
+			tst_resm(TINFO, "allocate %dMB", DEFAULT_ALLOCATION_MB);
+			consume(DEFAULT_ALLOCATION_MB);
+
+			inherit_fork();
+			inherit_fork2();
+			fork_malloc();
+			grandchild_maxrss();
+			zombie();
+			sig_ign();
+			exec_without_fork();
+		}
 	}
 	cleanup();
 	tst_exit();
@@ -95,11 +113,15 @@ int main(int argc, char *argv[])
  * expect: initial.self ~= child.self */
 static void inherit_fork(void)
 {
-	tst_resm(TINFO, "Testcase #01: fork inherit");
-
 	SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
 	tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
 
+	tst_resm(TINFO, "Testcase #01: fork inherit");
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
+
 	switch (pid = fork()) {
 	case -1:
 		tst_brkm(TBROK | TERRNO, cleanup, "fork #1");
@@ -119,17 +141,21 @@ static void inherit_fork(void)
 }
 
 /* Testcase #02: fork inherit (cont.)
- * expect: initial.children ~= 100MB, child.children = 0 */
+ * expect: initial.children ~= DEFAULT_ALLOCATION_MB, child.children = 0 */
 static void inherit_fork2(void)
 {
 	tst_resm(TINFO, "Testcase #02: fork inherit(cont.)");
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
 	if (is_in_delta(ru.ru_maxrss - 102400))
-		tst_resm(TPASS, "initial.children ~= 100MB");
+		tst_resm(TPASS, "initial.children ~= %dMB", DEFAULT_ALLOCATION_MB);
 	else
-		tst_resm(TFAIL, "initial.children !~= 100MB");
+		tst_resm(TFAIL, "initial.children !~= %dMB", DEFAULT_ALLOCATION_MB);
 
 	switch (pid = fork()) {
 	case -1:
@@ -153,6 +179,10 @@ static void inherit_fork2(void)
 static void fork_malloc(void)
 {
 	tst_resm(TINFO, "Testcase #03: fork + malloc");
+	if (avail_memory_mb < (DEFAULT_ALLOCATION_MB*2+50)) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
 	tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
@@ -182,6 +212,10 @@ static void fork_malloc(void)
 static void grandchild_maxrss(void)
 {
 	tst_resm(TINFO, "Testcase #04: grandchild maxrss");
+	if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+300)) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
@@ -216,6 +250,10 @@ static void grandchild_maxrss(void)
 static void zombie(void)
 {
 	tst_resm(TINFO, "Testcase #05: zombie");
+	if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+400)) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
@@ -259,6 +297,10 @@ static void zombie(void)
 static void sig_ign(void)
 {
 	tst_resm(TINFO, "Testcase #06: SIG_IGN");
+	if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+500)) {
+		tst_resm(TCONF, "Not enough memory to run test case");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/1 Check available memory to support embedded devices

----- Original Message -----

Quoted 14 lines

From: "Julio Cruz" <[email protected]>
To: "julio cruz" <[email protected]>, [email protected], [email protected], [email protected]
Cc: "Julio Cruz" <[email protected]>
Sent: Wednesday, 16 March, 2016 4:01:15 AM
Subject: [PATCH] Check available memory to support embedded devices

This patch check the available memory before to perform the different test cases. If the memory is not enough (according with each test case), the test finish as TCONF.
This could be usefull when you are testing on embedded devices with RAM memory limitation (i.e. 512MB)
This patch no changed the test case procedure and is still valid for non-embedded devices. It just verify the available memory.

Quoted 5 lines
---
 testcases/kernel/syscalls/getrusage/getrusage03.c | 80
 +++++++++++++++++------
 1 file changed, 61 insertions(+), 19 deletions(-)

Hi,

Quoted 19 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -1
index 54cdc83..8967c8e 100644
@@ -45,7 +45,10 @@
 char *TCID = "getrusage03";
 int TST_TOTAL = 1;
 
-#define DELTA_MAX	10240
+#define DELTA_MAX	        10240
+#define DEFAULT_ALLOCATION_MB	100
+
+unsigned long avail_memory_mb;
 
 static struct rusage ru;
 static long maxrss_init;

testcases/kernel/syscalls/getrusage/getrusage03.c static void consume(int mega); · line 76

Quoted 9 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +6 -0
@@ -65,6 +68,14 @@ static void consume(int mega);
 static void setup(void);
 static void cleanup(void);
 
+unsigned long get_available_memory_mb(void)
+{
+	unsigned long long ps, pn;
+	ps = sysconf(_SC_PAGESIZE);
+	pn = sysconf(_SC_AVPHYS_PAGES);
+	return ps * pn / (1024*1024);

What I was suggesting was:
return (ps / 1024) * pn / 1024;

All of the page sizes are at least 4k (grep "define PAGE_SHIFT" -r linux/arch/). This way "unsigned long" should do just fine for all variables.

Quoted 17 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +2 -8
+}
+
 int main(int argc, char *argv[])
 {
 	int lc;
@@ -73,19 +84,26 @@ int main(int argc, char *argv[])
 
 	setup();
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		tst_resm(TINFO, "allocate 100MB");
-		consume(100);
-
-		inherit_fork();
-		inherit_fork2();

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 91

Quoted 10 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +5 -5
-		fork_malloc();
-		grandchild_maxrss();
-		zombie();
-		sig_ign();
-		exec_without_fork();
+	avail_memory_mb = get_available_memory_mb();
+
+	tst_resm(TINFO, "Available memory: %ldMB\n", avail_memory_mb);
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");

We have tst_brkm(TCONF, cleanup, "...");, which will print message, run cleanup and exit testcase.

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 97

Quoted 6 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +6 -0
+	} else {
+		for (lc = 0; TEST_LOOPING(lc); lc++) {
+			tst_count = 0;
+
+			tst_resm(TINFO, "allocate %dMB", DEFAULT_ALLOCATION_MB);
+			consume(DEFAULT_ALLOCATION_MB);

This consume() is within loop, and it doesn't look like same loop frees any memory. I suggest you move it before loop. You can try running the testcase with "-i 20".

Quoted 13 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +9 -0
+
+			inherit_fork();
+			inherit_fork2();
+			fork_malloc();
+			grandchild_maxrss();
+			zombie();
+			sig_ign();
+			exec_without_fork();
+		}
 	}
 	cleanup();
 	tst_exit();

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 120

Quoted 10 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +2 -2
@@ -95,11 +113,15 @@ int main(int argc, char *argv[])
  * expect: initial.self ~= child.self */
 static void inherit_fork(void)
 {
-	tst_resm(TINFO, "Testcase #01: fork inherit");
-
 	SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
 	tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
 
+	tst_resm(TINFO, "Testcase #01: fork inherit");
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Is this needed? Child is not allocating anything, and fork should use copy-on-write.

Quoted 8 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -0
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
+
 	switch (pid = fork()) {
 	case -1:
 		tst_brkm(TBROK | TERRNO, cleanup, "fork #1");

testcases/kernel/syscalls/getrusage/getrusage03.c static void inherit_fork(void) · line 148

Quoted 9 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +2 -1
@@ -119,17 +141,21 @@ static void inherit_fork(void)
 }
 
 /* Testcase #02: fork inherit (cont.)
- * expect: initial.children ~= 100MB, child.children = 0 */
+ * expect: initial.children ~= DEFAULT_ALLOCATION_MB, child.children = 0 */
 static void inherit_fork2(void)
 {
 	tst_resm(TINFO, "Testcase #02: fork inherit(cont.)");
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Same as above.

Quoted 16 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +5 -2
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
 	if (is_in_delta(ru.ru_maxrss - 102400))
-		tst_resm(TPASS, "initial.children ~= 100MB");
+		tst_resm(TPASS, "initial.children ~= %dMB", DEFAULT_ALLOCATION_MB);
 	else
-		tst_resm(TFAIL, "initial.children !~= 100MB");
+		tst_resm(TFAIL, "initial.children !~= %dMB", DEFAULT_ALLOCATION_MB);
 
 	switch (pid = fork()) {
 	case -1:

testcases/kernel/syscalls/getrusage/getrusage03.c static void inherit_fork2(void) · line 182

Quoted 4 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +1 -0
@@ -153,6 +179,10 @@ static void inherit_fork2(void)
 static void fork_malloc(void)
 {
 	tst_resm(TINFO, "Testcase #03: fork + malloc");
+	if (avail_memory_mb < (DEFAULT_ALLOCATION_MB*2+50)) {

Same as above, isn't "50" going to be enough?

Regards,
Jan

Quoted 43 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +15 -1
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
 	tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
@@ -182,6 +212,10 @@ static void fork_malloc(void)
 static void grandchild_maxrss(void)
 {
 	tst_resm(TINFO, "Testcase #04: grandchild maxrss");
+	if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+300)) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
@@ -216,6 +250,10 @@ static void grandchild_maxrss(void)
 static void zombie(void)
 {
 	tst_resm(TINFO, "Testcase #05: zombie");
+	if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+400)) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
@@ -259,6 +297,10 @@ static void zombie(void)
 static void sig_ign(void)
 {
 	tst_resm(TINFO, "Testcase #06: SIG_IGN");
+	if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+500)) {
+		tst_resm(TCONF, "Not enough memory to run test case");
+		return;
+	}
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
--

1.9.1

author
#2 lore raw

PATCH 1/1 Check available memory to support embedded devices

Hi Jan,

On Mar 17, 2016, at 5:20 PM, Jan Stancek <[email protected]> wrote:

----- Original Message -----

Quoted 14 lines

From: "Julio Cruz" <[email protected]>
To: "julio cruz" <[email protected]>, [email protected], [email protected], [email protected]
Cc: "Julio Cruz" <[email protected]>
Sent: Wednesday, 16 March, 2016 4:01:15 AM
Subject: [PATCH] Check available memory to support embedded devices

This patch check the available memory before to perform the different test cases. If the memory is not enough (according with each test case), the test finish as TCONF.
This could be usefull when you are testing on embedded devices with RAM memory limitation (i.e. 512MB)
This patch no changed the test case procedure and is still valid for non-embedded devices. It just verify the available memory.

Quoted 5 lines

---
testcases/kernel/syscalls/getrusage/getrusage03.c | 80
+++++++++++++++++------

1 file changed, 61 insertions(+), 19 deletions(-)

Hi,

Quoted 19 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -1
index 54cdc83..8967c8e 100644
@@ -45,7 +45,10 @@
 char *TCID = "getrusage03";
 int TST_TOTAL = 1;
 
-#define DELTA_MAX    10240
+#define DELTA_MAX            10240
+#define DEFAULT_ALLOCATION_MB    100
+
+unsigned long avail_memory_mb;
 
 static struct rusage ru;
 static long maxrss_init;

testcases/kernel/syscalls/getrusage/getrusage03.c static void consume(int mega); · line 76

Quoted 9 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +6 -0
@@ -65,6 +68,14 @@ static void consume(int mega);
 static void setup(void);
 static void cleanup(void);
 
+unsigned long get_available_memory_mb(void)
+{
+    unsigned long long ps, pn;
+    ps = sysconf(_SC_PAGESIZE);
+    pn = sysconf(_SC_AVPHYS_PAGES);
+    return ps * pn / (1024*1024);

What I was suggesting was:
return (ps / 1024) * pn / 1024;

All of the page sizes are at least 4k (grep "define PAGE_SHIFT" -r linux/arch/). This way "unsigned long" should do just fine for all variables.

Ok!

Quoted 17 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +2 -8
+}
+
 int main(int argc, char *argv[])
 {
   int lc;
@@ -73,19 +84,26 @@ int main(int argc, char *argv[])
 
   setup();
 
-    for (lc = 0; TEST_LOOPING(lc); lc++) {
-        tst_count = 0;
-
-        tst_resm(TINFO, "allocate 100MB");
-        consume(100);
-
-        inherit_fork();
-        inherit_fork2();

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 91

Quoted 10 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +5 -5
-        fork_malloc();
-        grandchild_maxrss();
-        zombie();
-        sig_ign();
-        exec_without_fork();
+    avail_memory_mb = get_available_memory_mb();
+
+    tst_resm(TINFO, "Available memory: %ldMB\n", avail_memory_mb);
+    if (avail_memory_mb < DEFAULT_ALLOCATION_MB) {
+        tst_resm(TCONF, "Not enough memory to run this case\n");

We have tst_brkm(TCONF, cleanup, "...");, which will print message, run cleanup and exit testcase.

Much better!

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 97

Quoted 6 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +6 -0
+    } else {
+        for (lc = 0; TEST_LOOPING(lc); lc++) {
+            tst_count = 0;
+
+            tst_resm(TINFO, "allocate %dMB", DEFAULT_ALLOCATION_MB);
+            consume(DEFAULT_ALLOCATION_MB);

This consume() is within loop, and it doesn't look like same loop frees any memory. I suggest you move it before loop. You can try running the testcase with "-i 20".

You are right. I didn't see this because my test run one time. I will fix.

Quoted 13 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +9 -0
+
+            inherit_fork();
+            inherit_fork2();
+            fork_malloc();
+            grandchild_maxrss();
+            zombie();
+            sig_ign();
+            exec_without_fork();
+        }
   }
   cleanup();
   tst_exit();

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 120

Quoted 10 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +2 -2
@@ -95,11 +113,15 @@ int main(int argc, char *argv[])
 * expect: initial.self ~= child.self */
 static void inherit_fork(void)
 {
-    tst_resm(TINFO, "Testcase #01: fork inherit");
-
   SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
   tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
 
+    tst_resm(TINFO, "Testcase #01: fork inherit");
+    if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Is this needed? Child is not allocating anything, and fork should use copy-on-write.

During my testing, if the memory available is not the double, the test had problem. I will recheck.

Quoted 8 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -0
+        tst_resm(TCONF, "Not enough memory to run this case\n");
+        return;
+    }
+
   switch (pid = fork()) {
   case -1:
       tst_brkm(TBROK | TERRNO, cleanup, "fork #1");

testcases/kernel/syscalls/getrusage/getrusage03.c static void inherit_fork(void) · line 148

Quoted 9 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +2 -1
@@ -119,17 +141,21 @@ static void inherit_fork(void)
 }
 
 /* Testcase #02: fork inherit (cont.)
- * expect: initial.children ~= 100MB, child.children = 0 */
+ * expect: initial.children ~= DEFAULT_ALLOCATION_MB, child.children = 0 */
 static void inherit_fork2(void)
 {
   tst_resm(TINFO, "Testcase #02: fork inherit(cont.)");
+    if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Same as above.

Similar to previous problem

Quoted 16 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +5 -2
+        tst_resm(TCONF, "Not enough memory to run this case\n");
+        return;
+    }
 
   SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
   tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
   if (is_in_delta(ru.ru_maxrss - 102400))
-        tst_resm(TPASS, "initial.children ~= 100MB");
+        tst_resm(TPASS, "initial.children ~= %dMB", DEFAULT_ALLOCATION_MB);
   else
-        tst_resm(TFAIL, "initial.children !~= 100MB");
+        tst_resm(TFAIL, "initial.children !~= %dMB", DEFAULT_ALLOCATION_MB);
 
   switch (pid = fork()) {
   case -1:

testcases/kernel/syscalls/getrusage/getrusage03.c static void inherit_fork2(void) · line 182

Quoted 4 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +1 -0
@@ -153,6 +179,10 @@ static void inherit_fork2(void)
 static void fork_malloc(void)
 {
   tst_resm(TINFO, "Testcase #03: fork + malloc");
+    if (avail_memory_mb < (DEFAULT_ALLOCATION_MB*2+50)) {

Same as above, isn't "50" going to be enough?

Again, similar.

Thanks for your review. I will send another patch.

Regards,
Jan

Quoted 43 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +15 -1
+        tst_resm(TCONF, "Not enough memory to run this case\n");
+        return;
+    }
 
   SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
   tst_resm(TINFO, "initial.self = %ld", ru.ru_maxrss);
@@ -182,6 +212,10 @@ static void fork_malloc(void)
 static void grandchild_maxrss(void)
 {
   tst_resm(TINFO, "Testcase #04: grandchild maxrss");
+    if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+300)) {
+        tst_resm(TCONF, "Not enough memory to run this case\n");
+        return;
+    }
 
   SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
   tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
@@ -216,6 +250,10 @@ static void grandchild_maxrss(void)
 static void zombie(void)
 {
   tst_resm(TINFO, "Testcase #05: zombie");
+    if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+400)) {
+        tst_resm(TCONF, "Not enough memory to run this case\n");
+        return;
+    }
 
   SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
   tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
@@ -259,6 +297,10 @@ static void zombie(void)
 static void sig_ign(void)
 {
   tst_resm(TINFO, "Testcase #06: SIG_IGN");
+    if (avail_memory_mb <= (DEFAULT_ALLOCATION_MB+500)) {
+        tst_resm(TCONF, "Not enough memory to run test case");
+        return;
+    }
 
   SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
   tst_resm(TINFO, "initial.children = %ld", ru.ru_maxrss);
--

1.9.1

#3 lore raw

PATCH 1/1 Check available memory to support embedded devices

In reply to Jan Stancek

Hi Jan,

Quoted 3 lines

What I was suggesting was:
return (ps / 1024) * pn / 1024;

Done.

Quoted 8 lines

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 91

Quoted 4 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +4 -0
+
+	tst_resm(TINFO, "Available memory: %ldMB\n", avail_memory_mb);
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB) {
+		tst_resm(TCONF, "Not enough memory to run this case\n");

We have tst_brkm(TCONF, cleanup, "...");, which will print message, run cleanup and exit testcase.

Done. Applied also in other points.

Quoted 6 lines

DEFAULT_ALLOCATION_MB);

testcases/kernel/syscalls/getrusage/getrusage03.c int main(int argc, char *argv[]) · line 97

Quoted 1 line
testcases/kernel/syscalls/getrusage/getrusage03.c +1 -0
+			consume(DEFAULT_ALLOCATION_MB);

This consume() is within loop, and it doesn't look like same loop frees any memory. I suggest you move it before loop.
You can try running the testcase with "-i 20".

Done.

Quoted 5 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Is this needed? Child is not allocating anything, and fork should use copy-on- write.

If before the call to fork() the available memory is not the double as DEFAULT_ALLOCATION_MB, the test fail (ENOMEM: fork() failed to allocate the necessary kernel structures because memory is tight).

In summary (let me know if I'm wrong), the fork() 'means that all that the state is copied, including open files, register state and all memory allocations, which includes the memory used at consume() function' [ref]. However, with COW, 'rather than copy all the memory at once, it pretends it was copied and only actually copies when the parent and child need to hold different values at the same address' [ref]. According with those assumptions, the kernel need to reserve the allocated memory anyway, and that explain why I place the condition.

Quoted 5 lines

testcases/kernel/syscalls/getrusage/getrusage03.c static void inherit_fork(void) · line 148

Quoted 2 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +1 -0
 	tst_resm(TINFO, "Testcase #02: fork inherit(cont.)");
+	if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Same as above.

Same condition.

Quoted 4 lines

testcases/kernel/syscalls/getrusage/getrusage03.c static void inherit_fork2(void) · line 182

Quoted 2 lines
testcases/kernel/syscalls/getrusage/getrusage03.c +1 -0
 	tst_resm(TINFO, "Testcase #03: fork + malloc");
+	if (avail_memory_mb < (DEFAULT_ALLOCATION_MB*2+50)) {

Same as above, isn't "50" going to be enough?

Same assumption. If there is not memory available, the test will fail. The idea is to avoid it with the condition.

Quoted 3 lines

Regards,
Jan

Regards

Julio

[ref] some references about CoW:
https://www.reddit.com/r/compsci/comments/31szui/trying_to_understand_fork_and_copyonwrite_cow/
https://en.wikipedia.org/wiki/Copy-on-write

#8 lore raw

PATCH 1/1 Check available memory to support embedded devices

In reply to a message not in the archive

Hi Jan,

The patch version 2 for the test case 'getrusage03' was send to the list.

Thanks for your suggestions in the previous emails.

In summary:

- The initial 'consume' was moved out the loop
- The variable 'avail_memory_mb' was remove and the function 'get_available_memory_mb' is used all the times
- As concluded, the kernel not allocate the memory (for every fork) but still check if there is available. So, this is the reason to include the verification prior each test case (with fork)
- In error (TCONF) the function 'tst_brkm(TCONF, cleanup,...' is used
- The patch was build and tested on final boards
- The patch was check with 'checkpatch.pl'

Regards

Julio

Quoted 215 lines

-----Original Message-----
From: Jan Stancek [mailto:[email protected]]
Sent: Saturday, April 02, 2016 2:57 PM
To: Julio Cruz Barroso
Subject: Re: [PATCH] Check available memory to support embedded devices

----- Original Message -----

Quoted 6 lines

From: "Julio Cruz Barroso" <[email protected]>
To: "Jan Stancek" <[email protected]>
Sent: Saturday, 2 April, 2016 8:39:08 AM
Subject: RE: [PATCH] Check available memory to support embedded devices

Quoted 49 lines

Hi Jan,

Thanks for your comments. My reply inline

Quoted 45 lines

-----Original Message-----
From: Jan Stancek [mailto:[email protected]]
Sent: Wednesday, March 30, 2016 8:19 PM
To: Julio Cruz Barroso
Subject: Re: [PATCH] Check available memory to support embedded devices

----- Original Message -----

Quoted 9 lines

From: "Julio Cruz Barroso" <[email protected]>
To: "Jan Stancek" <[email protected]>
Sent: Friday, 25 March, 2016 2:23:11 AM
Subject: Re: [PATCH] Check available memory to support embedded devices

Hi Jan,

Any comment feedback related with this test case?

comments inline

Quoted 21 lines

Let me know to send the new patch.

Regards

Julio

On Mar 23, 2016, at 12:28 PM, Julio Cruz Barroso
<[email protected]>
wrote:

Hi Jan,

Quoted 3 lines

What I was suggesting was:
return (ps / 1024) * pn / 1024;

Done.

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+
+    tst_resm(TINFO, "Available memory: %ldMB\n",

avail_memory_mb);

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+    if (avail_memory_mb < DEFAULT_ALLOCATION_MB) {
+        tst_resm(TCONF, "Not enough memory to run this
+ case\n");

We have tst_brkm(TCONF, cleanup, "...");, which will print message, run cleanup and exit testcase.

Done. Applied also in other points.

Quoted 6 lines

DEFAULT_ALLOCATION_MB);

Quoted 1 line

+ consume(DEFAULT_ALLOCATION_MB);

This consume() is within loop, and it doesn't look like same loop frees any memory. I suggest you move it before loop. You can try running the testcase with "-i 20".

Done.

Quoted 7 lines
Quoted 1 line

+ if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Is this needed? Child is not allocating anything, and fork should use
copy-on-
write.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

If before the call to fork() the available memory is not the double as DEFAULT_ALLOCATION_MB, the test fail (ENOMEM: fork() failed to allocate the necessary kernel structures because memory is

tight).

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

In summary (let me know if I'm wrong), the fork() 'means that all that the state is copied, including open files, register state and all memory allocations, which includes the memory used at consume()

function'

Quoted 61 lines
Quoted 11 lines

[ref].

Quoted 5 lines

However, with COW, 'rather than copy all the memory at once, it pretends it was copied and only actually copies when the parent and child need to hold different values at the same address' [ref]. According with those assumptions, the kernel need to reserve the allocated memory anyway, and that explain why I place the condition.

I don't think kernel is going to reserve any memory. But, there is still a possibility that it will make an overcommit check (depending on vm.overcommit_memory).
So to check for memory that's needed for fork is OK.

I agree with you. I don’t think there is an reservation but somehow there is a checking. About your suggestion with overcommit_memory, my current system has CommitLimit 254884 kB in /proc/meminfo.

root@emad:~# cat /proc/meminfo
MemTotal:         509768 kB
MemFree:          323228 kB
MemAvailable:     346020 kB
Buffers:            6712 kB
Cached:            26384 kB
SwapCached:            0 kB
Active:            16224 kB
Inactive:          20420 kB
Active(anon):       3652 kB
Inactive(anon):     4340 kB
Active(file):      12572 kB
Inactive(file):    16080 kB
Unevictable:           0 kB
Mlocked:               0 kB
HighTotal:             0 kB
HighFree:              0 kB
LowTotal:         509768 kB
LowFree:          323228 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:          3544 kB
Mapped:             7804 kB
Shmem:              4448 kB
Slab:               8624 kB
SReclaimable:       3028 kB
SUnreclaim:         5596 kB
KernelStack:         648 kB
PageTables:          344 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      254884 kB
Committed_AS:      27428 kB
VmallocTotal:    1548288 kB
VmallocUsed:        5856 kB
VmallocChunk:    1365276 kB
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

   tst_resm(TINFO, "Testcase #02: fork inherit(cont.)");

+ if (avail_memory_mb < DEFAULT_ALLOCATION_MB*2) {

Quoted 7 lines
Quoted 4 lines

In which case there should be no "*2", because memory in parent is already allocated. You should need the same amount in child, right? This is now checking, that we have twice the available memory that parent allocated.

I understand you point. But in this case, the variable "avail_memory_mb" is set in the beginning.

I missed that. You're right then it needs to be "DEFAULT_ALLOCATION_MB*2".

Regards,
Jan

Quoted 38 lines

Maybe, to avoid confusions, I could set this variable every time.

If you thinks is OK, I could remake another patch version.

Quoted 33 lines
Quoted 9 lines
Quoted 4 lines

Same as above.

Same condition.

Quoted 2 lines
Quoted 2 lines
   tst_resm(TINFO, "Testcase #03: fork + malloc");

+ if (avail_memory_mb < (DEFAULT_ALLOCATION_MB*2+50)) {

Same here.

Regards,
Jan

Quoted 18 lines
Quoted 2 lines

Same as above, isn't "50" going to be enough?

Same assumption. If there is not memory available, the test will fail. The idea is to avoid it with the condition.

Quoted 3 lines

Regards,
Jan

Regards

Julio

[ref] some references about CoW:
https://www.reddit.com/r/compsci/comments/31szui/trying_to_underst
and_ fork_and_copyonwrite_cow/
https://en.wikipedia.org/wiki/Copy-on-write