file_read_time.c
1.92 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#define BLOCK_SIZE 512
void main()
{
char fname[20]="/mnt/file_";
char *fno[] = {"1", "2", "3", "4", "5", "6", "7"};
char *file;
char buffer[BLOCK_SIZE] __attribute__ ((__aligned__ (512)));
struct timeval start, end;
int fd=0, count = 0;
int i, j;
unsigned long long time;
int status;
for (i = 0; i < 7; i++) {
count = 0;
file = strcat(fname, fno[i]);
printf("File %d: %s\n", i, file);
fflush(NULL);
//fd = open(file, O_RDONLY);
fd = open(file, O_RDONLY | O_DIRECT | O_SYNC);
if (!fd) {
printf("unable to open\n");
exit(0);
}
posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
// while(read(fd, buffer, BLOCK_SIZE)){
// count++;
// }
// lseek(fd, 0, SEEK_SET);
printf("You are here\n");
gettimeofday(&start, NULL);
for (j = 0; j < 10; j++) {
count = 0;
while(BLOCK_SIZE == read(fd, buffer, BLOCK_SIZE)) {
count++;
}
lseek(fd, 0, SEEK_SET);
}
gettimeofday(&end, NULL);
printf("You are here2\n");
time = (end.tv_sec*1e6+end.tv_usec) - (start.tv_sec*1e6+start.tv_usec);
printf("timestamp %llu - %llu\n", end.tv_sec*1e6+end.tv_usec, start.tv_sec*1e6+start.tv_usec);
printf("time taken to read file # %d - %f\n", i, time/((float)count*10));
sync();
status = system("sudo /sbin/sysctl vm.drop_caches=3");
sync();
gettimeofday(&start, NULL);
for (j = 0; j < 1000; j++) {
pread(fd, buffer, BLOCK_SIZE, BLOCK_SIZE*(rand()%count));
}
gettimeofday(&end, NULL);
time = (end.tv_sec*1e6+end.tv_usec) - (start.tv_sec*1e6+start.tv_usec);
printf("timestamp %llu - %llu\n", end.tv_sec*1e6+end.tv_usec, start.tv_sec*1e6+start.tv_usec);
printf("time taken to read file # %d - %f\n", i, time/1000.0);
sync();
status = system("sudo /sbin/sysctl vm.drop_caches=3");
sync();
strcpy(fname, "/mnt/file_");
}
}