t/stest: make the test more challenging
[fio.git] / t / stest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4
5 #include "../smalloc.h"
6 #include "../flist.h"
7 #include "../arch/arch.h"
8 #include "debug.h"
9
10 #define MAGIC1  0xa9b1c8d2
11 #define MAGIC2  0xf0a1e9b3
12
13 #define LOOPS   32
14 #define MAXSMALLOC      120*1024*1024UL
15 #define LARGESMALLOC    128*1024U
16
17 struct elem {
18         unsigned int magic1;
19         struct flist_head list;
20         unsigned int magic2;
21         unsigned int size;
22 };
23
24 static FLIST_HEAD(list);
25
26 static int do_rand_allocs(void)
27 {
28         unsigned int size, nr, rounds = 0;
29         unsigned long total;
30         struct elem *e;
31         bool error;
32
33         while (rounds++ < LOOPS) {
34 #ifdef STEST_SEED
35                 srand(MAGIC1);
36 #endif
37                 error = false;
38                 nr = total = 0;
39                 while (total < MAXSMALLOC) {
40                         size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0)));
41                         e = smalloc(size);
42                         if (!e) {
43                                 printf("fail at %lu, size %u\n", total, size);
44                                 break;
45                         }
46                         e->magic1 = MAGIC1;
47                         e->magic2 = MAGIC2;
48                         e->size = size;
49                         total += size;
50                         flist_add_tail(&e->list, &list);
51                         nr++;
52                 }
53
54                 printf("Got items: %u\n", nr);
55
56                 while (!flist_empty(&list)) {
57                         e = flist_entry(list.next, struct elem, list);
58                         assert(e->magic1 == MAGIC1);
59                         assert(e->magic2 == MAGIC2);
60                         total -= e->size;
61                         flist_del(&e->list);
62                         sfree(e);
63
64                         if (!error) {
65                                 e = smalloc(LARGESMALLOC);
66                                 if (!e) {
67                                         error = true;
68                                         printf("failure allocating %u bytes at %lu allocated during sfree phase\n",
69                                                 LARGESMALLOC, total);
70                                 }
71                                 else
72                                         sfree(e);
73                         }
74                 }
75         }
76
77         return 0;
78 }
79
80 int main(int argc, char *argv[])
81 {
82         arch_init(argv);
83         sinit();
84         debug_init();
85
86         do_rand_allocs();
87         smalloc_debug(0);       /* free and total blocks should match */
88
89         scleanup();
90         return 0;
91 }