qemumemlocktest.c 4.2 KB
Newer Older
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
#include <config.h>

#include <unistd.h>

#include <sys/types.h>
#include <fcntl.h>

#include "testutils.h"

#ifdef WITH_QEMU

# include "datatypes.h"
# include "internal.h"
# include "virstring.h"
# include "conf/domain_conf.h"
# include "qemu/qemu_domain.h"

# include "testutilsqemu.h"

# define VIR_FROM_THIS VIR_FROM_QEMU

static virQEMUDriver driver;

struct testInfo {
    const char *name;
    unsigned long long memlock;
};

static int
testCompareMemLock(const void *data)
{
    const struct testInfo *info = data;
    virDomainDefPtr def = NULL;
    char *xml = NULL;
    int ret = -1;

    if (virAsprintf(&xml, "%s/qemumemlockdata/qemumemlock-%s.xml",
                    abs_srcdir, info->name) < 0)
        goto cleanup;

    if (!(def = virDomainDefParseFile(xml, driver.caps, driver.xmlopt, NULL,
                                      VIR_DOMAIN_DEF_PARSE_INACTIVE))) {
        goto cleanup;
    }

    ret = virTestCompareToULL(info->memlock, qemuDomainGetMemLockLimitBytes(def));

 cleanup:
49
    virDomainDefFree(def);
50 51 52 53 54
    VIR_FREE(xml);

    return ret;
}

A
Andrea Bolognani 已提交
55
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
56 57 58 59 60

static int
mymain(void)
{
    int ret = 0;
A
Andrea Bolognani 已提交
61
    char *fakerootdir;
62
    virQEMUCapsPtr qemuCaps = NULL;
A
Andrea Bolognani 已提交
63 64 65 66 67 68 69 70 71 72 73 74

    if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) {
        fprintf(stderr, "Out of memory\n");
        abort();
    }

    if (!mkdtemp(fakerootdir)) {
        fprintf(stderr, "Cannot create fakerootdir");
        abort();
    }

    setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1);
75

76 77
    if (qemuTestDriverInit(&driver) < 0) {
        VIR_FREE(fakerootdir);
78
        return EXIT_FAILURE;
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

    driver.privileged = true;

# define DO_TEST(name, memlock) \
    do { \
        static struct testInfo info = { \
            name, memlock \
        }; \
        if (virTestRun("QEMU MEMLOCK " name, testCompareMemLock, &info) < 0) \
            ret = -1; \
    } while (0)

    /* The tests below make sure that the memory locking limit is being
     * calculated correctly in a number of situations. Each test is
     * performed both on x86_64/pc and ppc64/pseries in order to account
     * for some architecture-specific details.
     *
     * kvm: simple KMV guest
     * tcg: simple TCG guest
     *
     * hardlimit: guest where <memtune><hard_limit> has been configured
     * locked:    guest where <memoryBacking><locked> has been enabled
     * hostdev:   guest that has some hostdev assigned
     *
     * The remaining tests cover different combinations of the above to
     * ensure settings are prioritized as expected.
     */

    qemuTestSetHostArch(driver.caps, VIR_ARCH_X86_64);

    DO_TEST("pc-kvm", 0);
    DO_TEST("pc-tcg", 0);

    DO_TEST("pc-hardlimit", 2147483648);
    DO_TEST("pc-locked", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED);
    DO_TEST("pc-hostdev", 2147483648);

    DO_TEST("pc-hardlimit+locked", 2147483648);
    DO_TEST("pc-hardlimit+hostdev", 2147483648);
    DO_TEST("pc-hardlimit+locked+hostdev", 2147483648);
    DO_TEST("pc-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED);

    qemuTestSetHostArch(driver.caps, VIR_ARCH_PPC64);
123 124 125 126 127 128 129 130 131 132
    if (!(qemuCaps = virQEMUCapsNew())) {
        ret = -1;
        goto cleanup;
    }

    virQEMUCapsSet(qemuCaps, QEMU_CAPS_DEVICE_SPAPR_PCI_HOST_BRIDGE);
    if (qemuTestCapsCacheInsert(driver.qemuCapsCache, qemuCaps) < 0) {
        ret = -1;
        goto cleanup;
    };
133 134 135 136 137 138

    DO_TEST("pseries-kvm", 20971520);
    DO_TEST("pseries-tcg", 0);

    DO_TEST("pseries-hardlimit", 2147483648);
    DO_TEST("pseries-locked", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED);
139
    DO_TEST("pseries-hostdev", 4320133120);
140 141 142 143 144 145

    DO_TEST("pseries-hardlimit+locked", 2147483648);
    DO_TEST("pseries-hardlimit+hostdev", 2147483648);
    DO_TEST("pseries-hardlimit+locked+hostdev", 2147483648);
    DO_TEST("pseries-locked+hostdev", VIR_DOMAIN_MEMORY_PARAM_UNLIMITED);

146 147 148
 cleanup:
    virObjectUnref(qemuCaps);

A
Andrea Bolognani 已提交
149 150 151
    if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
        virFileDeleteTree(fakerootdir);

152
    qemuTestDriverFree(&driver);
A
Andrea Bolognani 已提交
153
    VIR_FREE(fakerootdir);
154 155 156 157

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

158
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virpci"))
159 160 161 162 163 164 165 166 167 168

#else

int
main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_QEMU */