summaryrefslogtreecommitdiffstats
path: root/tests/migration/stress.c
blob: 88acf8dc25792d1ca2300a7f55bb5b2cd2f5450f (plain) (blame)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Migration stress workload
 *
 * Copyright (c) 2016 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"
#include <getopt.h>
#include <sys/reboot.h>
#include <sys/syscall.h>
#include <linux/random.h>
#include <pthread.h>
#include <sys/mount.h>

const char *argv0;

#define RAM_PAGE_SIZE 4096

#ifndef CONFIG_GETTID
static int gettid(void)
{
    return syscall(SYS_gettid);
}
#endif

static __attribute__((noreturn)) void exit_failure(void)
{
    if (getpid() == 1) {
        sync();
        reboot(RB_POWER_OFF);
        fprintf(stderr, "%s (%05d): ERROR: cannot reboot: %s\n",
                argv0, gettid(), strerror(errno));
        abort();
    } else {
        exit(1);
    }
}

static int get_command_arg_str(const char *name,
                               char **val)
{
    static char line[1024];
    FILE *fp = fopen("/proc/cmdline", "r");
    char *start, *end;

    if (fp == NULL) {
        fprintf(stderr, "%s (%05d): ERROR: cannot open /proc/cmdline: %s\n",
                argv0, gettid(), strerror(errno));
        return -1;
    }

    if (!fgets(line, sizeof line, fp)) {
        fprintf(stderr, "%s (%05d): ERROR: cannot read /proc/cmdline: %s\n",
                argv0, gettid(), strerror(errno));
        fclose(fp);
        return -1;
    }
    fclose(fp);

    start = strstr(line, name);
    if (!start)
        return 0;

    start += strlen(name);

    if (*start != '=') {
        fprintf(stderr, "%s (%05d): ERROR: no value provided for '%s' in /proc/cmdline\n",
                argv0, gettid(), name);
    }
    start++;

    end = strstr(start, " ");
    if (!end)
        end = strstr(start, "\n");

    if (end == start) {
        fprintf(stderr, "%s (%05d): ERROR: no value provided for '%s' in /proc/cmdline\n",
                argv0, gettid(), name);
        return -1;
    }

    if (end)
        *val = g_strndup(start, end - start);
    else
        *val = g_strdup(start);
    return 1;
}


static int get_command_arg_ull(const char *name,
                               unsigned long long *val)
{
    char *valstr;
    char *end;

    int ret = get_command_arg_str(name, &valstr);
    if (ret <= 0)
        return ret;

    errno = 0;
    *val = strtoll(valstr, &end, 10);
    if (errno || *end) {
        fprintf(stderr, "%s (%05d): ERROR: cannot parse %s value %s\n",
                argv0, gettid(), name, valstr);
        g_free(valstr);
        return -1;
    }
    g_free(valstr);
    return 0;
}


static int random_bytes(char *buf, size_t len)
{
    int fd;

    fd = open("/dev/urandom", O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "%s (%05d): ERROR: cannot open /dev/urandom: %s\n",
                argv0, gettid(), strerror(errno));
        return -1;
    }

    if (read(fd, buf, len) != len) {
        fprintf(stderr, "%s (%05d): ERROR: cannot read /dev/urandom: %s\n",
                argv0, gettid(), strerror(errno));
        close(fd);
        return -1;
    }

    close(fd);

    return 0;
}


static unsigned long long now(void)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);

    return (tv.tv_sec * 1000ull) + (tv.tv_usec / 1000ull);
}

static void stressone(unsigned long long ramsizeMB)
{
    size_t pagesPerMB = 1024 * 1024 / RAM_PAGE_SIZE;
    g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024);
    char *ramptr;
    size_t i, j, k;
    g_autofree char *data = g_malloc(RAM_PAGE_SIZE);
    char *dataptr;
    size_t nMB = 0;
    unsigned long long before, after;

    /* We don't care about initial state, but we do want
     * to fault it all into RAM, otherwise the first iter
     * of the loop below will be quite slow. We can't use
     * 0x0 as the byte as gcc optimizes that away into a
     * calloc instead :-) */
    memset(ram, 0xfe, ramsizeMB * 1024 * 1024);

    if (random_bytes(data, RAM_PAGE_SIZE) < 0) {
        return;
    }

    before = now();

    while (1) {

        ramptr = ram;
        for (i = 0; i < ramsizeMB; i++, nMB++) {
            for (j = 0; j < pagesPerMB; j++) {
                dataptr = data;
                for (k = 0; k < RAM_PAGE_SIZE; k += sizeof(long long)) {
                    ramptr += sizeof(long long);
                    dataptr += sizeof(long long);
                    *(unsigned long long *)ramptr ^= *(unsigned long long *)dataptr;
                }
            }

            if (nMB == 1024) {
                after = now();
                fprintf(stderr, "%s (%05d): INFO: %06llums copied 1 GB in %05llums\n",
                        argv0, gettid(), after, after - before);
                before = now();
                nMB = 0;
            }
        }
    }
}


static void *stressthread(void *arg)
{
    unsigned long long ramsizeMB = *(unsigned long long *)arg;

    stressone(ramsizeMB);

    return NULL;
}

static void stress(unsigned long long ramsizeGB, int ncpus)
{
    size_t i;
    unsigned long long ramsizeMB = ramsizeGB * 1024 / ncpus;
    ncpus--;

    for (i = 0; i < ncpus; i++) {
        pthread_t thr;
        pthread_create(&thr, NULL,
                       stressthread,   &ramsizeMB);
    }

    stressone(ramsizeMB);
}


static int mount_misc(const char *fstype, const char *dir)
{
    if (g_mkdir_with_parents(dir, 0755) < 0 && errno != EEXIST) {
        fprintf(stderr, "%s (%05d): ERROR: cannot create %s: %s\n",
                argv0, gettid(), dir, strerror(errno));
        return -1;
    }

    if (mount("none", dir, fstype, 0, NULL) < 0) {
        fprintf(stderr, "%s (%05d): ERROR: cannot mount %s: %s\n",
                argv0, gettid(), dir, strerror(errno));
        return -1;
    }

    return 0;
}

static int mount_all(void)
{
    if (mount_misc("proc", "/proc") < 0 ||
        mount_misc("sysfs", "/sys") < 0 ||
        mount_misc("tmpfs", "/dev") < 0)
        return -1;

    mknod("/dev/urandom", 0777 | S_IFCHR, makedev(1, 9));
    mknod("/dev/random", 0777 | S_IFCHR, makedev(1, 8));

    return 0;
}

int main(int argc, char **argv)
{
    unsigned long long ramsizeGB = 1;
    char *end;
    int ch;
    int opt_ind = 0;
    const char *sopt = "hr:c:";
    struct option lopt[] = {
        { "help", no_argument, NULL, 'h' },
        { "ramsize", required_argument, NULL, 'r' },
        { "cpus", required_argument, NULL, 'c' },
        { NULL, 0, NULL, 0 }
    };
    int ret;
    int ncpus = 0;

    argv0 = argv[0];

    while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
        switch (ch) {
        case 'r':
            errno = 0;
            ramsizeGB = strtoll(optarg, &end, 10);
            if (errno != 0 || *end) {
                fprintf(stderr, "%s (%05d): ERROR: Cannot parse RAM size %s\n",
                        argv0, gettid(), optarg);
                exit_failure();
            }
            break;

        case 'c':
            errno = 0;
            ncpus = strtoll(optarg, &end, 10);
            if (errno != 0 || *end) {
                fprintf(stderr, "%s (%05d): ERROR: Cannot parse CPU count %s\n",
                        argv0, gettid(), optarg);
                exit_failure();
            }
            break;

        case '?':
        case 'h':
            fprintf(stderr, "%s: [--help][--ramsize GB][--cpus N]\n", argv0);
            exit_failure();
        }
    }

    if (getpid() == 1) {
        if (mount_all() < 0)
            exit_failure();

        ret = get_command_arg_ull("ramsize", &ramsizeGB);
        if (ret < 0)
            exit_failure();
    }

    if (ncpus == 0)
        ncpus = sysconf(_SC_NPROCESSORS_ONLN);

    fprintf(stdout, "%s (%05d): INFO: RAM %llu GiB across %d CPUs\n",
            argv0, gettid(), ramsizeGB, ncpus);

    stress(ramsizeGB, ncpus);

    exit_failure();
}