-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_read_test.cpp
116 lines (100 loc) · 3.2 KB
/
file_read_test.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
/**
* Create a (10GB) file by writing 1MB chunks sequentially
**/
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
#define KB 1024L
#define MB (KB*KB)
#define GB (KB*KB*KB)
#define FILESIZE (1*GB) // 1GB usually
#define FILENAME "/home/aarati/workspace/CS736/ssd-measurements/test_files/test_32kb_rand"
#define RECORD_FILE "/mnt/hdd/record/32kb_rand_read_1.txt"
#define CHUNK_SIZE (128*KB) // 1MB usually
#define RANDOM_ORDER 1 // 0 for false, 1 for true
long getTimeDiff(struct timeval startTime, struct timeval endTime) {
return (long)((endTime.tv_sec - startTime.tv_sec)*1000000 +
(endTime.tv_usec - startTime.tv_usec));
}
void polluteSSDCache(int fd, void* buf, int numReads=30000) {
for (int i=0; i<numReads; i++) {
read(fd, (char*)buf, 4*KB);
}
fsync(fd);
}
int main() {
int fd;
void* buf;
void* buf_pollute;
long numChunks;
struct timeval startTime, endTime;
long timeTaken;
ofstream record_time(RECORD_FILE);
vector<int> chunkOrder;
vector<int>::iterator it;
long offset;
if (!record_time.is_open()) {
cout << "Failed opening record file" << endl;
exit(1);
}
fd = open(FILENAME, O_RDWR | O_SYNC | O_DIRECT);
if (fd <0) {
cout << "Failed opening file" << endl;
exit(1);
}
fsync(fd);
// fd_pollute = open("/home/aarati/workspace/CS736/ssd-measurements/pollute", O_RDWR | O_DIRECT);
// if (fd_pollute < 0) {
// cout << "Failed to open pollute file" << endl;
// exit(3);
// }
// Write to the file sequentially in CHUNK_SIZE chunks
if (posix_memalign(&buf, 4*KB, CHUNK_SIZE) != 0) {
cout << "posix_memalign failed" << endl;
exit(2);
}
// if (posix_memalign(&buf_pollute, 4*KB, 4*KB) != 0) {
// cout << "Posix memalign failed" << endl;
// exit(1);
// }
numChunks = (FILESIZE)/(CHUNK_SIZE);
cout << numChunks << " chunks" << endl;
// system("blktrace -d /dev/nvme0n1p6 -o - | blkparse -f \"%p - %N\n\" -i - >> /mnt/hdd/random/blktrace.output &");
for (int i=0; i<numChunks; i++) {
chunkOrder.push_back(i);
}
if (RANDOM_ORDER) {
srand(time(0));
random_shuffle(chunkOrder.begin(), chunkOrder.end());
}
for (it=chunkOrder.begin(); it!=chunkOrder.end(); it++) {
// polluteSSDCache(fd_pollute, buf_pollute, 50);
offset = (*it) * CHUNK_SIZE;
// cout << offset << endl;
lseek(fd, (off_t)offset, SEEK_SET);
// start time
gettimeofday(&startTime, NULL);
read(fd, (char*)buf, CHUNK_SIZE);
// end time
gettimeofday(&endTime, NULL);
// fsync(fd);
// fsync(fd_pollute);
timeTaken = getTimeDiff(startTime, endTime);
// Time taken is in micro seconds
record_time << float(1000000 * CHUNK_SIZE)/(1 * MB * timeTaken) << endl;
// fsync(fd);
}
// fsync(fd);
close(fd);
cout << "Last time taken: " << timeTaken << endl;
// system("pkill blkparse");
// system("pkill blktrace");
record_time.close();
return 0;
}