Skip to contentSkip to conversation

c/r tests: Add futex c/r tests

PATCH In review Matt Helsley 2 participants 2 messages Jul 6, 2009 1 message not in the archive Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 c/r tests: Add futex c/r tests

Add tests for plain, robust, and pi futexes. Each test sets up a
typical contended futex scenario and then awaits checkpoint. We only test the contended case since the uncontended cases are entirely based on the state of userspace memory. After checkpoint each test verifies that the critical semantics of the futex still works.

For plain futexes we ensure that the same number of tasks that
were asleep on the futex are woken up.

For robust futexes we set the robust list head of each process
and wait for checkpoint. After checkpoint we verify that the kernel still knows about the robust list head then each child exits without releasing the futex. Since the child still holds the futex at exit the kernel wakes another waiting child.

For pi futexes we set up the contended priority inversion case
which is supposed to cause priority inheritance. Then we wait for checkpoint. After checkpoint we verify that the priority is inherited and we also check that the remaining waiters are woken in priority order.

We do not test some variations on these such as private futexes,
bitsets, requeing, and futexes mapped in filesystem files. All of the futexes in these tests are in anonymous shared mappings.

README.txt describes the tests and their log output. run.sh shows
how to run these tests.

---
v2:
Clean up some unnecessary typecasts/differences
Expand the run.sh script to test for some prereqs
Add a section to the runall.sh script
Fixed segfault using patch that immediately followed the last posting. Added slow thread-safe lock around the log output.
Print orginating thread id for each log message, make the INFO, FAIL,
etc. "tags" more formal (separate parameter).
Factored the common logging bits into libfutex.a
Could potentially move into libcrtest.a in the future.
Tested on Intel x86-64 and ppc64
Fix Makefile to be more cross-platform -- only gcc configured to
compile to i386-*-linux seems to have trouble with GCC atomic builtins.

 Makefile                  |    2 
 futex/Makefile            |   44 ++
 futex/README.txt          |   37 ++
 futex/libfutex/Makefile   |   16 +
 futex/libfutex/atomic.h   |   31 ++
 futex/libfutex/libfutex.c |   25 +
 futex/libfutex/libfutex.h |  101 ++++++
 futex/pi.c                |  699 ++++++++++++++++++++++++++++++++++++++++++++++
 futex/plain.c             |  183 ++++++++++++
 futex/robust.c            |  400 ++++++++++++++++++++++++++
 futex/run.sh              |   59 +++
 runall.sh                 |    8 
 12 files changed, 1604 insertions(+), 1 deletion(-)
Makefile +1 -1
index ceba676..cf88ed1 100644
@@ -1,5 +1,5 @@
 SUBDIRS = libcrtest counterloop fileio simple cr-ipc-test userns ipc \
-	  sleep process-tree
+	  sleep process-tree futex
 
 targets = ns_exec
 
futex/Makefile new file +44 -0

44 lines not on this page · raw message

futex/README.txt new file +37 -0
new file mode 100644
index 0000000..bba03bc
@@ -0,0 +1,37 @@
+Futexes are synchronization primitives which optimize the non-contended case and
+arbitrate the contended case via the kernel. Furthermore, somewhat like undo
+lists that manage semaphores when a task exits, the robust futex list helps
+clean up futexes on exit. Finally, to ensure better realtime response there are 
+priority-inheritance (pi) futexes.
+
+The non-contended plain futex case is uninteresting as it simply involves
+atomically incrementing/decrementing a value. Similarly, robust futexes and pi
+futexes have uninteresting non-contended cases. Unlike plain futexes, these set
+the futex value to be the thread id.
+
+These tests are designed to trigger the contended cases. We can do this
+by carefully setting the initial value of plain futexes, by setting
+the initial tid for robust futexes, and by waiting on a plain futex before
+trying to grab the pi futex.
+
+Not all architectures support robust and priority-inheritance futexes
+because they require futex_atomic_cmpxchg_inatomic. Known-good archs:
+	x86, x86_64, sparc64, sh, s390, powerpc
+
+The checkpoint/restart portions of this test require kernel versions
+2.6.XX or higher.
+
+Log lines begin with "INFO:", "WARN:", "PASS:", or "FAIL:".
+
+"FAIL:" in any log indicates a failure of the test. Failure is propagated
+        via exit codes to the main thread which reports failures via its exit
+	code.
+
+"INFO:" Usually indicates what step is about to be taken. It often includes
+        specific details such as process ids, futex operations, etc.
+
+"WARN:" Is an unusual condition that doesn't indicate an error but
+        which the test was designed to avoid.
+
+"PASS:" Indicates that part of the test passed. Check the exit code for
+        a summary of PASS/FAIL.
futex/libfutex/Makefile new file +16 -0
new file mode 100644
index 0000000..379e62c
@@ -0,0 +1,16 @@
+SRCS := $(wildcard *.c)
+OBJS := $(SRCS:%.c=%.o)
+
+CFLAGS += -I./ # LTP -I../../../../include
+
+TARGET := libfutex.a
+
+all: $(TARGET)
+
+libfutex.a: $(OBJS)
+	$(AR) -cr $@ libfutex.o
+
+clean:
+	rm -f $(TARGET) $(OBJS)
+
+install:
futex/libfutex/atomic.h new file +31 -0
new file mode 100644
index 0000000..f82b7de
@@ -0,0 +1,31 @@
+#ifndef _ASM_GENERIC_ATOMIC_H_
+#define _ASM_GENERIC_ATOMIC_H_
+/*
+ * Implement the Linux Kernel's atomic_t type in userspace based on:
+ * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
+ */
+
+typedef struct {
+	volatile int counter;
+} atomic_t;
+
+static inline int atomic_read(atomic_t *v)
+{
+	return v->counter;
+}
+
+static inline void atomic_set(atomic_t *v, int val)
+{
+	v->counter = val;
+}
+
+static inline void atomic_inc(atomic_t *v)
+{
+	__sync_add_and_fetch(&v->counter, 1);
+}
+
+static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
+{
+	return __sync_val_compare_and_swap(&v->counter, old, new);
+}
+#endif /* _ASM_GENERIC_ATOMIC_H_ */
futex/libfutex/libfutex.c new file +25 -0
new file mode 100644
index 0000000..4b1872b
@@ -0,0 +1,25 @@
+#include <stdlib.h>
+#include <sys/mman.h>
+#include "libfutex.h"
+
+void *alloc_futex_mem(size_t sz)
+{
+	void *p;
+	size_t pagesize = sysconf(_SC_PAGE_SIZE);
+	int rc;
+
+	if (pagesize == -1)
+		return NULL;
+
+	rc = posix_memalign(&p, pagesize, sz);
+	if (rc != 0) {
+		errno = rc;
+		return NULL;
+	}
+
+	rc = mprotect(p, sz, PROT_READ|PROT_WRITE|PROT_SEM);
+	if (rc == 0)
+		return p;
+	free(p);
+	return NULL;
+}
futex/libfutex/libfutex.h new file +101 -0

101 lines not on this page · raw message

futex/pi.c new file +699 -0

699 lines not on this page · raw message

futex/plain.c new file +183 -0

183 lines not on this page · raw message

futex/robust.c new file +400 -0

400 lines not on this page · raw message

futex/run.sh new file +59 -0

59 lines not on this page · raw message

runall.sh +8 -0
index 8700c3b..231e720 100644
@@ -107,4 +107,12 @@ if [ $? -ne 0 ]; then
 fi
 popd
 
+echo Running futex tests
+pushd futex
+bash run.sh
+if [ $? -ne 0 ]; then
+	echo FAIL
+	exit 8
+fi
+popd
 exit 0
Signature · 1 line
1.5.6.3

A message that is not in the archive (sent off-list or not yet received): 1246910097-30792-1-git-send-email-matthltc-r/[email protected]

#2 lore raw
Quoted 5 lines · Matt Helsley

Add tests for plain, robust, and pi futexes. Each test sets up a
typical contended futex scenario and then awaits checkpoint. We only test the contended case since the uncontended cases are entirely based on the state of userspace memory. After checkpoint each test verifies that the critical semantics of the futex still works.

Thanks, Matt, these now compile and run on s390.

Quoted 2 lines

For plain futexes we ensure that the same number of tasks that
were asleep on the futex are woken up.

But plain always returns 0 (success)?

Quoted 5 lines

For robust futexes we set the robust list head of each process
and wait for checkpoint. After checkpoint we verify that the kernel still knows about the robust list head then each child exits without releasing the futex. Since the child still holds the futex at exit the kernel wakes another waiting child.

run.sh doesn't check for the return value. (trivially fixed as bash wait returns its return value)

I think you also have several places where you log a FAIL when you should probably log BROK. But I've applied this to cr_tests plus a fix for run.sh to check return values. Can you send me a fix to have plain.c log failures (unless I'm misreading and its all fine)?

thanks,
-serge