-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.c
56 lines (46 loc) · 1.42 KB
/
cache.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
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
double getTime() {
struct timeval time;
gettimeofday(&time, NULL);
return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
void readFromFile(const char* filename, int block_size) {
int fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("Error opening file for reading");
exit(EXIT_FAILURE);
}
char* buffer = malloc(block_size);
double start = getTime();
while (read(fd, buffer, block_size) > 0) {
// Reading data...
}
double end = getTime();
free(buffer);
close(fd);
printf("Time taken to read the file: %.6f seconds\n", end - start);
}
void clearCache() {
// Clear the cache - requires root access
system("sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'");
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <filename> <block_size>\n", argv[0]);
exit(EXIT_FAILURE);
}
const char *filename = argv[1];
int block_size = atoi(argv[2]);
// First read (potentially with warm cache)
printf("Reading with warm cache...\n");
readFromFile(filename, block_size);
// Clear cache and read again
printf("Clearing cache and reading again...\n");
clearCache();
readFromFile(filename, block_size);
return 0;
}