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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2020 Kent Overstreet
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
static const unsigned nr_procs = 3;
int main(int argc, char *argv[])
{
unsigned long i, nr_iters = 0;
char *fname = NULL, *my_file, *next_file;
pid_t children[nr_procs];
int childnr, my_fd, next_fd, c;
while ((c = getopt(argc, argv, "n:f:")) >= 0) {
switch (c) {
case 'n':
errno = 0;
nr_iters = strtoul(optarg, NULL, 10);
if (errno) {
fprintf(stderr, "Error parsing -n: %m\n");
exit(EXIT_FAILURE);
}
break;
case 'f':
fname = optarg;
break;
default:
fprintf(stderr, "Usage: dio_mmap_torture -n iters -f filename\n");
exit(EXIT_FAILURE);
}
}
for (i = 0; i < nr_procs; i++) {
children[i] = fork();
if (!children[i])
goto do_io;
}
for (i = 0; i < nr_procs; i++) {
int status;
waitpid(children[i], &status, 0);
}
exit(EXIT_SUCCESS);
do_io:
childnr = i;
asprintf(&my_file, "%s.%lu", fname, i);
my_fd = open(my_file, O_RDWR|O_CREAT|O_DIRECT, 0644);
if (my_fd < 0) {
fprintf(stderr, "Error opening %s: %m\n", my_file);
exit(EXIT_FAILURE);
}
i = (i + 1) % nr_procs;
asprintf(&next_file, "%s.%lu", fname, i);
next_fd = open(next_file, O_RDWR|O_CREAT, 0644);
if (next_fd < 0) {
fprintf(stderr, "Error opening %s: %m\n", next_file);
exit(EXIT_FAILURE);
}
if (ftruncate(next_fd, 4096)) {
fprintf(stderr, "ftruncate error: %m\n");
exit(EXIT_FAILURE);
}
void *p = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, next_fd, 0);
if (p == MAP_FAILED) {
fprintf(stderr, "mmap error: %m\n");
exit(EXIT_FAILURE);
}
mlockall(MCL_CURRENT|MCL_FUTURE);
for (i = 0; i < nr_iters; i++) {
int ret = pwrite(my_fd, p, 4096, 0);
//fprintf(stderr, "child %u write ret %i\n", childnr, ret);
if (ret < 0) {
fprintf(stderr, "write error: %m\n");
exit(EXIT_FAILURE);
}
if (ret != 4096)
fprintf(stderr, "short write: %u/%u\n", ret, 4096);
}
exit(EXIT_SUCCESS);
}
|