forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-sysinfo.c
267 lines (231 loc) · 6.11 KB
/
stress-sysinfo.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#if defined(HAVE_LINUX_FS_H)
#include <linux/fs.h>
#else
UNEXPECTED
#endif
#if defined(HAVE_SYS_SYSMACROS_H)
#include <sys/sysmacros.h>
#else
UNEXPECTED
#endif
#if defined(HAVE_SYS_STATFS_H)
#include <sys/statfs.h>
#else
UNEXPECTED
#endif
#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h>
#else
UNEXPECTED
#endif
static const stress_help_t help[] = {
{ NULL, "sysinfo N", "start N workers reading system information" },
{ NULL, "sysinfo-ops N", "stop after sysinfo bogo operations" },
{ NULL, NULL, NULL }
};
/*
* stress on system information
* stress system by rapid fetches of system information
*/
static int stress_sysinfo(const stress_args_t *args)
{
int n_mounts;
char *mnts[128];
#if defined(HAVE_SYS_SYSINFO_H) && \
defined(HAVE_SYSINFO) && \
defined(HAVE_SYS_STATFS_H)
const int bad_fd = stress_get_bad_fd();
#endif
(void)memset(mnts, 0, sizeof(mnts));
n_mounts = stress_mount_get(mnts, SIZEOF_ARRAY(mnts));
if (n_mounts < 0) {
pr_err("%s: failed to get mount points\n", args->name);
return EXIT_FAILURE;
}
if (args->instance == 0)
pr_dbg("%s: found %d mount points\n",
args->name, n_mounts);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
struct tms tms_buf;
clock_t clk;
#if defined(HAVE_SYS_SYSINFO_H) && \
defined(HAVE_SYSINFO) && \
defined(HAVE_SYS_STATFS_H)
{
struct sysinfo sysinfo_buf;
struct statfs statfs_buf;
int i, ret;
ret = sysinfo(&sysinfo_buf);
if ((ret < 0) &&
(g_opt_flags & OPT_FLAGS_VERIFY) &&
(errno != EPERM))
pr_fail("%s: sysinfo failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
if (!keep_stressing_flag())
break;
/* Linux statfs variant */
for (i = 0; i < n_mounts; i++) {
int fd;
if (!keep_stressing_flag())
break;
if (!mnts[i])
continue;
ret = statfs(mnts[i], &statfs_buf);
/* Mount may have been removed, so purge it */
if ((ret < 0) && (errno == ENOENT)) {
free(mnts[i]);
mnts[i] = NULL;
continue;
}
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
if (errno != ENOSYS &&
errno != EOVERFLOW &&
errno != EACCES &&
errno != EPERM) {
pr_fail("%s: statfs on %s failed: errno=%d (%s)\n",
args->name, mnts[i], errno,
strerror(errno));
}
}
/*
* Exercise invalid mount points
*/
VOID_RET(int, statfs("/invalid_stress_ng", &statfs_buf));
VOID_RET(int, statfs("", &statfs_buf));
fd = open(mnts[i], O_RDONLY | O_DIRECTORY);
if (fd < 0)
continue;
#if defined(FS_IOC_GETFSLABEL) && \
defined(FSLABEL_MAX)
{
char label[FSLABEL_MAX];
VOID_RET(int, ioctl(fd, FS_IOC_GETFSLABEL, label));
}
#else
UNEXPECTED
#endif
ret = fstatfs(fd, &statfs_buf);
(void)close(fd);
if ((ret < 0) && (errno == ENOENT))
continue;
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
if (errno != ENOSYS &&
errno != EOVERFLOW &&
errno != EACCES &&
errno != EPERM) {
pr_fail("%s: fstatfs on %s failed: errno=%d (%s)\n",
args->name, mnts[i], errno,
strerror(errno));
}
}
/*
* Exercise invalid fd
*/
VOID_RET(int, fstatfs(bad_fd, &statfs_buf));
}
}
#endif
{
int i, ret;
struct stat sbuf;
struct shim_ustat ubuf;
if (!keep_stressing_flag())
break;
for (i = 0; i < n_mounts; i++) {
if (!mnts[i])
continue;
ret = stat(mnts[i], &sbuf);
if (ret < 0)
continue;
ret = shim_ustat(sbuf.st_dev, &ubuf);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
if (errno != EINVAL &&
errno != EFAULT &&
errno != ENOSYS &&
errno != EPERM) {
pr_fail("%s: ustat on %s failed: errno=%d (%s)\n",
args->name, mnts[i], errno,
strerror(errno));
}
}
}
#if defined(HAVE_SYS_SYSMACROS_H)
/*
* Exercise invalid ustat, assuming that major ~0 is
* invalid
*/
sbuf.st_dev = makedev(~0, stress_mwc32());
VOID_RET(int, shim_ustat(sbuf.st_dev, &ubuf));
#endif
}
if (!keep_stressing_flag())
break;
#if defined(HAVE_SYS_STATVFS_H)
{
int i;
struct statvfs statvfs_buf;
/* POSIX.1-2001 statfs variant */
for (i = 0; i < n_mounts; i++) {
int ret;
if (!keep_stressing_flag())
break;
if (!mnts[i])
continue;
ret = statvfs(mnts[i], &statvfs_buf);
if ((ret < 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
if (errno != ENOSYS &&
errno != EOVERFLOW &&
errno != EACCES &&
errno != EPERM) {
pr_fail("%s: statvfs on %s failed: errno=%d (%s)\n",
args->name, mnts[i], errno,
strerror(errno));
}
}
/*
* Exercise invalid mount point
*/
VOID_RET(int, statvfs("/invalid_stress_ng", &statvfs_buf));
}
}
#endif
if (!keep_stressing_flag())
break;
clk = times(&tms_buf);
if ((clk == (clock_t)-1) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
pr_fail("%s: times failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
inc_counter(args);
} while (keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
stress_mount_free(mnts, n_mounts);
return EXIT_SUCCESS;
}
stressor_info_t stress_sysinfo_info = {
.stressor = stress_sysinfo,
.class = CLASS_OS,
.verify = VERIFY_OPTIONAL,
.help = help
};