1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* Contribution from Aleksandar Lazic <al-haproxy@none.at>
*
* Build with :
* gcc -O2 -o test_pools test_pools.c
* or with dlmalloc too :
* gcc -O2 -o test_pools -D USE_DLMALLOC test_pools.c -DUSE_DL_PREFIX dlmalloc.c
*/
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
static struct timeval timeval_current(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv;
}
static double timeval_elapsed(struct timeval *tv)
{
struct timeval tv2 = timeval_current();
return (tv2.tv_sec - tv->tv_sec) +
(tv2.tv_usec - tv->tv_usec)*1.0e-6;
}
#define torture_assert(test, expr, str) if (!(expr)) { \
printf("failure: %s [\n%s: Expression %s failed: %s\n]\n", \
test, __location__, #expr, str); \
return false; \
}
#define torture_assert_str_equal(test, arg1, arg2, desc) \
if (strcmp(arg1, arg2)) { \
printf("failure: %s [\n%s: Expected %s, got %s: %s\n]\n", \
test, __location__, arg1, arg2, desc); \
return false; \
}
/* added pools from haproxy */
#include <stdlib.h>
/*
* Returns a pointer to an area of <__len> bytes taken from the pool <pool> or
* dynamically allocated. In the first case, <__pool> is updated to point to
* the next element in the list.
*/
#define pool_alloc_from(__pool, __len) \
({ \
void *__p; \
if ((__p = (__pool)) == NULL) \
__p = malloc(((__len) >= sizeof (void *)) ? \
(__len) : sizeof(void *)); \
else { \
__pool = *(void **)(__pool); \
} \
__p; \
})
/*
* Puts a memory area back to the corresponding pool.
* Items are chained directly through a pointer that
* is written in the beginning of the memory area, so
* there's no need for any carrier cell. This implies
* that each memory area is at least as big as one
* pointer.
*/
#define pool_free_to(__pool, __ptr) \
({ \
*(void **)(__ptr) = (void *)(__pool); \
__pool = (void *)(__ptr); \
})
/*
* Returns a pointer to type <type> taken from the
* pool <pool_type> or dynamically allocated. In the
* first case, <pool_type> is updated to point to the
* next element in the list.
*/
#define pool_alloc(type) \
({ \
void *__p; \
if ((__p = pool_##type) == NULL) \
__p = malloc(sizeof_##type); \
else { \
pool_##type = *(void **)pool_##type; \
} \
__p; \
})
/*
* Puts a memory area back to the corresponding pool.
* Items are chained directly through a pointer that
* is written in the beginning of the memory area, so
* there's no need for any carrier cell. This implies
* that each memory area is at least as big as one
* pointer.
*/
#define pool_free(type, ptr) \
({ \
*(void **)ptr = (void *)pool_##type; \
pool_##type = (void *)ptr; \
})
/*
* This function destroys a pull by freeing it completely.
* This should be called only under extreme circumstances.
*/
static inline void pool_destroy(void **pool)
{
void *temp, *next;
next = pool;
while (next) {
temp = next;
next = *(void **)temp;
free(temp);
}
}
#define sizeof_talloc 1000
/*
measure the speed of hapx versus malloc
*/
static bool test_speed1(void)
{
void **pool_talloc = NULL;
void *ctx = pool_alloc(talloc);
unsigned count;
const int loop = 1000;
int i;
struct timeval tv;
printf("test: speed [\nhaproxy-pool VS MALLOC SPEED 2\n]\n");
tv = timeval_current();
count = 0;
do {
void *p1, *p2, *p3;
for (i=0;i<loop;i++) {
p1 = pool_alloc_from(pool_talloc, 10 + loop % 100);
p2 = pool_alloc_from(pool_talloc, strlen("foo bar") + 1);
strcpy(p2, "foo bar");
p3 = pool_alloc_from(pool_talloc, 300);
pool_free_to(pool_talloc,p1);
pool_free_to(pool_talloc,p3);
pool_free_to(pool_talloc,p2);
}
count += 3 * loop;
} while (timeval_elapsed(&tv) < 5.0);
fprintf(stderr, "haproxy : %10.0f ops/sec\n", count/timeval_elapsed(&tv));
pool_destroy(pool_talloc);
tv = timeval_current();
count = 0;
do {
void *p1, *p2, *p3;
for (i=0;i<loop;i++) {
p1 = malloc(10 + loop % 100);
p2 = malloc(strlen("foo bar") + 1);
strcpy(p2, "foo bar");
p3 = malloc(300);
free(p1);
free(p2);
free(p3);
}
count += 3 * loop;
} while (timeval_elapsed(&tv) < 5.0);
fprintf(stderr, "malloc : %10.0f ops/sec\n", count/timeval_elapsed(&tv));
#ifdef USE_DLMALLOC
tv = timeval_current();
count = 0;
do {
void *p1, *p2, *p3;
for (i=0;i<loop;i++) {
p1 = dlmalloc(10 + loop % 100);
p2 = dlmalloc(strlen("foo bar") + 1);
strcpy(p2, "foo bar");
p3 = dlmalloc(300);
dlfree(p1);
dlfree(p2);
dlfree(p3);
}
count += 3 * loop;
} while (timeval_elapsed(&tv) < 5.0);
fprintf(stderr, "dlmalloc: %10.0f ops/sec\n", count/timeval_elapsed(&tv));
#endif
printf("success: speed1\n");
return true;
}
int main(void)
{
bool ret = test_speed1();
if (!ret)
return -1;
return 0;
}
|