2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
36 #include <xtypes/xobj.h>
41 #define FOO_OBJ_H_MAGIC 1
51 unsigned long size = 1024*1024 * 100;
52 unsigned long al_unit = 12;
53 unsigned long nr_threads = 8;
55 struct xobject_h obj_h;
58 unsigned long allocations = 0;
62 struct xobject_h *obj_h;
68 void *thread_test(void *arg)
70 struct thread_arg *targ = (struct thread_arg *) arg;
71 struct xobject_h *obj_h = targ->obj_h;
73 unsigned long c = targ->c;
78 foo_obj = xobj_get_obj(obj_h, X_ALLOC);
81 memset(foo_obj, 1, sizeof(struct foo));
83 xobj_put_obj(obj_h, foo_obj);
87 }while (foo_obj != NULL);
90 targ->puts = targ->c - c;
97 unsigned long gets = 0;
98 unsigned long puts = 0;
100 int r = xheap_init(heap, size, al_unit, mem);
102 printf("threads: xheap init error\n");
105 xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
107 struct thread_arg *targs = malloc(sizeof(struct thread_arg) * nr_threads);
109 printf("error malloc\n");
113 pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
115 printf("error malloc\n");
119 for (i = 0; i < nr_threads; i++) {
121 targs[i].obj_h = &obj_h;
127 for (i = 0; i < nr_threads; i++) {
128 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
130 printf("error pthread_create\n");
135 for (i = 0; i < nr_threads; i++) {
136 pthread_join(threads[i], NULL);
137 gets += targs[i].gets;
138 puts += targs[i].puts;
140 if (gets - puts != allocations)
150 struct timeval start, end;
152 r = xheap_init(heap, size, al_unit, mem);
154 printf("error heap_init\n");
158 xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
159 gettimeofday(&start, NULL);
160 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
161 // printf("foo_obj: %lx\n", foo_obj);
164 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
165 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
167 gettimeofday(&end, NULL);
169 timersub(&end, &start, &end);
171 us = end.tv_sec * 1000000 + end.tv_usec;
172 printf("Allocated %lu objects of size %d (Total size: %lu)\n",
173 c, sizeof(struct foo), c*sizeof(struct foo));
174 printf("Total time: %lu us\n", us);
175 unsigned long tpa = (unsigned long) ((us*1000) / allocations);
176 printf("Time per allocation: %lu ns\n", tpa);
177 unsigned long aps = (unsigned long) ((allocations * 1000000)/us);
178 printf("Allocations per second: %lu\n\n", aps);
189 buf = malloc(sizeof(void *) * allocations);
191 printf("error malloc\n");
194 for (i = 0; i < allocations; i++) {
198 r = xheap_init(heap, size, al_unit, mem);
200 printf("error heap_init\n");
204 xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
206 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
207 // printf("foo_obj: %lx\n", foo_obj);
212 memset(foo_obj, 1, sizeof(struct foo));
213 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
214 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
216 if (c != allocations) {
217 printf("allocated %lu instead of expected %lu\n", c, allocations);
222 for (i = 0; i < allocations; i++) {
227 xobj_put_obj(&obj_h, foo_obj);
229 if (c != allocations) {
230 printf("put %lu instead of expected %lu\n", c, allocations);
234 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
235 // printf("foo_obj: %lx\n", foo_obj);
239 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
240 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
242 if (c != allocations) {
243 printf("reallocated %lu instead of expected %lu\n", c, allocations);
251 int main(int argc, const char *argv[])
254 heap = malloc(sizeof(struct xheap));
258 printf("error malloc\n");
264 printf("Basic test failed\n");
266 printf("Basic test completed\n");
270 printf("Get-Put-Get test failed\n");
272 printf("Get-Put-Get test completed\n");
276 printf("Threaded test failed\n");
278 printf("Threaded test completed\n");