xref: /linux/tools/testing/selftests/sync/sync_stress_parallelism.c (revision 9095bf25ea08135a5b74875dd0e3eeaddc4218a0)
1*54b519f3SEmilio López /*
2*54b519f3SEmilio López  *  sync stress test: parallelism
3*54b519f3SEmilio López  *  Copyright 2015-2016 Collabora Ltd.
4*54b519f3SEmilio López  *
5*54b519f3SEmilio López  *  Based on the implementation from the Android Open Source Project,
6*54b519f3SEmilio López  *
7*54b519f3SEmilio López  *  Copyright 2012 Google, Inc
8*54b519f3SEmilio López  *
9*54b519f3SEmilio López  *  Permission is hereby granted, free of charge, to any person obtaining a
10*54b519f3SEmilio López  *  copy of this software and associated documentation files (the "Software"),
11*54b519f3SEmilio López  *  to deal in the Software without restriction, including without limitation
12*54b519f3SEmilio López  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13*54b519f3SEmilio López  *  and/or sell copies of the Software, and to permit persons to whom the
14*54b519f3SEmilio López  *  Software is furnished to do so, subject to the following conditions:
15*54b519f3SEmilio López  *
16*54b519f3SEmilio López  *  The above copyright notice and this permission notice shall be included in
17*54b519f3SEmilio López  *  all copies or substantial portions of the Software.
18*54b519f3SEmilio López  *
19*54b519f3SEmilio López  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*54b519f3SEmilio López  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*54b519f3SEmilio López  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22*54b519f3SEmilio López  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23*54b519f3SEmilio López  *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24*54b519f3SEmilio López  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25*54b519f3SEmilio López  *  OTHER DEALINGS IN THE SOFTWARE.
26*54b519f3SEmilio López  */
27*54b519f3SEmilio López 
28*54b519f3SEmilio López #include <pthread.h>
29*54b519f3SEmilio López 
30*54b519f3SEmilio López #include "sync.h"
31*54b519f3SEmilio López #include "sw_sync.h"
32*54b519f3SEmilio López #include "synctest.h"
33*54b519f3SEmilio López 
34*54b519f3SEmilio López static struct {
35*54b519f3SEmilio López 	int iterations;
36*54b519f3SEmilio López 	int timeline;
37*54b519f3SEmilio López 	int counter;
38*54b519f3SEmilio López } test_data_two_threads;
39*54b519f3SEmilio López 
test_stress_two_threads_shared_timeline_thread(void * d)40*54b519f3SEmilio López static int test_stress_two_threads_shared_timeline_thread(void *d)
41*54b519f3SEmilio López {
42*54b519f3SEmilio López 	int thread_id = (long)d;
43*54b519f3SEmilio López 	int timeline = test_data_two_threads.timeline;
44*54b519f3SEmilio López 	int iterations = test_data_two_threads.iterations;
45*54b519f3SEmilio López 	int fence, valid, ret, i;
46*54b519f3SEmilio López 
47*54b519f3SEmilio López 	for (i = 0; i < iterations; i++) {
48*54b519f3SEmilio López 		fence = sw_sync_fence_create(timeline, "fence",
49*54b519f3SEmilio López 					     i * 2 + thread_id);
50*54b519f3SEmilio López 		valid = sw_sync_fence_is_valid(fence);
51*54b519f3SEmilio López 		ASSERT(valid, "Failure allocating fence\n");
52*54b519f3SEmilio López 
53*54b519f3SEmilio López 		/* Wait on the prior thread to complete */
54*54b519f3SEmilio López 		ret = sync_wait(fence, -1);
55*54b519f3SEmilio López 		ASSERT(ret > 0, "Problem occurred on prior thread\n");
56*54b519f3SEmilio López 
57*54b519f3SEmilio López 		/*
58*54b519f3SEmilio López 		 * Confirm the previous thread's writes are visible
59*54b519f3SEmilio López 		 * and then increment
60*54b519f3SEmilio López 		 */
61*54b519f3SEmilio López 		ASSERT(test_data_two_threads.counter == i * 2 + thread_id,
62*54b519f3SEmilio López 		       "Counter got damaged!\n");
63*54b519f3SEmilio López 		test_data_two_threads.counter++;
64*54b519f3SEmilio López 
65*54b519f3SEmilio López 		/* Kick off the other thread */
66*54b519f3SEmilio López 		ret = sw_sync_timeline_inc(timeline, 1);
67*54b519f3SEmilio López 		ASSERT(ret == 0, "Advancing timeline failed\n");
68*54b519f3SEmilio López 
69*54b519f3SEmilio López 		sw_sync_fence_destroy(fence);
70*54b519f3SEmilio López 	}
71*54b519f3SEmilio López 
72*54b519f3SEmilio López 	return 0;
73*54b519f3SEmilio López }
74*54b519f3SEmilio López 
test_stress_two_threads_shared_timeline(void)75*54b519f3SEmilio López int test_stress_two_threads_shared_timeline(void)
76*54b519f3SEmilio López {
77*54b519f3SEmilio López 	pthread_t a, b;
78*54b519f3SEmilio López 	int valid;
79*54b519f3SEmilio López 	int timeline = sw_sync_timeline_create();
80*54b519f3SEmilio López 
81*54b519f3SEmilio López 	valid = sw_sync_timeline_is_valid(timeline);
82*54b519f3SEmilio López 	ASSERT(valid, "Failure allocating timeline\n");
83*54b519f3SEmilio López 
84*54b519f3SEmilio López 	test_data_two_threads.iterations = 1 << 16;
85*54b519f3SEmilio López 	test_data_two_threads.counter = 0;
86*54b519f3SEmilio López 	test_data_two_threads.timeline = timeline;
87*54b519f3SEmilio López 
88*54b519f3SEmilio López 	/*
89*54b519f3SEmilio López 	 * Use a single timeline to synchronize two threads
90*54b519f3SEmilio López 	 * hammmering on the same counter.
91*54b519f3SEmilio López 	 */
92*54b519f3SEmilio López 
93*54b519f3SEmilio López 	pthread_create(&a, NULL, (void *(*)(void *))
94*54b519f3SEmilio López 		       test_stress_two_threads_shared_timeline_thread,
95*54b519f3SEmilio López 		       (void *)0);
96*54b519f3SEmilio López 	pthread_create(&b, NULL, (void *(*)(void *))
97*54b519f3SEmilio López 		       test_stress_two_threads_shared_timeline_thread,
98*54b519f3SEmilio López 		       (void *)1);
99*54b519f3SEmilio López 
100*54b519f3SEmilio López 	pthread_join(a, NULL);
101*54b519f3SEmilio López 	pthread_join(b, NULL);
102*54b519f3SEmilio López 
103*54b519f3SEmilio López 	/* make sure the threads did not trample on one another */
104*54b519f3SEmilio López 	ASSERT(test_data_two_threads.counter ==
105*54b519f3SEmilio López 	       test_data_two_threads.iterations * 2,
106*54b519f3SEmilio López 	       "Counter has unexpected value\n");
107*54b519f3SEmilio López 
108*54b519f3SEmilio López 	sw_sync_timeline_destroy(timeline);
109*54b519f3SEmilio López 
110*54b519f3SEmilio López 	return 0;
111*54b519f3SEmilio López }
112