forked from MatiasBjorling/flashsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_correctness.cpp
187 lines (158 loc) · 4.76 KB
/
run_correctness.cpp
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
/* Test to see if drive write and read data correct.
* Matias Bjørling 24/2-2011
*/
#include "ssd.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <string>
using namespace ssd;
int open_temp_file(unsigned int file_size = 10 * 1024 * 1024)
{
std::string out_path = "/tmp/garbage.XXXXXX";
int fd = mkstemp(&*out_path.begin());
if (fd != -1)
{
ftruncate(fd, file_size);
printf("Created temporary file %s of size %u\n", out_path.c_str(), file_size);
}
else
{
printf("Failed to create temp file\n");
}
return fd;
}
double timings = 0.0;
double do_seq(Ssd *ssd, event_type type, void *test, unsigned int file_size)
{
unsigned int adr, i = 0;
double result = 0;
for (adr = 0; adr < file_size;adr += PAGE_SIZE)
{
double iotime = ssd->event_arrive(type, i, 1, timings, (char*)test + adr);
//printf("IO Execution time: %f\n", iotime);
result += iotime;
timings += iotime;
if (type == READ)
{
if (ssd->get_result_buffer() == NULL)
printf("Data has not been written\n");
else if (memcmp(ssd->get_result_buffer(), (char*)test + adr, PAGE_SIZE) != 0)
fprintf(stderr, "i: %i ", i);
}
i++;
}
fprintf(stderr,"\n");
return result;
}
double do_seq_backward(Ssd *ssd, event_type type, void *test, unsigned int file_size)
{
unsigned int adr, i = BLOCK_SIZE-1, j=0;
double result = 0;
for (adr = file_size; adr > 0;adr -= PAGE_SIZE)
{
double iotime = ssd->event_arrive(type, j+i, 1, timings, (char*)test + adr - PAGE_SIZE);
if (type == READ && memcmp(ssd->get_result_buffer(), (char*)test + adr - PAGE_SIZE, PAGE_SIZE) != 0)
fprintf(stderr, "Err. Data does not compare. i: %i\n", j+i);
result += iotime;
timings += iotime;
i--;
if (i == -1u)
{
i = BLOCK_SIZE - 1;
j += BLOCK_SIZE;
}
}
return result;
}
double do_random(Ssd *ssd, event_type type, void *test, unsigned int file_size)
{
unsigned int adr, i = 0;
double result = 0;
for (adr = 0; adr < file_size;adr += PAGE_SIZE)
{
result += ssd->event_arrive(type, i, 1, (double) adr, (char*)test + adr);
if (type == READ)
{
if (memcmp(ssd->get_result_buffer(), (char*)test + adr, PAGE_SIZE) != 0)
fprintf(stderr, "Err. Data does not compare. i: %i\n", i);
}
i++;
}
return result;
}
int main(int argc, char** argv)
{
load_config();
print_config(NULL);
printf("\n");
Ssd *ssd = new Ssd();
// create memory mapping of file that we are going to check with
int fd;
if (argc == 1)
fd = open_temp_file();
else
fd = open(argv[1], O_RDONLY);
struct stat st;
fstat(fd, &st);
void *test_data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (test_data == MAP_FAILED)
fprintf(stderr, "File not mapped.");
printf("Size of testfile: %iKB\n", (int)st.st_size/1024);
/*
* Experiment setup
* 1. Do linear write and read.
* 2. Write linear again and read.
* 2. Do semi-random linear
* 3. Do random
* 4. Do backward linear
*/
double result = 0;
printf("Test 1. Write sequential test data.\n");
result += do_seq(ssd, WRITE, test_data, st.st_size);
printf("Test 1. Write sequential test data.\n");
result += do_seq(ssd, WRITE, test_data, st.st_size);
printf("Test 1. Write sequential test data.\n");
result += do_seq(ssd, WRITE, test_data, st.st_size);
//
// printf("Test 1. Trim data.\n");
// result += do_seq(ssd, TRIM, test_data, st.st_size);
printf("Test 1. Write sequential test data.\n");
result += do_seq(ssd, WRITE, test_data, st.st_size);
printf("Test 2. Read sequential test data.\n");
result += do_seq(ssd, READ, test_data, st.st_size);
// printf("Test 6. Write backward sequential test data.\n");
// result += do_seq_backward(ssd, WRITE, test_data, st.st_size);
//
// printf("Test 9. Read backward sequential test data.\n");
// result += do_seq_backward(ssd, READ, test_data, st.st_size);
// printf("Test 3. Write second write.\n");
// result += do_seq(ssd, WRITE, test_data, st.st_size);
//
// printf("Test 4. Write third write.\n");
// result += do_seq_backward(ssd, WRITE, test_data, st.st_size);
//
// printf("Test 5. Read sequential test data.\n");
// result += do_seq_backward(ssd, READ, test_data, st.st_size);
//
// printf("Test 6. Write backward sequential test data.\n");
// result += do_seq_backward(ssd, WRITE, test_data, st.st_size);
//
// printf("Test 7. Read backward sequential test data.\n");
// result += do_seq_backward(ssd, READ, test_data, st.st_size);
//
// printf("Test 8. Write backward sequential test data again.\n");
// result += do_seq_backward(ssd, WRITE, test_data, st.st_size);
//
// printf("Test 9. Read backward sequential test data.\n");
// result += do_seq_backward(ssd, READ, test_data, st.st_size);
printf("Write time: %.10lfs\n", result);
ssd->print_statistics();
delete ssd;
return 0;
}