2014-09-08 10:58:04 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
JEMALLOC_INLINE_C void
|
2014-09-08 12:46:12 +08:00
|
|
|
time_func(timedelta_t *timer, uint64_t nwarmup, uint64_t niter, void (*func)(void))
|
2014-09-08 10:58:04 +08:00
|
|
|
{
|
|
|
|
uint64_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < nwarmup; i++)
|
|
|
|
func();
|
|
|
|
timer_start(timer);
|
|
|
|
for (i = 0; i < niter; i++)
|
|
|
|
func();
|
|
|
|
timer_stop(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
compare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,
|
|
|
|
void (*func_a), const char *name_b, void (*func_b))
|
|
|
|
{
|
2014-09-08 12:46:12 +08:00
|
|
|
timedelta_t timer_a, timer_b;
|
2014-09-08 10:58:04 +08:00
|
|
|
char ratio_buf[6];
|
2014-10-05 05:59:38 +08:00
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = mallocx(1, 0);
|
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected mallocx() failure");
|
|
|
|
return;
|
|
|
|
}
|
2014-09-08 10:58:04 +08:00
|
|
|
|
|
|
|
time_func(&timer_a, nwarmup, niter, func_a);
|
|
|
|
time_func(&timer_b, nwarmup, niter, func_b);
|
|
|
|
|
|
|
|
timer_ratio(&timer_a, &timer_b, ratio_buf, sizeof(ratio_buf));
|
|
|
|
malloc_printf("%"PRIu64" iterations, %s=%"PRIu64"us, "
|
|
|
|
"%s=%"PRIu64"us, ratio=1:%s\n",
|
|
|
|
niter, name_a, timer_usec(&timer_a), name_b, timer_usec(&timer_b),
|
|
|
|
ratio_buf);
|
2014-10-05 05:59:38 +08:00
|
|
|
|
|
|
|
dallocx(p, 0);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-09 07:23:48 +08:00
|
|
|
malloc_free(void)
|
2014-09-08 10:58:04 +08:00
|
|
|
{
|
2014-09-09 07:23:48 +08:00
|
|
|
/* The compiler can optimize away free(malloc(1))! */
|
|
|
|
void *p = malloc(1);
|
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected malloc() failure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(p);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-09 07:23:48 +08:00
|
|
|
mallocx_free(void)
|
2014-09-08 10:58:04 +08:00
|
|
|
{
|
2014-09-09 07:23:48 +08:00
|
|
|
void *p = mallocx(1, 0);
|
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected mallocx() failure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(p);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_BEGIN(test_malloc_vs_mallocx)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
|
2014-09-09 07:23:48 +08:00
|
|
|
malloc_free, "mallocx", mallocx_free);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
static void
|
2014-09-09 07:23:48 +08:00
|
|
|
malloc_dallocx(void)
|
2014-09-08 10:58:04 +08:00
|
|
|
{
|
2014-09-09 07:23:48 +08:00
|
|
|
void *p = malloc(1);
|
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected malloc() failure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dallocx(p, 0);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
|
2014-08-29 03:41:48 +08:00
|
|
|
static void
|
|
|
|
malloc_sdallocx(void)
|
|
|
|
{
|
|
|
|
void *p = malloc(1);
|
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected malloc() failure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sdallocx(p, 1, 0);
|
|
|
|
}
|
|
|
|
|
2014-09-08 10:58:04 +08:00
|
|
|
TEST_BEGIN(test_free_vs_dallocx)
|
|
|
|
{
|
|
|
|
|
2014-09-09 07:23:48 +08:00
|
|
|
compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
|
|
|
|
"dallocx", malloc_dallocx);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2014-08-29 03:41:48 +08:00
|
|
|
TEST_BEGIN(test_dallocx_vs_sdallocx)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare_funcs(10*1000*1000, 100*1000*1000, "dallocx", malloc_dallocx,
|
|
|
|
"sdallocx", malloc_sdallocx);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2014-09-08 10:58:04 +08:00
|
|
|
static void
|
2014-09-09 07:47:51 +08:00
|
|
|
malloc_mus_free(void)
|
2014-09-08 10:58:04 +08:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = malloc(1);
|
2014-10-16 05:49:14 +08:00
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected malloc() failure");
|
|
|
|
return;
|
|
|
|
}
|
2014-09-08 10:58:04 +08:00
|
|
|
malloc_usable_size(p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-09-09 07:47:51 +08:00
|
|
|
malloc_sallocx_free(void)
|
2014-09-08 10:58:04 +08:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = malloc(1);
|
2014-10-16 05:49:14 +08:00
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected malloc() failure");
|
|
|
|
return;
|
|
|
|
}
|
2014-09-09 07:47:51 +08:00
|
|
|
if (sallocx(p, 0) < 1)
|
|
|
|
test_fail("Unexpected sallocx() failure");
|
2014-09-08 10:58:04 +08:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_BEGIN(test_mus_vs_sallocx)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare_funcs(10*1000*1000, 100*1000*1000, "malloc_usable_size",
|
2014-09-09 07:47:51 +08:00
|
|
|
malloc_mus_free, "sallocx", malloc_sallocx_free);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
static void
|
2014-09-09 07:47:51 +08:00
|
|
|
malloc_nallocx_free(void)
|
2014-09-08 10:58:04 +08:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = malloc(1);
|
2014-10-16 05:49:14 +08:00
|
|
|
if (p == NULL) {
|
|
|
|
test_fail("Unexpected malloc() failure");
|
|
|
|
return;
|
|
|
|
}
|
2014-09-09 07:47:51 +08:00
|
|
|
if (nallocx(1, 0) < 1)
|
|
|
|
test_fail("Unexpected nallocx() failure");
|
2014-09-08 10:58:04 +08:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_BEGIN(test_sallocx_vs_nallocx)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare_funcs(10*1000*1000, 100*1000*1000, "sallocx",
|
2014-09-09 07:47:51 +08:00
|
|
|
malloc_sallocx_free, "nallocx", malloc_nallocx_free);
|
2014-09-08 10:58:04 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (test(
|
|
|
|
test_malloc_vs_mallocx,
|
|
|
|
test_free_vs_dallocx,
|
2014-08-29 03:41:48 +08:00
|
|
|
test_dallocx_vs_sdallocx,
|
2014-09-08 10:58:04 +08:00
|
|
|
test_mus_vs_sallocx,
|
|
|
|
test_sallocx_vs_nallocx));
|
|
|
|
}
|