Skip to content

Commit e5c04d0

Browse files
andrealmeidKAGA-KOKO
authored andcommitted
selftests/futex: Refactor futex_wait with kselftest_harness.h
To reduce the boilerplate code, refactor futex_wait test to use kselftest_harness header instead of futex's logging header. Signed-off-by: André Almeida <andrealmeid@igalia.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 14d016b commit e5c04d0

2 files changed

Lines changed: 39 additions & 66 deletions

File tree

tools/testing/selftests/futex/functional/futex_wait.c

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,16 @@
99
#include <sys/shm.h>
1010
#include <sys/mman.h>
1111
#include <fcntl.h>
12-
#include "logging.h"
12+
1313
#include "futextest.h"
14+
#include "../../kselftest_harness.h"
1415

15-
#define TEST_NAME "futex-wait"
1616
#define timeout_ns 30000000
1717
#define WAKE_WAIT_US 10000
1818
#define SHM_PATH "futex_shm_file"
1919

2020
void *futex;
2121

22-
void usage(char *prog)
23-
{
24-
printf("Usage: %s\n", prog);
25-
printf(" -c Use color\n");
26-
printf(" -h Display this help message\n");
27-
printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
28-
VQUIET, VCRITICAL, VINFO);
29-
}
30-
3122
static void *waiterfn(void *arg)
3223
{
3324
struct timespec to;
@@ -45,53 +36,37 @@ static void *waiterfn(void *arg)
4536
return NULL;
4637
}
4738

48-
int main(int argc, char *argv[])
39+
TEST(private_futex)
4940
{
50-
int res, ret = RET_PASS, fd, c, shm_id;
51-
u_int32_t f_private = 0, *shared_data;
5241
unsigned int flags = FUTEX_PRIVATE_FLAG;
42+
u_int32_t f_private = 0;
5343
pthread_t waiter;
54-
void *shm;
44+
int res;
5545

5646
futex = &f_private;
5747

58-
while ((c = getopt(argc, argv, "cht:v:")) != -1) {
59-
switch (c) {
60-
case 'c':
61-
log_color(1);
62-
break;
63-
case 'h':
64-
usage(basename(argv[0]));
65-
exit(0);
66-
case 'v':
67-
log_verbosity(atoi(optarg));
68-
break;
69-
default:
70-
usage(basename(argv[0]));
71-
exit(1);
72-
}
73-
}
74-
75-
ksft_print_header();
76-
ksft_set_plan(3);
77-
ksft_print_msg("%s: Test futex_wait\n", basename(argv[0]));
78-
7948
/* Testing a private futex */
80-
info("Calling private futex_wait on futex: %p\n", futex);
49+
ksft_print_dbg_msg("Calling private futex_wait on futex: %p\n", futex);
8150
if (pthread_create(&waiter, NULL, waiterfn, (void *) &flags))
82-
error("pthread_create failed\n", errno);
51+
ksft_exit_fail_msg("pthread_create failed\n");
8352

8453
usleep(WAKE_WAIT_US);
8554

86-
info("Calling private futex_wake on futex: %p\n", futex);
55+
ksft_print_dbg_msg("Calling private futex_wake on futex: %p\n", futex);
8756
res = futex_wake(futex, 1, FUTEX_PRIVATE_FLAG);
8857
if (res != 1) {
8958
ksft_test_result_fail("futex_wake private returned: %d %s\n",
9059
errno, strerror(errno));
91-
ret = RET_FAIL;
9260
} else {
9361
ksft_test_result_pass("futex_wake private succeeds\n");
9462
}
63+
}
64+
65+
TEST(anon_page)
66+
{
67+
u_int32_t *shared_data;
68+
pthread_t waiter;
69+
int res, shm_id;
9570

9671
/* Testing an anon page shared memory */
9772
shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
@@ -105,67 +80,65 @@ int main(int argc, char *argv[])
10580
*shared_data = 0;
10681
futex = shared_data;
10782

108-
info("Calling shared (page anon) futex_wait on futex: %p\n", futex);
83+
ksft_print_dbg_msg("Calling shared (page anon) futex_wait on futex: %p\n", futex);
10984
if (pthread_create(&waiter, NULL, waiterfn, NULL))
110-
error("pthread_create failed\n", errno);
85+
ksft_exit_fail_msg("pthread_create failed\n");
11186

11287
usleep(WAKE_WAIT_US);
11388

114-
info("Calling shared (page anon) futex_wake on futex: %p\n", futex);
89+
ksft_print_dbg_msg("Calling shared (page anon) futex_wake on futex: %p\n", futex);
11590
res = futex_wake(futex, 1, 0);
11691
if (res != 1) {
11792
ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",
11893
errno, strerror(errno));
119-
ret = RET_FAIL;
12094
} else {
12195
ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");
12296
}
12397

98+
shmdt(shared_data);
99+
}
100+
101+
TEST(file_backed)
102+
{
103+
u_int32_t f_private = 0;
104+
pthread_t waiter;
105+
int res, fd;
106+
void *shm;
124107

125108
/* Testing a file backed shared memory */
126109
fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
127-
if (fd < 0) {
128-
perror("open");
129-
exit(1);
130-
}
110+
if (fd < 0)
111+
ksft_exit_fail_msg("open");
131112

132-
if (ftruncate(fd, sizeof(f_private))) {
133-
perror("ftruncate");
134-
exit(1);
135-
}
113+
if (ftruncate(fd, sizeof(f_private)))
114+
ksft_exit_fail_msg("ftruncate");
136115

137116
shm = mmap(NULL, sizeof(f_private), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
138-
if (shm == MAP_FAILED) {
139-
perror("mmap");
140-
exit(1);
141-
}
117+
if (shm == MAP_FAILED)
118+
ksft_exit_fail_msg("mmap");
142119

143120
memcpy(shm, &f_private, sizeof(f_private));
144121

145122
futex = shm;
146123

147-
info("Calling shared (file backed) futex_wait on futex: %p\n", futex);
124+
ksft_print_dbg_msg("Calling shared (file backed) futex_wait on futex: %p\n", futex);
148125
if (pthread_create(&waiter, NULL, waiterfn, NULL))
149-
error("pthread_create failed\n", errno);
126+
ksft_exit_fail_msg("pthread_create failed\n");
150127

151128
usleep(WAKE_WAIT_US);
152129

153-
info("Calling shared (file backed) futex_wake on futex: %p\n", futex);
130+
ksft_print_dbg_msg("Calling shared (file backed) futex_wake on futex: %p\n", futex);
154131
res = futex_wake(shm, 1, 0);
155132
if (res != 1) {
156133
ksft_test_result_fail("futex_wake shared (file backed) returned: %d %s\n",
157134
errno, strerror(errno));
158-
ret = RET_FAIL;
159135
} else {
160136
ksft_test_result_pass("futex_wake shared (file backed) succeeds\n");
161137
}
162138

163-
/* Freeing resources */
164-
shmdt(shared_data);
165139
munmap(shm, sizeof(f_private));
166140
remove(SHM_PATH);
167141
close(fd);
168-
169-
ksft_print_cnts();
170-
return ret;
171142
}
143+
144+
TEST_HARNESS_MAIN

tools/testing/selftests/futex/functional/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ echo
5151
./futex_wait_private_mapped_file
5252

5353
echo
54-
./futex_wait $COLOR
54+
./futex_wait
5555

5656
echo
5757
./futex_requeue $COLOR

0 commit comments

Comments
 (0)