2 * This file is used to test simple I/O operation for accurate mode
25 g++ -g -Wall -I../../src -I../../src/lib -L../../src/lib justdisk.c -lbac -lpthread -lssl -D_TEST_BUF
45 #define NB_ELT 50000000
52 static int my_cmp(void *item1, void *item2)
57 printf("cmp(%s, %s)\n", elt1->buf, elt2->buf);
58 return strcmp(elt1->buf, elt2->buf);
61 rblist *load_rb(const char *file)
67 lst = New(rblist(res, res->link));
69 fp = fopen(file, "r");
73 while (fgets(buffer, sizeof(buffer), fp)) {
75 int len = strlen(buffer);
77 buffer[len-1]=0; /* zap \n */
79 MY *buf = (MY *)malloc(sizeof(MY));
80 memset(buf, 0, sizeof(MY));
81 buf->buf = bstrdup(buffer);
82 res = (MY *)lst->insert(buf, my_cmp);
94 /* buffer used for 4k io operations */
103 memset(_buf, 0, sizeof(_buf));
105 void *get_elt(int nb);
108 void update_elt(int nb);
120 void AccurateBuffer::init()
122 _fp = fopen("testfile", "w+");
130 void AccurateBuffer::destroy()
138 void *AccurateBuffer::get_elt(int nb)
140 int page=nb*sizeof(AccurateElt)/sizeof(_buf);
146 if (page != _nb) { /* not the same page */
147 if (_dirty) { /* have to sync on disk */
148 // printf("put dirty page on disk %i\n", _nb);
149 if (fseek(_fp, _nb*sizeof(_buf), SEEK_SET) == -1) {
153 if (fwrite(_buf, sizeof(_buf), 1, _fp) != 1) {
154 perror("writing...");
159 if (page <= _max_nb) { /* we read it only if the page exists */
160 // printf("read page from disk %i <= %i\n", page, _max_nb);
161 fseek(_fp, page*sizeof(_buf), SEEK_SET);
162 if (fread(_buf, sizeof(_buf), 1, _fp) != 1) {
163 // printf("memset to zero\n");
164 memset(_buf, 0, sizeof(_buf));
167 memset(_buf, 0, sizeof(_buf));
170 _max_nb = MAX(_max_nb, page);
173 /* compute addr of the element in _buf */
174 int addr=(nb%(sizeof(_buf)/sizeof(AccurateElt)))*sizeof(AccurateElt);
175 // printf("addr=%i\n", addr);
176 return (void *) (_buf + addr);
179 void AccurateBuffer::update_elt(int nb)
191 char *start_heap = (char *)sbrk(0);
193 rblist *rb_file = load_rb("files");
194 rblist *rb_path = load_rb("path");
196 char *etc = (char *)rb_path->search((void *)"/etc/", my_cmp);
200 fd = open("testfile", O_CREAT | O_RDWR, 0600);
202 perror("E: Can't open testfile ");
206 memset(&elt, 0, sizeof(elt));
209 for (i=0; i<NB_ELT; i++) {
210 write(fd, &elt, sizeof(elt));
213 lseek(fd, 0, SEEK_SET); /* rewind */
215 /* 2) load and update */
216 for (i=0; i<NB_ELT; i++) {
217 lseek(fd, i*sizeof(AccurateElt), SEEK_SET);
218 read(fd, &elt, sizeof(elt));
219 lseek(fd, i*sizeof(AccurateElt), SEEK_SET);
220 write(fd, &elt, sizeof(elt));
223 lseek(fd, 0, SEEK_SET); /* rewind */
225 /* 3) Fetch all of them */
226 for (i=0; i<NB_ELT; i++) {
227 read(fd, &elt, sizeof(elt));
232 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
236 #else /* _TEST_OPEN */
240 char *start_heap = (char *)sbrk(0);
244 AccurateBuffer *buf = new AccurateBuffer;
247 printf("Loading...\n");
248 for (i=0; i<NB_ELT; i++) {
249 elt = (AccurateElt *) buf->get_elt(i);
254 /* 2) load and update */
255 printf("Load and update...\n");
256 for (i=0; i<NB_ELT; i++) {
257 elt = (AccurateElt *) buf->get_elt(i);
258 if (elt->mtime != i) {
259 printf("Something is wrong with elt %i mtime=%lli\n", i, elt->mtime);
266 /* 3) Fetch all of them */
267 printf("Fetch them...\n");
268 for (i=0; i<NB_ELT; i++) {
269 elt = (AccurateElt *) buf->get_elt(i);
270 if (elt->seen != i || elt->mtime != i) {
271 printf("Something is wrong with elt %i mtime=%lli seen=%i\n", i, elt->mtime, elt->seen);
275 fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
287 fd = fopen("testfile", "w+");
289 perror("E: Can't open testfile ");
293 memset(&elt, 0, sizeof(elt));
296 printf("Loading...\n");
297 for (i=0; i<NB_ELT; i++) {
298 fwrite(&elt, sizeof(elt), 1, fd);
301 fseek(fd, 0, SEEK_SET); /* rewind */
303 /* 2) load and update */
304 printf("Load and update...\n");
305 for (i=0; i<NB_ELT; i++) {
306 fseek(fd, i*sizeof(AccurateElt), SEEK_SET);
307 fread(&elt, sizeof(elt), 1, fd);
308 fseek(fd, i*sizeof(AccurateElt), SEEK_SET);
309 fwrite(&elt, sizeof(elt), 1, fd);
312 fseek(fd, 0, SEEK_SET); /* rewind */
314 /* 3) Fetch all of them */
315 printf("Fetch them...\n");
316 for (i=0; i<NB_ELT; i++) {
317 fread(&elt, sizeof(elt), 1, fd);
323 #endif /* _TEST_BUF */
324 #endif /* _TEST_OPEN */