182208160SEmilio López /*
282208160SEmilio López * sync test runner
382208160SEmilio López * Copyright 2015-2016 Collabora Ltd.
482208160SEmilio López *
582208160SEmilio López * Based on the implementation from the Android Open Source Project,
682208160SEmilio López *
782208160SEmilio López * Copyright 2012 Google, Inc
882208160SEmilio López *
982208160SEmilio López * Permission is hereby granted, free of charge, to any person obtaining a
1082208160SEmilio López * copy of this software and associated documentation files (the "Software"),
1182208160SEmilio López * to deal in the Software without restriction, including without limitation
1282208160SEmilio López * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1382208160SEmilio López * and/or sell copies of the Software, and to permit persons to whom the
1482208160SEmilio López * Software is furnished to do so, subject to the following conditions:
1582208160SEmilio López *
1682208160SEmilio López * The above copyright notice and this permission notice shall be included in
1782208160SEmilio López * all copies or substantial portions of the Software.
1882208160SEmilio López *
1982208160SEmilio López * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2082208160SEmilio López * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2182208160SEmilio López * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2282208160SEmilio López * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2382208160SEmilio López * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2482208160SEmilio López * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2582208160SEmilio López * OTHER DEALINGS IN THE SOFTWARE.
2682208160SEmilio López */
2782208160SEmilio López
2882208160SEmilio López #include <stdio.h>
2982208160SEmilio López #include <unistd.h>
3082208160SEmilio López #include <stdlib.h>
3182208160SEmilio López #include <sys/types.h>
324996976fSMichael Ellerman #include <sys/stat.h>
3382208160SEmilio López #include <sys/wait.h>
34f6c44bbbSShuah Khan #include <errno.h>
35f471e1fdSShuah Khan #include <string.h>
3682208160SEmilio López
37f471e1fdSShuah Khan #include "../kselftest.h"
3882208160SEmilio López #include "synctest.h"
3982208160SEmilio López
run_test(int (* test)(void),char * name)4082208160SEmilio López static int run_test(int (*test)(void), char *name)
4182208160SEmilio López {
4282208160SEmilio López int result;
4382208160SEmilio López pid_t childpid;
44f471e1fdSShuah Khan int ret;
4582208160SEmilio López
4682208160SEmilio López fflush(stdout);
4782208160SEmilio López childpid = fork();
4882208160SEmilio López
4982208160SEmilio López if (childpid) {
5082208160SEmilio López waitpid(childpid, &result, 0);
51f471e1fdSShuah Khan if (WIFEXITED(result)) {
52f471e1fdSShuah Khan ret = WEXITSTATUS(result);
53f471e1fdSShuah Khan if (!ret)
54f471e1fdSShuah Khan ksft_test_result_pass("[RUN]\t%s\n", name);
55f471e1fdSShuah Khan else
56f471e1fdSShuah Khan ksft_test_result_fail("[RUN]\t%s\n", name);
57f471e1fdSShuah Khan return ret;
58f471e1fdSShuah Khan }
5982208160SEmilio López return 1;
6082208160SEmilio López }
6182208160SEmilio López
6282208160SEmilio López exit(test());
6382208160SEmilio López }
6482208160SEmilio López
sync_api_supported(void)65f471e1fdSShuah Khan static void sync_api_supported(void)
664996976fSMichael Ellerman {
674996976fSMichael Ellerman struct stat sbuf;
68f6c44bbbSShuah Khan int ret;
694996976fSMichael Ellerman
70f6c44bbbSShuah Khan ret = stat("/sys/kernel/debug/sync/sw_sync", &sbuf);
71f6c44bbbSShuah Khan if (!ret)
72f471e1fdSShuah Khan return;
73f6c44bbbSShuah Khan
74f471e1fdSShuah Khan if (errno == ENOENT)
75f471e1fdSShuah Khan ksft_exit_skip("Sync framework not supported by kernel\n");
76f6c44bbbSShuah Khan
77f471e1fdSShuah Khan if (errno == EACCES)
78f471e1fdSShuah Khan ksft_exit_skip("Run Sync test as root.\n");
79f6c44bbbSShuah Khan
80f471e1fdSShuah Khan ksft_exit_fail_msg("stat failed on /sys/kernel/debug/sync/sw_sync: %s",
81f471e1fdSShuah Khan strerror(errno));
824996976fSMichael Ellerman }
834996976fSMichael Ellerman
main(void)8482208160SEmilio López int main(void)
8582208160SEmilio López {
86f471e1fdSShuah Khan int err;
8782208160SEmilio López
88f471e1fdSShuah Khan ksft_print_header();
894996976fSMichael Ellerman
90f471e1fdSShuah Khan sync_api_supported();
9163aa57f5SPaolo Bonzini ksft_set_plan(3 + 7);
9282208160SEmilio López
93f471e1fdSShuah Khan ksft_print_msg("[RUN]\tTesting sync framework\n");
9482208160SEmilio López
95f471e1fdSShuah Khan RUN_TEST(test_alloc_timeline);
96f471e1fdSShuah Khan RUN_TEST(test_alloc_fence);
97f471e1fdSShuah Khan RUN_TEST(test_alloc_fence_negative);
986a5b7d2cSEmilio López
99f471e1fdSShuah Khan RUN_TEST(test_fence_one_timeline_wait);
100f471e1fdSShuah Khan RUN_TEST(test_fence_one_timeline_merge);
101f471e1fdSShuah Khan RUN_TEST(test_fence_merge_same_fence);
102f471e1fdSShuah Khan RUN_TEST(test_fence_multi_timeline_wait);
103f471e1fdSShuah Khan RUN_TEST(test_stress_two_threads_shared_timeline);
104f471e1fdSShuah Khan RUN_TEST(test_consumer_stress_multi_producer_single_consumer);
105f471e1fdSShuah Khan RUN_TEST(test_merge_stress_random_merge);
106f471e1fdSShuah Khan
107f471e1fdSShuah Khan err = ksft_get_fail_cnt();
10882208160SEmilio López if (err)
109f471e1fdSShuah Khan ksft_exit_fail_msg("%d out of %d sync tests failed\n",
110f471e1fdSShuah Khan err, ksft_test_num());
11182208160SEmilio López
112*102690beSNathan Chancellor ksft_exit_pass();
11382208160SEmilio López }
114