Blame test/dlopen_test.cpp

Packit 345191
/*
Packit 345191
 * Copyright (C) 2017 - 2018 Intel Corporation.
Packit 345191
 * All rights reserved.
Packit 345191
 *
Packit 345191
 * Redistribution and use in source and binary forms, with or without
Packit 345191
 * modification, are permitted provided that the following conditions are met:
Packit 345191
 * 1. Redistributions of source code must retain the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer.
Packit 345191
 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer in the documentation
Packit 345191
 *    and/or other materials provided with the distribution.
Packit 345191
 *
Packit 345191
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit 345191
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit 345191
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit 345191
 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 345191
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 345191
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 345191
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 345191
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit 345191
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 345191
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 345191
 */
Packit 345191
#include "allocator_perf_tool/HugePageOrganizer.hpp"
Packit 345191
Packit 345191
#include <dlfcn.h>
Packit 345191
Packit 345191
#include "common.h"
Packit 345191
Packit 345191
class DlopenTest: public :: testing::Test
Packit 345191
{
Packit 345191
protected:
Packit 345191
    DlopenTest()
Packit 345191
    {
Packit 345191
        const char *path = "/usr/lib64/libmemkind.so";
Packit 345191
        if (!pathExists(path)) {
Packit 345191
            path = "/usr/lib/libmemkind.so";
Packit 345191
        }
Packit 345191
        dlerror();
Packit 345191
        handle = dlopen(path, RTLD_LAZY);
Packit 345191
        assert((handle != NULL && dlerror() == NULL) && "Couldn't open libmemkind.so");
Packit 345191
        memkind_malloc = (memkind_malloc_t)dlsym(handle, "memkind_malloc");
Packit 345191
        assert(dlerror() == NULL && "Couldn't get memkind_malloc from memkind library");
Packit 345191
        memkind_free = (memkind_free_t)dlsym(handle, "memkind_free");
Packit 345191
        assert(dlerror() == NULL && "Couldn't get memkind_free from memkind library");
Packit 345191
    }
Packit 345191
Packit 345191
    ~DlopenTest()
Packit 345191
    {
Packit 345191
        dlclose(handle);
Packit 345191
    }
Packit 345191
Packit 345191
    void test(const char *kind_name, size_t alloc_size)
Packit 345191
    {
Packit 345191
        void **kind_ptr = (void **)dlsym(handle, kind_name);
Packit 345191
        EXPECT_TRUE(dlerror() == NULL) << "Couldn't get kind from memkind library";
Packit 345191
        EXPECT_TRUE(kind_ptr != NULL) << "Kind ptr to memkind library is NULL";
Packit 345191
Packit 345191
        void *allocation_ptr = memkind_malloc((*kind_ptr), alloc_size);
Packit 345191
        EXPECT_TRUE(allocation_ptr != NULL) << "Allocation with memkind_malloc failed";
Packit 345191
Packit 345191
        memset(allocation_ptr, 0, alloc_size);
Packit 345191
Packit 345191
        memkind_free((*kind_ptr), allocation_ptr);
Packit 345191
    }
Packit 345191
Packit 345191
    bool pathExists(const char *p)
Packit 345191
    {
Packit 345191
        struct stat info;
Packit 345191
        if (0 != stat(p, &info)) {
Packit 345191
            return false;
Packit 345191
        }
Packit 345191
        return true;
Packit 345191
    }
Packit 345191
Packit 345191
private:
Packit 345191
    void *handle;
Packit 345191
    typedef void *(*memkind_malloc_t)(void *, size_t);
Packit 345191
    typedef void (*memkind_free_t)(void *, void *);
Packit 345191
    memkind_malloc_t memkind_malloc;
Packit 345191
    memkind_free_t memkind_free;
Packit 345191
};
Packit 345191
Packit 345191
TEST_F(DlopenTest, test_TC_MEMKIND_DEFAULT_4194305_bytes)
Packit 345191
{
Packit 345191
    test("MEMKIND_DEFAULT", 4194305);
Packit 345191
}
Packit 345191
Packit 345191
TEST_F(DlopenTest, test_TC_MEMKIND_HBW_4194305_bytes)
Packit 345191
{
Packit 345191
    test("MEMKIND_HBW", 4194305);
Packit 345191
}
Packit 345191
Packit 345191
TEST_F(DlopenTest, test_TC_MEMKIND_HBW_HUGETLB_4194305_bytes)
Packit 345191
{
Packit 345191
    HugePageOrganizer huge_page_organizer(8);
Packit 345191
    test("MEMKIND_HBW_HUGETLB", 4194305);
Packit 345191
}
Packit 345191
Packit 345191
TEST_F(DlopenTest, test_TC_MEMKIND_HBW_PREFERRED_4194305_bytes)
Packit 345191
{
Packit 345191
    test("MEMKIND_HBW_PREFERRED", 4194305);
Packit 345191
}
Packit 345191
Packit 345191
TEST_F(DlopenTest, test_TC_MEMKIND_HBW_INTERLEAVE_4194305_bytes)
Packit 345191
{
Packit 345191
    test("MEMKIND_HBW_INTERLEAVE", 4194305);
Packit 345191
}