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.1 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/>.
20 #include "qemu/osdep.h"
22 #include <sys/reboot.h>
23 #include <sys/syscall.h>
24 #include <linux/random.h>
26 #include <sys/mount.h>
30 #define RAM_PAGE_SIZE 4096
33 static int gettid(void)
35 return syscall(SYS_gettid
);
39 static __attribute__((noreturn
)) void exit_failure(void)
44 fprintf(stderr
, "%s (%05d): ERROR: cannot reboot: %s\n",
45 argv0
, gettid(), strerror(errno
));
52 static int get_command_arg_str(const char *name
,
55 static char line
[1024];
56 FILE *fp
= fopen("/proc/cmdline", "r");
60 fprintf(stderr
, "%s (%05d): ERROR: cannot open /proc/cmdline: %s\n",
61 argv0
, gettid(), strerror(errno
));
65 if (!fgets(line
, sizeof line
, fp
)) {
66 fprintf(stderr
, "%s (%05d): ERROR: cannot read /proc/cmdline: %s\n",
67 argv0
, gettid(), strerror(errno
));
73 start
= strstr(line
, name
);
77 start
+= strlen(name
);
80 fprintf(stderr
, "%s (%05d): ERROR: no value provided for '%s' in /proc/cmdline\n",
81 argv0
, gettid(), name
);
85 end
= strstr(start
, " ");
87 end
= strstr(start
, "\n");
90 fprintf(stderr
, "%s (%05d): ERROR: no value provided for '%s' in /proc/cmdline\n",
91 argv0
, gettid(), name
);
96 *val
= g_strndup(start
, end
- start
);
98 *val
= g_strdup(start
);
103 static int get_command_arg_ull(const char *name
,
104 unsigned long long *val
)
109 int ret
= get_command_arg_str(name
, &valstr
);
114 *val
= strtoll(valstr
, &end
, 10);
116 fprintf(stderr
, "%s (%05d): ERROR: cannot parse %s value %s\n",
117 argv0
, gettid(), name
, valstr
);
126 static int random_bytes(char *buf
, size_t len
)
130 fd
= open("/dev/urandom", O_RDONLY
);
132 fprintf(stderr
, "%s (%05d): ERROR: cannot open /dev/urandom: %s\n",
133 argv0
, gettid(), strerror(errno
));
137 if (read(fd
, buf
, len
) != len
) {
138 fprintf(stderr
, "%s (%05d): ERROR: cannot read /dev/urandom: %s\n",
139 argv0
, gettid(), strerror(errno
));
150 static unsigned long long now(void)
154 gettimeofday(&tv
, NULL
);
156 return (tv
.tv_sec
* 1000ull) + (tv
.tv_usec
/ 1000ull);
159 static void stressone(unsigned long long ramsizeMB
)
161 size_t pagesPerMB
= 1024 * 1024 / RAM_PAGE_SIZE
;
162 g_autofree
char *ram
= g_malloc(ramsizeMB
* 1024 * 1024);
165 g_autofree
char *data
= g_malloc(RAM_PAGE_SIZE
);
168 unsigned long long before
, after
;
170 /* We don't care about initial state, but we do want
171 * to fault it all into RAM, otherwise the first iter
172 * of the loop below will be quite slow. We can't use
173 * 0x0 as the byte as gcc optimizes that away into a
174 * calloc instead :-) */
175 memset(ram
, 0xfe, ramsizeMB
* 1024 * 1024);
177 if (random_bytes(data
, RAM_PAGE_SIZE
) < 0) {
186 for (i
= 0; i
< ramsizeMB
; i
++, nMB
++) {
187 for (j
= 0; j
< pagesPerMB
; j
++) {
189 for (k
= 0; k
< RAM_PAGE_SIZE
; k
+= sizeof(long long)) {
190 ramptr
+= sizeof(long long);
191 dataptr
+= sizeof(long long);
192 *(unsigned long long *)ramptr
^= *(unsigned long long *)dataptr
;
198 fprintf(stderr
, "%s (%05d): INFO: %06llums copied 1 GB in %05llums\n",
199 argv0
, gettid(), after
, after
- before
);
208 static void *stressthread(void *arg
)
210 unsigned long long ramsizeMB
= *(unsigned long long *)arg
;
212 stressone(ramsizeMB
);
217 static void stress(unsigned long long ramsizeGB
, int ncpus
)
220 unsigned long long ramsizeMB
= ramsizeGB
* 1024 / ncpus
;
223 for (i
= 0; i
< ncpus
; i
++) {
225 pthread_create(&thr
, NULL
,
226 stressthread
, &ramsizeMB
);
229 stressone(ramsizeMB
);
233 static int mount_misc(const char *fstype
, const char *dir
)
235 if (mkdir(dir
, 0755) < 0 && errno
!= EEXIST
) {
236 fprintf(stderr
, "%s (%05d): ERROR: cannot create %s: %s\n",
237 argv0
, gettid(), dir
, strerror(errno
));
241 if (mount("none", dir
, fstype
, 0, NULL
) < 0) {
242 fprintf(stderr
, "%s (%05d): ERROR: cannot mount %s: %s\n",
243 argv0
, gettid(), dir
, strerror(errno
));
250 static int mount_all(void)
252 if (mount_misc("proc", "/proc") < 0 ||
253 mount_misc("sysfs", "/sys") < 0 ||
254 mount_misc("tmpfs", "/dev") < 0)
257 mknod("/dev/urandom", 0777 | S_IFCHR
, makedev(1, 9));
258 mknod("/dev/random", 0777 | S_IFCHR
, makedev(1, 8));
263 int main(int argc
, char **argv
)
265 unsigned long long ramsizeGB
= 1;
269 const char *sopt
= "hr:c:";
270 struct option lopt
[] = {
271 { "help", no_argument
, NULL
, 'h' },
272 { "ramsize", required_argument
, NULL
, 'r' },
273 { "cpus", required_argument
, NULL
, 'c' },
281 while ((ch
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_ind
)) != -1) {
285 ramsizeGB
= strtoll(optarg
, &end
, 10);
286 if (errno
!= 0 || *end
) {
287 fprintf(stderr
, "%s (%05d): ERROR: Cannot parse RAM size %s\n",
288 argv0
, gettid(), optarg
);
295 ncpus
= strtoll(optarg
, &end
, 10);
296 if (errno
!= 0 || *end
) {
297 fprintf(stderr
, "%s (%05d): ERROR: Cannot parse CPU count %s\n",
298 argv0
, gettid(), optarg
);
305 fprintf(stderr
, "%s: [--help][--ramsize GB][--cpus N]\n", argv0
);
314 ret
= get_command_arg_ull("ramsize", &ramsizeGB
);
320 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
322 fprintf(stdout
, "%s (%05d): INFO: RAM %llu GiB across %d CPUs\n",
323 argv0
, gettid(), ramsizeGB
, ncpus
);
325 stress(ramsizeGB
, ncpus
);