Skip to contentSkip to conversation

starvation.c: New case for sched starvation

PATCH Superseded Wei Gao via ltp 3 participants 7 messages Mar 21, 2023 – Jul 13, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 starvation.c: New case for sched starvation

Add scheduller thread starvation test case base following link:
https://lwn.net/ml/linux-kernel/[email protected]/

---
 runtest/sched                                 |  1 +
 .../kernel/sched/cfs-scheduler/.gitignore     |  1 +
 .../kernel/sched/cfs-scheduler/starvation.c   | 97 +++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100644 testcases/kernel/sched/cfs-scheduler/starvation.c
runtest/sched +1 -0
index 592898723..172fe4174 100644
@@ -9,6 +9,7 @@ trace_sched01		trace_sched -c 1
 cfs_bandwidth01 cfs_bandwidth01 -i 5
 hackbench01 hackbench 50 process 1000
 hackbench02 hackbench 20 thread 1000
+starvation starvation
 
 sched_cli_serv run_sched_cliserv.sh
 # Run this stress test for 2 minutes
testcases/kernel/sched/cfs-scheduler/.gitignore +1 -0
index c5dacd6ef..e86178f80 100644
@@ -1,2 +1,3 @@
 /hackbench
 cfs_bandwidth01
+/starvation
testcases/kernel/sched/cfs-scheduler/starvation.c new file +97 -0

97 lines not on this page · raw message

Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Wei,

could you please fix warnings? Both should be trivial.

starvation.c: In function ‘child’:
starvation.c:70:1: warning: no return statement in function returning non-void [-Wreturn-type]
70 | }

      | ^

In file included from ../../../../include/tst_test.h:110,
from starvation.c:22:
starvation.c: In function ‘do_test’:
../../../../include/tst_safe_macros.h:493:51: warning: passing argument 4 of ‘safe_signal’ from incompatible pointer type [-Wincompatible-pointer-types]

  493 |         safe_signal(__FILE__, __LINE__, (signum), (handler))
      |                                                   ^~~~~~~~~
      |                                                   |
      |                                                   void (*)(void)
starvation.c:81:9: note: in expansion of macro ‘SAFE_SIGNAL’
   81 |         SAFE_SIGNAL(SIGUSR1, handler);
      |         ^~~~~~~~~~~
../../../../include/tst_safe_macros.h:490:34: note: expected ‘sighandler_t’ but argument is of type ‘void (*)(void)’
  490 |         int signum, sighandler_t handler);
      |                     ~~~~~~~~~~~~~^~~~~~~

make check-starvation
make -C "/home/pevik/install/src/ltp.git/tools/sparse" all
...
CHECK testcases/kernel/sched/cfs-scheduler/starvation.c starvation.c:80:9: warning: incorrect type in argument 4 (different argument counts)
starvation.c:80:9: expected void ( *[usertype] handler )( ... )
starvation.c:80:9: got void ( * )( ... )

Quoted 4 lines

Add scheduller thread starvation test case base following link:
https://lwn.net/ml/linux-kernel/[email protected]/

nit: we should use lore, not lwn:

https://lore.kernel.org/lkml/[email protected]/

...

testcases/kernel/sched/cfs-scheduler/starvation.c line 2

Quoted 4 lines

+++ b/testcases/kernel/sched/cfs-scheduler/starvation.c

testcases/kernel/sched/cfs-scheduler/starvation.c new file +2 -0
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright 2023 Mike Galbraith <efault-AT-gmx.de> */

You can add your credit for porting to LTP :).

testcases/kernel/sched/cfs-scheduler/starvation.c line 9

Quoted 7 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +7 -0
+/*\
+ *
+ * [Description]
+ *
+ * Thread starvation test.
+ * This case copy from following link:
+ * https://lwn.net/ml/linux-kernel/[email protected]/

https://lore.kernel.org/lkml/[email protected]/

Review line 10

Quoted 1 line
(quoted diff) +1 -0
+ *

Please avoid extra blank line here.

Quoted 4 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +4 -0
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>

testcases/kernel/sched/cfs-scheduler/starvation.c line 24

Quoted 10 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +10 -0
+#include <signal.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <sched.h>
+
+#include "tst_test.h"
+
+static unsigned long loop = 10000000;

It'd be nice to have option to run less iterations.

Quoted 9 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +9 -0
+
+static int wait_for_pid(pid_t pid)
+{
+	int status, ret;
+
+again:
+	ret = waitpid(pid, &status, 0);
+	if (ret == -1) {
+		if (errno == EINTR)

testcases/kernel/sched/cfs-scheduler/starvation.c line 43

Quoted 10 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +10 -0
+			goto again;
+
+		return -1;
+	}
+
+	if (WIFSIGNALED(status))
+		return 0;
+
+	return -1;
+}

I'm not sure by this part. I'd be good if somebody else reviewed it. We cannot use tst_reap_children(), because we want to be signaled.

Quoted 16 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +16 -0
+
+static void setup(void)
+{
+	cpu_set_t mask;
+
+	CPU_ZERO(&mask);
+
+	CPU_SET(0, &mask);
+
+	TST_EXP_POSITIVE(sched_setaffinity(0, sizeof(mask), &mask));
+}
+
+static void handler(void)
+{
+	if (loop > 0)
+		--loop;

testcases/kernel/sched/cfs-scheduler/starvation.c line 69

Quoted 10 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +10 -0
+}
+
+static int child(void)
+{
+	pid_t ppid = getppid();
+
+	TST_CHECKPOINT_WAIT(0);
+
+	while (1)
+		kill(ppid, SIGUSR1);

SAFE_KILL(ppid, SIGUSR1) ?

Quoted 17 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +17 -0
+}
+
+static void do_test(void)
+{
+	pid_t child_pid;
+
+	child_pid = SAFE_FORK();
+
+	if (!child_pid)
+		child();
+
+	SAFE_SIGNAL(SIGUSR1, handler);
+	TST_CHECKPOINT_WAKE(0);
+
+	while (loop)
+		sleep(1);
+

testcases/kernel/sched/cfs-scheduler/starvation.c line 96

Quoted 10 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +10 -0
+	kill(child_pid, SIGTERM);
+	TST_EXP_PASS(wait_for_pid(child_pid));
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+	.setup = setup,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+	.max_runtime = 120,

Are you sure 2 min is enough? Maybe we need to use tst_remaining_runtime() to check if we're not running out of time.
Also, if we set getopt to choose number of options, we'd need to adjust it by tst_set_max_runtime().

Kind regards,
Petr

Quoted 1 line
testcases/kernel/sched/cfs-scheduler/starvation.c new file +1 -0
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 10 lines · Petr Vorel

Hi Wei,

testcases/kernel/sched/cfs-scheduler/starvation.c line 96

Quoted 6 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +6 -0
+static struct tst_test test = {
+	.test_all = do_test,
+	.setup = setup,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+	.max_runtime = 120,

Are you sure 2 min is enough? Maybe we need to use tst_remaining_runtime() to check if we're not running out of time.

Seems difficult to check with tst_remaining_runtime since i can use code such as:
while(tst_remaining_runtime())

	wait_for_pid();
Quoted 7 lines

Also, if we set getopt to choose number of options, we'd need to adjust it by tst_set_max_runtime().

Kind regards,
Petr

Quoted 1 line

+};

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 12 lines · Petr Vorel
Quoted 10 lines

Hi Wei,

testcases/kernel/sched/cfs-scheduler/starvation.c line 96

Quoted 6 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +6 -0
+static struct tst_test test = {
+	.test_all = do_test,
+	.setup = setup,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+	.max_runtime = 120,

Are you sure 2 min is enough? Maybe we need to use tst_remaining_runtime() to check if we're not running out of time.

Quoted 3 lines

Seems difficult to check with tst_remaining_runtime since i can use code such as:
while(tst_remaining_runtime())

	wait_for_pid();

If you don't know how to use tst_remaining_runtime() how about getting inspiration from lib/newlib_tests/test_runtime01.c ?

static void run(void)

{
	int runtime;

	tst_res(TINFO, "...");

	do {
		runtime = tst_remaining_runtime();
		tst_res(TINFO, "Remaining runtime %d", runtime);
		sleep(1);
	} while (runtime);

	tst_res(TPASS, "Test passed");
}
static struct tst_test test = {
	.test_all = run,
	.max_runtime = 120,
};

=> Also, it's a question, how long should the test be running. Is it worth to be running for 2 mins? It's kind of stress test, right? But even that wouldn't be 30 sec enough?

Kind regards,
Petr

Quoted 7 lines
Quoted 7 lines

Also, if we set getopt to choose number of options, we'd need to adjust it by tst_set_max_runtime().

Kind regards,
Petr

Quoted 1 line

+};

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 12 lines · Petr Vorel
Quoted 11 lines · Petr Vorel
Quoted 10 lines

Hi Wei,

testcases/kernel/sched/cfs-scheduler/starvation.c line 96

Quoted 6 lines
testcases/kernel/sched/cfs-scheduler/starvation.c new file +6 -0
+static struct tst_test test = {
+	.test_all = do_test,
+	.setup = setup,
+	.forks_child = 1,
+	.needs_checkpoints = 1,
+	.max_runtime = 120,

Are you sure 2 min is enough? Maybe we need to use tst_remaining_runtime() to check if we're not running out of time.

Quoted 6 lines
Quoted 3 lines

Seems difficult to check with tst_remaining_runtime since i can use code such as:
while(tst_remaining_runtime())

	wait_for_pid();

If you don't know how to use tst_remaining_runtime() how about getting inspiration from lib/newlib_tests/test_runtime01.c ?

What i mean is i can not use while or do loop to check tst_remaining_runtime, since the last function wait_for_pid() already hold(block) the current process and you do not need and can not use tst_remaining_runtime(). Sorry if any misunderstood.

Quoted 16 lines

static void run(void)

{
	int runtime;

	tst_res(TINFO, "...");

	do {
		runtime = tst_remaining_runtime();
		tst_res(TINFO, "Remaining runtime %d", runtime);
		sleep(1);
	} while (runtime);

	tst_res(TPASS, "Test passed");
}
Quoted 8 lines
static struct tst_test test = {
	.test_all = run,
	.max_runtime = 120,
};

=> Also, it's a question, how long should the test be running. Is it worth to be running for 2 mins? It's kind of stress test, right? But even that wouldn't be 30 sec enough?

Normally the default value which i set will spend ~30s i test in virtual machine. I suppose 2mins max_runtime should be safe enough.
Yes it can be taken as stress test, you need a big loop number to let test run.

In latest v2 patch the number of loop and max_runtime both configurable, so if your machine is too slow maybe you need adjust parameter.

Quoted 11 lines

Kind regards,
Petr

Quoted 7 lines
Quoted 7 lines

Also, if we set getopt to choose number of options, we'd need to adjust it by tst_set_max_runtime().

Kind regards,
Petr

Quoted 1 line

+};

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH v2 1/1 starvation.c: New case for sched starvation

In reply to Wei Gao via ltp

Add scheduller thread starvation test case base following link:
https://lwn.net/ml/linux-kernel/[email protected]/

---
 runtest/sched                                 |   1 +
 .../kernel/sched/cfs-scheduler/.gitignore     |   1 +
 .../kernel/sched/cfs-scheduler/starvation.c   | 112 ++++++++++++++++++
 3 files changed, 114 insertions(+)
 create mode 100644 testcases/kernel/sched/cfs-scheduler/starvation.c
runtest/sched +1 -0
index 592898723..172fe4174 100644
@@ -9,6 +9,7 @@ trace_sched01		trace_sched -c 1
 cfs_bandwidth01 cfs_bandwidth01 -i 5
 hackbench01 hackbench 50 process 1000
 hackbench02 hackbench 20 thread 1000
+starvation starvation
 
 sched_cli_serv run_sched_cliserv.sh
 # Run this stress test for 2 minutes
testcases/kernel/sched/cfs-scheduler/.gitignore +1 -0
index c5dacd6ef..e86178f80 100644
@@ -1,2 +1,3 @@
 /hackbench
 cfs_bandwidth01
+/starvation
testcases/kernel/sched/cfs-scheduler/starvation.c new file +112 -0

112 lines not on this page · raw message

Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi!

Quoted 1 line

+static long loop = 10000000;

I've reduced the default number of loops to 2000000 since the test runtime on small ARM board was quite close to the timeout. Given that when the scheduller was broken the original reproducer haven't finished after running for 40 times longer than on working kernel, 2000000 should be enough to trip the timeout even on fairly fast machine.

Also added note to the test description that the test is expected to timeout when scheduller is broken.

And pushed, thanks.

Signature · 5 lines
Cyril Hrubis
[email protected]

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