Unify process_alloc() and process_realloc() with PROCESS_ALLOC_MODE_REALLOC bit
[moveable_pool.git] / process_test_gen.c
1 #include <assert.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #ifdef __FUZIX__
7 #include <unistd.h>
8 #endif
9 #include "process_test.h"
10 #include "rassert.h"
11
12 #define PARA_SHIFT 8
13 #define BLOCK_SHIFT 9
14
15 #define BLOCK_PARAS_SHIFT (BLOCK_SHIFT - PARA_SHIFT)
16 #define BLOCK_PARAS (1 << BLOCK_PARAS_SHIFT)
17
18 int rand_int(int n) {
19   assert(sizeof(long) > sizeof(int));
20   return (int)((long)rand() * n / (RAND_MAX + 1L));
21 }
22
23 int calc_blocks(int para_base, int para_limit) {
24   return
25     ((para_limit + (BLOCK_PARAS - 1)) >> BLOCK_PARAS_SHIFT) -
26       (para_base >> BLOCK_PARAS_SHIFT);
27 }
28
29 struct process {
30   bool active;
31   int para_base;
32   int para_limit;
33 };
34
35 int main(int argc, char **argv) {
36   if (argc < 5) {
37     printf("usage: %s n_processes pool_blocks n_events para_base para_limit [do_base [seed]]\n", argv[0]);
38     exit(EXIT_FAILURE);
39   }
40   int n_processes = atoi(argv[1]);
41   int pool_blocks = atoi(argv[2]);
42   int n_events = atoi(argv[3]);
43   int para_base = atoi(argv[4]);
44   int para_limit = atoi(argv[5]);
45   bool do_base = argc >= 7 ? strcmp(argv[6], "false") != 0 : false;
46   int seed = argc >= 8 ? atoi(argv[7]) : 1;
47
48   rassert(para_limit >= para_base);
49
50   struct process *processes = malloc(n_processes * sizeof(struct process));
51   rassert(processes);
52   memset(processes, 0, n_processes * sizeof(struct process));
53
54   srand(seed);
55   int pool_used = 0;
56   for (int i = 0; i < n_events; ++i) {
57     struct process_test test;
58 #ifdef __FUZIX__
59     memset(&test, 0, sizeof(test));
60 #endif
61
62     test.process = rand_int(n_processes);
63     struct process *process = processes + test.process;
64
65     test.old_para_base = process->para_base;
66     test.old_para_limit = process->para_limit;
67     if (!process->active) {
68       test.para_base =
69         (do_base ? rand_int(para_limit - para_base + 1) : 0) + para_base;
70       test.para_limit =
71         rand_int(para_limit - para_base + 1) + para_base;
72       if (test.para_base > test.para_limit) {
73         int i = test.para_base;
74         test.para_base = test.para_limit;
75         test.para_limit = i;
76       }
77       int blocks = calc_blocks(test.para_base, test.para_limit);
78       test.success = pool_used + blocks <= pool_blocks;
79
80 #ifdef __FUZIX__
81       test.type = PROCESS_TEST_TYPE_ALLOC;
82       write(1, &test, sizeof(test));
83 #else
84       printf(
85         "alloc %d %d %d %s\n",
86         test.process,
87         test.para_base,
88         test.para_limit,
89         test.success ? "true" : "false"
90       );
91 #endif
92
93       if (test.success) {
94         process->active = true;
95         process->para_base = test.para_base;
96         process->para_limit = test.para_limit;
97         pool_used += blocks;
98       }
99     }
100     else {
101       int old_blocks = calc_blocks(process->para_base, process->para_limit);
102       if (rand_int(64)) {
103 #ifdef __FUZIX__
104         test.type = PROCESS_TEST_TYPE_RUN;
105         write(1, &test, sizeof(test));
106 #else
107         printf("run %d\n", test.process);
108 #endif
109
110         if (rand_int(8) == 0) {
111           if (do_base && rand_int(3) == 0) {
112             test.para_base =
113               rand_int(process->para_limit - para_base + 1) +
114                 para_base;
115             int blocks = calc_blocks(test.para_base, process->para_limit);
116             test.success = pool_used + blocks - old_blocks <= pool_blocks;
117
118 #ifdef __FUZIX__
119             test.type = PROCESS_TEST_TYPE_REALLOC_BASE;
120             write(1, &test, sizeof(test));
121 #else
122             printf(
123               "realloc_base %d %d %d %s\n",
124               test.process,
125               test.old_para_base,
126               test.para_base,
127               test.success ? "true" : "false"
128             );
129 #endif
130
131             if (test.success) {
132               process->para_base = test.para_base;
133               pool_used += blocks - old_blocks;
134             }
135           }
136           else {
137             test.para_limit =
138               rand_int(para_limit - process->para_base + 1) +
139                 process->para_base;
140             int blocks = calc_blocks(process->para_base, test.para_limit);
141             test.success = pool_used + blocks - old_blocks <= pool_blocks;
142
143 #ifdef __FUZIX__
144             test.type = PROCESS_TEST_TYPE_REALLOC;
145             write(1, &test, sizeof(test));
146 #else
147             printf(
148               "realloc %d %d %d %s\n",
149               test.process,
150               test.old_para_limit,
151               test.para_limit,
152               test.success ? "true" : "false"
153             );
154 #endif
155
156             if (test.success) {
157               process->para_limit = test.para_limit;
158               pool_used += blocks - old_blocks;
159             }
160           }
161         }
162       }
163       else {
164 #ifdef __FUZIX__
165         test.type = PROCESS_TEST_TYPE_FREE;
166         write(1, &test, sizeof(test));
167 #else
168         printf(
169           "free %d %d %d\n",
170           test.process,
171           test.old_para_base,
172           test.old_para_limit
173         );
174 #endif
175         process->active = false;
176         pool_used -= old_blocks;
177       }
178     }
179   }
180
181   return 0;
182 }