1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * sync test runner
3*4882a593Smuzhiyun * Copyright 2015-2016 Collabora Ltd.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Based on the implementation from the Android Open Source Project,
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright 2012 Google, Inc
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
10*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
11*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
12*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
14*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
17*4882a593Smuzhiyun * all copies or substantial portions of the Software.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25*4882a593Smuzhiyun * OTHER DEALINGS IN THE SOFTWARE.
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include <stdio.h>
29*4882a593Smuzhiyun #include <unistd.h>
30*4882a593Smuzhiyun #include <stdlib.h>
31*4882a593Smuzhiyun #include <sys/types.h>
32*4882a593Smuzhiyun #include <sys/stat.h>
33*4882a593Smuzhiyun #include <sys/wait.h>
34*4882a593Smuzhiyun #include <errno.h>
35*4882a593Smuzhiyun #include <string.h>
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include "../kselftest.h"
38*4882a593Smuzhiyun #include "synctest.h"
39*4882a593Smuzhiyun
run_test(int (* test)(void),char * name)40*4882a593Smuzhiyun static int run_test(int (*test)(void), char *name)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun int result;
43*4882a593Smuzhiyun pid_t childpid;
44*4882a593Smuzhiyun int ret;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun fflush(stdout);
47*4882a593Smuzhiyun childpid = fork();
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun if (childpid) {
50*4882a593Smuzhiyun waitpid(childpid, &result, 0);
51*4882a593Smuzhiyun if (WIFEXITED(result)) {
52*4882a593Smuzhiyun ret = WEXITSTATUS(result);
53*4882a593Smuzhiyun if (!ret)
54*4882a593Smuzhiyun ksft_test_result_pass("[RUN]\t%s\n", name);
55*4882a593Smuzhiyun else
56*4882a593Smuzhiyun ksft_test_result_fail("[RUN]\t%s\n", name);
57*4882a593Smuzhiyun return ret;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun return 1;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun exit(test());
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
sync_api_supported(void)65*4882a593Smuzhiyun static void sync_api_supported(void)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun struct stat sbuf;
68*4882a593Smuzhiyun int ret;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun ret = stat("/sys/kernel/debug/sync/sw_sync", &sbuf);
71*4882a593Smuzhiyun if (!ret)
72*4882a593Smuzhiyun return;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun if (errno == ENOENT)
75*4882a593Smuzhiyun ksft_exit_skip("Sync framework not supported by kernel\n");
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (errno == EACCES)
78*4882a593Smuzhiyun ksft_exit_skip("Run Sync test as root.\n");
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun ksft_exit_fail_msg("stat failed on /sys/kernel/debug/sync/sw_sync: %s",
81*4882a593Smuzhiyun strerror(errno));
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
main(void)84*4882a593Smuzhiyun int main(void)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun int err;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun ksft_print_header();
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun sync_api_supported();
91*4882a593Smuzhiyun ksft_set_plan(3 + 7);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun ksft_print_msg("[RUN]\tTesting sync framework\n");
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun RUN_TEST(test_alloc_timeline);
96*4882a593Smuzhiyun RUN_TEST(test_alloc_fence);
97*4882a593Smuzhiyun RUN_TEST(test_alloc_fence_negative);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun RUN_TEST(test_fence_one_timeline_wait);
100*4882a593Smuzhiyun RUN_TEST(test_fence_one_timeline_merge);
101*4882a593Smuzhiyun RUN_TEST(test_fence_merge_same_fence);
102*4882a593Smuzhiyun RUN_TEST(test_fence_multi_timeline_wait);
103*4882a593Smuzhiyun RUN_TEST(test_stress_two_threads_shared_timeline);
104*4882a593Smuzhiyun RUN_TEST(test_consumer_stress_multi_producer_single_consumer);
105*4882a593Smuzhiyun RUN_TEST(test_merge_stress_random_merge);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun err = ksft_get_fail_cnt();
108*4882a593Smuzhiyun if (err)
109*4882a593Smuzhiyun ksft_exit_fail_msg("%d out of %d sync tests failed\n",
110*4882a593Smuzhiyun err, ksft_test_num());
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /* need this return to keep gcc happy */
113*4882a593Smuzhiyun return ksft_exit_pass();
114*4882a593Smuzhiyun }
115