8 #define BLOCK_SIZE 0x200
11 #define INDIRECT_SIZE 0x100
12 #define INDIRECT_SHIFT 8
15 assert(sizeof(long) > sizeof(int));
16 return (int)((long)rand() * n / (RAND_MAX + 1L));
19 int bytes_to_blocks(int size) {
20 size = (size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT;
23 // has indirect blocks
24 size = (size + (INDIRECT_SIZE - 1 - 18)) >> INDIRECT_SHIFT;
27 // has double indirect blocks
28 size = (size + (INDIRECT_SIZE - 1 - 1)) >> INDIRECT_SHIFT;
36 int main(int argc, char **argv) {
38 printf("usage: %s n_files n_blocks n_events file_size [seed]\n", argv[0]);
41 int n_files = atoi(argv[1]);
42 int n_blocks = atoi(argv[2]);
43 int n_events = atoi(argv[3]);
44 int file_size = atoi(argv[4]);
45 int seed = argc >= 6 ? atoi(argv[5]) : 1;
47 int *files = malloc(n_files * sizeof(int));
49 memset(files, -1, n_files * sizeof(int));
53 for (int i = 0; i < n_events; ++i) {
54 int file = rand_int(n_files);
55 int old_size = files[file];
57 int size = rand_int((file_size << BLOCK_SHIFT) + 1);
58 int size_blocks = bytes_to_blocks(size);
59 bool success = blocks_used + size_blocks <= n_blocks;
64 success ? "true" : "false"
68 blocks_used += size_blocks;
71 else if (rand_int(4)) {
72 int size = rand_int((file_size << BLOCK_SHIFT) + 1);
73 int size_blocks = bytes_to_blocks(size);
74 int old_size_blocks = bytes_to_blocks(old_size);
75 bool success = blocks_used + size_blocks - old_size_blocks <= n_blocks;
77 "realloc %d %d %d %s\n",
81 success ? "true" : "false"
85 blocks_used += size_blocks - old_size_blocks;
89 printf("free %d %d\n", file, old_size);
91 blocks_used -= bytes_to_blocks(old_size);