-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmsync-test.c
153 lines (132 loc) · 3.53 KB
/
msync-test.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
#define _GNU_SOURCE
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<signal.h>
#include<time.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#include<sys/mman.h>
#include <sys/ioctl.h>
#include<sys/time.h>
#define END_SIZE (4UL * 1024 * 1024)
const int start_size = 512;
unsigned long long global_file_size;
int global_fd;
char *global_addr;
int main(int argc, char **argv)
{
int fd, i;
unsigned long long FILE_SIZE;
unsigned long bufsize;
size_t len, offset;
char c;
char unit;
int size;
unsigned long long count;
void *buf1 = NULL;
char *buf, *data;
char file_size_num[20];
char filename[60];
int req_size;
struct timespec begin, finish;
long long time1;
if (argc < 4) {
printf("Usage: ./msync-test $REQ_SIZE $FILE_SIZE $filename\n");
return 0;
}
strcpy(file_size_num, argv[2]);
len = strlen(file_size_num);
unit = file_size_num[len - 1];
file_size_num[len - 1] = '\0';
FILE_SIZE = atoll(file_size_num);
switch (unit) {
case 'K':
case 'k':
FILE_SIZE *= 1024;
break;
case 'M':
case 'm':
FILE_SIZE *= 1048576;
break;
case 'G':
case 'g':
FILE_SIZE *= 1073741824;
break;
default:
printf("ERROR: FILE_SIZE should be #K/M/G format.\n");
return 0;
break;
}
if (FILE_SIZE < 4096)
FILE_SIZE = 4096;
if (FILE_SIZE > 2147483648) // RAM disk size
FILE_SIZE = 2147483648;
strcpy(filename, argv[3]);
c = filename[0];
bufsize = END_SIZE;
if (bufsize > FILE_SIZE)
bufsize = FILE_SIZE;
if (posix_memalign(&buf1, bufsize, bufsize)) { // up to 64MB
printf("ERROR - POSIX NOMEM!\n");
return 0;
}
buf = (char *)buf1;
memset(buf, 'a', bufsize);
fd = open("/mnt/ramdisk/test1", O_CREAT | O_RDWR, 0640);
printf("fd: %d\n", fd);
count = FILE_SIZE / bufsize;
if (count == 0)
count++;
for (i = 0; i < count; i++) {
write(fd, buf, bufsize);
}
req_size = atoi(argv[1]);
if (req_size > END_SIZE)
req_size = END_SIZE;
offset = 0;
data = (char *)mmap(NULL, FILE_SIZE - offset, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
size = req_size;
memset(buf, c, bufsize);
lseek(fd, 0, SEEK_SET);
count = FILE_SIZE / size;
printf("Start c: %c\n", c);
printf("addr: %p\n", data);
printf("count: %llu\n", count);
global_file_size = FILE_SIZE;
global_fd = fd;
global_addr = data;
memcpy(data, buf, size);
clock_gettime(CLOCK_MONOTONIC, &begin);
msync(data, FILE_SIZE, MS_SYNC);
clock_gettime(CLOCK_MONOTONIC, &finish);
time1 = (finish.tv_sec * 1e9 + finish.tv_nsec) - (begin.tv_sec * 1e9 + begin.tv_nsec);
printf("Msync %lld ns, average %lld ns\n", time1, time1 / count);
printf("Sleep for 5 seconds..\n");
sleep(5);
*data = 'd';
clock_gettime(CLOCK_MONOTONIC, &begin);
msync(data, FILE_SIZE, MS_SYNC);
clock_gettime(CLOCK_MONOTONIC, &finish);
time1 = (finish.tv_sec * 1e9 + finish.tv_nsec) - (begin.tv_sec * 1e9 + begin.tv_nsec);
printf("Msync %lld ns, average %lld ns\n", time1, time1 / count);
printf("Sleep for 5 seconds..\n");
sleep(5);
*data = 'a';
clock_gettime(CLOCK_MONOTONIC, &begin);
msync(data, FILE_SIZE, MS_SYNC);
clock_gettime(CLOCK_MONOTONIC, &finish);
time1 = (finish.tv_sec * 1e9 + finish.tv_nsec) - (begin.tv_sec * 1e9 + begin.tv_nsec);
printf("Msync %lld ns, average %lld ns\n", time1, time1 / count);
printf("Sleep for 5 seconds..\n");
sleep(5);
clock_gettime(CLOCK_MONOTONIC, &begin);
msync(data, FILE_SIZE, MS_SYNC);
clock_gettime(CLOCK_MONOTONIC, &finish);
time1 = (finish.tv_sec * 1e9 + finish.tv_nsec) - (begin.tv_sec * 1e9 + begin.tv_nsec);
printf("Msync %lld ns, average %lld ns\n", time1, time1 / count);
close(fd);
free(buf1);
return 0;
}