-
Notifications
You must be signed in to change notification settings - Fork 71
/
mmap-write-concurrent.c
155 lines (136 loc) · 2.98 KB
/
mmap-write-concurrent.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
// SPDX-License-Identifier: GPL-2.0-or-newer
/*
* Copyright (c) 2019 Oracle.
* All Rights Reserved.
*
* Create writable mappings to multiple files and write them all to test
* concurrent mmap writes to the same shared blocks.
*/
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
struct file_info {
char *mapping;
off_t file_offset;
off_t file_length;
int fd;
};
int
main(
int argc,
char *argv[])
{
struct file_info *fi;
size_t length;
char *endptr;
unsigned int nr_files;
unsigned int i;
char *buf;
int ret;
if (argc < 4) {
printf("Usage: %s len offset file [offset file]...\n", argv[0]);
return 1;
}
/* Parse mwrite length. */
errno = 0;
length = strtoul(argv[1], &endptr, 0);
if (errno) {
perror(argv[1]);
return 1;
}
if (*endptr != '\0') {
fprintf(stderr, "%s: not a proper file length?\n", argv[2]);
return 1;
}
/* Allocate file info */
nr_files = (argc - 2) / 2;
fi = calloc(nr_files, sizeof(struct file_info));
if (!fi) {
perror("calloc file info");
return 1;
}
buf = malloc(length);
if (!buf) {
perror("malloc buf");
return 1;
}
for (i = 0; i < nr_files; i++) {
struct stat statbuf;
char *offset = argv[((i + 1) * 2)];
char *fname = argv[((i + 1) * 2) + 1];
/* Open file, create mapping for the range we want. */
fi[i].fd = open(fname, O_RDWR);
if (fi[i].fd < 0) {
perror(fname);
return 1;
}
/* Parse mwrite offset */
errno = 0;
fi[i].file_offset = strtoul(offset, &endptr, 0);
if (errno) {
perror(argv[1]);
return 1;
}
/* Remember file size */
ret = fstat(fi[i].fd, &statbuf);
if (ret) {
perror(fname);
return 1;
}
fi[i].file_length = statbuf.st_size;
if (fi[i].file_offset + length > fi[i].file_length) {
fprintf(stderr, "%s: file must be %llu bytes\n",
fname,
(unsigned long long)fi[i].file_offset + length);
return 1;
}
/* Create the mapping */
fi[i].mapping = mmap(NULL, fi[i].file_length,
PROT_READ | PROT_WRITE, MAP_SHARED,
fi[i].fd, 0);
if (fi[i].mapping == MAP_FAILED) {
perror(fname);
return 1;
}
/*
* Make sure the mapping for region we're going to write is
* already populated in the page cache.
*/
memcpy(buf, fi[i].mapping + fi[i].file_offset, length);
}
/* Dirty the same region in each file to test COW. */
for (i = 0; i < nr_files; i++) {
memset(buf, 0x62 + i, length);
memcpy(fi[i].mapping + fi[i].file_offset, buf, length);
}
for (i = 0; i < nr_files; i++) {
ret = msync(fi[i].mapping, fi[i].file_offset + length, MS_SYNC);
if (ret) {
perror("msync");
return 1;
}
}
/* Close everything. */
for (i = 0; i < nr_files; i++) {
ret = munmap(fi[i].mapping, fi[i].file_length);
if (ret) {
perror("munmap");
return 1;
}
ret = close(fi[i].fd);
if (ret) {
perror("close");
return 1;
}
}
/* Free everything. */
free(buf);
free(fi);
return 0;
}