forked from stasis/stasis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseekMap.c
158 lines (141 loc) · 4.71 KB
/
seekMap.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
#include <config.h>
#include <stasis/common.h>
#include <stasis/util/time.h>
#include <stasis/util/random.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
void * buf;
static void my_pread_help(int fd, long long off) {
int err = pread(fd, buf, 512, off);
if(err == -1) {
perror("Could not read from file");
abort();
}
}
static void drop_buffers(void) {
int fd = open("/proc/sys/vm/drop_caches", O_TRUNC|O_WRONLY);
if(fd == -1) { perror("Couldn't drop page cache"); abort(); }
char * str = "1\n";
int err = write(fd, str, 3);
if(err == -1) {
perror("write to drop page cache failed");
abort();
}
err = close(fd);
if(err == -1) {
perror("could not close file");
abort();
}
}
#define my_pread(start_off, off) my_pread_help(fd, start_off + off)
int main(int argc, char * argv[]) {
if(argc != 7) {
printf("Usage: %s filename steps iterations start_off length random_mode\n", argv[0]);
abort();
}
#ifdef HAVE_POSIX_MEMALIGN
posix_memalign(&buf, 512, 512);
#else
buf = malloc(2 * 512);
buf = (void*)(((intptr_t)buf) & ~(512-1));
#endif
const char * filename = argv[1];
int fd = open(filename, O_RDONLY);//|O_DIRECT);
if(fd == -1) {
perror("Could not open file");
abort();
}
int steps = atoi(argv[2]);
int iter = atoi(argv[3]);
long length = atoll(argv[4]);
long start_off = atoll(argv[5]);
long random_mode = atoi(argv[6]);
double** sum_x = stasis_calloc(steps, double*);
double** sum_x2 = stasis_calloc(steps, double*);
long** sample_count = stasis_calloc(steps, long*);
for(int s = 0; s < steps; s++) {
sum_x[s] = stasis_calloc(steps, double);
sum_x2[s] = stasis_calloc(steps, double);
sample_count[s] = stasis_calloc(steps, long);
}
long stride = length / steps;
printf("filename = %s steps = %d iter = %d length = %ld start_off = %ld stride = %ld\n",
filename, steps, iter, length, start_off, stride);
assert(stride);
for(int iteration = 0; iteration < iter; iteration++) {
if(!random_mode || random_mode == 2) {
for(int xstep = 0; xstep < steps; xstep++) {
for(int ystep = 0; ystep < steps; ystep++) {
drop_buffers();
// position head (do not count the time this takes)
if(random_mode) {
my_pread(start_off, xstep * stride + stasis_util_random64(stride));
} else {
my_pread(start_off, xstep * stride);
}
struct timeval start, stop;
gettimeofday(&start, 0);
if(random_mode) {
my_pread(start_off, ystep * stride + stasis_util_random64(stride));
} else {
my_pread(start_off, ystep * stride);
}
gettimeofday(&stop, 0);
double elapsed = stasis_timeval_to_double(
stasis_subtract_timeval(stop, start));
sum_x [xstep][ystep] += elapsed;
sum_x2[xstep][ystep] += elapsed*elapsed;
}
printf("%d", xstep % 10); fflush(stdout);
}
} else {
for(long x = 0; x < steps * steps; x++) {
long long start_pos = stasis_util_random64(length);
long long stop_pos = stasis_util_random64(length);
int xstep = start_pos / stride;
int ystep = stop_pos / stride;
drop_buffers();
// position head
my_pread(start_off, start_pos);
struct timeval start, stop;
gettimeofday(&start, 0);
my_pread(start_off, stop_pos);
gettimeofday(&stop, 0);
double elapsed = stasis_timeval_to_double(
stasis_subtract_timeval(stop, start));
sum_x [xstep][ystep] += elapsed;
sum_x2[xstep][ystep] += elapsed*elapsed;
sample_count[xstep][ystep] ++;
}
}
printf("\nIteration %d mean seek time:\n", iteration);
for(int xstep = 0; xstep < steps; xstep++) {
for(int ystep = 0; ystep < steps; ystep++) {
double n = (double) random_mode==1
?(sample_count[xstep][ystep]
?sample_count[xstep][ystep]
:1)
:(iteration+1);
printf("%f%s", sum_x[xstep][ystep] / n,
(ystep==steps-1)?"\n":"\t");
}
}
printf("\nIteration %d stddev seek time:\n", iteration);
for(int xstep = 0; xstep < steps; xstep++) {
for(int ystep = 0; ystep < steps; ystep++) {
double n = (double) random_mode==1
?(sample_count[xstep][ystep]
?sample_count[xstep][ystep]
:1)
:(iteration+1);
double mean = sum_x[xstep][ystep]/n;
printf("%f%s", sqrt(sum_x2[xstep][ystep]/n - mean*mean),
(ystep==steps-1)?"\n":"\t");
}
}
fflush(stdout);
}
close(fd);
return 0;
}