Skip to contentSkip to conversation

syscalls/getrusage03: Use constants instead of magic numbers

PATCH New Yuriy Kolerov 2 participants 3 messages Aug 22, 2016 – Oct 5, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 syscalls/getrusage03: Use constants instead of magic numbers

---
 testcases/kernel/syscalls/getrusage/getrusage03.c  | 53 +++++++++++-----------
 testcases/kernel/syscalls/getrusage/getrusage03.h  | 36 +++++++++++++++
 .../kernel/syscalls/getrusage/getrusage03_child.c  |  7 ++-
 3 files changed, 66 insertions(+), 30 deletions(-)
 create mode 100644 testcases/kernel/syscalls/getrusage/getrusage03.h
testcases/kernel/syscalls/getrusage/getrusage03.c +27 -26
index 54cdc83..5214d36 100644
@@ -41,12 +41,11 @@
 
 #include "test.h"
 #include "safe_macros.h"
+#include "getrusage03.h"
 
 char *TCID = "getrusage03";
 int TST_TOTAL = 1;
 
-#define DELTA_MAX	10240
-
 static struct rusage ru;
 static long maxrss_init;
 static int retval, status;
@@ -76,8 +75,8 @@ int main(int argc, char *argv[])
 	for (lc = 0; TEST_LOOPING(lc); lc++) {
 		tst_count = 0;
 
-		tst_resm(TINFO, "allocate 100MB");
-		consume(100);
+		tst_resm(TINFO, "allocate %dMB", CONSUME_INITIAL_MB);
+		consume(CONSUME_INITIAL_MB);
 
 		inherit_fork();
 		inherit_fork2();
@@ -126,10 +125,10 @@ static void inherit_fork2(void)
 
 	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");
+	if (is_in_delta(ru.ru_maxrss - CONSUME_INITIAL_MB * 1024))
+		tst_resm(TPASS, "initial.children ~= %dMB", CONSUME_INITIAL_MB);
 	else
-		tst_resm(TFAIL, "initial.children !~= 100MB");
+		tst_resm(TFAIL, "initial.children !~= %dMB", CONSUME_INITIAL_MB);
 
 	switch (pid = fork()) {
 	case -1:
@@ -162,19 +161,23 @@ static void fork_malloc(void)
 		tst_brkm(TBROK | TERRNO, cleanup, "fork #3");
 	case 0:
 		maxrss_init = ru.ru_maxrss;
-		tst_resm(TINFO, "child allocate +50MB");
-		consume(50);
+		tst_resm(TINFO, "child allocate +%dMB", CONSUME_FORK_MALLOC_MB);
+		consume(CONSUME_FORK_MALLOC_MB);
 		SAFE_GETRUSAGE(cleanup, RUSAGE_SELF, &ru);
 		tst_resm(TINFO, "child.self = %ld", ru.ru_maxrss);
-		exit(is_in_delta(maxrss_init + 51200 - ru.ru_maxrss));
+		exit(is_in_delta(maxrss_init + CONSUME_FORK_MALLOC_MB * 1024 -
+				 ru.ru_maxrss));
 	default:
 		break;
 	}
 
 	if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
 		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
-	check_return(WEXITSTATUS(status), "initial.self + 50MB ~= child.self",
-		     "initial.self + 50MB !~= child.self");
+	check_return(WEXITSTATUS(status),
+		     "initial.self + " N_TO_STR(CONSUME_FORK_MALLOC_MB)
+		     "MB ~= child.self",
+		     "initial.self + " N_TO_STR(CONSUME_FORK_MALLOC_MB)
+		     "MB !~= child.self");
 }
 
 /* Testcase #04: grandchild maxrss
@@ -190,7 +193,8 @@ static void grandchild_maxrss(void)
 	case -1:
 		tst_brkm(TBROK | TERRNO, cleanup, "fork #4");
 	case 0:
-		retval = system("getrusage03_child -g 300");
+		retval = system("getrusage03_child -g "
+				N_TO_STR(CONSUME_GRANDCHILD_MB));
 		if ((WIFEXITED(retval) && WEXITSTATUS(retval) != 0))
 			tst_brkm(TBROK | TERRNO, cleanup, "system");
 		exit(0);
@@ -205,10 +209,10 @@ static void grandchild_maxrss(void)
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "post_wait.children = %ld", ru.ru_maxrss);
-	if (is_in_delta(ru.ru_maxrss - 307200))
-		tst_resm(TPASS, "child.children ~= 300MB");
+	if (is_in_delta(ru.ru_maxrss - CONSUME_GRANDCHILD_MB * 1024))
+		tst_resm(TPASS, "child.children ~= %dMB", CONSUME_GRANDCHILD_MB);
 	else
-		tst_resm(TFAIL, "child.children !~= 300MB");
+		tst_resm(TFAIL, "child.children !~= %dMB", CONSUME_GRANDCHILD_MB);
 }
 
 /* Testcase #05: zombie
@@ -225,7 +229,8 @@ static void zombie(void)
 	case -1:
 		tst_brkm(TBROK, cleanup, "fork #5");
 	case 0:
-		retval = system("getrusage03_child -n 400");
+		retval = system("getrusage03_child -n "
+				N_TO_STR(CONSUME_ZOMBIE_MB));
 		if ((WIFEXITED(retval) && WEXITSTATUS(retval) != 0))
 			tst_brkm(TBROK | TERRNO, cleanup, "system");
 		exit(0);
@@ -248,10 +253,10 @@ static void zombie(void)
 
 	SAFE_GETRUSAGE(cleanup, RUSAGE_CHILDREN, &ru);
 	tst_resm(TINFO, "post_wait.children = %ld", ru.ru_maxrss);
-	if (is_in_delta(ru.ru_maxrss - 409600))
-		tst_resm(TPASS, "post_wait.children ~= 400MB");
+	if (is_in_delta(ru.ru_maxrss - CONSUME_ZOMBIE_MB * 1024))
+		tst_resm(TPASS, "post_wait.children ~= %dMB", CONSUME_ZOMBIE_MB);
 	else
-		tst_resm(TFAIL, "post_wait.children !~= 400MB");
+		tst_resm(TFAIL, "post_wait.children !~= %dMB", CONSUME_ZOMBIE_MB);
 }
 
 /* Testcase #06: SIG_IGN
@@ -269,7 +274,8 @@ static void sig_ign(void)
 	case -1:
 		tst_brkm(TBROK, cleanup, "fork #6");
 	case 0:
-		retval = system("getrusage03_child -n 500");
+		retval = system("getrusage03_child -n "
+				N_TO_STR(CONSUME_SIG_IGN_MB));
 		if ((WIFEXITED(retval) && WEXITSTATUS(retval) != 0))
 			tst_brkm(TBROK | TERRNO, cleanup, "system");
 		exit(0);
@@ -310,11 +316,6 @@ static void exec_without_fork(void)
 		tst_brkm(TBROK | TERRNO, cleanup, "execlp");
 }
 
-static int is_in_delta(long value)
-{
-	return (value >= -DELTA_MAX && value <= DELTA_MAX);
-}
-
 static void check_return(int status, char *pass_msg, char *fail_msg)
 {
 	switch (status) {
testcases/kernel/syscalls/getrusage/getrusage03.h new file +36 -0
new file mode 100644
index 0000000..7dc6d0a
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 Synopsys, Inc. (www.synopsys.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GETRUSAGE03_H
+#define GETRUSAGE03_H
+
+#define DELTA_MAX_KB		102400
+#define CONSUME_INITIAL_MB	100
+#define CONSUME_FORK_MALLOC_MB	50
+#define CONSUME_GRANDCHILD_MB	300
+#define CONSUME_ZOMBIE_MB	400
+#define CONSUME_SIG_IGN_MB	500
+
+#define N_TO_STR(s) _N_TO_STR(s)
+#define _N_TO_STR(s) #s
+
+static int is_in_delta(long value)
+{
+	return (value >= -DELTA_MAX_KB && value <= DELTA_MAX_KB);
+}
+
+#endif
testcases/kernel/syscalls/getrusage/getrusage03_child.c +3 -4
index 24a6f33..6eeae75 100644
@@ -34,12 +34,11 @@
 
 #include "test.h"
 #include "safe_macros.h"
+#include "getrusage03.h"
 
 char *TCID = "getrusage03_child";
 int TST_TOTAL = 1;
 
-#define DELTA_MAX	10240
-
 static int opt_consume, opt_grand, opt_show, opt_self, opt_child;
 static char *consume_str, *grand_consume_str, *self_str, *child_str;
 
@@ -111,7 +110,7 @@ int main(int argc, char *argv[])
 				self_nr = SAFE_STRTOL(cleanup,
 						      self_str, 0, LONG_MAX);
 				delta = maxrss_self - self_nr;
-				if (delta >= -DELTA_MAX && delta <= DELTA_MAX)
+				if (is_in_delta(delta))
 					tst_resm(TPASS,
 						 "initial.self ~= exec.self");
 				else
@@ -122,7 +121,7 @@ int main(int argc, char *argv[])
 				child_nr = SAFE_STRTOL(cleanup,
 						       child_str, 0, LONG_MAX);
 				delta = maxrss_children - child_nr;
-				if (delta >= -DELTA_MAX && delta <= DELTA_MAX)
+				if (is_in_delta(delta))
 					tst_resm(TPASS,
 						 "initial.children ~= exec.children");
 				else
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 syscalls/getrusage03: Reduce memory consumption

Some development platforms may lack of large amounts of memory (~500MB). It is possible to reduce memory consumption in this test without harm to functionality.

---
 testcases/kernel/syscalls/getrusage/getrusage03.c |  6 +++---
 testcases/kernel/syscalls/getrusage/getrusage03.h | 10 +++++-----
 2 files changed, 8 insertions(+), 8 deletions(-)
testcases/kernel/syscalls/getrusage/getrusage03.c +3 -3
index 5214d36..9bc984d 100644
@@ -118,7 +118,7 @@ static void inherit_fork(void)
 }
 
 /* Testcase #02: fork inherit (cont.)
- * expect: initial.children ~= 100MB, child.children = 0 */
+ * expect: initial.children ~= 50MB, child.children = 0 */
 static void inherit_fork2(void)
 {
 	tst_resm(TINFO, "Testcase #02: fork inherit(cont.)");
@@ -181,7 +181,7 @@ static void fork_malloc(void)
 }
 
 /* Testcase #04: grandchild maxrss
- * expect: post_wait.children ~= 300MB */
+ * expect: post_wait.children ~= 150MB */
 static void grandchild_maxrss(void)
 {
 	tst_resm(TINFO, "Testcase #04: grandchild maxrss");
@@ -216,7 +216,7 @@ static void grandchild_maxrss(void)
 }
 
 /* Testcase #05: zombie
- * expect: initial ~= pre_wait, post_wait ~= 400MB */
+ * expect: initial ~= pre_wait, post_wait ~= 150MB */
 static void zombie(void)
 {
 	tst_resm(TINFO, "Testcase #05: zombie");
testcases/kernel/syscalls/getrusage/getrusage03.h +5 -5
index 7dc6d0a..c7bc0af 100644
@@ -18,12 +18,12 @@
 #ifndef GETRUSAGE03_H
 #define GETRUSAGE03_H
 
-#define DELTA_MAX_KB		102400
-#define CONSUME_INITIAL_MB	100
+#define DELTA_MAX_KB		51200
+#define CONSUME_INITIAL_MB	50
 #define CONSUME_FORK_MALLOC_MB	50
-#define CONSUME_GRANDCHILD_MB	300
-#define CONSUME_ZOMBIE_MB	400
-#define CONSUME_SIG_IGN_MB	500
+#define CONSUME_GRANDCHILD_MB	150
+#define CONSUME_ZOMBIE_MB	200
+#define CONSUME_SIG_IGN_MB	250
 
 #define N_TO_STR(s) _N_TO_STR(s)
 #define _N_TO_STR(s) #s
Signature · 1 line
2.7.4
#2 lore raw

PATCH 1/2 syscalls/getrusage03: Use constants instead of magic numbers

In reply to Yuriy Kolerov

Hi!

Quoted 30 lines
testcases/kernel/syscalls/getrusage/getrusage03.h new file +24 -0
new file mode 100644
index 0000000..7dc6d0a
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2016 Synopsys, Inc. (www.synopsys.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GETRUSAGE03_H
+#define GETRUSAGE03_H
+
+#define DELTA_MAX_KB		102400
+#define CONSUME_INITIAL_MB	100
+#define CONSUME_FORK_MALLOC_MB	50
+#define CONSUME_GRANDCHILD_MB	300

testcases/kernel/syscalls/getrusage/getrusage03.h line 34

Quoted 10 lines
testcases/kernel/syscalls/getrusage/getrusage03.h new file +10 -0
+#define CONSUME_ZOMBIE_MB	400
+#define CONSUME_SIG_IGN_MB	500
+
+#define N_TO_STR(s) _N_TO_STR(s)
+#define _N_TO_STR(s) #s
+
+static int is_in_delta(long value)
+{
+	return (value >= -DELTA_MAX_KB && value <= DELTA_MAX_KB);
+}

I do not like much that you are replacing the hardcoded values with macros. Why not integer variables instead? Then we can actually tune the values at test start automatically.

So what about making all these CONSUME_FOO macros integer varibles instead?

Also sorry for the delay in review.

Signature · 2 lines
Cyril Hrubis
[email protected]