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.
22 #define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base)))
24 static gdouble parse_memsize (const gchar *cstring);
25 static void usage (void);
28 fill_memory (guint **mem,
33 for (j = 0; j < n; j++)
38 access_memory3 (guint **mema,
44 guint64 accu = 0, i, j;
46 for (i = 0; i < repeats; i++)
48 for (j = 1; j < n; j += 2)
49 memd[j][o] = mema[j][o] + memb[j][o];
51 for (i = 0; i < repeats; i++)
52 for (j = 0; j < n; j++)
58 touch_mem (guint64 block_size,
62 guint64 j, accu, n = n_blocks;
63 guint **mema = g_new (guint*, n);
64 for (j = 0; j < n; j++)
65 mema[j] = g_slice_alloc (block_size);
66 guint **memb = g_new (guint*, n);
67 for (j = 0; j < n; j++)
68 memb[j] = g_slice_alloc (block_size);
69 guint **memc = g_new (guint*, n);
70 for (j = 0; j < n; j++)
71 memc[j] = g_slice_alloc (block_size);
73 GTimer *timer = g_timer_new();
74 fill_memory (mema, n, 2);
75 fill_memory (memb, n, 3);
76 fill_memory (memc, n, 4);
77 access_memory3 (mema, memb, memc, n, 3);
78 g_timer_start (timer);
79 accu = access_memory3 (mema, memb, memc, n, repeats);
82 g_print ("Access-time = %fs\n", g_timer_elapsed (timer, NULL));
83 g_assert (accu / repeats == (2 + 3) * n / 2 + 4 * n / 2);
85 for (j = 0; j < n; j++)
87 g_slice_free1 (block_size, mema[j]);
88 g_slice_free1 (block_size, memb[j]);
89 g_slice_free1 (block_size, memc[j]);
91 g_timer_destroy (timer);
100 g_print ("Usage: slice-color <block-size> [memory-size] [repeats] [colorization]\n");
107 guint64 block_size = 512, area_size = 1024 * 1024, n_blocks, repeats = 1000000;
110 block_size = parse_memsize (argv[1]);
117 area_size = parse_memsize (argv[2]);
119 repeats = parse_memsize (argv[3]);
121 g_slice_set_config (G_SLICE_CONFIG_COLOR_INCREMENT, parse_memsize (argv[4]));
123 /* figure number of blocks from block and area size.
124 * divide area by 3 because touch_mem() allocates 3 areas
126 n_blocks = area_size / 3 / ALIGN (block_size, sizeof (gsize) * 2);
128 /* basic sanity checks */
129 if (!block_size || !n_blocks || block_size >= area_size)
131 g_printerr ("Invalid arguments: block-size=%llu memory-size=%llu\n", block_size, area_size);
136 g_printerr ("Will allocate and touch %llu blocks of %llu bytes (= %llu bytes) %llu times with color increment: 0x%08llx\n",
137 n_blocks, block_size, n_blocks * block_size, repeats, g_slice_get_config (G_SLICE_CONFIG_COLOR_INCREMENT));
139 touch_mem (block_size, n_blocks, repeats);
145 parse_memsize (const gchar *cstring)
147 gchar *mem = g_strdup (cstring);
148 gchar *string = g_strstrip (mem);
149 guint l = strlen (string);
151 switch (l ? string[l - 1] : 0)
153 case 'k': f = 1000; break;
154 case 'K': f = 1024; break;
155 case 'm': f = 1000000; break;
156 case 'M': f = 1024 * 1024; break;
157 case 'g': f = 1000000000; break;
158 case 'G': f = 1024 * 1024 * 1024; break;
163 gdouble msize = g_ascii_strtod (string, &derr);
167 g_printerr ("failed to parse number at: %s\n", derr);