File: stress-vm-splice.c

package info (click to toggle)
stress-ng 0.07.16-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,240 kB
  • ctags: 3,428
  • sloc: ansic: 36,083; makefile: 551; sh: 129
file content (123 lines) | stat: -rw-r--r-- 3,134 bytes parent folder | download
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
/*
 * Copyright (C) 2013-2017 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.
 *
 */
#include "stress-ng.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);
}

#if defined(__linux__) && NEED_GLIBC(2,5,0)

/*
 *  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) {
		int rc = exit_status(errno);

		pr_fail_dbg(name, "mmap");
		return rc;
	}

	if (pipe(fds) < 0) {
		(void)munmap(buf, sz);
		pr_fail_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_fail_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;
}
#else
int stress_vm_splice(
	uint64_t *const counter,
	const uint32_t instance,
	const uint64_t max_ops,
	const char *name)
{
	return stress_not_implemented(counter, instance, max_ops, name);
}
#endif