1 /* GLIB sliced memory - fast threaded memory chunk allocator
2 * Copyright (C) 2005 Tim Janik
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.
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.
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.
23 #include <sys/time.h> // gettimeofday
25 #define quick_rand32() (rand_accu = 1664525 * rand_accu + 1013904223, rand_accu)
26 static guint prime_size = 1021; // 769; // 509
29 test_sliced_mem_thread (gpointer data)
31 guint32 rand_accu = 2147483563;
32 /* initialize random numbers */
34 rand_accu = *(guint32*) data;
37 struct timeval rand_tv;
38 gettimeofday (&rand_tv, NULL);
39 rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
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++)
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)
60 ss[i] = quick_rand32() % prime_size;
61 ps[i] = g_slice_alloc (ss[i]);
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++)
70 guint sz = quick_rand32() % prime_size;
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]);
84 g_print ("Usage: gslicedmemory [n_threads] [G|S|M][f][c] [maxblocksize] [seed]\n");
91 guint seed32, *seedp = NULL;
92 gboolean ccounters = FALSE;
94 const gchar *mode = "slab allocator + magazine cache", *emode = " ";
96 n_threads = g_ascii_strtoull (argv[1], NULL, 10);
99 guint i, l = strlen (argv[2]);
100 for (i = 0; i < l; i++)
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";
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";
113 case 'M': /* malloc mode */
114 g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
115 mode = "system malloc";
117 case 'f': /* eager freeing */
118 g_slice_set_config (G_SLICE_CONFIG_ALWAYS_FREE, TRUE);
119 emode = " with eager freeing";
121 case 'c': /* print contention counters */
130 prime_size = g_ascii_strtoull (argv[3], NULL, 10);
133 seed32 = g_ascii_strtoull (argv[4], NULL, 10);
137 g_thread_init (NULL);
142 gchar strseed[64] = "<random>";
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);
147 GThread *threads[n_threads];
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]);
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++)
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]);