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