fix dropcache and retry-open mechanism
[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", tpa);
143         unsigned long aps =  (unsigned long) ((allocations * 1000000)/us);
144         printf("Allocations per second: %lu\n\n", aps);
145         return 0;
146 }
147
148 int get_put_test()
149 {
150         unsigned long c = 0;
151         struct foo *foo_obj;
152         int r;  
153         void **buf;
154         unsigned long i;
155         buf = malloc(sizeof(void *) * allocations);
156         if (!buf) {
157                 printf("error malloc\n");
158                 return -1;
159         }
160         for (i = 0; i < allocations; i++) {
161                 buf[i] = NULL;
162         }
163
164         r = xheap_init(heap, size, al_unit, mem);
165         if (r < 0) {
166                 printf("error heap_init\n");
167                 return -1;
168         }
169
170         xobj_handler_init(&obj_h, mem, FOO_OBJ_H_MAGIC, sizeof(struct foo), heap);
171
172         foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
173 //      printf("foo_obj: %lx\n", foo_obj);
174         c = 0;
175         while (foo_obj){
176                 buf[c] = foo_obj;
177                 c++;
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);
181         }
182         if (c != allocations) {
183                 printf("allocated %lu instead of expected %lu\n", c, allocations);
184                 return -1;
185         }
186         
187         c = 0;
188         for (i = 0; i < allocations; i++) {
189                 foo_obj = buf[i];
190                 if (foo_obj == NULL)
191                         continue;
192                 c++;
193                 xobj_put_obj(&obj_h, foo_obj);
194         }
195         if (c != allocations) {
196                 printf("put %lu instead of expected %lu\n", c, allocations);
197                 return -1;
198         }
199
200         foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
201 //      printf("foo_obj: %lx\n", foo_obj);
202         c = 0;
203         while (foo_obj){
204                 c++;
205                 foo_obj = xobj_get_obj(&obj_h, X_ALLOC);
206 //              printf("foo_obj[%lu]: %lx\n", c, foo_obj);
207         }
208         if (c != allocations) {
209                 printf("reallocated %lu instead of expected %lu\n", c, allocations);
210                 return -1;
211         }
212
213         return 0;
214 }
215
216
217 int main(int argc, const char *argv[])
218 {
219         int r;
220         heap = malloc(sizeof(struct xheap));
221
222         mem = malloc(size);
223         if (!mem) {
224                 printf("error malloc\n");
225                 return -1;
226         }
227
228         r = basic_test();
229         if (r < 0) 
230                 printf("Basic test failed\n");
231         else
232                 printf("Basic test completed\n");
233
234         r = get_put_test();
235         if (r < 0) 
236                 printf("Get-Put-Get test failed\n");
237         else
238                 printf("Get-Put-Get test completed\n");
239
240         r = test_threads();
241         if (r < 0) 
242                 printf("Threaded test failed\n");
243         else
244                 printf("Threaded test completed\n");
245
246         return 0;
247 }