make xhash request for less allocated space
[archipelago] / xseg / xtypes / xobj_test.c
1 #include <stdio.h>
2 #include <xtypes/xobj.h>
3 #include <pthread.h>
4 #include <sys/time.h>
5
6 #define OBJ_H_MAGIC 0
7 #define FOO_OBJ_H_MAGIC 1
8
9 struct foo {
10         uint64_t id;
11         uint64_t bar;
12         uint64_t bar1;
13         uint64_t bar2;
14 };
15
16 void *mem;
17 unsigned long size = 1024*1024 * 100;
18 unsigned long al_unit = 12;
19 unsigned long nr_threads = 8;
20 struct xheap *heap;
21 struct xobject_h obj_h;
22
23
24 unsigned long allocations = 0;
25
26 struct thread_arg{
27         int id;
28         struct xobject_h *obj_h;
29         unsigned long c;
30         unsigned long gets;
31         unsigned long puts;
32 };
33
34 void *thread_test(void *arg)
35 {
36         struct thread_arg *targ = (struct thread_arg *) arg;
37         struct xobject_h *obj_h = targ->obj_h;
38         int id = targ->id;
39         unsigned long c = targ->c;
40
41         struct foo *foo_obj;
42         unsigned long i = 0;
43         do {
44                 foo_obj = xobj_get_obj(obj_h, X_ALLOC);
45                 if (foo_obj != NULL){
46                         i++;
47                         memset(foo_obj, 1, sizeof(struct foo));
48                         if (c) {
49                                 xobj_put_obj(obj_h, foo_obj);
50                                 c--;
51                         }
52                 }
53         }while (foo_obj != NULL);
54
55         targ->gets = i;
56         targ->puts = targ->c - c;
57         return NULL;
58 }
59
60 int test_threads()
61 {
62         int i;
63         unsigned long gets = 0;
64         unsigned long puts = 0;
65
66         int r = xheap_init(heap, size, al_unit, mem);
67         if (r < 0){
68                 printf("threads: xheap init error\n");
69                 return -1;
70         }
71         xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
72
73         struct thread_arg *targs = malloc(sizeof(struct thread_arg) * nr_threads);
74         if (!targs) {
75                 printf("error malloc\n");
76                 return -1;
77         }
78
79         pthread_t *threads = malloc(sizeof(pthread_t) * nr_threads);
80         if (!threads){
81                 printf("error malloc\n");
82                 return -1;
83         }
84
85         for (i = 0; i < nr_threads; i++) {
86                 targs[i].id = i;
87                 targs[i].obj_h = &obj_h;
88                 targs[i].c = 256;
89                 targs[i].gets = 0;
90                 targs[i].puts = 0;
91         }
92
93         for (i = 0; i < nr_threads; i++) {
94                 r = pthread_create(&threads[i], NULL, thread_test, &targs[i]);
95                 if (r) {
96                         printf("error pthread_create\n");
97                         return -1;
98                 }
99         }
100
101         for (i = 0; i < nr_threads; i++) {
102                 pthread_join(threads[i], NULL);
103                 gets +=  targs[i].gets;
104                 puts += targs[i].puts;
105         }
106         if (gets - puts != allocations)
107                 return -1;
108         return 0;
109 }
110
111 int basic_test()
112 {
113         unsigned long c = 0;
114         int r;
115         struct foo *foo_obj;
116         struct timeval start, end;
117
118         r = xheap_init(heap, size, al_unit, mem);
119         if (r < 0) {
120                 printf("error heap_init\n");
121                 return -1;
122         }
123
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);
128         while (foo_obj){
129                 c++;
130                 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
131 //              printf("foo_obj[%lu]: %lx\n", c, foo_obj);
132         }
133         gettimeofday(&end, NULL);
134         allocations = c;
135         timersub(&end, &start, &end);
136         unsigned long us;
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);
143         return 0;
144 }
145
146 int get_put_test()
147 {
148         unsigned long c = 0;
149         struct foo *foo_obj;
150         int r;  
151         void **buf;
152         unsigned long i;
153         buf = malloc(sizeof(void *) * allocations);
154         if (!buf) {
155                 printf("error malloc\n");
156                 return -1;
157         }
158         for (i = 0; i < allocations; i++) {
159                 buf[i] = NULL;
160         }
161
162         r = xheap_init(heap, size, al_unit, mem);
163         if (r < 0) {
164                 printf("error heap_init\n");
165                 return -1;
166         }
167
168         xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
169
170         foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
171 //      printf("foo_obj: %lx\n", foo_obj);
172         c = 0;
173         while (foo_obj){
174                 buf[c] = foo_obj;
175                 c++;
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);
179         }
180         if (c != allocations) {
181                 printf("allocated %lu instead of expected %lu\n", c, allocations);
182                 return -1;
183         }
184         
185         c = 0;
186         for (i = 0; i < allocations; i++) {
187                 foo_obj = buf[i];
188                 if (foo_obj == NULL)
189                         continue;
190                 c++;
191                 xobj_put_obj(&obj_h, foo_obj);
192         }
193         if (c != allocations) {
194                 printf("put %lu instead of expected %lu\n", c, allocations);
195                 return -1;
196         }
197
198         foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
199 //      printf("foo_obj: %lx\n", foo_obj);
200         c = 0;
201         while (foo_obj){
202                 c++;
203                 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
204 //              printf("foo_obj[%lu]: %lx\n", c, foo_obj);
205         }
206         if (c != allocations) {
207                 printf("reallocated %lu instead of expected %lu\n", c, allocations);
208                 return -1;
209         }
210
211         return 0;
212 }
213
214
215 int main(int argc, const char *argv[])
216 {
217         int r;
218         heap = malloc(sizeof(struct xheap));
219
220         mem = malloc(size);
221         if (!mem) {
222                 printf("error malloc\n");
223                 return -1;
224         }
225
226         r = basic_test();
227         if (r < 0) 
228                 printf("Basic test failed\n");
229         else
230                 printf("Basic test completed\n");
231
232         r = get_put_test();
233         if (r < 0) 
234                 printf("Get-Put-Get test failed\n");
235         else
236                 printf("Get-Put-Get test completed\n");
237
238         r = test_threads();
239         if (r < 0) 
240                 printf("Threaded test failed\n");
241         else
242                 printf("Threaded test completed\n");
243
244         return 0;
245 }