new slice allocator implementation.
[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
28 static gpointer
29 test_sliced_mem_thread (gpointer data)
30 {
31   guint32 rand_accu = 2147483563;
32   /* initialize random numbers */
33   if (data)
34     rand_accu = *(guint32*) data;
35   else
36     {
37       struct timeval rand_tv;
38       gettimeofday (&rand_tv, NULL);
39       rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
40     }
41
42   guint i, m = 10000;   /* number of blocks */
43   guint j, n = 10000;   /* number of alloc+free repetitions */
44   guint8 **ps = g_new (guint8*, m);
45   guint   *ss = g_new (guint, m);
46   /* create m random sizes */
47   for (i = 0; i < m; i++)
48     ss[i] = quick_rand32() % prime_size;
49   /* allocate m blocks */
50   for (i = 0; i < m; i++)
51     ps[i] = g_slice_alloc (ss[i]);
52   for (j = 0; j < n; j++)
53     {
54       /* free m/2 blocks */
55       for (i = 0; i < m; i += 2)
56         g_slice_free1 (ss[i], ps[i]);
57       /* allocate m/2 blocks with new sizes */
58       for (i = 0; i < m; i += 2)
59         {
60           ss[i] = quick_rand32() % prime_size;
61           ps[i] = g_slice_alloc (ss[i]);
62         }
63     }
64   /* free m blocks */
65   for (i = 0; i < m; i++)
66     g_slice_free1 (ss[i], ps[i]);
67   /* alloc and free many equally sized chunks in a row */
68   for (i = 0; i < n; i++)
69     {
70       guint sz = quick_rand32() % prime_size;
71       guint k = m / 100;
72       for (j = 0; j < k; j++)
73         ps[j] = g_slice_alloc (sz);
74       for (j = 0; j < k; j++)
75         g_slice_free1 (sz, ps[j]);
76     }
77
78   return NULL;
79 }
80
81 static void
82 usage (void)
83 {
84   g_print ("Usage: gslicedmemory [n_threads] [G|S|M][f][c] [maxblocksize] [seed]\n");
85 }
86
87 int
88 main (int   argc,
89       char *argv[])
90 {
91   guint seed32, *seedp = NULL;
92   gboolean ccounters = FALSE;
93   guint n_threads = 1;
94   const gchar *mode = "slab allocator + magazine cache", *emode = " ";
95   if (argc > 1)
96     n_threads = g_ascii_strtoull (argv[1], NULL, 10);
97   if (argc > 2)
98     {
99       guint i, l = strlen (argv[2]);
100       for (i = 0; i < l; i++)
101         switch (argv[2][i])
102           {
103           case 'G': /* GLib mode */
104             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE);
105             g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, FALSE);
106             mode = "slab allocator + magazine cache";
107             break;
108           case 'S': /* slab mode */
109             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE);
110             g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, TRUE);
111             mode = "slab allocator";
112             break;
113           case 'M': /* malloc mode */
114             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
115             mode = "system malloc";
116             break;
117           case 'f': /* eager freeing */
118             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_FREE, TRUE);
119             emode = " with eager freeing";
120             break;
121           case 'c': /* print contention counters */
122             ccounters = TRUE;
123             break;
124           default:
125             usage();
126             return 1;
127           }
128     }
129   if (argc > 3)
130     prime_size = g_ascii_strtoull (argv[3], NULL, 10);
131   if (argc > 4)
132     {
133       seed32 = g_ascii_strtoull (argv[4], NULL, 10);
134       seedp = &seed32;
135     }
136
137   g_thread_init (NULL);
138
139   if (argc <= 1)
140     usage();
141
142   gchar strseed[64] = "<random>";
143   if (seedp)
144     g_snprintf (strseed, 64, "%u", *seedp);
145   g_print ("Starting %d threads allocating random blocks <= %u bytes with seed=%s using %s%s\n", n_threads, prime_size, strseed, mode, emode);
146   
147   GThread *threads[n_threads];
148   guint i;
149   for (i = 0; i < n_threads; i++)
150     threads[i] = g_thread_create_full (test_sliced_mem_thread, seedp, 0, TRUE, FALSE, 0, NULL);
151   for (i = 0; i < n_threads; i++)
152     g_thread_join (threads[i]);
153   
154   if (ccounters)
155     {
156       guint n, n_chunks = g_slice_get_config (G_SLICE_CONFIG_CHUNK_SIZES);
157       g_print ("    ChunkSize | MagazineSize | Contention\n");
158       for (i = 0; i < n_chunks; i++)
159         {
160           gint64 *vals = g_slice_get_config_state (G_SLICE_CONFIG_CONTENTION_COUNTER, i, &n);
161           g_print ("  %9llu   |  %9llu   |  %9llu\n", vals[0], vals[2], vals[1]);
162           g_free (vals);
163         }
164     }
165   else
166     g_print ("Done.\n");
167   return 0;
168 }