2 * Migration stress workload
4 * Copyright (c) 2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include <sys/reboot.h>
27 #include <sys/syscall.h>
28 #include <linux/random.h>
32 #include <sys/mount.h>
38 #define PAGE_SIZE 4096
40 static int gettid(void)
42 return syscall(SYS_gettid
);
45 static __attribute__((noreturn
)) void exit_failure(void)
50 fprintf(stderr
, "%s (%05d): ERROR: cannot reboot: %s\n",
51 argv0
, gettid(), strerror(errno
));
58 static __attribute__((noreturn
)) void exit_success(void)
63 fprintf(stderr
, "%s (%05d): ERROR: cannot reboot: %s\n",
64 argv0
, gettid(), strerror(errno
));
71 static int get_command_arg_str(const char *name
,
74 static char line
[1024];
75 FILE *fp
= fopen("/proc/cmdline", "r");
79 fprintf(stderr
, "%s (%05d): ERROR: cannot open /proc/cmdline: %s\n",
80 argv0
, gettid(), strerror(errno
));
84 if (!fgets(line
, sizeof line
, fp
)) {
85 fprintf(stderr
, "%s (%05d): ERROR: cannot read /proc/cmdline: %s\n",
86 argv0
, gettid(), strerror(errno
));
92 start
= strstr(line
, name
);
96 start
+= strlen(name
);
99 fprintf(stderr
, "%s (%05d): ERROR: no value provided for '%s' in /proc/cmdline\n",
100 argv0
, gettid(), name
);
104 end
= strstr(start
, " ");
106 end
= strstr(start
, "\n");
109 fprintf(stderr
, "%s (%05d): ERROR: no value provided for '%s' in /proc/cmdline\n",
110 argv0
, gettid(), name
);
115 *val
= strndup(start
, end
- start
);
117 *val
= strdup(start
);
122 static int get_command_arg_ull(const char *name
,
123 unsigned long long *val
)
128 int ret
= get_command_arg_str(name
, &valstr
);
133 *val
= strtoll(valstr
, &end
, 10);
135 fprintf(stderr
, "%s (%05d): ERROR: cannot parse %s value %s\n",
136 argv0
, gettid(), name
, valstr
);
145 static int random_bytes(char *buf
, size_t len
)
149 fd
= open("/dev/urandom", O_RDONLY
);
151 fprintf(stderr
, "%s (%05d): ERROR: cannot open /dev/urandom: %s\n",
152 argv0
, gettid(), strerror(errno
));
156 if (read(fd
, buf
, len
) != len
) {
157 fprintf(stderr
, "%s (%05d): ERROR: cannot read /dev/urandom: %s\n",
158 argv0
, gettid(), strerror(errno
));
169 static unsigned long long now(void)
173 gettimeofday(&tv
, NULL
);
175 return (tv
.tv_sec
* 1000ull) + (tv
.tv_usec
/ 1000ull);
178 static int stressone(unsigned long long ramsizeMB
)
180 size_t pagesPerMB
= 1024 * 1024 / PAGE_SIZE
;
181 char *ram
= malloc(ramsizeMB
* 1024 * 1024);
184 char *data
= malloc(PAGE_SIZE
);
187 unsigned long long before
, after
;
190 fprintf(stderr
, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
191 argv0
, gettid(), ramsizeMB
, strerror(errno
));
195 fprintf(stderr
, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n",
196 argv0
, gettid(), PAGE_SIZE
, strerror(errno
));
201 /* We don't care about initial state, but we do want
202 * to fault it all into RAM, otherwise the first iter
203 * of the loop below will be quite slow. We cna't use
204 * 0x0 as the byte as gcc optimizes that away into a
205 * calloc instead :-) */
206 memset(ram
, 0xfe, ramsizeMB
* 1024 * 1024);
208 if (random_bytes(data
, PAGE_SIZE
) < 0) {
219 for (i
= 0; i
< ramsizeMB
; i
++, nMB
++) {
220 for (j
= 0; j
< pagesPerMB
; j
++) {
222 for (k
= 0; k
< PAGE_SIZE
; k
+= sizeof(long long)) {
223 ramptr
+= sizeof(long long);
224 dataptr
+= sizeof(long long);
225 *(unsigned long long *)ramptr
^= *(unsigned long long *)dataptr
;
231 fprintf(stderr
, "%s (%05d): INFO: %06llums copied 1 GB in %05llums\n",
232 argv0
, gettid(), after
, after
- before
);
244 static void *stressthread(void *arg
)
246 unsigned long long ramsizeMB
= *(unsigned long long *)arg
;
248 stressone(ramsizeMB
);
253 static int stress(unsigned long long ramsizeGB
, int ncpus
)
256 unsigned long long ramsizeMB
= ramsizeGB
* 1024 / ncpus
;
259 for (i
= 0; i
< ncpus
; i
++) {
261 pthread_create(&thr
, NULL
,
262 stressthread
, &ramsizeMB
);
265 stressone(ramsizeMB
);
271 static int mount_misc(const char *fstype
, const char *dir
)
273 if (mkdir(dir
, 0755) < 0 && errno
!= EEXIST
) {
274 fprintf(stderr
, "%s (%05d): ERROR: cannot create %s: %s\n",
275 argv0
, gettid(), dir
, strerror(errno
));
279 if (mount("none", dir
, fstype
, 0, NULL
) < 0) {
280 fprintf(stderr
, "%s (%05d): ERROR: cannot mount %s: %s\n",
281 argv0
, gettid(), dir
, strerror(errno
));
288 static int mount_all(void)
290 if (mount_misc("proc", "/proc") < 0 ||
291 mount_misc("sysfs", "/sys") < 0 ||
292 mount_misc("tmpfs", "/dev") < 0)
295 mknod("/dev/urandom", 0777 | S_IFCHR
, makedev(1, 9));
296 mknod("/dev/random", 0777 | S_IFCHR
, makedev(1, 8));
301 int main(int argc
, char **argv
)
303 unsigned long long ramsizeGB
= 1;
307 const char *sopt
= "hr:c:";
308 struct option lopt
[] = {
309 { "help", no_argument
, NULL
, 'h' },
310 { "ramsize", required_argument
, NULL
, 'r' },
311 { "cpus", required_argument
, NULL
, 'c' },
319 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
323 ramsizeGB
= strtoll(optarg
, &end
, 10);
324 if (errno
!= 0 || *end
) {
325 fprintf(stderr
, "%s (%05d): ERROR: Cannot parse RAM size %s\n",
326 argv0
, gettid(), optarg
);
333 ncpus
= strtoll(optarg
, &end
, 10);
334 if (errno
!= 0 || *end
) {
335 fprintf(stderr
, "%s (%05d): ERROR: Cannot parse CPU count %s\n",
336 argv0
, gettid(), optarg
);
343 fprintf(stderr
, "%s: [--help][--ramsize GB][--cpus N]\n", argv0
);
352 ret
= get_command_arg_ull("ramsize", &ramsizeGB
);
358 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
360 fprintf(stdout
, "%s (%05d): INFO: RAM %llu GiB across %d CPUs\n",
361 argv0
, gettid(), ramsizeGB
, ncpus
);
363 if (stress(ramsizeGB
, ncpus
) < 0)