qemu/tests/migration/stress.c

368 lines
9.0 KiB
C
Raw Normal View History

tests: introduce a framework for testing migration performance This introduces a moderately general purpose framework for testing performance of migration. The initial guest workload is provided by the included 'stress' program, which is configured to spawn one thread per guest CPU and run a maximally memory intensive workload. It will loop over GB of memory, xor'ing each byte with data from a 4k array of random bytes. This ensures heavy read and write load across all of guest memory to stress the migration performance. While running the 'stress' program will record how long it takes to xor each GB of memory and print this data for later reporting. The test engine will spawn a pair of QEMU processes, either on the same host, or with the target on a remote host via ssh, using the host kernel and a custom initrd built with 'stress' as the /init binary. Kernel command line args are set to ensure a fast kernel boot time (< 1 second) between launching QEMU and the stress program starting execution. None the less, the test engine will initially wait N seconds for the guest workload to stablize, before starting the migration operation. When migration is running, the engine will use pause, post-copy, autoconverge, xbzrle compression and multithread compression features, as well as downtime & bandwidth tuning to encourage completion. If migration completes, the test engine will wait N seconds again for the guest workooad to stablize on the target host. If migration does not complete after a preset number of iterations, it will be aborted. While the QEMU process is running on the source host, the test engine will sample the host CPU usage of QEMU as a whole, and each vCPU thread. While migration is running, it will record all the stats reported by 'query-migration'. Finally, it will capture the output of the stress program running in the guest. All the data produced from a single test execution is recorded in a structured JSON file. A separate program is then able to create interactive charts using the "plotly" python + javascript libraries, showing the characteristics of the migration. The data output provides visualization of the effect on guest vCPU workloads from the migration process, the corresponding vCPU utilization on the host, and the overall CPU hit from QEMU on the host. This is correlated from statistics from the migration process, such as downtime, vCPU throttling and iteration number. While the tests can be run individually with arbitrary parameters, there is also a facility for producing batch reports for a number of pre-defined scenarios / comparisons, in order to be able to get standardized results across different hardware configurations (eg TCP vs RDMA, or comparing different VCPU counts / memory sizes, etc). To use this, first you must build the initrd image $ make tests/migration/initrd-stress.img To run a a one-shot test with all default parameters $ ./tests/migration/guestperf.py > result.json This has many command line args for varying its behaviour. For example, to increase the RAM size and CPU count and bind it to specific host NUMA nodes $ ./tests/migration/guestperf.py \ --mem 4 --cpus 2 \ --src-mem-bind 0 --src-cpu-bind 0,1 \ --dst-mem-bind 1 --dst-cpu-bind 2,3 \ > result.json Using mem + cpu binding is strongly recommended on NUMA machines, otherwise the guest performance results will vary wildly between runs of the test due to lucky/unlucky NUMA placement, making sensible data analysis impossible. To make it run across separate hosts: $ ./tests/migration/guestperf.py \ --dst-host somehostname > result.json To request that post-copy is enabled, with switchover after 5 iterations $ ./tests/migration/guestperf.py \ --post-copy --post-copy-iters 5 > result.json Once a result.json file is created, a graph of the data can be generated, showing guest workload performance per thread and the migration iteration points: $ ./tests/migration/guestperf-plot.py --output result.html \ --migration-iters --split-guest-cpu result.json To further include host vCPU utilization and overall QEMU utilization $ ./tests/migration/guestperf-plot.py --output result.html \ --migration-iters --split-guest-cpu \ --qemu-cpu --vcpu-cpu result.json NB, the 'guestperf-plot.py' command requires that you have the plotly python library installed. eg you must do $ pip install --user plotly Viewing the result.html file requires that you have the plotly.min.js file in the same directory as the HTML output. This js file is installed as part of the plotly python library, so can be found in $HOME/.local/lib/python2.7/site-packages/plotly/offline/plotly.min.js The guestperf-plot.py program can accept multiple json files to plot, enabling results from different configurations to be compared. Finally, to run the entire standardized set of comparisons $ ./tests/migration/guestperf-batch.py \ --dst-host somehost \ --mem 4 --cpus 2 \ --src-mem-bind 0 --src-cpu-bind 0,1 \ --dst-mem-bind 1 --dst-cpu-bind 2,3 --output tcp-somehost-4gb-2cpu will store JSON files from all scenarios in the directory named tcp-somehost-4gb-2cpu Signed-off-by: Daniel P. Berrange <berrange@redhat.com> Message-Id: <1469020993-29426-7-git-send-email-berrange@redhat.com> Signed-off-by: Amit Shah <amit.shah@redhat.com>
2016-07-20 16:23:13 +03:00
/*
* 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();
}