-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_read.h
178 lines (136 loc) · 4.65 KB
/
disk_read.h
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
#include <stdio.h>
#include <string.h>
#include "benchmark.h"
#include "constants.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#define BLOCK_SIZE 4096
void readFile(const char* filename, int trials, int iterations) {
char *buffer = (char*) malloc(BLOCK_SIZE);
posix_memalign(&buffer, BLOCK_SIZE, BLOCK_SIZE);
int fd = open(filename, O_RDWR | O_SYNC | __O_DIRECT);
long filesize = lseek(fd, 0, SEEK_END);
long blockCount = filesize / BLOCK_SIZE;
lseek(fd, 0, SEEK_SET);
fprintf(stderr, "file size is %ld\n", filesize);
fprintf(stderr, "Oh dear, something went wrong with open()! %s\n", strerror(errno));
// we will only ever access first block of file. We advise that first two blocks don't need to be cached
int advice = posix_fadvise(fd, 0, filesize, POSIX_FADV_DONTNEED);
// no setup required
void setup() {
lseek(fd, 0, SEEK_SET);
};
void test() {
while(read(fd, buffer, BLOCK_SIZE) == BLOCK_SIZE);
};
const char* testname = "Disk Read Time";
runTestSetup(benchmarkCycles, test, testname, iterations, trials, setup);
free(buffer);
close(fd);
}
/* Arrange the N elements of ARRAY in random order.
Only effective if N is much smaller than RAND_MAX;
if this may not be the case, use a better random
number generator. */
void shuffle(long *array, size_t n)
{
if (n > 1)
{
size_t i;
for (i = 0; i < n - 1; i++)
{
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
void readFileSeq(const char* filename, int trials, int iterations) {
char *buffer = (char*) malloc(BLOCK_SIZE);
posix_memalign(&buffer, BLOCK_SIZE, BLOCK_SIZE);
int fd = open(filename, O_RDWR | O_SYNC | __O_DIRECT);
long filesize = lseek(fd, 0, SEEK_END);
long blockCount = filesize / BLOCK_SIZE;
long blockLocations[blockCount];
for (int i=0; i < blockCount; i++ ) {
blockLocations[i] = i*BLOCK_SIZE;
}
lseek(fd, 0, SEEK_SET);
fprintf(stderr, "file size is %ld\n", filesize);
// we will only ever access first block of file. We advise that first two blocks don't need to be cached
int advice = posix_fadvise(fd, 0, filesize, POSIX_FADV_DONTNEED);
void test() {
read(fd, buffer, BLOCK_SIZE);
};
uint64_t trial_results[trials];
uint64_t iteration_results[iterations];
for (int i=0; i < trials; i++) {
memset(iteration_results, 0, sizeof(iteration_results));
for (int j=0; j < iterations; j++) {
for (int k = 0; k < blockCount; k++) {
lseek(fd, blockLocations[k], SEEK_SET);
iteration_results[j] += benchmarkCycles(test);
}
}
trial_results[i] = median(iteration_results, iterations);
}
benchmark_stats stats = fill_stats(trial_results, trials);
printf("Testing Seq File Reads\n");
printf("%d Trials of %d Iterations\n", trials, iterations);
printf("Mean: %f\n", stats.mean);
printf("Median: %ld\n", stats.median);
printf("StDev: %f\n", stats.stdev);
printf("Min: %ld\n", stats.min);
printf("Max: %ld\n", stats.max);
printf("\n");
free(buffer);
close(fd);
}
void readFileRand(const char* filename, int trials, int iterations) {
char *buffer = (char*) malloc(BLOCK_SIZE);
posix_memalign(&buffer, BLOCK_SIZE, BLOCK_SIZE);
int fd = open(filename, O_RDWR | O_SYNC | __O_DIRECT);
long filesize = lseek(fd, 0, SEEK_END);
long blockCount = filesize / BLOCK_SIZE;
long blockLocations[blockCount];
for (int i=0; i < blockCount; i++ ) {
blockLocations[i] = i*BLOCK_SIZE;
}
// shuffle through all block locations
shuffle(blockLocations, blockCount);
lseek(fd, 0, SEEK_SET);
fprintf(stderr, "file size is %ld\n", filesize);
// we will only ever access first block of file. We advise that first two blocks don't need to be cached
int advice = posix_fadvise(fd, 0, filesize, POSIX_FADV_DONTNEED);
void test() {
read(fd, buffer, BLOCK_SIZE);
};
uint64_t trial_results[trials];
uint64_t iteration_results[iterations];
for (int i=0; i < trials; i++) {
memset(iteration_results, 0, sizeof(iteration_results));
for (int j=0; j < iterations; j++) {
for (int k = 0; k < blockCount; k++) {
lseek(fd, blockLocations[k], SEEK_SET);
iteration_results[j] += benchmarkCycles(test);
}
}
trial_results[i] = median(iteration_results, iterations);
}
benchmark_stats stats = fill_stats(trial_results, trials);
printf("Testing Random File Reads\n");
printf("%d Trials of %d Iterations\n", trials, iterations);
printf("Mean: %f\n", stats.mean);
printf("Median: %ld\n", stats.median);
printf("StDev: %f\n", stats.stdev);
printf("Min: %ld\n", stats.min);
printf("Max: %ld\n", stats.max);
printf("\n");
free(buffer);
close(fd);
}