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\n", tpa);
153 buf = malloc(sizeof(void *) * allocations);
155 printf("error malloc\n");
158 for (i = 0; i < allocations; i++) {
162 r = xheap_init(heap, size, al_unit, mem);
164 printf("error heap_init\n");
168 xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
170 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
171 // printf("foo_obj: %lx\n", foo_obj);
176 memset(foo_obj, 1, sizeof(struct foo));
177 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
178 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
180 if (c != allocations) {
181 printf("allocated %lu instead of expected %lu\n", c, allocations);
186 for (i = 0; i < allocations; i++) {
191 xobj_put_obj(&obj_h, foo_obj);
193 if (c != allocations) {
194 printf("put %lu instead of expected %lu\n", c, allocations);
198 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
199 // printf("foo_obj: %lx\n", foo_obj);
203 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
204 // printf("foo_obj[%lu]: %lx\n", c, foo_obj);
206 if (c != allocations) {
207 printf("reallocated %lu instead of expected %lu\n", c, allocations);
215 int main(int argc, const char *argv[])
218 heap = malloc(sizeof(struct xheap));
222 printf("error malloc\n");
228 printf("Basic test failed\n");
230 printf("Basic test completed\n");
234 printf("Get-Put-Get test failed\n");
236 printf("Get-Put-Get test completed\n");
240 printf("Threaded test failed\n");
242 printf("Threaded test completed\n");