* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <stdio.h>
#include <string.h>
-#include <sys/time.h> // gettimeofday
#define quick_rand32() (rand_accu = 1664525 * rand_accu + 1013904223, rand_accu)
-static guint prime_size = 1021; // 769; // 509
+static guint prime_size = 1021; /* 769; 509 */
static gboolean clean_memchunks = FALSE;
static guint number_of_blocks = 10000; /* total number of blocks allocated */
static guint number_of_repetitions = 10000; /* number of alloc+free repetitions */
+static gboolean want_corruption = FALSE;
/* --- old memchunk prototypes (memchunks.c) --- */
-void old_mem_chunks_init (void);
GMemChunk* old_mem_chunk_new (const gchar *name,
gint atom_size,
gulong area_size,
#endif
/* --- functions --- */
+static inline int
+corruption (void)
+{
+ if (G_UNLIKELY (want_corruption))
+ {
+ /* corruption per call likelyness is about 1:4000000 */
+ guint32 r = g_random_int() % 8000009;
+ return r == 277 ? +1 : r == 281 ? -1 : 0;
+ }
+ return 0;
+}
+
static inline gpointer
memchunk_alloc (GMemChunk **memchunkp,
guint size)
rand_accu = *(guint32*) data;
else
{
- struct timeval rand_tv;
- gettimeofday (&rand_tv, NULL);
+ GTimeVal rand_tv;
+ g_get_current_time (&rand_tv);
rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
}
test_sliced_mem_thread (gpointer data)
{
guint32 rand_accu = 2147483563;
+ guint i, j;
+ guint8 **ps;
+ guint *ss;
+
/* initialize random numbers */
if (data)
rand_accu = *(guint32*) data;
else
{
- struct timeval rand_tv;
- gettimeofday (&rand_tv, NULL);
+ GTimeVal rand_tv;
+ g_get_current_time (&rand_tv);
rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
}
- guint i, j;
- guint8 **ps = g_new (guint8*, number_of_blocks);
- guint *ss = g_new (guint, number_of_blocks);
+ ps = g_new (guint8*, number_of_blocks);
+ ss = g_new (guint, number_of_blocks);
/* create number_of_blocks random sizes */
for (i = 0; i < number_of_blocks; i++)
ss[i] = quick_rand32() % prime_size;
/* allocate number_of_blocks blocks */
for (i = 0; i < number_of_blocks; i++)
- ps[i] = g_slice_alloc (ss[i]);
+ ps[i] = g_slice_alloc (ss[i] + corruption());
for (j = 0; j < number_of_repetitions; j++)
{
/* free number_of_blocks/2 blocks */
for (i = 0; i < number_of_blocks; i += 2)
- g_slice_free1 (ss[i], ps[i]);
+ g_slice_free1 (ss[i] + corruption(), ps[i] + corruption());
/* allocate number_of_blocks/2 blocks with new sizes */
for (i = 0; i < number_of_blocks; i += 2)
{
ss[i] = quick_rand32() % prime_size;
- ps[i] = g_slice_alloc (ss[i]);
+ ps[i] = g_slice_alloc (ss[i] + corruption());
}
}
/* free number_of_blocks blocks */
for (i = 0; i < number_of_blocks; i++)
- g_slice_free1 (ss[i], ps[i]);
+ g_slice_free1 (ss[i] + corruption(), ps[i] + corruption());
/* alloc and free many equally sized chunks in a row */
for (i = 0; i < number_of_repetitions; i++)
{
guint sz = quick_rand32() % prime_size;
guint k = number_of_blocks / 100;
for (j = 0; j < k; j++)
- ps[j] = g_slice_alloc (sz);
+ ps[j] = g_slice_alloc (sz + corruption());
for (j = 0; j < k; j++)
- g_slice_free1 (sz, ps[j]);
+ g_slice_free1 (sz + corruption(), ps[j] + corruption());
}
g_free (ps);
g_free (ss);
static void
usage (void)
{
- g_print ("Usage: gslicedmemory [n_threads] [G|S|M|O][f][c] [maxblocksize] [seed]\n");
+ g_print ("Usage: slice-test [n_threads] [G|S|M|O][f][c][~] [maxblocksize] [seed]\n");
}
int
case 'c': /* print contention counters */
ccounters = TRUE;
break;
+ case '~':
+ want_corruption = TRUE; /* force occasional corruption */
+ break;
default:
usage();
return 1;
seedp = &seed32;
}
- g_thread_init (NULL);
-
if (argc <= 1)
usage();
- gchar strseed[64] = "<random>";
- if (seedp)
- g_snprintf (strseed, 64, "%u", *seedp);
- g_print ("Starting %d threads allocating random blocks <= %u bytes with seed=%s using %s%s\n", n_threads, prime_size, strseed, mode, emode);
+ {
+ gchar strseed[64] = "<random>";
+ GThread **threads;
+ guint i;
+
+ if (seedp)
+ g_snprintf (strseed, 64, "%u", *seedp);
+ g_print ("Starting %d threads allocating random blocks <= %u bytes with seed=%s using %s%s\n", n_threads, prime_size, strseed, mode, emode);
- GThread *threads[n_threads];
- guint i;
- if (!use_memchunks)
- for (i = 0; i < n_threads; i++)
- threads[i] = g_thread_create_full (test_sliced_mem_thread, seedp, 0, TRUE, FALSE, 0, NULL);
- else
- {
- old_mem_chunks_init();
+ threads = g_alloca (sizeof(GThread*) * n_threads);
+ if (!use_memchunks)
for (i = 0; i < n_threads; i++)
- threads[i] = g_thread_create_full (test_memchunk_thread, seedp, 0, TRUE, FALSE, 0, NULL);
- }
- for (i = 0; i < n_threads; i++)
- g_thread_join (threads[i]);
+ threads[i] = g_thread_create (test_sliced_mem_thread, seedp, TRUE, NULL);
+ else
+ {
+ for (i = 0; i < n_threads; i++)
+ threads[i] = g_thread_create (test_memchunk_thread, seedp, TRUE, NULL);
+ }
+ for (i = 0; i < n_threads; i++)
+ g_thread_join (threads[i]);
- if (ccounters)
- {
- guint n, n_chunks = g_slice_get_config (G_SLICE_CONFIG_CHUNK_SIZES);
- g_print (" ChunkSize | MagazineSize | Contention\n");
- for (i = 0; i < n_chunks; i++)
- {
- gint64 *vals = g_slice_get_config_state (G_SLICE_CONFIG_CONTENTION_COUNTER, i, &n);
- g_print (" %9llu | %9llu | %9llu\n", vals[0], vals[2], vals[1]);
- g_free (vals);
- }
- }
- else
- g_print ("Done.\n");
- return 0;
+ if (ccounters)
+ {
+ guint n, n_chunks = g_slice_get_config (G_SLICE_CONFIG_CHUNK_SIZES);
+ g_print (" ChunkSize | MagazineSize | Contention\n");
+ for (i = 0; i < n_chunks; i++)
+ {
+ gint64 *vals = g_slice_get_config_state (G_SLICE_CONFIG_CONTENTION_COUNTER, i, &n);
+ g_print (" %9" G_GINT64_FORMAT " | %9" G_GINT64_FORMAT " | %9" G_GINT64_FORMAT "\n", vals[0], vals[2], vals[1]);
+ g_free (vals);
+ }
+ }
+ else
+ g_print ("Done.\n");
+ return 0;
+ }
}