Skip to contentSkip to conversation

syscalls/prctl08: New test for prctl() with PR_{SET, GET}_TIMERSLACK

PATCH Superseded Yang Xu 2 participants 3 messages Jul 27, 2019 – Jul 30, 2019

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

PATCH v2 1/1 syscalls/prctl08: New test for prctl() with PR_{SET, GET}_TIMERSLACK

-----
v1->v2:
1)remove useless struct member
2)compare time should be (sleep + timer_slack) if time is not enough large

---
 include/lapi/prctl.h                       |   5 +
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/prctl/.gitignore |   1 +
 testcases/kernel/syscalls/prctl/Makefile   |   1 +
 testcases/kernel/syscalls/prctl/prctl08.c  | 118 +++++++++++++++++++++
 5 files changed, 126 insertions(+)
 create mode 100644 testcases/kernel/syscalls/prctl/prctl08.c
include/lapi/prctl.h +5 -0
index 8ee492259..0b4e196c3 100644
@@ -19,6 +19,11 @@
 # define PR_SET_SECCOMP  22
 #endif
 
+#ifndef PR_SET_TIMERSLACK
+# define PR_SET_TIMERSLACK 29
+# define PR_GET_TIMERSLACK 30
+#endif
+
 #ifndef PR_SET_CHILD_SUBREAPER
 # define PR_SET_CHILD_SUBREAPER	36
 # define PR_GET_CHILD_SUBREAPER	37
runtest/syscalls +1 -0
index 0114b002b..48836f422 100644
@@ -872,6 +872,7 @@ prctl04 prctl04
 prctl05 prctl05
 prctl06 prctl06
 prctl07 prctl07
+prctl08 prctl08
 
 pread01 pread01
 pread01_64 pread01_64
testcases/kernel/syscalls/prctl/.gitignore +1 -0
index 2178db366..fe36a8e0f 100644
@@ -6,3 +6,4 @@
 /prctl06
 /prctl06_execve
 /prctl07
+/prctl08
testcases/kernel/syscalls/prctl/Makefile +1 -0
index cf19507c0..d3c41ff4e 100644
@@ -21,5 +21,6 @@ top_srcdir		?= ../../../..
 include $(top_srcdir)/include/mk/testcases.mk
 
 prctl07: LDLIBS += $(CAP_LIBS)
+prctl08: LDLIBS+=-lrt
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/prctl/prctl08.c new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.18.1
#1 lore raw

PATCH v2 1/1 syscalls/prctl08: New test for prctl() with PR_{SET, GET}_TIMERSLACK

Hi!

Quoted 49 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +49 -0
+static struct tcase {
+	unsigned long setvalue;
+	unsigned long cmptime;
+} tcases[] = {
+	{1, 50000},
+	{70000, 120000},
+	{INT_MAX, 50000},
+};
+
+static int proc_flag = 1;
+
+static void check_proc_ns(char *message, unsigned long value)
+{
+	unsigned long proc_value;
+
+	SAFE_FILE_SCANF(PROC_NS_PATH, "%lu", &proc_value);
+	if (proc_value == value)
+		tst_res(TPASS, "%s %s  got %lu expectedly",
+				message, PROC_NS_PATH, proc_value);
+	else
+		tst_res(TFAIL, "%s %s expected %lu got %lu",
+				message, PROC_NS_PATH, value, proc_value);
+}
+
+static void check_get_timerslack(char *message, unsigned long value)
+{
+	TEST(prctl(PR_GET_TIMERSLACK));
+	if ((unsigned long)TST_RET == value)
+		tst_res(TPASS, "%s prctl(PR_GET_TIMERSLACK) got %lu expectedly",
+				message, value);
+	else
+		tst_res(TFAIL, "%s prctl(PR_GET_TIMERSLACK) expected %lu got %lu",
+				message, value, TST_RET);
+
+	if (proc_flag)
+		check_proc_ns(message, value);
+}
+
+static void verify_prctl(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+	int pid;
+
+	struct timespec timereq = { .tv_sec = 0, .tv_nsec = 50000 };
+	struct timespec timecmp = { .tv_sec = 0, .tv_nsec = tc->cmptime};
+
+	TEST(prctl(PR_SET_TIMERSLACK, tc->setvalue));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "prctl(PR_SET_TIMERSLACK, %lu) failed",

testcases/kernel/syscalls/prctl/prctl08.c line 89

Quoted 10 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +10 -0
+				tc->setvalue);
+		return;
+	}
+	tst_res(TPASS, "prctl(PR_SET_TIMERSLACK, %lu) success", tc->setvalue);
+
+	pid = SAFE_FORK();
+	if (pid == 0) {
+		check_get_timerslack("child process", tc->setvalue);
+		/* A value of 0 means using default */
+		prctl(PR_SET_TIMERSLACK, 0);

Why do we reset the slack before the measurements?

testcases/kernel/syscalls/prctl/prctl08.c line 97

Quoted 8 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +8 -0
+		check_get_timerslack("After set 0, child process", tc->setvalue);
+
+		tst_timer_start(CLOCK_MONOTONIC);
+		TEST(nanosleep(&timereq, NULL));
+		tst_timer_stop();
+
+		if (tst_timespec_lt(tst_timer_elapsed(), timecmp))
+			tst_brk(TFAIL, "nanosleep() slept less than timecmp");

I do not get what we are trying to assert here.

As far as I understand it the timer slack is a way how to inform kernel that it's okay if the timers are slightly less precise. However the timer still can fire somewhere between sleep time and sleep time + slack, or even maybe later if the system is under load.

BTW we do have a formula that tries to compute maximal time the timers should sleep based on timer slack in lib/tst_timer_test.c but even with that we have to take more samples and compute truncated mean because single short sleep may be delayed unless it's a RT kernel...

Quoted 28 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +20 -0
+		tst_res(TPASS, "nanosleep() slept more than timecmp, %llius",
+				tst_timer_elapsed_us());
+		exit(0);
+	}
+}
+
+static void setup(void)
+{
+	if (access(PROC_NS_PATH, F_OK) == -1) {
+		tst_res(TCONF, "proc doesn't support timerslack_ns interface");
+		proc_flag = 0;
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_prctl,
+	.tcnt = ARRAY_SIZE(tcases),
+	.forks_child = 1,
+};
Signature · 7 lines
2.18.1




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH v2 1/1 syscalls/prctl08: New test for prctl() with PR_{SET, GET}_TIMERSLACK

Quoted 54 lines

Hi!

Quoted 52 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +52 -0
+static struct tcase {
+	unsigned long setvalue;
+	unsigned long cmptime;
+} tcases[] = {
+	{1, 50000},
+	{70000, 120000},
+	{INT_MAX, 50000},
+};
+
+static int proc_flag = 1;
+
+static void check_proc_ns(char *message, unsigned long value)
+{
+	unsigned long proc_value;
+
+	SAFE_FILE_SCANF(PROC_NS_PATH, "%lu",&proc_value);
+	if (proc_value == value)
+		tst_res(TPASS, "%s %s  got %lu expectedly",
+				message, PROC_NS_PATH, proc_value);
+	else
+		tst_res(TFAIL, "%s %s expected %lu got %lu",
+				message, PROC_NS_PATH, value, proc_value);
+}
+
+static void check_get_timerslack(char *message, unsigned long value)
+{
+	TEST(prctl(PR_GET_TIMERSLACK));
+	if ((unsigned long)TST_RET == value)
+		tst_res(TPASS, "%s prctl(PR_GET_TIMERSLACK) got %lu expectedly",
+				message, value);
+	else
+		tst_res(TFAIL, "%s prctl(PR_GET_TIMERSLACK) expected %lu got %lu",
+				message, value, TST_RET);
+
+	if (proc_flag)
+		check_proc_ns(message, value);
+}
+
+static void verify_prctl(unsigned int n)
+{
+	struct tcase *tc =&tcases[n];
+	int pid;
+
+	struct timespec timereq = { .tv_sec = 0, .tv_nsec = 50000 };
+	struct timespec timecmp = { .tv_sec = 0, .tv_nsec = tc->cmptime};
+
+	TEST(prctl(PR_SET_TIMERSLACK, tc->setvalue));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "prctl(PR_SET_TIMERSLACK, %lu) failed",
+				tc->setvalue);
+		return;
+	}
Quoted 8 lines

testcases/kernel/syscalls/prctl/prctl08.c line 89

Quoted 7 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +7 -0
+	tst_res(TPASS, "prctl(PR_SET_TIMERSLACK, %lu) success", tc->setvalue);
+
+	pid = SAFE_FORK();
+	if (pid == 0) {
+		check_get_timerslack("child process", tc->setvalue);
+		/* A value of 0 means using default */
+		prctl(PR_SET_TIMERSLACK, 0);

Why do we reset the slack before the measurements?

Hi Cyril

I reset it because I want to test whether default timer_slack in child process is the same as the current value
of the creating thread.

I also sent a patch about PR_SET_TIMERSLACK to man-page, as below:
https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=c14f79303f2885f7f4f4bf0d63922551b520a4b8

Quoted 10 lines

testcases/kernel/syscalls/prctl/prctl08.c line 97

Quoted 8 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +8 -0
+		check_get_timerslack("After set 0, child process", tc->setvalue);
+
+		tst_timer_start(CLOCK_MONOTONIC);
+		TEST(nanosleep(&timereq, NULL));
+		tst_timer_stop();
+
+		if (tst_timespec_lt(tst_timer_elapsed(), timecmp))
+			tst_brk(TFAIL, "nanosleep() slept less than timecmp");

I do not get what we are trying to assert here.

Quoted 4 lines

As far as I understand it the timer slack is a way how to inform kernel that it's okay if the timers are slightly less precise. However the timer still can fire somewhere between sleep time and sleep time + slack, or even maybe later if the system is under load.

Yes. timer still can fire somewhere in [sleep, sleep+slack] range even later.

Quoted 5 lines

BTW we do have a formula that tries to compute maximal time the timers should sleep based on timer slack in lib/tst_timer_test.c but even with that we have to take more samples and compute truncated mean because single short sleep may be delayed unless it's a RT kernel...

Yes . Agree. Single short sleep may be delayed and I will take more samples.

Quoted 28 lines
Quoted 28 lines
testcases/kernel/syscalls/prctl/prctl08.c new file +20 -0
+		tst_res(TPASS, "nanosleep() slept more than timecmp, %llius",
+				tst_timer_elapsed_us());
+		exit(0);
+	}
+}
+
+static void setup(void)
+{
+	if (access(PROC_NS_PATH, F_OK) == -1) {
+		tst_res(TCONF, "proc doesn't support timerslack_ns interface");
+		proc_flag = 0;
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test = verify_prctl,
+	.tcnt = ARRAY_SIZE(tcases),
+	.forks_child = 1,
+};
Signature · 7 lines
2.18.1




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp