xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/sync/sync_stress_parallelism.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  sync stress test: parallelism
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 <pthread.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include "sync.h"
31*4882a593Smuzhiyun #include "sw_sync.h"
32*4882a593Smuzhiyun #include "synctest.h"
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static struct {
35*4882a593Smuzhiyun 	int iterations;
36*4882a593Smuzhiyun 	int timeline;
37*4882a593Smuzhiyun 	int counter;
38*4882a593Smuzhiyun } test_data_two_threads;
39*4882a593Smuzhiyun 
test_stress_two_threads_shared_timeline_thread(void * d)40*4882a593Smuzhiyun static int test_stress_two_threads_shared_timeline_thread(void *d)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	int thread_id = (long)d;
43*4882a593Smuzhiyun 	int timeline = test_data_two_threads.timeline;
44*4882a593Smuzhiyun 	int iterations = test_data_two_threads.iterations;
45*4882a593Smuzhiyun 	int fence, valid, ret, i;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	for (i = 0; i < iterations; i++) {
48*4882a593Smuzhiyun 		fence = sw_sync_fence_create(timeline, "fence",
49*4882a593Smuzhiyun 					     i * 2 + thread_id);
50*4882a593Smuzhiyun 		valid = sw_sync_fence_is_valid(fence);
51*4882a593Smuzhiyun 		ASSERT(valid, "Failure allocating fence\n");
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 		/* Wait on the prior thread to complete */
54*4882a593Smuzhiyun 		ret = sync_wait(fence, -1);
55*4882a593Smuzhiyun 		ASSERT(ret > 0, "Problem occurred on prior thread\n");
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 		/*
58*4882a593Smuzhiyun 		 * Confirm the previous thread's writes are visible
59*4882a593Smuzhiyun 		 * and then increment
60*4882a593Smuzhiyun 		 */
61*4882a593Smuzhiyun 		ASSERT(test_data_two_threads.counter == i * 2 + thread_id,
62*4882a593Smuzhiyun 		       "Counter got damaged!\n");
63*4882a593Smuzhiyun 		test_data_two_threads.counter++;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 		/* Kick off the other thread */
66*4882a593Smuzhiyun 		ret = sw_sync_timeline_inc(timeline, 1);
67*4882a593Smuzhiyun 		ASSERT(ret == 0, "Advancing timeline failed\n");
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 		sw_sync_fence_destroy(fence);
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	return 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
test_stress_two_threads_shared_timeline(void)75*4882a593Smuzhiyun int test_stress_two_threads_shared_timeline(void)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	pthread_t a, b;
78*4882a593Smuzhiyun 	int valid;
79*4882a593Smuzhiyun 	int timeline = sw_sync_timeline_create();
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	valid = sw_sync_timeline_is_valid(timeline);
82*4882a593Smuzhiyun 	ASSERT(valid, "Failure allocating timeline\n");
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	test_data_two_threads.iterations = 1 << 16;
85*4882a593Smuzhiyun 	test_data_two_threads.counter = 0;
86*4882a593Smuzhiyun 	test_data_two_threads.timeline = timeline;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/*
89*4882a593Smuzhiyun 	 * Use a single timeline to synchronize two threads
90*4882a593Smuzhiyun 	 * hammmering on the same counter.
91*4882a593Smuzhiyun 	 */
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	pthread_create(&a, NULL, (void *(*)(void *))
94*4882a593Smuzhiyun 		       test_stress_two_threads_shared_timeline_thread,
95*4882a593Smuzhiyun 		       (void *)0);
96*4882a593Smuzhiyun 	pthread_create(&b, NULL, (void *(*)(void *))
97*4882a593Smuzhiyun 		       test_stress_two_threads_shared_timeline_thread,
98*4882a593Smuzhiyun 		       (void *)1);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	pthread_join(a, NULL);
101*4882a593Smuzhiyun 	pthread_join(b, NULL);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	/* make sure the threads did not trample on one another */
104*4882a593Smuzhiyun 	ASSERT(test_data_two_threads.counter ==
105*4882a593Smuzhiyun 	       test_data_two_threads.iterations * 2,
106*4882a593Smuzhiyun 	       "Counter has unexpected value\n");
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	sw_sync_timeline_destroy(timeline);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	return 0;
111*4882a593Smuzhiyun }
112