This repository has been archived by the owner on Mar 21, 2020. It is now read-only.
forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstress-copy-file.c
132 lines (118 loc) · 3.75 KB
/
stress-copy-file.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
/*
* Copyright (C) 2013-2018 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 <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
int stress_set_copy_file_bytes(const char *opt)
{
uint64_t copy_file_bytes;
copy_file_bytes = get_uint64_byte_filesystem(opt, 1);
check_range_bytes("copy-file-bytes", copy_file_bytes,
MIN_COPY_FILE_BYTES, MAX_COPY_FILE_BYTES);
return set_setting("copy-file-bytes", TYPE_ID_UINT64, ©_file_bytes);
}
#if defined(__linux__) && (__NR_copy_file_range)
/*
* stress_copy_file
* stress reading chunks of file using copy_file_range()
*/
static int stress_copy_file(const args_t *args)
{
int fd_in, fd_out, rc = EXIT_FAILURE;
char filename[PATH_MAX - 5], tmp[PATH_MAX];
uint64_t copy_file_bytes = DEFAULT_COPY_FILE_BYTES;
if (!get_setting("copy-file-bytes", ©_file_bytes)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
copy_file_bytes = MAX_HDD_BYTES;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
copy_file_bytes = MIN_HDD_BYTES;
}
copy_file_bytes /= args->num_instances;
if (copy_file_bytes < DEFAULT_COPY_FILE_SIZE)
copy_file_bytes = DEFAULT_COPY_FILE_SIZE * 2;
if (copy_file_bytes < MIN_COPY_FILE_BYTES)
copy_file_bytes = MIN_COPY_FILE_BYTES;
if (stress_temp_dir_mk(args->name, args->pid, args->instance) < 0)
goto tidy_dir;
(void)stress_temp_filename_args(args,
filename, sizeof(filename), mwc32());
(void)snprintf(tmp, sizeof(tmp), "%s-orig", filename);
if ((fd_in = open(tmp, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
rc = exit_status(errno);
pr_fail_err("open");
goto tidy_dir;
}
(void)unlink(tmp);
if (ftruncate(fd_in, copy_file_bytes) < 0) {
rc = exit_status(errno);
pr_fail_err("ftruncate");
goto tidy_in;
}
if (fsync(fd_in) < 0) {
pr_fail_err("fsync");
goto tidy_in;
}
(void)snprintf(tmp, sizeof(tmp), "%s-copy", filename);
if ((fd_out = open(tmp, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) < 0) {
rc = exit_status(errno);
pr_fail_err("open");
goto tidy_in;
}
(void)unlink(tmp);
do {
ssize_t ret;
loff_t off_in, off_out;
off_in = mwc64() % (copy_file_bytes - DEFAULT_COPY_FILE_SIZE);
off_out = mwc64() % (copy_file_bytes - DEFAULT_COPY_FILE_SIZE);
ret = shim_copy_file_range(fd_in, &off_in, fd_out,
&off_out, DEFAULT_COPY_FILE_SIZE, 0);
if (ret < 0) {
if ((errno == EAGAIN) ||
(errno == EINTR) ||
(errno == ENOSPC))
continue;
pr_fail_err("copy_file_range");
goto tidy_out;
}
(void)fsync(fd_out);
inc_counter(args);
} while (keep_stressing());
rc = EXIT_SUCCESS;
tidy_out:
(void)close(fd_out);
tidy_in:
(void)close(fd_in);
tidy_dir:
(void)stress_temp_dir_rm_args(args);
return rc;
}
stressor_info_t stress_copy_file_info = {
.stressor = stress_copy_file,
.class = CLASS_FILESYSTEM | CLASS_OS
};
#else
stressor_info_t stress_copy_file_info = {
.stressor = stress_not_implemented,
.class = CLASS_FILESYSTEM | CLASS_OS
};
#endif