added mem_error() and mem_assert() to test and handle errors without
[platform/upstream/glib.git] / tests / slice-test.c
1 /* GLIB sliced memory - fast threaded memory chunk allocator
2  * Copyright (C) 2005 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <glib.h>
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/time.h> // gettimeofday
24
25 #define quick_rand32()  (rand_accu = 1664525 * rand_accu + 1013904223, rand_accu)
26 static guint    prime_size = 1021; // 769; // 509
27 static gboolean clean_memchunks = FALSE;
28 static guint    number_of_blocks = 10000;          /* total number of blocks allocated */
29 static guint    number_of_repetitions = 10000;     /* number of alloc+free repetitions */
30
31 /* --- old memchunk prototypes (memchunks.c) --- */
32 void            old_mem_chunks_init     (void);
33 GMemChunk*      old_mem_chunk_new       (const gchar  *name,
34                                          gint          atom_size,
35                                          gulong        area_size,
36                                          gint          type);
37 void            old_mem_chunk_destroy   (GMemChunk *mem_chunk);
38 gpointer        old_mem_chunk_alloc     (GMemChunk *mem_chunk);
39 gpointer        old_mem_chunk_alloc0    (GMemChunk *mem_chunk);
40 void            old_mem_chunk_free      (GMemChunk *mem_chunk,
41                                          gpointer   mem);
42 void            old_mem_chunk_clean     (GMemChunk *mem_chunk);
43 void            old_mem_chunk_reset     (GMemChunk *mem_chunk);
44 void            old_mem_chunk_print     (GMemChunk *mem_chunk);
45 void            old_mem_chunk_info      (void);
46 #ifndef G_ALLOC_AND_FREE
47 #define G_ALLOC_AND_FREE  2
48 #endif
49
50 /* --- functions --- */
51 static inline gpointer
52 memchunk_alloc (GMemChunk **memchunkp,
53                 guint       size)
54 {
55   size = MAX (size, 1);
56   if (G_UNLIKELY (!*memchunkp))
57     *memchunkp = old_mem_chunk_new ("", size, 4096, G_ALLOC_AND_FREE);
58   return old_mem_chunk_alloc (*memchunkp);
59 }
60
61 static inline void
62 memchunk_free (GMemChunk *memchunk,
63                gpointer   chunk)
64 {
65   old_mem_chunk_free (memchunk, chunk);
66   if (clean_memchunks)
67     old_mem_chunk_clean (memchunk);
68 }
69
70 static gpointer
71 test_memchunk_thread (gpointer data)
72 {
73   guint32 rand_accu = 2147483563;
74   /* initialize random numbers */
75   if (data)
76     rand_accu = *(guint32*) data;
77   else
78     {
79       struct timeval rand_tv;
80       gettimeofday (&rand_tv, NULL);
81       rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
82     }
83
84   /* prepare for memchunk creation */
85   GMemChunk **memchunks = g_alloca (sizeof (memchunks[0]) * prime_size);
86   memset (memchunks, 0, sizeof (memchunks[0]) * prime_size);
87
88   guint i, j;
89   guint8 **ps = g_new (guint8*, number_of_blocks);
90   guint   *ss = g_new (guint, number_of_blocks);
91   /* create number_of_blocks random sizes */
92   for (i = 0; i < number_of_blocks; i++)
93     ss[i] = quick_rand32() % prime_size;
94   /* allocate number_of_blocks blocks */
95   for (i = 0; i < number_of_blocks; i++)
96     ps[i] = memchunk_alloc (&memchunks[ss[i]], ss[i]);
97   for (j = 0; j < number_of_repetitions; j++)
98     {
99       /* free number_of_blocks/2 blocks */
100       for (i = 0; i < number_of_blocks; i += 2)
101         memchunk_free (memchunks[ss[i]], ps[i]);
102       /* allocate number_of_blocks/2 blocks with new sizes */
103       for (i = 0; i < number_of_blocks; i += 2)
104         {
105           ss[i] = quick_rand32() % prime_size;
106           ps[i] = memchunk_alloc (&memchunks[ss[i]], ss[i]);
107         }
108     }
109   /* free number_of_blocks blocks */
110   for (i = 0; i < number_of_blocks; i++)
111     memchunk_free (memchunks[ss[i]], ps[i]);
112   /* alloc and free many equally sized chunks in a row */
113   for (i = 0; i < number_of_repetitions; i++)
114     {
115       guint sz = quick_rand32() % prime_size;
116       guint k = number_of_blocks / 100;
117       for (j = 0; j < k; j++)
118         ps[j] = memchunk_alloc (&memchunks[sz], sz);
119       for (j = 0; j < k; j++)
120         memchunk_free (memchunks[sz], ps[j]);
121     }
122   /* cleanout memchunks */
123   for (i = 0; i < prime_size; i++)
124     if (memchunks[i])
125       old_mem_chunk_destroy (memchunks[i]);
126
127   return NULL;
128 }
129
130 static gpointer
131 test_sliced_mem_thread (gpointer data)
132 {
133   guint32 rand_accu = 2147483563;
134   /* initialize random numbers */
135   if (data)
136     rand_accu = *(guint32*) data;
137   else
138     {
139       struct timeval rand_tv;
140       gettimeofday (&rand_tv, NULL);
141       rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
142     }
143
144   guint i, j;
145   guint8 **ps = g_new (guint8*, number_of_blocks);
146   guint   *ss = g_new (guint, number_of_blocks);
147   /* create number_of_blocks random sizes */
148   for (i = 0; i < number_of_blocks; i++)
149     ss[i] = quick_rand32() % prime_size;
150   /* allocate number_of_blocks blocks */
151   for (i = 0; i < number_of_blocks; i++)
152     ps[i] = g_slice_alloc (ss[i]);
153   for (j = 0; j < number_of_repetitions; j++)
154     {
155       /* free number_of_blocks/2 blocks */
156       for (i = 0; i < number_of_blocks; i += 2)
157         g_slice_free1 (ss[i], ps[i]);
158       /* allocate number_of_blocks/2 blocks with new sizes */
159       for (i = 0; i < number_of_blocks; i += 2)
160         {
161           ss[i] = quick_rand32() % prime_size;
162           ps[i] = g_slice_alloc (ss[i]);
163         }
164     }
165   /* free number_of_blocks blocks */
166   for (i = 0; i < number_of_blocks; i++)
167     g_slice_free1 (ss[i], ps[i]);
168   /* alloc and free many equally sized chunks in a row */
169   for (i = 0; i < number_of_repetitions; i++)
170     {
171       guint sz = quick_rand32() % prime_size;
172       guint k = number_of_blocks / 100;
173       for (j = 0; j < k; j++)
174         ps[j] = g_slice_alloc (sz);
175       for (j = 0; j < k; j++)
176         g_slice_free1 (sz, ps[j]);
177     }
178
179   return NULL;
180 }
181
182 static void
183 usage (void)
184 {
185   g_print ("Usage: gslicedmemory [n_threads] [G|S|M|O][f][c] [maxblocksize] [seed]\n");
186 }
187
188 int
189 main (int   argc,
190       char *argv[])
191 {
192   guint seed32, *seedp = NULL;
193   gboolean ccounters = FALSE, use_memchunks = FALSE;
194   guint n_threads = 1;
195   const gchar *mode = "slab allocator + magazine cache", *emode = " ";
196   if (argc > 1)
197     n_threads = g_ascii_strtoull (argv[1], NULL, 10);
198   if (argc > 2)
199     {
200       guint i, l = strlen (argv[2]);
201       for (i = 0; i < l; i++)
202         switch (argv[2][i])
203           {
204           case 'G': /* GLib mode */
205             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE);
206             g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, FALSE);
207             mode = "slab allocator + magazine cache";
208             break;
209           case 'S': /* slab mode */
210             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE);
211             g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, TRUE);
212             mode = "slab allocator";
213             break;
214           case 'M': /* malloc mode */
215             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
216             mode = "system malloc";
217             break;
218           case 'O': /* old memchunks */
219             use_memchunks = TRUE;
220             mode = "old memchunks";
221             break;
222           case 'f': /* eager freeing */
223             g_slice_set_config (G_SLICE_CONFIG_WORKING_SET_MSECS, 0);
224             clean_memchunks = TRUE;
225             emode = " with eager freeing";
226             break;
227           case 'c': /* print contention counters */
228             ccounters = TRUE;
229             break;
230           default:
231             usage();
232             return 1;
233           }
234     }
235   if (argc > 3)
236     prime_size = g_ascii_strtoull (argv[3], NULL, 10);
237   if (argc > 4)
238     {
239       seed32 = g_ascii_strtoull (argv[4], NULL, 10);
240       seedp = &seed32;
241     }
242
243   g_thread_init (NULL);
244
245   if (argc <= 1)
246     usage();
247
248   gchar strseed[64] = "<random>";
249   if (seedp)
250     g_snprintf (strseed, 64, "%u", *seedp);
251   g_print ("Starting %d threads allocating random blocks <= %u bytes with seed=%s using %s%s\n", n_threads, prime_size, strseed, mode, emode);
252   
253   GThread *threads[n_threads];
254   guint i;
255   if (!use_memchunks)
256     for (i = 0; i < n_threads; i++)
257       threads[i] = g_thread_create_full (test_sliced_mem_thread, seedp, 0, TRUE, FALSE, 0, NULL);
258   else
259     {
260       old_mem_chunks_init();
261       for (i = 0; i < n_threads; i++)
262         threads[i] = g_thread_create_full (test_memchunk_thread, seedp, 0, TRUE, FALSE, 0, NULL);
263     }
264   for (i = 0; i < n_threads; i++)
265     g_thread_join (threads[i]);
266   
267   if (ccounters)
268     {
269       guint n, n_chunks = g_slice_get_config (G_SLICE_CONFIG_CHUNK_SIZES);
270       g_print ("    ChunkSize | MagazineSize | Contention\n");
271       for (i = 0; i < n_chunks; i++)
272         {
273           gint64 *vals = g_slice_get_config_state (G_SLICE_CONFIG_CONTENTION_COUNTER, i, &n);
274           g_print ("  %9llu   |  %9llu   |  %9llu\n", vals[0], vals[2], vals[1]);
275           g_free (vals);
276         }
277     }
278   else
279     g_print ("Done.\n");
280   return 0;
281 }