File: stest.c

package info (click to toggle)
fio 3.41-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,012 kB
  • sloc: ansic: 82,290; python: 9,862; sh: 6,067; makefile: 813; yacc: 204; lex: 184
file content (121 lines) | stat: -rw-r--r-- 2,218 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "../smalloc.h"
#include "../flist.h"
#include "../arch/arch.h"
#include "debug.h"

#define MAGIC1	0xa9b1c8d2
#define MAGIC2	0xf0a1e9b3

#define LOOPS	32
#define MAXSMALLOC	120*1024*1024UL
#define LARGESMALLOC	128*1024U

struct elem {
	unsigned int magic1;
	struct flist_head list;
	unsigned int magic2;
	unsigned int size;
};

static FLIST_HEAD(list);

static int do_rand_allocs(void)
{
	unsigned int i, size, nr, rounds = 0, ret = 0;
	unsigned long total;
	struct elem *e;
	bool error;
	char *c;

	while (rounds++ < LOOPS) {
#ifdef STEST_SEED
		srand(MAGIC1);
#endif
		error = false;
		nr = total = 0;
		while (total < MAXSMALLOC) {
			size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0)));
			e = scalloc(1, size);
			if (!e) {
				printf("fail at %lu, size %u\n", total, size);
				ret++;
				break;
			}

			c = (char *)e;
			for (i = 0; i < size; i++) {
				if (*(c+i) != 0) {
					printf("buffer not cleared at %lu, size %u\n", total, size);
					ret++;
					break;
				}
			}

			/* stop the while loop if buffer was not cleared */
			if (i < size)
				break;

			e->magic1 = MAGIC1;
			e->magic2 = MAGIC2;
			e->size = size;
			total += size;
			flist_add_tail(&e->list, &list);
			nr++;
		}

		printf("Got items: %u\n", nr);

		while (!flist_empty(&list)) {
			e = flist_entry(list.next, struct elem, list);
			assert(e->magic1 == MAGIC1);
			assert(e->magic2 == MAGIC2);
			total -= e->size;
			flist_del(&e->list);
			sfree(e);

			if (!error) {
				e = scalloc(1, LARGESMALLOC);
				if (!e) {
					ret++;
					printf("failure allocating %u bytes at %lu allocated during sfree phase\n",
						LARGESMALLOC, total);
					break;
				}

				c = (char *)e;
				for (i = 0; i < LARGESMALLOC; i++) {
					if (*(c+i) != 0) {
						error = true;
						ret++;
						printf("large buffer not cleared at %lu, size %u\n", total, size);
						break;
					}
				}

				sfree(e);
			}
		}
	}

	return ret;
}

int main(int argc, char *argv[])
{
	int ret;

	arch_init(argv);
	sinit();
	debug_init();

	ret = do_rand_allocs();
	smalloc_debug(0);	/* TODO: check that free and total blocks
				** match */

	scleanup();
	return ret;
}