Convert tests/slice-color.c to glib test framework

This commit is contained in:
Emmanuel Fleury 2022-03-20 19:51:06 +01:00 committed by Philip Withnall
parent 495508cf84
commit 6211971c41

View File

@ -14,22 +14,21 @@
* 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 <glib.h>
#include <string.h>
#define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base))) #define ALIGN(size, base) \
((base) * (gsize) (((size) + (base) - 1) / (base)))
static gdouble parse_memsize (const gchar *cstring);
static void usage (void);
static void static void
fill_memory (guint **mem, fill_memory (guint **mem,
guint n, guint n,
guint val) guint val)
{ {
guint j, o = 0; guint j;
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
mem[j][o] = val; mem[j][0] = val;
} }
static guint64 static guint64
@ -40,15 +39,17 @@ access_memory3 (guint **mema,
guint64 repeats) guint64 repeats)
{ {
guint64 accu = 0, i, j; guint64 accu = 0, i, j;
const guint o = 0;
for (i = 0; i < repeats; i++) for (i = 0; i < repeats; i++)
{ {
for (j = 1; j < n; j += 2) for (j = 1; j < n; j += 2)
memd[j][o] = mema[j][o] + memb[j][o]; memd[j][0] = mema[j][0] + memb[j][0];
} }
for (i = 0; i < repeats; i++) for (i = 0; i < repeats; i++)
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
accu += memd[j][o]; accu += memd[j][0];
return accu; return accu;
} }
@ -57,31 +58,36 @@ touch_mem (guint64 block_size,
guint64 n_blocks, guint64 n_blocks,
guint64 repeats) guint64 repeats)
{ {
guint64 j, accu, n = n_blocks;
GTimer *timer; GTimer *timer;
guint **memc; guint **mema, **memb, **memc;
guint **memb; guint64 j, accu, n = n_blocks;
guint **mema = g_new (guint*, n);
mema = g_new (guint*, n);
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
mema[j] = g_slice_alloc (block_size); mema[j] = g_slice_alloc (block_size);
memb = g_new (guint*, n); memb = g_new (guint*, n);
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
memb[j] = g_slice_alloc (block_size); memb[j] = g_slice_alloc (block_size);
memc = g_new (guint*, n); memc = g_new (guint*, n);
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
memc[j] = g_slice_alloc (block_size); memc[j] = g_slice_alloc (block_size);
timer = g_timer_new(); timer = g_timer_new();
fill_memory (mema, n, 2); fill_memory (mema, n, 2);
fill_memory (memb, n, 3); fill_memory (memb, n, 3);
fill_memory (memc, n, 4); fill_memory (memc, n, 4);
access_memory3 (mema, memb, memc, n, 3); access_memory3 (mema, memb, memc, n, 3);
g_timer_start (timer); g_timer_start (timer);
accu = access_memory3 (mema, memb, memc, n, repeats); accu = access_memory3 (mema, memb, memc, n, repeats);
g_timer_stop (timer); g_timer_stop (timer);
g_print ("Access-time = %fs\n", g_timer_elapsed (timer, NULL)); g_test_message ("Access-time = %fs", g_timer_elapsed (timer, NULL));
g_assert (accu / repeats == (2 + 3) * n / 2 + 4 * n / 2); g_assert_cmpuint (accu / repeats, ==, (2 + 3) * n / 2 + 4 * n / 2);
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
{ {
@ -89,6 +95,7 @@ touch_mem (guint64 block_size,
g_slice_free1 (block_size, memb[j]); g_slice_free1 (block_size, memb[j]);
g_slice_free1 (block_size, memc[j]); g_slice_free1 (block_size, memc[j]);
} }
g_timer_destroy (timer); g_timer_destroy (timer);
g_free (mema); g_free (mema);
g_free (memb); g_free (memb);
@ -96,82 +103,31 @@ touch_mem (guint64 block_size,
} }
static void static void
usage (void) test_slice_colors (void)
{ {
g_print ("Usage: slice-color <block-size> [memory-size] [repeats] [colorization]\n"); 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 int
main (int argc, main (int argc, char **argv)
char *argv[])
{ {
guint64 block_size = 512, area_size = 1024 * 1024, n_blocks, repeats = 1000000; g_test_init (&argc, &argv, NULL);
if (argc > 1) g_test_add_func ("/slice/colors", test_slice_colors);
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. return g_test_run ();
* 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;
} }