-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.c
388 lines (350 loc) · 13.6 KB
/
utils.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "utils.h"
// Set some defaults
int LOOP_COUNTER = 100;
double CPU_FREQ = 2593.696; // Mhz
int BLOCK_SIZE = (32 * 1024);
int FILE_COUNT = 1000;
int MAX_FILES = 5000;
int FILE_SIZE_WR = 64;
/*
* Name rdtscp
* Description Runs the RDTSCP instruction which returns a 64-bit time
* stamp counter (TSC).This function also runs the CPUID
* instruction to ensure that the measurements happen in a
* serial fashion.
* Reference:
* https://www.ccsl.carleton.ca/~jamuir/rdtscpm1.pdf
* Input None
* Output timestamp, which is a 64-bit
*/
inline timestamp rdtscp(void) {
unsigned a, d;
__asm__ __volatile__("rdtscp": "=a" (a), "=d" (d) );
return (((timestamp)a) | (((timestamp)d) << 32));
}
/*
* Name cpuid
* Description Runs the CPUID instruction. Notice that this function does
* not read registers and return the CPU information.
* Instead, it only runs the CPUID instruction to ensure that
* the code runs in a serial fashion. This function is meant
* to be used as a barrier only.
* Input None
* Output None
*/
inline void cpuid(void) {
__asm__ __volatile__("cpuid");
return;
}
int dummy_call(char* buf) {
return (buf[0] == '0');
}
void set_LOOP_COUNTER(int loops) {
LOOP_COUNTER = loops;
}
void set_CPU_FREQ() {
// Find and set CPU frequency, but for now do nothing.
}
void set_BLOCK_SIZE(int block_size) {
BLOCK_SIZE = block_size;
}
void set_FILE_COUNT(int file_count) {
FILE_COUNT = file_count;
}
void set_MAX_FILES(int files) {
MAX_FILES = files;
}
void set_FILE_SIZE(int size) {
FILE_SIZE_WR = size;
}
/*
* Randomize an array.
*/
void randomize(int *index, int size) {
int i;
for( i = 0 ; i < size; i++) {
index[i] = i;
}
for( i = size - 1 ; i > 0; i--) {
int idx = rand() % i;
SWAP((index + idx) , (index + i));
}
return;
}
/*
* Randomize an array.
*/
void randomize_more(int *index, int size,struct drand48_data *randBuffer ) {
int i;
long int random = 0;
for( i = 0 ; i < size; i++) {
lrand48_r(randBuffer, &random);
index[i] = random % size + 1;
}
return;
}
/*
* Name read_sequential
* Description Read all the files in a sequential way.
* - The files to be read must be opened and placed in share_it.fd_list
* We choose the files in a random order, so that the effects of
* prefetching are minimized.
* - Reads the number of files FILE_COUNT sequentially by reading a block
* of BLOCK_SIZE in each read operation.
* - Small files are 64 bytes - 32 kB.
* - Big files are 32 kb onwards.
* - For small files, I have meassured with block size as 64 bytes
* For big files, I have measured with block size as 32 kB
* - This routine is also used to measure the time to read a full file by
* share_it.block_size to same as file size.
* - Measures only read. Overhead is only the overhead of measuring time
* itself.
* Input struct share_it
* Output Boolean to indicate if all reads succeed.
*/
bool read_sequential(struct share_it* my_state) {
size_t size = my_state->size;
timestamp start = 0;
timestamp end = 0;
int bytes = 0;
int i = 0;
for (i = 0; i < my_state->count; i++) {
size_t size = my_state->size;
int fd = my_state->fd_list[i];
if (lseek(fd, 0, SEEK_SET) == -1) {
int err = errno;
printf("Seek to start of file failed with errno %d\n",
err);
exit(1);
}
while ((size > 0)) {
RDTSCP(start);
bytes = read(fd, my_state->buf, my_state->block_size);
RDTSCP(end);
if (bytes <= 0 || bytes != my_state->block_size) {
int err = errno;
printf("Read failed with err=%d and bytes =%d while block_size=%zu\n", errno, bytes, my_state->block_size);
return false;
}
dummy_call(my_state->buf);
*(my_state->total_bytes) += bytes;
my_state->duration += (end - start);
size -= bytes;
}
}
return true;
}
/*
* Name read_random
* Description Read all the files in a random order.
* - The files to be read must be opened and placed in share_it.fd_list
* We choose the files in a random order, so that the effects of
* prefetching are minimized.
* - Reads the number of files FILE_COUNT with random access by reading a block
* of BLOCK_SIZE in each read operation.
* - Random access offset is determined by share_it.index.
* - Read block sizes are determined by share_it.block_size.
* - Defaults are same as read_sequential.
* - Measures only read. Overhead is only the overhead of measuring time
* itself.
* Input struct share_it
* Output Boolean to indicate if all reads succeed.
*/
bool read_random(struct share_it* my_state) {
size_t size = my_state->size;
timestamp start = 0;
timestamp end = 0;
int bytes = 0;
int i = 0;
for (i = 0; i < my_state->count; i++) {
size_t size = my_state->size;
int fd = my_state->fd_list[i];
int j = 0;
while ((size > 0)) {
if (lseek(fd, my_state->offsets[j] * BLOCK_SIZE, SEEK_SET) == -1) {
int err = errno;
printf("Seek to start of file failed with errno %d\n",
err);
exit(1);
}
RDTSCP(start);
bytes = read(fd, my_state->buf, my_state->block_size);
RDTSCP(end);
if (bytes <= 0 || bytes != my_state->block_size)
return false;
dummy_call(my_state->buf);
*(my_state->total_bytes) += bytes;
my_state->duration += (end - start);
size -= bytes;
j++;
}
}
return true;
}
/*
* Name open_read_close
* Description This metric is only applicable for small files.
* - Measures the time taken to open a small file, read all of the file and
* close the file.
* - Files to be opened/read/closed are chosen in random ( lessen prefetching
* effects.)
* - Overhead : one if-loop + overhead of measuring time.
* Input struct share_it
* Input filepath - to pick files from.
* Output Boolean to indicate if all reads succeed.
*/
bool open_read_close(struct share_it* my_state, char *filepath) {
timestamp start = 0;
timestamp end = 0;
int bytes = 0;
struct drand48_data randBuffer;
srand48_r(time(NULL), &randBuffer);
int i = 0;
long int random = 0;
int idx = 0;
for (i = 0; i < my_state->count; i++) {
lrand48_r(&randBuffer, &random);
idx = random % MAX_FILES + 1;
char num[5];
sprintf(num, "%d", idx);
char my_file[100] = {'\0'};
strcat(my_file, filepath);
strcat(my_file, "/file");
strcat(my_file, num);
RDTSCP(start);
int fd = open(my_file, FLAGS);
if (fd == -1) {
int err = errno;
printf("Could not open file descriptor for file %s. Error = %d\n",
my_file, err);
return false;
}
bytes = read(fd, my_state->buf, my_state->block_size);
close(fd);
RDTSCP(end);
if (bytes <= 0 || bytes != my_state->block_size)
return false;
dummy_call(my_state->buf);
*(my_state->total_bytes) += bytes;
my_state->duration += (end - start);
}
return true;
}
/*
* Name write_sequential
* Description Write all the files in a sequential way.
* - The files to be written to must be opened and placed in share_it.fd_list
* - Writes the number of files FILE_COUNT sequentially by writing a block
* of BLOCK_SIZE in each write operation.
* We choose the files in a random order, so that the effects of
* prefetching are minimized.
* - Writes the number of files FILE_COUNT sequentially by writing a block
* of BLOCK_SIZE in each write operation.
* - Small files are 64 bytes - 32 kB.
* - Big files are 32 kb onwards.
* - For small files, I have meassured with block size as 64 bytes
* For big files, I have measured with block size as 32 kB
* - This routine is also used to measure the time to write a full file by
* share_it.block_size to same as file size.
* - Measures only write. Overhead is only the overhead of measuring time
* itself.
* Input struct share_it
* Output Boolean to indicate if all writes succeed.
*/
bool write_sequential(struct share_it* my_state) {
size_t size = my_state->size;
timestamp start = 0;
timestamp end = 0;
int bytes = 0;
int rand_bytes = 0;
int i = 0;
for (i = 0; i < my_state->count; i++) {
size_t size = my_state->size;
int fd = my_state->fd_list[i];
if (lseek(fd, 0, SEEK_SET) == -1) {
int err = errno;
printf("Seek to start of file failed with errno %d\n",
err);
exit(1);
}
while ((size > 0)) {
// fill buf with random data
/* rand_bytes = syscall(SYS_getrandom, my_state->buf, my_state->block_size, 0);
if (rand_bytes == -1 || rand_bytes != my_state->block_size) {
int err = errno;
printf("Could not get random data, failed with err=%d and bytes =%d while block_size=%zu\n", errno, bytes, my_state->block_size);
return false;
}
*/
RDTSCP(start);
bytes = write(fd, my_state->buf, my_state->block_size);
RDTSCP(end);
if (bytes <= 0 || bytes != my_state->block_size) {
int err = errno;
printf("Write failed with err=%d and bytes =%d while block_size=%zu\n", errno, bytes, my_state->block_size);
return false;
}
dummy_call(my_state->buf);
*(my_state->total_bytes) += bytes;
my_state->duration += (end - start);
size -= bytes;
}
}
return true;
}
/*
* Name write_random
* Description Write all the files in a random order.
* - The files to be written must be opened and placed in share_it.fd_list
* We choose the files in a random order, so that the effects of
* prefetching are minimized.
* - Writes the number of files FILE_COUNT with random access by reading a block
* of BLOCK_SIZE in each write operation.
* - Random access offset is determined by share_it.index.
* - Write block sizes are determined by share_it.block_size.
* - Defaults are same as write_sequential.
* - Measures only write. Overhead is only the overhead of measuring time
* itself.
* Input struct share_it
* Output Boolean to indicate if all writes succeed.
*/
bool write_random(struct share_it* my_state) {
size_t size = my_state->size;
timestamp start = 0;
timestamp end = 0;
int bytes = 0;
int rand_bytes = 0;
int i = 0;
for (i = 0; i < my_state->count; i++) {
size_t size = my_state->size;
int fd = my_state->fd_list[i];
int j = 0;
while ((size > 0)) {
// fill buf with random data
/* rand_bytes = syscall(SYS_getrandom, my_state->buf, my_state->block_size, 0);
if (rand_bytes == -1 || rand_bytes != my_state->block_size) {
int err = errno;
printf("Could not get random data, failed with err=%d and bytes =%d while block_size=%zu\n", errno, bytes, my_state->block_size);
return false;
}
*/
if (lseek(fd, my_state->offsets[j] * BLOCK_SIZE, SEEK_SET) == -1) {
int err = errno;
printf("Seek to start of file failed with errno %d\n",
err);
exit(1);
}
RDTSCP(start);
bytes = write(fd, my_state->buf, my_state->block_size);
RDTSCP(end);
if (bytes <= 0 || bytes != my_state->block_size)
return false;
dummy_call(my_state->buf);
*(my_state->total_bytes) += bytes;
my_state->duration += (end - start);
size -= bytes;
j++;
}
}
return true;
}