-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathintegration-test.c
254 lines (230 loc) · 5.91 KB
/
integration-test.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* @file main.c
* @brief main program for test the interface
* @author Gijun Oh
* @version 0.2
* @date 2021-09-22
*/
#include <assert.h>
#include <climits>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include "module.h"
#include "flash.h"
#include "page.h"
#include "log.h"
#include "device.h"
#define USE_FORCE_ERASE
#define USE_RANDOM_WAIT
// #define USE_DEBUG_PRINT
// #define BLOCKSIZE_1MB
#define BLOCKSIZE_4KB
#define DEVICE_PATH "/dev/nvme0n2"
#ifdef DEVICE_USE_RASPBERRY
#define WRITE_SIZE ((size_t)64 * (size_t)(1 << 20)) // 64 MiB
#else
#define WRITE_SIZE ((size_t)128 * (size_t)(1 << 20)) // 128 MiB
#endif
#define NR_ERASE (10)
#if defined(BLOCKSIZE_4KB)
#define BLOCK_SIZE ((size_t)4 * (size_t)(1 << 10)) // 4 KiB
#elif defined(BLOCKSIZE_1MB)
#define BLOCK_SIZE ((size_t)(1 << 20)) // 1 MiB
#endif
int is_check[WRITE_SIZE / BLOCK_SIZE];
void *read_thread(void *data)
{
ssize_t buffer[BLOCK_SIZE / sizeof(size_t)];
struct flash_device *flash;
off_t offset;
offset = 0;
flash = (struct flash_device *)data;
while (offset < (off_t)WRITE_SIZE) {
ssize_t ret;
srand(((unsigned int)time(NULL) * (unsigned int)offset) %
UINT_MAX);
memset(buffer, 0, sizeof(buffer));
ret = flash->f_op->read(flash, (void *)buffer, BLOCK_SIZE,
offset);
if (ret < 0 || (offset > 0 && buffer[0] == 0)) {
continue;
}
if (buffer[0] == -1) {
continue;
}
assert(ret == BLOCK_SIZE);
#ifdef USE_DEBUG_PRINT
printf("%-12s: %-16zd(offset: %zu)\n", "read", buffer[0],
offset);
#endif
is_check[(size_t)(*(ssize_t *)buffer) / BLOCK_SIZE] = 1;
offset += BLOCK_SIZE;
#ifdef USE_RANDOM_WAIT
usleep((rand() % 500) + 1000);
#endif
}
return NULL;
}
void *write_thread(void *data)
{
ssize_t buffer[BLOCK_SIZE / sizeof(ssize_t)];
struct flash_device *flash;
off_t offset;
offset = 0;
flash = (struct flash_device *)data;
while (offset < (off_t)WRITE_SIZE) {
ssize_t ret;
srand((((unsigned int)time(NULL) * (unsigned int)offset) + 1) %
UINT_MAX);
memset(buffer, 0, sizeof(buffer));
buffer[0] = (ssize_t)offset;
ret = flash->f_op->write(flash, (void *)buffer, BLOCK_SIZE,
offset);
if (ret < 0) {
pr_err("write failed (offset: %zu)\n", (size_t)offset);
}
#ifdef USE_DEBUG_PRINT
printf("%-12s: %-16zd(offset: %zu)\n", "write", buffer[0],
(size_t)offset);
#endif
assert(ret == BLOCK_SIZE);
offset += BLOCK_SIZE;
#ifdef USE_RANDOM_WAIT
usleep((rand() % 500) + 100);
#endif
}
return NULL;
}
gint is_overwrite = 0;
void *overwrite_thread(void *data)
{
ssize_t buffer[BLOCK_SIZE / sizeof(ssize_t)];
struct flash_device *flash;
off_t offset;
offset = 0;
flash = (struct flash_device *)data;
g_atomic_int_set(&is_overwrite, 1);
sleep(2);
while (offset < (off_t)WRITE_SIZE) {
ssize_t ret;
srand((((unsigned int)time(NULL) * (unsigned int)offset) + 2) %
UINT_MAX);
memset(buffer, 0, sizeof(buffer));
buffer[0] = (ssize_t)offset;
ret = flash->f_op->write(flash, (void *)buffer, BLOCK_SIZE,
offset);
if (ret < 0) {
pr_err("overwrite failed (offset: %zu)\n",
(size_t)offset);
}
#ifdef USE_DEBUG_PRINT
printf("%-12s: %-16zd(offset: %zu)\n", "overwrite", buffer[0],
(size_t)offset);
#endif
offset += BLOCK_SIZE;
#ifdef USE_RANDOM_WAIT
usleep((rand() % 500) + 100);
#endif
}
return NULL;
}
void *erase_thread(void *data)
{
#ifdef USE_FORCE_ERASE
struct flash_device *flash;
int i;
flash = (struct flash_device *)data;
while (!g_atomic_int_get(&is_overwrite)) {
usleep(100);
}
for (i = 0; i < NR_ERASE; i++) {
usleep(1000 * 1000);
flash->f_op->ioctl(flash, PAGE_FTL_IOCTL_TRIM);
#ifdef USE_DEBUG_PRINT
printf("\tforced garbage collection!\n");
#endif
}
#endif
(void)data;
return NULL;
}
int main(void)
{
pthread_t threads[6]; // write, write, read, read, overwrite, erase;
int thread_id;
int is_all_valid;
size_t status;
size_t i;
struct flash_device *flash = NULL;
memset(is_check, 0, sizeof(is_check));
#if defined(DEVICE_USE_ZONED)
assert(0 == module_init(PAGE_FTL_MODULE, &flash, ZONE_MODULE));
#elif defined(DEVICE_USE_BLUEDBM)
assert(0 == module_init(PAGE_FTL_MODULE, &flash, BLUEDBM_MODULE));
#elif defined(DEVICE_USE_RASPBERRY)
assert(0 == module_init(PAGE_FTL_MODULE, &flash, RASPBERRY_MODULE));
#else
assert(0 == module_init(PAGE_FTL_MODULE, &flash, RAMDISK_MODULE));
#endif
assert(0 == flash->f_op->open(flash, DEVICE_PATH, O_CREAT | O_RDWR));
thread_id =
pthread_create(&threads[0], NULL, write_thread, (void *)flash);
if (thread_id < 0) {
perror("thread create failed");
exit(errno);
}
thread_id =
pthread_create(&threads[1], NULL, write_thread, (void *)flash);
if (thread_id < 0) {
perror("thread create failed");
exit(errno);
}
thread_id =
pthread_create(&threads[2], NULL, read_thread, (void *)flash);
if (thread_id < 0) {
perror("thread create failed");
exit(errno);
}
thread_id =
pthread_create(&threads[3], NULL, read_thread, (void *)flash);
if (thread_id < 0) {
perror("thread create failed");
exit(errno);
}
thread_id = pthread_create(&threads[4], NULL, overwrite_thread,
(void *)flash);
if (thread_id < 0) {
perror("thread create failed");
exit(errno);
}
thread_id =
pthread_create(&threads[5], NULL, erase_thread, (void *)flash);
if (thread_id < 0) {
perror("thread create failed");
exit(errno);
}
pthread_join(threads[0], (void **)&status);
pthread_join(threads[1], (void **)&status);
pthread_join(threads[2], (void **)&status);
pthread_join(threads[3], (void **)&status);
pthread_join(threads[4], (void **)&status);
pthread_join(threads[5], (void **)&status);
flash->f_op->close(flash);
assert(0 == module_exit(flash));
is_all_valid = 1;
for (i = 0; i < sizeof(is_check) / sizeof(int); i++) {
if (!is_check[i]) {
printf("read failed offset: %-20zu\n",
(i * BLOCK_SIZE));
is_all_valid = 0;
}
}
if (is_all_valid) {
printf("all data exist => FINISH\n");
}
return 0;
}