mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-11 19:54:05 +02:00
Merge branch 'slice_tests' into 'main'
Move test files on slices from tests/ to glib/tests/ See merge request GNOME/glib!2564
This commit is contained in:
@@ -98,6 +98,10 @@ glib_tests = {
|
|||||||
},
|
},
|
||||||
'shell' : {},
|
'shell' : {},
|
||||||
'slice' : {},
|
'slice' : {},
|
||||||
|
'slice-color' : {
|
||||||
|
'extra_sources' : ['memchunks.c'],
|
||||||
|
},
|
||||||
|
'slice-concurrent' : {},
|
||||||
'slist' : {},
|
'slist' : {},
|
||||||
'sort' : {},
|
'sort' : {},
|
||||||
'spawn-multithreaded' : {},
|
'spawn-multithreaded' : {},
|
||||||
|
133
glib/tests/slice-color.c
Normal file
133
glib/tests/slice-color.c
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
/* GLIB sliced memory - fast threaded memory chunk allocator
|
||||||
|
* Copyright (C) 2005 Tim Janik
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* 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, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
#define ALIGN(size, base) \
|
||||||
|
((base) * (gsize) (((size) + (base) - 1) / (base)))
|
||||||
|
|
||||||
|
static void
|
||||||
|
fill_memory (guint **mem,
|
||||||
|
guint n,
|
||||||
|
guint val)
|
||||||
|
{
|
||||||
|
guint j;
|
||||||
|
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
mem[j][0] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
static guint64
|
||||||
|
access_memory3 (guint **mema,
|
||||||
|
guint **memb,
|
||||||
|
guint **memd,
|
||||||
|
guint n,
|
||||||
|
guint64 repeats)
|
||||||
|
{
|
||||||
|
guint64 accu = 0, i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < repeats; i++)
|
||||||
|
{
|
||||||
|
for (j = 1; j < n; j += 2)
|
||||||
|
memd[j][0] = mema[j][0] + memb[j][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < repeats; i++)
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
accu += memd[j][0];
|
||||||
|
|
||||||
|
return accu;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
touch_mem (guint64 block_size,
|
||||||
|
guint64 n_blocks,
|
||||||
|
guint64 repeats)
|
||||||
|
{
|
||||||
|
GTimer *timer;
|
||||||
|
guint **mema, **memb, **memc;
|
||||||
|
guint64 j, accu, n = n_blocks;
|
||||||
|
|
||||||
|
mema = g_new (guint*, n);
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
mema[j] = g_slice_alloc (block_size);
|
||||||
|
|
||||||
|
memb = g_new (guint*, n);
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
memb[j] = g_slice_alloc (block_size);
|
||||||
|
|
||||||
|
memc = g_new (guint*, n);
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
memc[j] = g_slice_alloc (block_size);
|
||||||
|
|
||||||
|
timer = g_timer_new();
|
||||||
|
|
||||||
|
fill_memory (mema, n, 2);
|
||||||
|
fill_memory (memb, n, 3);
|
||||||
|
fill_memory (memc, n, 4);
|
||||||
|
|
||||||
|
access_memory3 (mema, memb, memc, n, 3);
|
||||||
|
|
||||||
|
g_timer_start (timer);
|
||||||
|
accu = access_memory3 (mema, memb, memc, n, repeats);
|
||||||
|
g_timer_stop (timer);
|
||||||
|
|
||||||
|
g_test_message ("Access-time = %fs", g_timer_elapsed (timer, NULL));
|
||||||
|
g_assert_cmpuint (accu / repeats, ==, (2 + 3) * n / 2 + 4 * n / 2);
|
||||||
|
|
||||||
|
for (j = 0; j < n; j++)
|
||||||
|
{
|
||||||
|
g_slice_free1 (block_size, mema[j]);
|
||||||
|
g_slice_free1 (block_size, memb[j]);
|
||||||
|
g_slice_free1 (block_size, memc[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_timer_destroy (timer);
|
||||||
|
g_free (mema);
|
||||||
|
g_free (memb);
|
||||||
|
g_free (memc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_slice_colors (void)
|
||||||
|
{
|
||||||
|
guint64 block_size = 512;
|
||||||
|
guint64 area_size = 1024 * 1024;
|
||||||
|
guint64 n_blocks, repeats = 1000000;
|
||||||
|
|
||||||
|
/* figure number of blocks from block and area size.
|
||||||
|
* divide area by 3 because touch_mem() allocates 3 areas */
|
||||||
|
n_blocks = area_size / 3 / ALIGN (block_size, sizeof (gsize) * 2);
|
||||||
|
|
||||||
|
g_test_message ("Allocate and touch %" G_GUINT64_FORMAT
|
||||||
|
" blocks of %" G_GUINT64_FORMAT " bytes"
|
||||||
|
" (= %" G_GUINT64_FORMAT " bytes) %" G_GUINT64_FORMAT
|
||||||
|
" times with color increment",
|
||||||
|
n_blocks, block_size, n_blocks * block_size, repeats);
|
||||||
|
|
||||||
|
touch_mem (block_size, n_blocks, repeats);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char **argv)
|
||||||
|
{
|
||||||
|
g_test_init (&argc, &argv, NULL);
|
||||||
|
|
||||||
|
g_test_add_func ("/slice/colors", test_slice_colors);
|
||||||
|
|
||||||
|
return g_test_run ();
|
||||||
|
}
|
@@ -15,19 +15,18 @@
|
|||||||
* You should have received a copy of the GNU Lesser General Public
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include <glib.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#ifdef G_OS_UNIX
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define N_THREADS 8
|
#include <glib.h>
|
||||||
#define N_ALLOCS 50000
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define N_THREADS 8
|
||||||
|
#define N_ALLOCS 50000
|
||||||
#define MAX_BLOCK_SIZE 64
|
#define MAX_BLOCK_SIZE 64
|
||||||
|
|
||||||
struct ThreadData
|
struct ThreadData
|
||||||
{
|
{
|
||||||
int thread_id;
|
int thread_id;
|
||||||
GThread* gthread;
|
GThread* gthread;
|
||||||
|
|
||||||
GMutex to_free_mutex;
|
GMutex to_free_mutex;
|
||||||
@@ -40,25 +39,23 @@ struct ThreadData
|
|||||||
static void *
|
static void *
|
||||||
thread_func (void *arg)
|
thread_func (void *arg)
|
||||||
{
|
{
|
||||||
struct ThreadData *td = arg;
|
|
||||||
int i;
|
int i;
|
||||||
/* g_print ("Thread %d starting\n", td->thread_id); */
|
struct ThreadData *td = arg;
|
||||||
|
|
||||||
for (i = 0; i < N_ALLOCS; i++)
|
for (i = 0; i < N_ALLOCS; i++)
|
||||||
{
|
{
|
||||||
int bytes;
|
int bytes, f, t;
|
||||||
char *mem;
|
char *mem;
|
||||||
int f;
|
|
||||||
int t;
|
|
||||||
|
|
||||||
if (rand() % (N_ALLOCS / 20) == 0)
|
if (rand() % (N_ALLOCS / 20) == 0)
|
||||||
g_print ("%c", 'a' - 1 + td->thread_id);
|
g_test_message ("%c", 'a' - 1 + td->thread_id);
|
||||||
|
|
||||||
/* allocate block of random size and randomly fill */
|
/* allocate block of random size and randomly fill */
|
||||||
bytes = rand() % MAX_BLOCK_SIZE + 1;
|
bytes = rand() % MAX_BLOCK_SIZE + 1;
|
||||||
mem = g_slice_alloc (bytes);
|
mem = g_slice_alloc (bytes);
|
||||||
|
|
||||||
for (f = 0; f < bytes; f++)
|
for (f = 0; f < bytes; f++)
|
||||||
mem[f] = rand();
|
mem[f] = rand();
|
||||||
|
|
||||||
/* associate block with random thread */
|
/* associate block with random thread */
|
||||||
t = rand() % N_THREADS;
|
t = rand() % N_THREADS;
|
||||||
@@ -80,19 +77,20 @@ thread_func (void *arg)
|
|||||||
/* free a block associated with this thread */
|
/* free a block associated with this thread */
|
||||||
g_mutex_lock (&td->to_free_mutex);
|
g_mutex_lock (&td->to_free_mutex);
|
||||||
if (td->n_to_free > 0)
|
if (td->n_to_free > 0)
|
||||||
{
|
{
|
||||||
td->n_to_free--;
|
td->n_to_free--;
|
||||||
g_slice_free1 (td->bytes_to_free[td->n_to_free], td->to_free[td->n_to_free]);
|
g_slice_free1 (td->bytes_to_free[td->n_to_free],
|
||||||
td->n_freed++;
|
td->to_free[td->n_to_free]);
|
||||||
}
|
td->n_freed++;
|
||||||
|
}
|
||||||
g_mutex_unlock (&td->to_free_mutex);
|
g_mutex_unlock (&td->to_free_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
static void
|
||||||
main (void)
|
test_concurrent_slice (void)
|
||||||
{
|
{
|
||||||
int t;
|
int t;
|
||||||
|
|
||||||
@@ -102,21 +100,31 @@ main (void)
|
|||||||
tdata[t].n_to_free = 0;
|
tdata[t].n_to_free = 0;
|
||||||
tdata[t].n_freed = 0;
|
tdata[t].n_freed = 0;
|
||||||
}
|
}
|
||||||
g_print ("Starting %d threads for concurrent GSlice usage...\n", N_THREADS);
|
|
||||||
for (t = 0; t < N_THREADS; t++)
|
for (t = 0; t < N_THREADS; t++)
|
||||||
{
|
{
|
||||||
tdata[t].gthread = g_thread_create (thread_func, &tdata[t], TRUE, NULL);
|
tdata[t].gthread = g_thread_new (NULL, thread_func, &tdata[t]);
|
||||||
g_assert (tdata[t].gthread != NULL);
|
g_assert_nonnull (tdata[t].gthread);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (t = 0; t < N_THREADS; t++)
|
for (t = 0; t < N_THREADS; t++)
|
||||||
{
|
{
|
||||||
g_thread_join (tdata[t].gthread);
|
g_thread_join (tdata[t].gthread);
|
||||||
}
|
}
|
||||||
g_print ("\n");
|
|
||||||
for (t = 0; t < N_THREADS; t++)
|
for (t = 0; t < N_THREADS; t++)
|
||||||
{
|
{
|
||||||
g_print ("Thread %d: %d blocks freed, %d blocks not freed\n",
|
g_test_message ("Thread %d: %d blocks freed, %d blocks not freed",
|
||||||
tdata[t].thread_id, tdata[t].n_freed, tdata[t].n_to_free);
|
tdata[t].thread_id, tdata[t].n_freed, tdata[t].n_to_free);
|
||||||
}
|
}
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char **argv)
|
||||||
|
{
|
||||||
|
g_test_init (&argc, &argv, NULL);
|
||||||
|
|
||||||
|
g_test_add_func ("/slice/concurrent", test_concurrent_slice);
|
||||||
|
|
||||||
|
return g_test_run ();
|
||||||
}
|
}
|
@@ -20,7 +20,6 @@ tests = {
|
|||||||
'mainloop-test' : {},
|
'mainloop-test' : {},
|
||||||
'mapping-test' : {},
|
'mapping-test' : {},
|
||||||
'onceinit' : {},
|
'onceinit' : {},
|
||||||
'slice-concurrent' : {},
|
|
||||||
'slice-threadinit' : {
|
'slice-threadinit' : {
|
||||||
'dependencies' : [libgthread_dep],
|
'dependencies' : [libgthread_dep],
|
||||||
},
|
},
|
||||||
@@ -46,9 +45,6 @@ test_extra_programs = {
|
|||||||
'slice-test' : {
|
'slice-test' : {
|
||||||
'extra_sources' : ['memchunks.c'],
|
'extra_sources' : ['memchunks.c'],
|
||||||
},
|
},
|
||||||
'slice-color' : {
|
|
||||||
'extra_sources' : ['memchunks.c'],
|
|
||||||
},
|
|
||||||
'assert-msg-test' : {},
|
'assert-msg-test' : {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,177 +0,0 @@
|
|||||||
/* GLIB sliced memory - fast threaded memory chunk allocator
|
|
||||||
* Copyright (C) 2005 Tim Janik
|
|
||||||
*
|
|
||||||
* This library is free software; you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
|
||||||
* License as published by the Free Software Foundation; either
|
|
||||||
* version 2.1 of the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This library is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* 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, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#include <glib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base)))
|
|
||||||
|
|
||||||
static gdouble parse_memsize (const gchar *cstring);
|
|
||||||
static void usage (void);
|
|
||||||
|
|
||||||
static void
|
|
||||||
fill_memory (guint **mem,
|
|
||||||
guint n,
|
|
||||||
guint val)
|
|
||||||
{
|
|
||||||
guint j, o = 0;
|
|
||||||
for (j = 0; j < n; j++)
|
|
||||||
mem[j][o] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
static guint64
|
|
||||||
access_memory3 (guint **mema,
|
|
||||||
guint **memb,
|
|
||||||
guint **memd,
|
|
||||||
guint n,
|
|
||||||
guint64 repeats)
|
|
||||||
{
|
|
||||||
guint64 accu = 0, i, j;
|
|
||||||
const guint o = 0;
|
|
||||||
for (i = 0; i < repeats; i++)
|
|
||||||
{
|
|
||||||
for (j = 1; j < n; j += 2)
|
|
||||||
memd[j][o] = mema[j][o] + memb[j][o];
|
|
||||||
}
|
|
||||||
for (i = 0; i < repeats; i++)
|
|
||||||
for (j = 0; j < n; j++)
|
|
||||||
accu += memd[j][o];
|
|
||||||
return accu;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
touch_mem (guint64 block_size,
|
|
||||||
guint64 n_blocks,
|
|
||||||
guint64 repeats)
|
|
||||||
{
|
|
||||||
guint64 j, accu, n = n_blocks;
|
|
||||||
GTimer *timer;
|
|
||||||
guint **memc;
|
|
||||||
guint **memb;
|
|
||||||
guint **mema = g_new (guint*, n);
|
|
||||||
for (j = 0; j < n; j++)
|
|
||||||
mema[j] = g_slice_alloc (block_size);
|
|
||||||
memb = g_new (guint*, n);
|
|
||||||
for (j = 0; j < n; j++)
|
|
||||||
memb[j] = g_slice_alloc (block_size);
|
|
||||||
memc = g_new (guint*, n);
|
|
||||||
for (j = 0; j < n; j++)
|
|
||||||
memc[j] = g_slice_alloc (block_size);
|
|
||||||
|
|
||||||
timer = g_timer_new();
|
|
||||||
fill_memory (mema, n, 2);
|
|
||||||
fill_memory (memb, n, 3);
|
|
||||||
fill_memory (memc, n, 4);
|
|
||||||
access_memory3 (mema, memb, memc, n, 3);
|
|
||||||
g_timer_start (timer);
|
|
||||||
accu = access_memory3 (mema, memb, memc, n, repeats);
|
|
||||||
g_timer_stop (timer);
|
|
||||||
|
|
||||||
g_print ("Access-time = %fs\n", g_timer_elapsed (timer, NULL));
|
|
||||||
g_assert (accu / repeats == (2 + 3) * n / 2 + 4 * n / 2);
|
|
||||||
|
|
||||||
for (j = 0; j < n; j++)
|
|
||||||
{
|
|
||||||
g_slice_free1 (block_size, mema[j]);
|
|
||||||
g_slice_free1 (block_size, memb[j]);
|
|
||||||
g_slice_free1 (block_size, memc[j]);
|
|
||||||
}
|
|
||||||
g_timer_destroy (timer);
|
|
||||||
g_free (mema);
|
|
||||||
g_free (memb);
|
|
||||||
g_free (memc);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
usage (void)
|
|
||||||
{
|
|
||||||
g_print ("Usage: slice-color <block-size> [memory-size] [repeats] [colorization]\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
main (int argc,
|
|
||||||
char *argv[])
|
|
||||||
{
|
|
||||||
guint64 block_size = 512, area_size = 1024 * 1024, n_blocks, repeats = 1000000;
|
|
||||||
|
|
||||||
if (argc > 1)
|
|
||||||
block_size = parse_memsize (argv[1]);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
usage();
|
|
||||||
block_size = 512;
|
|
||||||
}
|
|
||||||
if (argc > 2)
|
|
||||||
area_size = parse_memsize (argv[2]);
|
|
||||||
if (argc > 3)
|
|
||||||
repeats = parse_memsize (argv[3]);
|
|
||||||
if (argc > 4)
|
|
||||||
g_slice_set_config (G_SLICE_CONFIG_COLOR_INCREMENT, parse_memsize (argv[4]));
|
|
||||||
|
|
||||||
/* figure number of blocks from block and area size.
|
|
||||||
* divide area by 3 because touch_mem() allocates 3 areas
|
|
||||||
*/
|
|
||||||
n_blocks = area_size / 3 / ALIGN (block_size, sizeof (gsize) * 2);
|
|
||||||
|
|
||||||
/* basic sanity checks */
|
|
||||||
if (!block_size || !n_blocks || block_size >= area_size)
|
|
||||||
{
|
|
||||||
g_printerr ("Invalid arguments: block-size=%" G_GUINT64_FORMAT " memory-size=%" G_GUINT64_FORMAT "\n", block_size, area_size);
|
|
||||||
usage();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_printerr ("Will allocate and touch %" G_GUINT64_FORMAT " blocks of %" G_GUINT64_FORMAT " bytes (= %" G_GUINT64_FORMAT " bytes) %" G_GUINT64_FORMAT " times with color increment: 0x%08" G_GINT64_MODIFIER "x\n",
|
|
||||||
n_blocks, block_size, n_blocks * block_size, repeats,
|
|
||||||
(guint64)g_slice_get_config (G_SLICE_CONFIG_COLOR_INCREMENT));
|
|
||||||
|
|
||||||
touch_mem (block_size, n_blocks, repeats);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gdouble
|
|
||||||
parse_memsize (const gchar *cstring)
|
|
||||||
{
|
|
||||||
gchar *mem = g_strdup (cstring);
|
|
||||||
gchar *string = g_strstrip (mem);
|
|
||||||
guint l = strlen (string);
|
|
||||||
gdouble f = 0;
|
|
||||||
gchar *derr = NULL;
|
|
||||||
gdouble msize;
|
|
||||||
|
|
||||||
switch (l ? string[l - 1] : 0)
|
|
||||||
{
|
|
||||||
case 'k': f = 1000; break;
|
|
||||||
case 'K': f = 1024; break;
|
|
||||||
case 'm': f = 1000000; break;
|
|
||||||
case 'M': f = 1024 * 1024; break;
|
|
||||||
case 'g': f = 1000000000; break;
|
|
||||||
case 'G': f = 1024 * 1024 * 1024; break;
|
|
||||||
}
|
|
||||||
if (f)
|
|
||||||
string[l - 1] = 0;
|
|
||||||
msize = g_ascii_strtod (string, &derr);
|
|
||||||
g_free (mem);
|
|
||||||
if (derr && *derr)
|
|
||||||
{
|
|
||||||
g_printerr ("failed to parse number at: %s\n", derr);
|
|
||||||
msize = 0;
|
|
||||||
}
|
|
||||||
if (f)
|
|
||||||
msize *= f;
|
|
||||||
return msize;
|
|
||||||
}
|
|
Reference in New Issue
Block a user