Unify process_alloc() and process_realloc() with PROCESS_ALLOC_MODE_REALLOC bit
[moveable_pool.git] / inode_test_gen.c
1 #include <assert.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "rassert.h"
7
8 #define BLOCK_SIZE 0x200
9 #define BLOCK_SHIFT 9
10
11 #define INDIRECT_SIZE 0x100
12 #define INDIRECT_SHIFT 8
13
14 int rand_int(int n) {
15   assert(sizeof(long) > sizeof(int));
16   return (int)((long)rand() * n / (RAND_MAX + 1L));
17 }
18
19 int bytes_to_blocks(int size) {
20   size = (size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT;
21   int blocks = size;
22   if (size > 18) {
23     // has indirect blocks
24     size = (size + (INDIRECT_SIZE - 1 - 18)) >> INDIRECT_SHIFT;
25     blocks += size;
26     if (size > 1) {
27       // has double indirect blocks
28       size = (size + (INDIRECT_SIZE - 1 - 1)) >> INDIRECT_SHIFT;
29       rassert(size == 1);
30       blocks += size;
31     }
32   }
33   return blocks;
34 }
35
36 int main(int argc, char **argv) {
37   if (argc < 5) {
38     printf("usage: %s n_files n_blocks n_events file_size [seed]\n", argv[0]);
39     exit(EXIT_FAILURE);
40   }
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;
46
47   int *files = malloc(n_files * sizeof(int));
48   rassert(files);
49   memset(files, -1, n_files * sizeof(int));
50
51   srand(seed);
52   int blocks_used = 0;
53   for (int i = 0; i < n_events; ++i) {
54     int file = rand_int(n_files);
55     int old_size = files[file];
56     if (old_size == -1) {
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;
60       printf(
61         "alloc %d %d %s\n",
62         file,
63         size,
64         success ? "true" : "false"
65       );
66       if (success) {
67         files[file] = size;
68         blocks_used += size_blocks;
69       }
70     }
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;
76       printf(
77         "realloc %d %d %d %s\n",
78         file,
79         old_size,
80         size,
81         success ? "true" : "false"
82       );
83       if (success) {
84         files[file] = size;
85         blocks_used += size_blocks - old_size_blocks;
86       }
87     }
88     else {
89       printf("free %d %d\n", file, old_size);
90       files[file] = -1;
91       blocks_used -= bytes_to_blocks(old_size);
92     }
93   }
94
95   return 0;
96 }