]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/justdisk.c
3018aa6ac74976313ef60f36fc697654ffdf8359
[bacula/bacula] / bacula / patches / testing / justdisk.c
1 /*
2  * This file is used to test simple I/O operation for accurate mode
3  *
4  *
5  * 1) Loading
6  *    fseek()   ?
7  *    fwrite()
8  *
9  * 2) Fetch + Update
10  *    fseek(pos1)
11  *    fread()
12  *    fseek(pos1)
13  *    fwrite()
14  *
15  * 3) Read all
16  *    fseek()
17  *    fread()
18  *    
19  */
20
21 /*
22  cd regress/build
23  make
24  cd patches/testing
25  g++ -g -Wall -I../../src -I../../src/lib -L../../src/lib justdisk.c -lbac -lpthread -lssl -D_TEST_BUF
26  ./a.out
27  */
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include "bacula.h"
35
36 typedef struct {
37    int32_t path;
38    int32_t filename;
39    int32_t seen;
40    int32_t pad;
41    int64_t mtime;
42    int64_t ctime;
43 } AccurateElt;
44
45 #define NB_ELT 50000000
46
47 typedef struct {
48    char *buf;
49    rblink *link;
50 } MY;
51
52 static int my_cmp(void *item1, void *item2)
53 {
54    MY *elt1, *elt2;
55    elt1 = (MY *) item1;
56    elt2 = (MY *) item2;
57    printf("cmp(%s, %s)\n", elt1->buf, elt2->buf);
58    return strcmp(elt1->buf, elt2->buf);
59 }
60
61 rblist *load_rb(const char *file)
62 {
63    FILE *fp;
64    char buffer[1024];
65    MY *res;
66    rblist *lst;
67    lst = New(rblist(res, res->link));
68
69    fp = fopen(file, "r");
70    if (!fp) {
71       return NULL;
72    }
73    while (fgets(buffer, sizeof(buffer), fp)) {
74       if (*buffer) {
75          int len = strlen(buffer);
76          if (len > 0) {
77             buffer[len-1]=0;       /* zap \n */
78          }
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);
83          if (res != buf) {
84             free(buf->buf);
85             free(buf);
86          }
87       }
88    }
89    fclose(fp);
90
91    return lst;
92 }
93
94 /* buffer used for 4k io operations */
95 class AccurateBuffer
96 {
97 public:
98    AccurateBuffer() { 
99       _fp=NULL; 
100       _nb=-1;
101       _max_nb=-1;
102       _dirty=0; 
103       memset(_buf, 0, sizeof(_buf));
104    };
105    void *get_elt(int nb);
106    void init();
107    void destroy();
108    void update_elt(int nb);
109    ~AccurateBuffer() {
110       destroy();
111    }
112 private:
113    char _buf[4096];
114    int _nb;
115    int _max_nb;
116    char _dirty;
117    FILE *_fp;
118 };
119
120 void AccurateBuffer::init()
121 {
122    _fp = fopen("testfile", "w+");
123    if (!_fp) {
124       exit(1);
125    }
126    _nb=-1;
127    _max_nb=-1;
128 }
129
130 void AccurateBuffer::destroy()
131 {
132    if (_fp) {
133       fclose(_fp);
134       _fp = NULL;
135    }
136 }
137
138 void *AccurateBuffer::get_elt(int nb)
139 {
140    int page=nb*sizeof(AccurateElt)/sizeof(_buf);
141
142    if (!_fp) {
143       init();
144    }
145
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) {
150             perror("bad fseek");
151             exit(3);
152          }
153          if (fwrite(_buf, sizeof(_buf), 1, _fp) != 1) {
154             perror("writing...");
155             exit(2);
156          }
157          _dirty=0;
158       }
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));
165          }
166       } else {
167          memset(_buf, 0, sizeof(_buf));
168       }
169       _nb = page;
170       _max_nb = MAX(_max_nb, page);
171    }
172
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);
177 }
178
179 void AccurateBuffer::update_elt(int nb)
180 {
181    _dirty = 1;
182 }
183
184
185 #ifdef _TEST_OPEN
186 int main()
187 {
188    int fd;
189    int i;
190    AccurateElt elt;
191    char *start_heap = (char *)sbrk(0);
192
193    rblist *rb_file = load_rb("files");
194    rblist *rb_path = load_rb("path");
195
196    char *etc = (char *)rb_path->search((void *)"/etc/", my_cmp);
197
198    printf("%p\n", etc);
199
200    fd = open("testfile", O_CREAT | O_RDWR, 0600);
201    if (fd<0) {
202       perror("E: Can't open testfile ");
203       return(1);
204    }
205
206    memset(&elt, 0, sizeof(elt));
207
208    /* 1) Loading */
209    for (i=0; i<NB_ELT; i++) {
210       write(fd, &elt, sizeof(elt));
211    }
212
213    lseek(fd, 0, SEEK_SET);      /* rewind */
214
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));
221    }
222
223    lseek(fd, 0, SEEK_SET);      /* rewind */
224
225    /* 3) Fetch all of them */
226    for (i=0; i<NB_ELT; i++) {
227       read(fd, &elt, sizeof(elt));
228    }
229
230    close(fd);
231
232    fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
233    sleep(50);
234    return (0);
235 }
236 #else  /* _TEST_OPEN */
237 #ifdef _TEST_BUF
238 int main()
239 {
240    char *start_heap = (char *)sbrk(0);
241
242    int i;
243    AccurateElt *elt;
244    AccurateBuffer *buf = new AccurateBuffer;
245
246    /* 1) Loading */
247    printf("Loading...\n");
248    for (i=0; i<NB_ELT; i++) {
249       elt = (AccurateElt *) buf->get_elt(i);
250       elt->mtime = i;
251       buf->update_elt(i);
252    }
253
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);   
260          exit (0);
261       }
262       elt->seen = i;
263       buf->update_elt(i);
264    }
265
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);
272          exit (0);
273       }
274    }
275    fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
276    delete buf;
277
278    return(0);
279 }
280
281 #else
282 int main()
283 {
284    FILE *fd;
285    int i;
286    AccurateElt elt;
287    fd = fopen("testfile", "w+");
288    if (!fd) {
289       perror("E: Can't open testfile ");
290       return(1);
291    }
292
293    memset(&elt, 0, sizeof(elt));
294
295    /* 1) Loading */
296    printf("Loading...\n");
297    for (i=0; i<NB_ELT; i++) {
298       fwrite(&elt, sizeof(elt), 1, fd);
299    }
300
301    fseek(fd, 0, SEEK_SET);      /* rewind */
302
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);
310    }
311
312    fseek(fd, 0, SEEK_SET);      /* rewind */
313
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);
318    }
319
320    fclose(fd);
321    return(0);
322 }
323 #endif  /* _TEST_BUF */
324 #endif  /* _TEST_OPEN */