summaryrefslogtreecommitdiff
path: root/tests/migration/stress.c
blob: cf8ce8b16d0f7634cf5a20ba42b2013d0d70116d (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * 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 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 <stdio.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/reboot.h>
#include <sys/syscall.h>
#include <linux/random.h>
#include <sys/time.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/mman.h>

const char *argv0;

#define PAGE_SIZE 4096

static int gettid(void)
{
    return syscall(SYS_gettid);
}

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 __attribute__((noreturn)) void exit_success(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(0);
    }
}

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 = strndup(start, end - start);
    else
        *val = 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);
        free(valstr);
        return -1;
    }
    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 int stressone(unsigned long long ramsizeMB)
{
    size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE;
    char *ram = malloc(ramsizeMB * 1024 * 1024);
    char *ramptr;
    size_t i, j, k;
    char *data = malloc(PAGE_SIZE);
    char *dataptr;
    size_t nMB = 0;
    unsigned long long before, after;

    if (!ram) {
        fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
                argv0, gettid(), ramsizeMB, strerror(errno));
        return -1;
    }
    if (!data) {
        fprintf(stderr, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n",
                argv0, gettid(), PAGE_SIZE, strerror(errno));
        free(ram);
        return -1;
    }

    /* 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 cna'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, PAGE_SIZE) < 0) {
        free(ram);
        free(data);
        return -1;
    }

    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 < 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;
            }
        }
    }

    free(data);
    free(ram);
}


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

    stressone(ramsizeMB);

    return NULL;
}

static int 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);

    return 0;
}


static int mount_misc(const char *fstype, const char *dir)
{
    if (mkdir(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);

    if (stress(ramsizeGB, ncpus) < 0)
        exit_failure();

    exit_success();
}