All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] nice05: Add testcase for nice() syscall
@ 2022-05-07  7:36 Zhao Gongyi via ltp
  2022-05-27 11:07 ` Petr Vorel
  0 siblings, 1 reply; 3+ messages in thread
From: Zhao Gongyi via ltp @ 2022-05-07  7:36 UTC (permalink / raw)
  To: ltp

Add test verify that the low nice thread execute more cycles than
the high nice thread since the two thread binded on the same cpu.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/nice/.gitignore |   1 +
 testcases/kernel/syscalls/nice/Makefile   |   2 +
 testcases/kernel/syscalls/nice/nice05.c   | 183 ++++++++++++++++++++++
 4 files changed, 187 insertions(+)
 create mode 100644 testcases/kernel/syscalls/nice/nice05.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 24d0f955d..0b47e67c6 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -897,6 +897,7 @@ nice01 nice01
 nice02 nice02
 nice03 nice03
 nice04 nice04
+nice04 nice05

 open01 open01
 open01A symlink01 -T open01
diff --git a/testcases/kernel/syscalls/nice/.gitignore b/testcases/kernel/syscalls/nice/.gitignore
index 9d7a1bb43..58d64779e 100644
--- a/testcases/kernel/syscalls/nice/.gitignore
+++ b/testcases/kernel/syscalls/nice/.gitignore
@@ -2,3 +2,4 @@
 /nice02
 /nice03
 /nice04
+/nice05
diff --git a/testcases/kernel/syscalls/nice/Makefile b/testcases/kernel/syscalls/nice/Makefile
index 044619fb8..adba57060 100644
--- a/testcases/kernel/syscalls/nice/Makefile
+++ b/testcases/kernel/syscalls/nice/Makefile
@@ -3,6 +3,8 @@

 top_srcdir		?= ../../../..

+nice05: CFLAGS+=-pthread
+
 include $(top_srcdir)/include/mk/testcases.mk

 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/nice/nice05.c b/testcases/kernel/syscalls/nice/nice05.c
new file mode 100644
index 000000000..814260296
--- /dev/null
+++ b/testcases/kernel/syscalls/nice/nice05.c
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright(c) 2022 Huawei Technologies Co., Ltd
+ * Author: Li Mengfei <limengfei4@huawei.com>
+ *         Zhao Gongyi <zhaogongyi@huawei.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * 1. Create a high nice thread and a low nice thread, the main
+ *    thread wake them at the same time
+ * 2. Both threads run on the same CPU
+ * 3. Verify that the low nice thread execute more cycles than
+ *    the high nice thread
+ */
+
+#define _GNU_SOURCE
+#include <pthread.h>
+#include "tst_test.h"
+#include "tst_safe_pthread.h"
+
+#define LIMIT_TIME 3
+#define RUN_TIMES 1000000
+
+static pthread_barrier_t barrier;
+static long long time_nice_low, time_nice_high;
+static int some_cpu;
+static cpu_set_t *set;
+
+static void set_nice(int nice_inc)
+{
+	int orig_nice;
+
+	orig_nice = SAFE_GETPRIORITY(PRIO_PROCESS, 0);
+	TEST(nice(nice_inc));
+
+	if (TST_RET != (orig_nice + nice_inc)) {
+		tst_brk(TFAIL | TTERRNO, "nice(%d) returned %li, expected %i",
+				nice_inc, TST_RET, orig_nice + nice_inc);
+	}
+
+	if (TST_ERR)
+		tst_brk(TFAIL | TTERRNO, "nice(%d) failed", nice_inc);
+
+}
+
+static void* nice_low_thread(void* arg)
+{
+	int number = 0;
+	int ret = 0;
+
+	set_nice(*(int*)arg);
+	ret = pthread_barrier_wait(&barrier);
+	if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
+		tst_brk(TBROK, "pthread_barrier_wait() returned %s",
+				tst_strerrno(-ret));
+	}
+
+	while (1) {
+		for (int i = 0; i < RUN_TIMES; ++i)
+			number++;
+
+		pthread_testcancel();
+		time_nice_low++;
+	}
+	return NULL;
+}
+
+static void* nice_high_thread(void* arg)
+{
+	int number = 0;
+	int ret = 0;
+
+	set_nice(*(int*)arg);
+	ret = pthread_barrier_wait(&barrier);
+	if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
+		tst_brk(TBROK, "pthread_barrier_wait() returned %s",
+				tst_strerrno(-ret));
+	}
+
+	while (1) {
+		for (int i = 0; i < RUN_TIMES; ++i)
+			number++;
+
+		pthread_testcancel();
+		time_nice_high++;
+	}
+	return NULL;
+}
+
+static void setup(void)
+{
+	size_t size;
+	int index;
+	int nrcpus = 1024;
+
+	set = CPU_ALLOC(nrcpus);
+	if (!set)
+		tst_brk(TBROK | TERRNO, "CPU_ALLOC()");
+
+	size = CPU_ALLOC_SIZE(nrcpus);
+	CPU_ZERO_S(size, set);
+	if (sched_getaffinity(0, size, set) < 0) {
+		tst_brk(TBROK | TERRNO, "sched_getaffinity()");
+	}
+
+	for (index = 0; index < (int)size * 8; index++)
+		if (CPU_ISSET_S(index, size, set))
+			some_cpu = index;
+
+	CPU_ZERO_S(size, set);
+	CPU_SET_S(some_cpu, size, set);
+	if (sched_setaffinity(0, size, set) < 0) {
+		tst_brk(TBROK | TERRNO, "sched_setaffinity()");
+	}
+}
+
+static void cleanup(void)
+{
+	if (set)
+		CPU_FREE(set);
+}
+
+static void verify_nice(void)
+{
+	int ret;
+	int nice_inc_high = -1;
+	int nice_inc_low = -2;
+	pthread_t nice_low, nice_high;
+
+	ret = pthread_barrier_init(&barrier, NULL, 3);
+	if (ret != 0) {
+		tst_brk(TBROK, "pthread_barrier_init() returned %s",
+			tst_strerrno(-ret));
+	}
+
+	SAFE_PTHREAD_CREATE(&nice_high, NULL, nice_high_thread, (void*)&nice_inc_high);
+	SAFE_PTHREAD_CREATE(&nice_low, NULL, nice_low_thread, (void*)&nice_inc_low);
+
+	pthread_barrier_wait(&barrier);
+	if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
+		tst_brk(TBROK | TERRNO, "pthread_barrier_wait() failed");
+
+	sleep(LIMIT_TIME);
+
+	ret = pthread_cancel(nice_high);
+	if(ret) {
+		tst_brk(TBROK, "pthread_cancel() returned %s",
+			tst_strerrno(-ret));
+	}
+
+	ret = pthread_cancel(nice_low);
+	if(ret) {
+		tst_brk(TBROK, "pthread_cancel() returned %s",
+			tst_strerrno(-ret));
+	}
+
+	ret = pthread_barrier_destroy(&barrier);
+	if (ret) {
+		tst_brk(TBROK, "pthread_barrier_destroy() returned %s",
+			tst_strerrno(-ret));
+	}
+
+	SAFE_PTHREAD_JOIN(nice_high, NULL);
+	SAFE_PTHREAD_JOIN(nice_low, NULL);
+
+	if (time_nice_low > time_nice_high) {
+		tst_res(TPASS, "time_nice_low = %lld time_nice_high = %lld",
+			time_nice_low, time_nice_high);
+	} else {
+		tst_brk(TFAIL | TTERRNO, "Test failed :"
+			"time_nice_low = %lld time_nice_high = %lld",
+			time_nice_low, time_nice_high);
+	}
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_nice,
+	.needs_root = 1,
+};
--
2.17.1


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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH] nice05: Add testcase for nice() syscall
  2022-05-07  7:36 [LTP] [PATCH] nice05: Add testcase for nice() syscall Zhao Gongyi via ltp
@ 2022-05-27 11:07 ` Petr Vorel
  0 siblings, 0 replies; 3+ messages in thread
From: Petr Vorel @ 2022-05-27 11:07 UTC (permalink / raw)
  To: Zhao Gongyi; +Cc: ltp

Hi Zhao,

on first look LGTM, few notes below.

make check complains about style, could you please fix it?

$ make check-nice05
CHECK testcases/kernel/syscalls/nice/nice05.c
nice05.c:48: ERROR: "foo* bar" should be "foo *bar"
nice05.c:48: ERROR: "foo* bar" should be "foo *bar"
nice05.c:53: ERROR: "(foo*)" should be "(foo *)"
nice05.c:70: ERROR: "foo* bar" should be "foo *bar"
nice05.c:70: ERROR: "foo* bar" should be "foo *bar"
nice05.c:75: ERROR: "(foo*)" should be "(foo *)"
nice05.c:104: WARNING: braces {} are not necessary for single statement blocks
nice05.c:114: WARNING: braces {} are not necessary for single statement blocks
nice05.c:138: ERROR: "(foo*)" should be "(foo *)"
nice05.c:139: ERROR: "(foo*)" should be "(foo *)"
nice05.c:148: ERROR: space required before the open parenthesis '('
nice05.c:154: ERROR: space required before the open parenthesis '('
make: [../../../../include/mk/rules.mk:56: check-nice05] Error 1 (ignored)


> Add test verify that the low nice thread execute more cycles than
                                           ^ executes

> the high nice thread since the two thread binded on the same cpu.

> Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>

...
> diff --git a/testcases/kernel/syscalls/nice/nice05.c b/testcases/kernel/syscalls/nice/nice05.c
...
> +/*\
> + * [Description]
> + *
> + * 1. Create a high nice thread and a low nice thread, the main
> + *    thread wake them at the same time
> + * 2. Both threads run on the same CPU
> + * 3. Verify that the low nice thread execute more cycles than
                                         ^ executes

> + *    the high nice thread
> + */
> +
> +#define _GNU_SOURCE
> +#include <pthread.h>
> +#include "tst_test.h"
> +#include "tst_safe_pthread.h"
> +
> +#define LIMIT_TIME 3
> +#define RUN_TIMES 1000000
It might be useful if user could change this value with getopt switch (to debug
on error to speedup). I also wonder if we hit timeout on some slow machine.

...
> +static void verify_nice(void)
> +{
> +	int ret;
> +	int nice_inc_high = -1;
> +	int nice_inc_low = -2;
> +	pthread_t nice_low, nice_high;
> +
> +	ret = pthread_barrier_init(&barrier, NULL, 3);
> +	if (ret != 0) {
> +		tst_brk(TBROK, "pthread_barrier_init() returned %s",
> +			tst_strerrno(-ret));
> +	}
Maybe just:
	if (pthread_barrier_init(&barrier, NULL, 3) != 0)
		tst_brk(TBROK | TERRNO, "pthread_barrier_init() failed");

Or feel free to use ret if you need it (see below), the point is with tst_brk()
use TERRNO. Or am I missing something pthread specific?

> +	SAFE_PTHREAD_CREATE(&nice_high, NULL, nice_high_thread, (void*)&nice_inc_high);
> +	SAFE_PTHREAD_CREATE(&nice_low, NULL, nice_low_thread, (void*)&nice_inc_low);
> +
> +	pthread_barrier_wait(&barrier);
Not an expert on pthread, but IMHO you should compare
PTHREAD_BARRIER_SERIAL_THREAD with the result of pthread_barrier_wait(), with
your current code you compare with the result of pthread_barrier_init().
> +	if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
> +		tst_brk(TBROK | TERRNO, "pthread_barrier_wait() failed");
> +
> +	sleep(LIMIT_TIME);
> +
> +	ret = pthread_cancel(nice_high);
> +	if(ret) {
> +		tst_brk(TBROK, "pthread_cancel() returned %s",
> +			tst_strerrno(-ret));
if (pthread_cancel(nice_high))
	tst_brk(TBROK | TERRNO, "pthread_cancel() failed");

> +	}
> +
> +	ret = pthread_cancel(nice_low);
> +	if(ret) {
> +		tst_brk(TBROK, "pthread_cancel() returned %s",
> +			tst_strerrno(-ret));
and here as well.
> +	}
> +
> +	ret = pthread_barrier_destroy(&barrier);
> +	if (ret) {
> +		tst_brk(TBROK, "pthread_barrier_destroy() returned %s",
> +			tst_strerrno(-ret));
> +	}
> +
> +	SAFE_PTHREAD_JOIN(nice_high, NULL);
> +	SAFE_PTHREAD_JOIN(nice_low, NULL);
> +
> +	if (time_nice_low > time_nice_high) {
> +		tst_res(TPASS, "time_nice_low = %lld time_nice_high = %lld",
> +			time_nice_low, time_nice_high);
> +	} else {
> +		tst_brk(TFAIL | TTERRNO, "Test failed :"
Wrong space between ':', missing after it, use:
		tst_brk(TFAIL | TTERRNO, "Test failed: "


Kind regards,
Petr

> +			"time_nice_low = %lld time_nice_high = %lld",
> +			time_nice_low, time_nice_high);
> +	}
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = verify_nice,
> +	.needs_root = 1,
> +};

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH] nice05: Add testcase for nice() syscall
@ 2022-06-18  1:54 zhaogongyi via ltp
  0 siblings, 0 replies; 3+ messages in thread
From: zhaogongyi via ltp @ 2022-06-18  1:54 UTC (permalink / raw)
  To: Petr Vorel; +Cc: ltp

Hi Petr,

Thanks for your review!

> make check complains about style, could you please fix it?
> 
> $ make check-nice05
> CHECK testcases/kernel/syscalls/nice/nice05.c
> nice05.c:48: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:48: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:53: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:70: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:70: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:75: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:104: WARNING: braces {} are not necessary for single statement
> blocks
> nice05.c:114: WARNING: braces {} are not necessary for single statement
> blocks
> nice05.c:138: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:139: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:148: ERROR: space required before the open parenthesis '('
> nice05.c:154: ERROR: space required before the open parenthesis '('
> make: [../../../../include/mk/rules.mk:56: check-nice05] Error 1 (ignored)
>
I have fix it with "make check-nice05".

> > +#define LIMIT_TIME 3
> > +#define RUN_TIMES 1000000
> It might be useful if user could change this value with getopt switch (to
> debug on error to speedup). I also wonder if we hit timeout on some slow
> machine.
>
I have rewrite the check point with new implement.

> Maybe just:
> 	if (pthread_barrier_init(&barrier, NULL, 3) != 0)
> 		tst_brk(TBROK | TERRNO, "pthread_barrier_init() failed");
>

pthread_barrier_init do not set errno when it is failed according to "man 3"


> if (pthread_cancel(nice_high))
> 	tst_brk(TBROK | TERRNO, "pthread_cancel() failed");
It's ok now.


> > +	if (time_nice_low > time_nice_high) {
> > +		tst_res(TPASS, "time_nice_low = %lld time_nice_high = %lld",
> > +			time_nice_low, time_nice_high);
> > +	} else {
> > +		tst_brk(TFAIL | TTERRNO, "Test failed :"
> Wrong space between ':', missing after it, use:
> 		tst_brk(TFAIL | TTERRNO, "Test failed: "
Thanks, it's ok now.


I have rewrite the patch and submit it, please see: https://patchwork.ozlabs.org/project/ltp/patch/20220618014014.224668-1-zhaogongyi@huawei.com/

Best wishes,
Gongyi



> 
> Hi Zhao,
> 
> on first look LGTM, few notes below.
> 
> make check complains about style, could you please fix it?
> 
> $ make check-nice05
> CHECK testcases/kernel/syscalls/nice/nice05.c
> nice05.c:48: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:48: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:53: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:70: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:70: ERROR: "foo* bar" should be "foo *bar"
> nice05.c:75: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:104: WARNING: braces {} are not necessary for single statement
> blocks
> nice05.c:114: WARNING: braces {} are not necessary for single statement
> blocks
> nice05.c:138: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:139: ERROR: "(foo*)" should be "(foo *)"
> nice05.c:148: ERROR: space required before the open parenthesis '('
> nice05.c:154: ERROR: space required before the open parenthesis '('
> make: [../../../../include/mk/rules.mk:56: check-nice05] Error 1 (ignored)
> 
> 
> > Add test verify that the low nice thread execute more cycles than
>                                            ^ executes
> 
> > the high nice thread since the two thread binded on the same cpu.
> 
> > Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
> 
> ...
> > diff --git a/testcases/kernel/syscalls/nice/nice05.c
> > b/testcases/kernel/syscalls/nice/nice05.c
> ...
> > +/*\
> > + * [Description]
> > + *
> > + * 1. Create a high nice thread and a low nice thread, the main
> > + *    thread wake them at the same time
> > + * 2. Both threads run on the same CPU
> > + * 3. Verify that the low nice thread execute more cycles than
>                                          ^ executes
> 
> > + *    the high nice thread
> > + */
> > +
> > +#define _GNU_SOURCE
> > +#include <pthread.h>
> > +#include "tst_test.h"
> > +#include "tst_safe_pthread.h"
> > +
> > +#define LIMIT_TIME 3
> > +#define RUN_TIMES 1000000
> It might be useful if user could change this value with getopt switch (to
> debug on error to speedup). I also wonder if we hit timeout on some slow
> machine.
> 
> ...
> > +static void verify_nice(void)
> > +{
> > +	int ret;
> > +	int nice_inc_high = -1;
> > +	int nice_inc_low = -2;
> > +	pthread_t nice_low, nice_high;
> > +
> > +	ret = pthread_barrier_init(&barrier, NULL, 3);
> > +	if (ret != 0) {
> > +		tst_brk(TBROK, "pthread_barrier_init() returned %s",
> > +			tst_strerrno(-ret));
> > +	}
> Maybe just:
> 	if (pthread_barrier_init(&barrier, NULL, 3) != 0)
> 		tst_brk(TBROK | TERRNO, "pthread_barrier_init() failed");
> 
> Or feel free to use ret if you need it (see below), the point is with tst_brk()
> use TERRNO. Or am I missing something pthread specific?
> 
> > +	SAFE_PTHREAD_CREATE(&nice_high, NULL, nice_high_thread,
> (void*)&nice_inc_high);
> > +	SAFE_PTHREAD_CREATE(&nice_low, NULL, nice_low_thread,
> > +(void*)&nice_inc_low);
> > +
> > +	pthread_barrier_wait(&barrier);
> Not an expert on pthread, but IMHO you should compare
> PTHREAD_BARRIER_SERIAL_THREAD with the result of
> pthread_barrier_wait(), with your current code you compare with the
> result of pthread_barrier_init().
> > +	if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
> > +		tst_brk(TBROK | TERRNO, "pthread_barrier_wait() failed");
> > +
> > +	sleep(LIMIT_TIME);
> > +
> > +	ret = pthread_cancel(nice_high);
> > +	if(ret) {
> > +		tst_brk(TBROK, "pthread_cancel() returned %s",
> > +			tst_strerrno(-ret));
> if (pthread_cancel(nice_high))
> 	tst_brk(TBROK | TERRNO, "pthread_cancel() failed");
> 
> > +	}
> > +
> > +	ret = pthread_cancel(nice_low);
> > +	if(ret) {
> > +		tst_brk(TBROK, "pthread_cancel() returned %s",
> > +			tst_strerrno(-ret));
> and here as well.
> > +	}
> > +
> > +	ret = pthread_barrier_destroy(&barrier);
> > +	if (ret) {
> > +		tst_brk(TBROK, "pthread_barrier_destroy() returned %s",
> > +			tst_strerrno(-ret));
> > +	}
> > +
> > +	SAFE_PTHREAD_JOIN(nice_high, NULL);
> > +	SAFE_PTHREAD_JOIN(nice_low, NULL);
> > +
> > +	if (time_nice_low > time_nice_high) {
> > +		tst_res(TPASS, "time_nice_low = %lld time_nice_high = %lld",
> > +			time_nice_low, time_nice_high);
> > +	} else {
> > +		tst_brk(TFAIL | TTERRNO, "Test failed :"
> Wrong space between ':', missing after it, use:
> 		tst_brk(TFAIL | TTERRNO, "Test failed: "
> 
> 
> Kind regards,
> Petr
> 
> > +			"time_nice_low = %lld time_nice_high = %lld",
> > +			time_nice_low, time_nice_high);
> > +	}
> > +}
> > +
> > +static struct tst_test test = {
> > +	.setup = setup,
> > +	.cleanup = cleanup,
> > +	.test_all = verify_nice,
> > +	.needs_root = 1,
> > +};

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-18  1:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-07  7:36 [LTP] [PATCH] nice05: Add testcase for nice() syscall Zhao Gongyi via ltp
2022-05-27 11:07 ` Petr Vorel
2022-06-18  1:54 zhaogongyi via ltp

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.