Skip to contentSkip to conversation

Add TST_THREAD_STATE_WAIT macro

PATCH Superseded Andrea Cervesato 2 participants 3 messages Feb 9, 2022 – Feb 10, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/3 Add TST_THREAD_STATE_WAIT macro

The TST_THREAD_STATE_WAIT macro can be used to wait and check for pthread state changes.

---
In v5 TST_THREAD_STATE_WAIT has been added in the tst_process_state.h header.

 include/tst_process_state.h | 19 +++++++++++++++++++
 lib/tst_thread_state.c      | 38 +++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 lib/tst_thread_state.c
include/tst_process_state.h +19 -0
index 81532524a..b1d83e109 100644
@@ -36,6 +36,25 @@
 #define TST_PROCESS_EXIT_WAIT(pid, msec_timeout) \
 	tst_process_exit_wait((pid), (msec_timeout))
 
+/*
+ * Waits for thread state change.
+ *
+ * The state is one of the following:
+ *
+ * R - running
+ * S - sleeping
+ * D - disk sleep
+ * T - stopped
+ * t - tracing stopped
+ * Z - zombie
+ * X - dead
+ */
+#define TST_THREAD_STATE_WAIT(tid, state, msec_timeout) \
+	tst_thread_state_wait((tid), (state), (msec_timeout))
+
+int tst_thread_state_wait(pid_t tid, const char state,
+				unsigned int msec_timeout);
+
 #else
 /*
  * The same as above but does not use tst_brkm() interface.
lib/tst_thread_state.c new file +38 -0
new file mode 100644
index 000000000..6562dfaf0
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "tst_safe_file_ops.h"
+#include "tst_process_state.h"
+
+int tst_thread_state_wait(pid_t tid, const char state,
+			  unsigned int msec_timeout)
+{
+	char proc_path[128], cur_state;
+	unsigned int msecs = 0;
+
+	snprintf(proc_path, sizeof(proc_path), "/proc/self/task/%i/stat", tid);
+
+	for (;;) {
+		SAFE_FILE_SCANF(proc_path, "%*i %*s %c", &cur_state);
+
+		if (state == cur_state)
+			break;
+
+		usleep(1000);
+		msecs += 1;
+
+		if (msec_timeout && msecs >= msec_timeout) {
+			errno = ETIMEDOUT;
+			return -1;
+		}
+	}
+
+	return 0;
+}
Signature · 5 lines
2.35.1


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

PATCH v5 3/3 Add futex_waitv testing suite

---
In v5 there's a better approch to futex_waitv testing. futex_variant() has been kept as function and added FUTEX_VARIANTS for counting the test variants.

 testcases/kernel/syscalls/futex/.gitignore    |   3 +
 testcases/kernel/syscalls/futex/Makefile      |  14 ++-
 testcases/kernel/syscalls/futex/futex2test.h  |  30 +++++
 testcases/kernel/syscalls/futex/futex_utils.h |  38 +++---
 .../kernel/syscalls/futex/futex_waitv01.c     | 119 ++++++++++++++++++
 .../kernel/syscalls/futex/futex_waitv02.c     | 106 ++++++++++++++++
 .../kernel/syscalls/futex/futex_waitv03.c     | 119 ++++++++++++++++++
 testcases/kernel/syscalls/futex/futextest.h   |  47 +------
 8 files changed, 415 insertions(+), 61 deletions(-)
 create mode 100644 testcases/kernel/syscalls/futex/futex2test.h
 create mode 100644 testcases/kernel/syscalls/futex/futex_waitv01.c
 create mode 100644 testcases/kernel/syscalls/futex/futex_waitv02.c
 create mode 100644 testcases/kernel/syscalls/futex/futex_waitv03.c
testcases/kernel/syscalls/futex/.gitignore +3 -0
index 54cd02b02..9d08ba7d3 100644
@@ -10,3 +10,6 @@
 /futex_wake02
 /futex_wake03
 /futex_wake04
+/futex_waitv01
+/futex_waitv02
+/futex_waitv03
testcases/kernel/syscalls/futex/Makefile +12 -2
index 5713c615d..7228496bc 100644
@@ -3,8 +3,18 @@
 
 top_srcdir		?= ../../../..
 
-futex_cmp_requeue01 futex_cmp_requeue02 futex_wait02 futex_wake03 futex_wait05 futex_wait_bitset01: LDLIBS += -lrt
-futex_wait03 futex_wake02 futex_wake04: CFLAGS += -pthread
+futex_cmp_requeue01: LDLIBS+=-lrt
+futex_cmp_requeue02: LDLIBS+=-lrt
+futex_wait02: LDLIBS+=-lrt
+futex_wake03: LDLIBS+=-lrt
+futex_wait05: LDLIBS+=-lrt
+futex_wait_bitset01: LDLIBS+=-lrt
+
+futex_wait03: CFLAGS+=-pthread
+futex_wake02: CFLAGS+=-pthread
+futex_wake04: CFLAGS+=-pthread
+futex_waitv02: CFLAGS+=-pthread
+futex_waitv03: CFLAGS+=-pthread
 
 include $(top_srcdir)/include/mk/testcases.mk
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/futex/futex2test.h new file +30 -0
new file mode 100644
index 000000000..ce97f47c1
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Futex2 library addons for futex tests
+ *
+ * Copyright 2021 Collabora Ltd.
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+#ifndef FUTEX2TEST_H
+#define FUTEX2TEST_H
+
+#include <stdint.h>
+#include "lapi/syscalls.h"
+#include "futextest.h"
+
+/**
+ * futex_waitv - Wait at multiple futexes, wake on any
+ * @waiters:    Array of waiters
+ * @nr_waiters: Length of waiters array
+ * @flags: Operation flags
+ * @timo:  Optional timeout for operation
+ */
+static inline int futex_waitv(volatile struct futex_waitv *waiters,
+			      unsigned long nr_waiters, unsigned long flags,
+			      struct timespec *timo, clockid_t clockid)
+{
+	return tst_syscall(__NR_futex_waitv, waiters, nr_waiters, flags, timo, clockid);
+}
+
+#endif /* _FUTEX2TEST_H */
testcases/kernel/syscalls/futex/futex_utils.h +23 -15
index 156895efb..7ce13e627 100644
@@ -1,20 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (C) 2015 Cyril Hrubis <[email protected]>
- *
- * Licensed under the GNU GPLv2 or later.
- * 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, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
  */
 
 #ifndef FUTEX_UTILS_H__
@@ -23,6 +10,27 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#if __NR_futex != __LTP__NR_INVALID_SYSCALL && __NR_futex_time64 != __LTP__NR_INVALID_SYSCALL
+# define FUTEX_VARIANTS 2
+#else
+# define FUTEX_VARIANTS 1
+#endif
+
+static inline struct futex_test_variants futex_variant(void)
+{
+	struct futex_test_variants variants[] = {
+	#if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
+		{ .fntype = FUTEX_FN_FUTEX, .desc = "syscall with old kernel spec" },
+	#endif
+
+	#if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
+		{ .fntype = FUTEX_FN_FUTEX64, .desc = "syscall time64 with kernel spec" },
+	#endif
+	};
+
+	return variants[tst_variant];
+}
+
 /*
  * Wait for nr_threads to be sleeping
  */
testcases/kernel/syscalls/futex/futex_waitv01.c new file +119 -0

119 lines not on this page · raw message

testcases/kernel/syscalls/futex/futex_waitv02.c new file +106 -0

106 lines not on this page · raw message

testcases/kernel/syscalls/futex/futex_waitv03.c new file +119 -0

119 lines not on this page · raw message

testcases/kernel/syscalls/futex/futextest.h +3 -44

65 lines not on this page · raw message

Signature · 5 lines
2.35.1


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

Re: [LTP] [PATCH v5 3/3] Add futex_waitv testing suite

Hi!
I've fixed a few things in this patch and pushed the whole patchset, thanks.

Full diff:

testcases/kernel/syscalls/futex/futex_waitv02.c +12 -12
index 2897f6c78..0a0e2b620 100644
@@ -37,10 +37,10 @@ static void setup(void)
 	if (tst_parse_int(str_numfutex, &numfutex, 1, FUTEX_WAITV_MAX))
 		tst_brk(TBROK, "Invalid number of futexes '%s'", str_numfutex);
 
-	futexes = SAFE_MALLOC(sizeof(uint32_t) * numfutex);
+	futexes = tst_alloc(sizeof(uint32_t) * numfutex);
 	memset(futexes, FUTEX_INITIALIZER, sizeof(uint32_t) * numfutex);
 
-	waitv = SAFE_MALLOC(sizeof(struct futex_waitv) * numfutex);
+	waitv = tst_alloc(sizeof(struct futex_waitv) * numfutex);
 	memset(waitv, 0, sizeof(struct futex_waitv) * numfutex);
 
 	for (i = 0; i < numfutex; i++) {
@@ -60,8 +60,8 @@ static void *threaded(void *arg)
 	TEST(futex_wake(tv.fntype, (void *)(uintptr_t)waitv[numfutex - 1].uaddr,
 			1, FUTEX_PRIVATE_FLAG));
 	if (TST_RET < 0) {
-		tst_brk(TBROK, "futex_wake private returned: %ld %s", TST_RET,
-			tst_strerrno(TST_ERR));
+		tst_brk(TBROK | TTERRNO,
+			"futex_wake private returned: %ld", TST_RET);
 	}
 
 	return NULL;
@@ -70,7 +70,7 @@ static void *threaded(void *arg)
 static void run(void)
 {
 	struct timespec to;
-	int ret, tid;
+	int tid;
 	pthread_t t;
 
 	tid = tst_syscall(__NR_gettid);
@@ -81,13 +81,13 @@ static void run(void)
 	SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC, &to);
 	to.tv_sec++;
 
-	ret = futex_waitv(waitv, numfutex, 0, &to, CLOCK_MONOTONIC);
-	if (ret < 0)
-		tst_brk(TBROK, "futex_waitv returned: %d %s", ret,
-			tst_strerrno(-ret));
-	else if (ret != numfutex - 1)
-		tst_res(TFAIL, "futex_waitv returned: %d, expecting %d", ret,
-			numfutex - 1);
+	TEST(futex_waitv(waitv, numfutex, 0, &to, CLOCK_MONOTONIC));
+	if (TST_RET < 0) {
+		tst_brk(TBROK | TTERRNO, "futex_waitv returned: %ld", TST_RET);
+	} else if (TST_RET != numfutex - 1) {
+		tst_res(TFAIL, "futex_waitv returned: %ld, expecting %d",
+			TST_RET,  numfutex - 1);
+	}
 
 	SAFE_PTHREAD_JOIN(t, NULL);
 	tst_res(TPASS, "futex_waitv returned correctly");
testcases/kernel/syscalls/futex/futex_waitv03.c +7 -7
index 773cc5af3..d8e39c76a 100644
@@ -39,7 +39,7 @@ static void setup(void)
 	if (tst_parse_int(str_numfutex, &numfutex, 1, FUTEX_WAITV_MAX))
 		tst_brk(TBROK, "Invalid number of futexes '%s'", str_numfutex);
 
-	waitv = SAFE_MALLOC(sizeof(struct futex_waitv) * numfutex);
+	waitv = tst_alloc(sizeof(struct futex_waitv) * numfutex);
 	memset(waitv, 0, sizeof(struct futex_waitv) * numfutex);
 
 	for (i = 0; i < numfutex; i++) {
@@ -72,8 +72,8 @@ static void *threaded(void *arg)
 	TEST(futex_wake(tv.fntype, (void *)(uintptr_t)waitv[numfutex - 1].uaddr,
 			1, 0));
 	if (TST_RET < 0) {
-		tst_brk(TBROK, "futex_wake private returned: %ld %s", TST_RET,
-			tst_strerrno(TST_ERR));
+		tst_brk(TBROK | TTERRNO,
+			"futex_wake private returned: %ld", TST_RET);
 	}
 
 	return NULL;
@@ -94,12 +94,12 @@ static void run(void)
 	to.tv_sec++;
 
 	TEST(futex_waitv(waitv, numfutex, 0, &to, CLOCK_MONOTONIC));
-	if (TST_RET < 0)
-		tst_brk(TBROK, "futex_waitv returned: %ld %s", TST_RET,
-			tst_strerrno(TST_ERR));
-	else if (TST_RET != numfutex - 1)
+	if (TST_RET < 0) {
+		tst_brk(TBROK | TTERRNO, "futex_waitv returned: %ld", TST_RET);
+	} else if (TST_RET != numfutex - 1) {
 		tst_res(TFAIL, "futex_waitv returned: %ld, expecting %d",
 			TST_RET, numfutex - 1);
+	}
 
 	SAFE_PTHREAD_JOIN(t, NULL);
 	tst_res(TPASS, "futex_waitv returned correctly");
Signature · 5 lines
Cyril Hrubis
[email protected]

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