Branch data Line data Source code
1 : : /* Unit tests for GRecMutex
2 : : * Copyright (C) 2011 Red Hat, Inc
3 : : * Author: Matthias Clasen
4 : : *
5 : : * SPDX-License-Identifier: LicenseRef-old-glib-tests
6 : : *
7 : : * This work is provided "as is"; redistribution and modification
8 : : * in whole or in part, in any medium, physical or electronic is
9 : : * permitted without restriction.
10 : : *
11 : : * This work is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 : : *
15 : : * In no event shall the authors or contributors be liable for any
16 : : * direct, indirect, incidental, special, exemplary, or consequential
17 : : * damages (including, but not limited to, procurement of substitute
18 : : * goods or services; loss of use, data, or profits; or business
19 : : * interruption) however caused and on any theory of liability, whether
20 : : * in contract, strict liability, or tort (including negligence or
21 : : * otherwise) arising in any way out of the use of this software, even
22 : : * if advised of the possibility of such damage.
23 : : */
24 : :
25 : : /* We are testing some deprecated APIs here */
26 : : #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
27 : : #define GLIB_DISABLE_DEPRECATION_WARNINGS
28 : : #endif
29 : :
30 : : #include <glib.h>
31 : :
32 : : #include <stdio.h>
33 : :
34 : : static void
35 : 1 : test_rec_mutex1 (void)
36 : : {
37 : : GRecMutex mutex;
38 : :
39 : 1 : g_rec_mutex_init (&mutex);
40 : 1 : g_rec_mutex_lock (&mutex);
41 : 1 : g_rec_mutex_unlock (&mutex);
42 : 1 : g_rec_mutex_lock (&mutex);
43 : 1 : g_rec_mutex_unlock (&mutex);
44 : 1 : g_rec_mutex_clear (&mutex);
45 : 1 : }
46 : :
47 : : static void
48 : 1 : test_rec_mutex2 (void)
49 : : {
50 : : static GRecMutex mutex;
51 : :
52 : 1 : g_rec_mutex_lock (&mutex);
53 : 1 : g_rec_mutex_unlock (&mutex);
54 : 1 : g_rec_mutex_lock (&mutex);
55 : 1 : g_rec_mutex_unlock (&mutex);
56 : 1 : }
57 : :
58 : : static void
59 : 1 : test_rec_mutex3 (void)
60 : : {
61 : : static GRecMutex mutex;
62 : : gboolean ret;
63 : :
64 : 1 : ret = g_rec_mutex_trylock (&mutex);
65 : 1 : g_assert_true (ret);
66 : :
67 : 1 : ret = g_rec_mutex_trylock (&mutex);
68 : 1 : g_assert_true (ret);
69 : :
70 : 1 : g_rec_mutex_unlock (&mutex);
71 : 1 : g_rec_mutex_unlock (&mutex);
72 : 1 : }
73 : :
74 : : typedef struct {
75 : : size_t n_locks;
76 : : unsigned int n_iterations;
77 : : size_t n_threads;
78 : : GThread **threads; /* (array length=n_threads) */
79 : : GThread **owners; /* (array length=n_locks), each element is locked by the corresponding mutex in @locks */
80 : : GRecMutex *locks; /* (array length=n_locks) */
81 : : } ThreadTestData;
82 : :
83 : : static void
84 : 1 : thread_test_data_clear (ThreadTestData *data)
85 : : {
86 : 1 : g_free (data->locks);
87 : 1 : g_free (data->owners);
88 : 1 : g_free (data->threads);
89 : 1 : }
90 : :
91 : : static void
92 : 10000 : acquire (ThreadTestData *data,
93 : : unsigned int nr)
94 : : {
95 : : GThread *self;
96 : :
97 : 10000 : self = g_thread_self ();
98 : :
99 [ + + ]: 10000 : if (!g_rec_mutex_trylock (&data->locks[nr]))
100 : : {
101 [ - + ]: 4980 : if (g_test_verbose ())
102 : 0 : g_printerr ("thread %p going to block on lock %d\n", self, nr);
103 : :
104 : 4980 : g_rec_mutex_lock (&data->locks[nr]);
105 : : }
106 : :
107 : 10000 : g_assert_null (data->owners[nr]); /* hopefully nobody else is here */
108 : 10000 : data->owners[nr] = self;
109 : :
110 : : /* let some other threads try to ruin our day */
111 : 10000 : g_thread_yield ();
112 : 10000 : g_thread_yield ();
113 : :
114 : 10000 : g_assert_true (data->owners[nr] == self); /* hopefully this is still us... */
115 : :
116 [ - + ]: 10000 : if (g_test_verbose ())
117 : 0 : g_printerr ("thread %p recursively taking lock %d\n", self, nr);
118 : :
119 : 10000 : g_rec_mutex_lock (&data->locks[nr]); /* we're recursive, after all */
120 : :
121 : 10000 : g_assert_true (data->owners[nr] == self); /* hopefully this is still us... */
122 : :
123 : 10000 : g_rec_mutex_unlock (&data->locks[nr]);
124 : :
125 : 10000 : g_thread_yield ();
126 : 10000 : g_thread_yield ();
127 : :
128 : 10000 : g_assert_true (data->owners[nr] == self); /* hopefully this is still us... */
129 : 10000 : data->owners[nr] = NULL; /* make way for the next guy */
130 : :
131 : 10000 : g_rec_mutex_unlock (&data->locks[nr]);
132 : 10000 : }
133 : :
134 : : static gpointer
135 : 10 : thread_func (gpointer user_data)
136 : : {
137 : 10 : ThreadTestData *data = user_data;
138 : : GRand *rand;
139 : :
140 : 10 : rand = g_rand_new ();
141 : :
142 [ + + ]: 10010 : for (unsigned int i = 0; i < data->n_iterations; i++)
143 : 10000 : acquire (data, g_rand_int_range (rand, 0, data->n_locks));
144 : :
145 : 10 : g_rand_free (rand);
146 : :
147 : 10 : return NULL;
148 : : }
149 : :
150 : : static void
151 : 1 : test_rec_mutex4 (void)
152 : : {
153 : : ThreadTestData data;
154 : :
155 : 1 : data.n_locks = 48;
156 : 1 : data.n_iterations = 10000;
157 : 1 : data.n_threads = 100;
158 : :
159 : : /* Some CI runners have a hard time with this much contention, so tone it down
160 : : * a bit for CI. */
161 [ + - ]: 1 : if (!g_test_perf ())
162 : : {
163 : 1 : data.n_locks /= 10;
164 : 1 : data.n_iterations /= 10;
165 : 1 : data.n_threads /= 10;
166 : : }
167 : :
168 : 1 : data.threads = g_new0 (GThread*, data.n_threads);
169 : 1 : data.owners = g_new0 (GThread*, data.n_locks);
170 : 1 : data.locks = g_new0 (GRecMutex, data.n_locks);
171 : :
172 [ + + ]: 5 : for (size_t i = 0; i < data.n_locks; i++)
173 : 4 : g_rec_mutex_init (&data.locks[i]);
174 : :
175 [ + + ]: 11 : for (size_t i = 0; i < data.n_threads; i++)
176 : 10 : data.threads[i] = g_thread_new ("test", thread_func, &data);
177 : :
178 [ + + ]: 11 : for (size_t i = 0; i < data.n_threads; i++)
179 : 10 : g_thread_join (data.threads[i]);
180 : :
181 [ + + ]: 5 : for (size_t i = 0; i < data.n_locks; i++)
182 : 4 : g_rec_mutex_clear (&data.locks[i]);
183 : :
184 [ + + ]: 5 : for (size_t i = 0; i < data.n_locks; i++)
185 : 4 : g_assert_null (data.owners[i]);
186 : :
187 : 1 : thread_test_data_clear (&data);
188 : 1 : }
189 : :
190 : : static gint count_to = 0;
191 : : static gint depth;
192 : :
193 : : static gboolean
194 : 137 : do_addition (gint *value)
195 : : {
196 : : static GRecMutex lock;
197 : : gboolean more;
198 : : gint i;
199 : :
200 : : /* test performance of "good" cases (ie: short critical sections) */
201 [ + + ]: 562 : for (i = 0; i < depth; i++)
202 : 425 : g_rec_mutex_lock (&lock);
203 : :
204 [ + + ]: 137 : if ((more = *value != count_to))
205 [ + + ]: 82 : if (*value != -1)
206 : 75 : (*value)++;
207 : :
208 [ + + ]: 562 : for (i = 0; i < depth; i++)
209 : 425 : g_rec_mutex_unlock (&lock);
210 : :
211 : 137 : return more;
212 : : }
213 : :
214 : : static gpointer
215 : 55 : addition_thread (gpointer value)
216 : : {
217 [ + + ]: 137 : while (do_addition (value));
218 : :
219 : 55 : return NULL;
220 : : }
221 : :
222 : : static void
223 : 25 : test_mutex_perf (gconstpointer data)
224 : : {
225 : 25 : gint c = GPOINTER_TO_INT (data);
226 : : GThread *threads[100];
227 : : gint64 start_time;
228 : : gint n_threads;
229 : : gdouble rate;
230 : 25 : gint x = -1;
231 : : gint i;
232 : :
233 : 25 : n_threads = c / 256;
234 : 25 : depth = c % 256;
235 [ + - ]: 25 : count_to = g_test_perf () ? 100000000 : n_threads + 1;
236 : :
237 [ + + ]: 55 : for (i = 0; i < n_threads - 1; i++)
238 : 30 : threads[i] = g_thread_new ("test", addition_thread, &x);
239 : :
240 : : /* avoid measuring thread setup/teardown time */
241 : 25 : start_time = g_get_monotonic_time ();
242 : 25 : g_atomic_int_set (&x, 0);
243 : 25 : addition_thread (&x);
244 : 25 : g_assert_cmpint (g_atomic_int_get (&x), ==, count_to);
245 : 25 : rate = g_get_monotonic_time () - start_time;
246 : 25 : rate = x / rate;
247 : :
248 [ + + ]: 55 : for (i = 0; i < n_threads - 1; i++)
249 : 30 : g_thread_join (threads[i]);
250 : :
251 : 25 : g_test_maximized_result (rate, "%f mips", rate);
252 : 25 : }
253 : :
254 : :
255 : : int
256 : 1 : main (int argc, char *argv[])
257 : : {
258 : 1 : g_test_init (&argc, &argv, NULL);
259 : :
260 : 1 : g_test_add_func ("/thread/rec-mutex1", test_rec_mutex1);
261 : 1 : g_test_add_func ("/thread/rec-mutex2", test_rec_mutex2);
262 : 1 : g_test_add_func ("/thread/rec-mutex3", test_rec_mutex3);
263 : 1 : g_test_add_func ("/thread/rec-mutex4", test_rec_mutex4);
264 : :
265 : : {
266 : : gint i, j;
267 : :
268 [ + + ]: 6 : for (i = 0; i < 5; i++)
269 [ + + ]: 30 : for (j = 1; j <= 5; j++)
270 : : {
271 : : gchar name[80];
272 : : guint c;
273 : :
274 : 25 : c = i * 256 + j;
275 : :
276 [ + + ]: 25 : if (i)
277 : 20 : sprintf (name, "/thread/rec-mutex/perf/contended%d/depth%d", i, j);
278 : : else
279 : 5 : sprintf (name, "/thread/rec-mutex/perf/uncontended/depth%d", j);
280 : :
281 : 25 : g_test_add_data_func (name, GINT_TO_POINTER (c), test_mutex_perf);
282 : : }
283 : : }
284 : :
285 : 1 : return g_test_run ();
286 : : }
|