aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorEmilio López <emilio.lopez@collabora.co.uk>2016-10-19 08:49:48 -0400
committerShuah Khan <shuahkh@osg.samsung.com>2016-12-01 20:12:50 -0500
commit82208160ae35ca00b5494c5c90c1a8721b15bdb1 (patch)
treef68d64530389429e4aa09225b35eb0ec8fa1c3f1 /tools
parent1001354ca34179f3db924eb66672442a173147dc (diff)
selftest: sync: basic tests for sw_sync framework
These tests are based on the libsync test suite from Android. This commit lays the ground for future tests, as well as includes tests for a variety of basic allocation commands. Signed-off-by: Emilio López <emilio.lopez@collabora.co.uk> Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/testing/selftests/Makefile1
-rw-r--r--tools/testing/selftests/sync/.gitignore1
-rw-r--r--tools/testing/selftests/sync/Makefile18
-rw-r--r--tools/testing/selftests/sync/sw_sync.h46
-rw-r--r--tools/testing/selftests/sync/sync.c221
-rw-r--r--tools/testing/selftests/sync/sync.h40
-rw-r--r--tools/testing/selftests/sync/sync_alloc.c74
-rw-r--r--tools/testing/selftests/sync/sync_test.c71
-rw-r--r--tools/testing/selftests/sync/synctest.h47
9 files changed, 519 insertions, 0 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index f770dba2a6f6..69cf1a68e4d3 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -23,6 +23,7 @@ TARGETS += seccomp
23TARGETS += sigaltstack 23TARGETS += sigaltstack
24TARGETS += size 24TARGETS += size
25TARGETS += static_keys 25TARGETS += static_keys
26TARGETS += sync
26TARGETS += sysctl 27TARGETS += sysctl
27ifneq (1, $(quicktest)) 28ifneq (1, $(quicktest))
28TARGETS += timers 29TARGETS += timers
diff --git a/tools/testing/selftests/sync/.gitignore b/tools/testing/selftests/sync/.gitignore
new file mode 100644
index 000000000000..f5091e7792f2
--- /dev/null
+++ b/tools/testing/selftests/sync/.gitignore
@@ -0,0 +1 @@
sync_test
diff --git a/tools/testing/selftests/sync/Makefile b/tools/testing/selftests/sync/Makefile
new file mode 100644
index 000000000000..620a59ae2dab
--- /dev/null
+++ b/tools/testing/selftests/sync/Makefile
@@ -0,0 +1,18 @@
1CFLAGS += -O2 -g -std=gnu89 -pthread -Wall -Wextra
2CFLAGS += -I../../../../usr/include/
3LDFLAGS += -pthread
4
5TEST_PROGS = sync_test
6
7all: $(TEST_PROGS)
8
9include ../lib.mk
10
11OBJS = sync_test.o sync.o
12
13TESTS += sync_alloc.o
14
15sync_test: $(OBJS) $(TESTS)
16
17clean:
18 $(RM) sync_test $(OBJS) $(TESTS)
diff --git a/tools/testing/selftests/sync/sw_sync.h b/tools/testing/selftests/sync/sw_sync.h
new file mode 100644
index 000000000000..e2cfc6bad83e
--- /dev/null
+++ b/tools/testing/selftests/sync/sw_sync.h
@@ -0,0 +1,46 @@
1/*
2 * sw_sync abstraction
3 *
4 * Copyright 2015-2016 Collabora Ltd.
5 *
6 * Based on the implementation from the Android Open Source Project,
7 *
8 * Copyright 2013 Google, Inc
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29#ifndef SELFTESTS_SW_SYNC_H
30#define SELFTESTS_SW_SYNC_H
31
32/*
33 * sw_sync is mainly intended for testing and should not be compiled into
34 * production kernels
35 */
36
37int sw_sync_timeline_create(void);
38int sw_sync_timeline_is_valid(int fd);
39int sw_sync_timeline_inc(int fd, unsigned int count);
40void sw_sync_timeline_destroy(int fd);
41
42int sw_sync_fence_create(int fd, const char *name, unsigned int value);
43int sw_sync_fence_is_valid(int fd);
44void sw_sync_fence_destroy(int fd);
45
46#endif
diff --git a/tools/testing/selftests/sync/sync.c b/tools/testing/selftests/sync/sync.c
new file mode 100644
index 000000000000..f3d599f249b9
--- /dev/null
+++ b/tools/testing/selftests/sync/sync.c
@@ -0,0 +1,221 @@
1/*
2 * sync / sw_sync abstraction
3 * Copyright 2015-2016 Collabora Ltd.
4 *
5 * Based on the implementation from the Android Open Source Project,
6 *
7 * Copyright 2012 Google, Inc
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <fcntl.h>
29#include <malloc.h>
30#include <poll.h>
31#include <stdint.h>
32#include <string.h>
33#include <unistd.h>
34
35#include <sys/ioctl.h>
36#include <sys/stat.h>
37#include <sys/types.h>
38
39#include "sync.h"
40#include "sw_sync.h"
41
42#include <linux/sync_file.h>
43
44
45/* SW_SYNC ioctls */
46struct sw_sync_create_fence_data {
47 __u32 value;
48 char name[32];
49 __s32 fence;
50};
51
52#define SW_SYNC_IOC_MAGIC 'W'
53#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
54 struct sw_sync_create_fence_data)
55#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
56
57
58int sync_wait(int fd, int timeout)
59{
60 struct pollfd fds;
61
62 fds.fd = fd;
63 fds.events = POLLIN | POLLERR;
64
65 return poll(&fds, 1, timeout);
66}
67
68int sync_merge(const char *name, int fd1, int fd2)
69{
70 struct sync_merge_data data = {};
71 int err;
72
73 data.fd2 = fd2;
74 strncpy(data.name, name, sizeof(data.name) - 1);
75 data.name[sizeof(data.name) - 1] = '\0';
76
77 err = ioctl(fd1, SYNC_IOC_MERGE, &data);
78 if (err < 0)
79 return err;
80
81 return data.fence;
82}
83
84static struct sync_file_info *sync_file_info(int fd)
85{
86 struct sync_file_info *info;
87 struct sync_fence_info *fence_info;
88 int err, num_fences;
89
90 info = calloc(1, sizeof(*info));
91 if (info == NULL)
92 return NULL;
93
94 err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
95 if (err < 0) {
96 free(info);
97 return NULL;
98 }
99
100 num_fences = info->num_fences;
101
102 if (num_fences) {
103 info->flags = 0;
104 info->num_fences = num_fences;
105
106 fence_info = calloc(num_fences, sizeof(*fence_info));
107 if (!fence_info) {
108 free(info);
109 return NULL;
110 }
111
112 info->sync_fence_info = (uint64_t)fence_info;
113
114 err = ioctl(fd, SYNC_IOC_FILE_INFO, info);
115 if (err < 0) {
116 free(fence_info);
117 free(info);
118 return NULL;
119 }
120 }
121
122 return info;
123}
124
125static void sync_file_info_free(struct sync_file_info *info)
126{
127 free((void *)info->sync_fence_info);
128 free(info);
129}
130
131int sync_fence_size(int fd)
132{
133 int count;
134 struct sync_file_info *info = sync_file_info(fd);
135
136 if (!info)
137 return 0;
138
139 count = info->num_fences;
140
141 sync_file_info_free(info);
142
143 return count;
144}
145
146int sync_fence_count_with_status(int fd, int status)
147{
148 unsigned int i, count = 0;
149 struct sync_fence_info *fence_info = NULL;
150 struct sync_file_info *info = sync_file_info(fd);
151
152 if (!info)
153 return -1;
154
155 fence_info = (struct sync_fence_info *)info->sync_fence_info;
156 for (i = 0 ; i < info->num_fences ; i++) {
157 if (fence_info[i].status == status)
158 count++;
159 }
160
161 sync_file_info_free(info);
162
163 return count;
164}
165
166int sw_sync_timeline_create(void)
167{
168 return open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
169}
170
171int sw_sync_timeline_inc(int fd, unsigned int count)
172{
173 __u32 arg = count;
174
175 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
176}
177
178int sw_sync_timeline_is_valid(int fd)
179{
180 int status;
181
182 if (fd == -1)
183 return 0;
184
185 status = fcntl(fd, F_GETFD, 0);
186 return (status >= 0);
187}
188
189void sw_sync_timeline_destroy(int fd)
190{
191 if (sw_sync_timeline_is_valid(fd))
192 close(fd);
193}
194
195int sw_sync_fence_create(int fd, const char *name, unsigned int value)
196{
197 struct sw_sync_create_fence_data data = {};
198 int err;
199
200 data.value = value;
201 strncpy(data.name, name, sizeof(data.name) - 1);
202 data.name[sizeof(data.name) - 1] = '\0';
203
204 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
205 if (err < 0)
206 return err;
207
208 return data.fence;
209}
210
211int sw_sync_fence_is_valid(int fd)
212{
213 /* Same code! */
214 return sw_sync_timeline_is_valid(fd);
215}
216
217void sw_sync_fence_destroy(int fd)
218{
219 if (sw_sync_fence_is_valid(fd))
220 close(fd);
221}
diff --git a/tools/testing/selftests/sync/sync.h b/tools/testing/selftests/sync/sync.h
new file mode 100644
index 000000000000..fb7156148350
--- /dev/null
+++ b/tools/testing/selftests/sync/sync.h
@@ -0,0 +1,40 @@
1/*
2 * sync abstraction
3 * Copyright 2015-2016 Collabora Ltd.
4 *
5 * Based on the implementation from the Android Open Source Project,
6 *
7 * Copyright 2012 Google, Inc
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef SELFTESTS_SYNC_H
29#define SELFTESTS_SYNC_H
30
31#define FENCE_STATUS_ERROR (-1)
32#define FENCE_STATUS_ACTIVE (0)
33#define FENCE_STATUS_SIGNALED (1)
34
35int sync_wait(int fd, int timeout);
36int sync_merge(const char *name, int fd1, int fd2);
37int sync_fence_size(int fd);
38int sync_fence_count_with_status(int fd, int status);
39
40#endif
diff --git a/tools/testing/selftests/sync/sync_alloc.c b/tools/testing/selftests/sync/sync_alloc.c
new file mode 100644
index 000000000000..66a28afc05dc
--- /dev/null
+++ b/tools/testing/selftests/sync/sync_alloc.c
@@ -0,0 +1,74 @@
1/*
2 * sync allocation tests
3 * Copyright 2015-2016 Collabora Ltd.
4 *
5 * Based on the implementation from the Android Open Source Project,
6 *
7 * Copyright 2012 Google, Inc
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include "sync.h"
29#include "sw_sync.h"
30#include "synctest.h"
31
32int test_alloc_timeline(void)
33{
34 int timeline, valid;
35
36 timeline = sw_sync_timeline_create();
37 valid = sw_sync_timeline_is_valid(timeline);
38 ASSERT(valid, "Failure allocating timeline\n");
39
40 sw_sync_timeline_destroy(timeline);
41 return 0;
42}
43
44int test_alloc_fence(void)
45{
46 int timeline, fence, valid;
47
48 timeline = sw_sync_timeline_create();
49 valid = sw_sync_timeline_is_valid(timeline);
50 ASSERT(valid, "Failure allocating timeline\n");
51
52 fence = sw_sync_fence_create(timeline, "allocFence", 1);
53 valid = sw_sync_fence_is_valid(fence);
54 ASSERT(valid, "Failure allocating fence\n");
55
56 sw_sync_fence_destroy(fence);
57 sw_sync_timeline_destroy(timeline);
58 return 0;
59}
60
61int test_alloc_fence_negative(void)
62{
63 int fence, timeline;
64
65 timeline = sw_sync_timeline_create();
66 ASSERT(timeline > 0, "Failure allocating timeline\n");
67
68 fence = sw_sync_fence_create(-1, "fence", 1);
69 ASSERT(fence < 0, "Success allocating negative fence\n");
70
71 sw_sync_fence_destroy(fence);
72 sw_sync_timeline_destroy(timeline);
73 return 0;
74}
diff --git a/tools/testing/selftests/sync/sync_test.c b/tools/testing/selftests/sync/sync_test.c
new file mode 100644
index 000000000000..e471ba9390ca
--- /dev/null
+++ b/tools/testing/selftests/sync/sync_test.c
@@ -0,0 +1,71 @@
1/*
2 * sync test runner
3 * Copyright 2015-2016 Collabora Ltd.
4 *
5 * Based on the implementation from the Android Open Source Project,
6 *
7 * Copyright 2012 Google, Inc
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <sys/types.h>
32#include <sys/wait.h>
33
34#include "synctest.h"
35
36static int run_test(int (*test)(void), char *name)
37{
38 int result;
39 pid_t childpid;
40
41 fflush(stdout);
42 childpid = fork();
43
44 if (childpid) {
45 waitpid(childpid, &result, 0);
46 if (WIFEXITED(result))
47 return WEXITSTATUS(result);
48 return 1;
49 }
50
51 printf("[RUN]\tExecuting %s\n", name);
52 exit(test());
53}
54
55int main(void)
56{
57 int err = 0;
58
59 printf("[RUN]\tTesting sync framework\n");
60
61 err += RUN_TEST(test_alloc_timeline);
62 err += RUN_TEST(test_alloc_fence);
63 err += RUN_TEST(test_alloc_fence_negative);
64
65 if (err)
66 printf("[FAIL]\tsync errors: %d\n", err);
67 else
68 printf("[OK]\tsync\n");
69
70 return !!err;
71}
diff --git a/tools/testing/selftests/sync/synctest.h b/tools/testing/selftests/sync/synctest.h
new file mode 100644
index 000000000000..7d2d8ce1d71a
--- /dev/null
+++ b/tools/testing/selftests/sync/synctest.h
@@ -0,0 +1,47 @@
1/*
2 * sync tests
3 * Copyright 2015-2016 Collabora Ltd.
4 *
5 * Based on the implementation from the Android Open Source Project,
6 *
7 * Copyright 2012 Google, Inc
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef SELFTESTS_SYNCTEST_H
29#define SELFTESTS_SYNCTEST_H
30
31#include <stdio.h>
32
33#define ASSERT(cond, msg) do { \
34 if (!(cond)) { \
35 printf("[BAD]\t%s", (msg)); \
36 return 1; \
37 } \
38} while (0)
39
40#define RUN_TEST(x) run_test((x), #x)
41
42/* Allocation tests */
43int test_alloc_timeline(void);
44int test_alloc_fence(void);
45int test_alloc_fence_negative(void);
46
47#endif