forked from ColinIanKing/stress-ng
/
stress-vm-splice.c
122 lines (104 loc) · 3.02 KB
/
stress-vm-splice.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
/*
* Copyright (C) 2013-2015 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#define _GNU_SOURCE
#include "stress-ng.h"
#if defined(STRESS_VM_SPLICE)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
static size_t opt_vm_splice_bytes = DEFAULT_VM_SPLICE_BYTES;
static bool set_vm_splice_bytes = false;
void stress_set_vm_splice_bytes(const char *optarg)
{
set_vm_splice_bytes = true;
opt_vm_splice_bytes = (size_t)get_uint64_byte(optarg);
check_range("vm-splice-bytes", opt_vm_splice_bytes,
MIN_VM_SPLICE_BYTES, MAX_VM_SPLICE_BYTES);
}
/*
* stress_splice
* stress copying of /dev/zero to /dev/null
*/
int stress_vm_splice(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
int fd, fds[2];
uint8_t *buf;
const size_t page_size = stress_get_pagesize();
size_t sz;
(void)instance;
if (!set_vm_splice_bytes) {
if (opt_flags & OPT_FLAGS_MAXIMIZE)
opt_vm_splice_bytes = MAX_VM_SPLICE_BYTES;
if (opt_flags & OPT_FLAGS_MINIMIZE)
opt_vm_splice_bytes = MIN_VM_SPLICE_BYTES;
}
sz = opt_vm_splice_bytes & ~(page_size - 1);
buf = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (buf == MAP_FAILED) {
pr_failed_dbg(name, "mmap");
return(EXIT_FAILURE);
}
if (pipe(fds) < 0) {
(void)munmap(buf, sz);
pr_failed_err(name, "pipe");
return EXIT_FAILURE;
}
if ((fd = open("/dev/null", O_WRONLY)) < 0) {
(void)munmap(buf, sz);
(void)close(fds[0]);
(void)close(fds[1]);
pr_failed_err(name, "open");
return EXIT_FAILURE;
}
do {
int ret;
ssize_t bytes;
struct iovec iov;
iov.iov_base = buf;
iov.iov_len = sz;
bytes = vmsplice(fds[1], &iov, 1, 0);
if (bytes < 0)
break;
ret = splice(fds[0], NULL, fd, NULL, opt_vm_splice_bytes, SPLICE_F_MOVE);
if (ret < 0)
break;
(*counter)++;
} while (opt_do_run && (!max_ops || *counter < max_ops));
(void)munmap(buf, sz);
(void)close(fd);
(void)close(fds[0]);
(void)close(fds[1]);
return EXIT_SUCCESS;
}
#endif