2 #include <xtypes/xobj.h>
7 #define FOO_OBJ_H_MAGIC 1
17 unsigned long size = 1024*1024 * 100;
18 unsigned long al_unit = 12;
19 unsigned long nr_threads = 8;
21 struct xobject_h obj_h;
24 unsigned long allocations = 0;
28 struct xobject_h *obj_h;
34 void *thread_test(void *arg)
36 struct thread_arg *targ = (struct thread_arg *) arg;
37 struct xobject_h *obj_h = targ->obj_h;
39 unsigned long c = targ->c;
44 foo_obj = xobj_get_obj(obj_h, X_ALLOC);
47 memset(foo_obj, 1, sizeof(struct foo));
49 xobj_put_obj(obj_h, foo_obj);
53 }while (foo_obj != NULL);
56 targ->puts = targ->c - c;
63 unsigned long gets = 0;
64 unsigned long puts = 0;
66 int r = xheap_init(heap, size, al_unit, mem);
68 printf("threads: xheap init error\n");
71 xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
73 struct thread_arg *targs = malloc(sizeof(struct thread_arg) * nr_threads);
75 printf("error malloc\n");
79 pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
81 printf("error malloc\n");
85 for (i = 0; i < nr_threads; i++) {
87 targs[i].obj_h = &obj_h;
93 for (i = 0; i < nr_threads; i++) {
94 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
96 printf("error pthread_create\n");
101 for (i = 0; i < nr_threads; i++) {
102 pthread_join(threads[i], NULL);
103 gets += targs[i].gets;
104 puts += targs[i].puts;
106 if (gets - puts != allocations)
116 struct timeval start, end;
118 r = xheap_init(heap, size, al_unit, mem);
120 printf("error heap_init\n");
124 xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
125 gettimeofday(&start, NULL);
126 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
127 // printf("foo_obj: %lx\n", foo_obj);
130 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
131 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
133 gettimeofday(&end, NULL);
135 timersub(&end, &start, &end);
137 us = end.tv_sec * 1000000 + end.tv_usec;
138 printf("Allocated %lu objects of size %d (Total size: %lu)\n",
139 c, sizeof(struct foo), c*sizeof(struct foo));
140 printf("Total time: %lu us\n", us);
141 unsigned long tpa = (unsigned long) ((us*1000) / allocations);
142 printf("Time per allocation: %lu ns\n", tpa);
143 unsigned long aps = (unsigned long) ((allocations * 1000000)/us);
144 printf("Allocations per second: %lu\n\n", aps);
155 buf = malloc(sizeof(void *) * allocations);
157 printf("error malloc\n");
160 for (i = 0; i < allocations; i++) {
164 r = xheap_init(heap, size, al_unit, mem);
166 printf("error heap_init\n");
170 xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
172 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
173 // printf("foo_obj: %lx\n", foo_obj);
178 memset(foo_obj, 1, sizeof(struct foo));
179 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
180 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
182 if (c != allocations) {
183 printf("allocated %lu instead of expected %lu\n", c, allocations);
188 for (i = 0; i < allocations; i++) {
193 xobj_put_obj(&obj_h, foo_obj);
195 if (c != allocations) {
196 printf("put %lu instead of expected %lu\n", c, allocations);
200 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
201 // printf("foo_obj: %lx\n", foo_obj);
205 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
206 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
208 if (c != allocations) {
209 printf("reallocated %lu instead of expected %lu\n", c, allocations);
217 int main(int argc, const char *argv[])
220 heap = malloc(sizeof(struct xheap));
224 printf("error malloc\n");
230 printf("Basic test failed\n");
232 printf("Basic test completed\n");
236 printf("Get-Put-Get test failed\n");
238 printf("Get-Put-Get test completed\n");
242 printf("Threaded test failed\n");
244 printf("Threaded test completed\n");