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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (C) 2016 - 2021 Intel Corporation. */
#include "allocator_perf_tool/HugePageOrganizer.hpp"
#include "common.h"
#include "hbwmalloc.h"
#include "memkind.h"
#include "memkind/internal/memkind_hbw.h"
#include <numa.h>
#include <numaif.h>
#include <stdint.h>
#include <sys/mman.h>
#define SHIFT_BYTES(ptr, bytes) ((char *)(ptr) + (bytes))
/*
* Set of tests for hbw_verify_memory_region() function,
* which intend to check if allocated memory fully fall into high bandwidth
* memory. Note: in this tests we are using internal function
* memkind_hbw_all_get_mbind_nodemask(). In future we intend to rewrite this
* function when we will have replacement in new API.
*/
class HbwVerifyFunctionTest: public ::testing::Test
{
protected:
const size_t BLOCK_SIZE = 64;
const size_t page_size = sysconf(_SC_PAGESIZE);
const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
};
/*
* Group of basic tests for different sizes of allocations
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_page_size_not_round)
{
size_t size = page_size * 1024 + 5;
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
hbw_free(ptr);
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_page_size_round)
{
size_t size = page_size * 1024;
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
hbw_free(ptr);
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_iterate_1_byte_to_8194_bytes)
{
for (size_t size = 1; size <= (page_size * 2 + 2);
size++) { // iterate through 2 pages and 2 bytes
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
hbw_free(ptr);
}
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_ext_5GB)
{
size_t size = 5ull * (1 << 30); // 5GB - big allocation
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
hbw_free(ptr);
}
/*
* Test setting memory without HBW_TOUCH_PAGES flag
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_setting_memory_without_flag)
{
size_t size = page_size;
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
memset(ptr, '.', size);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, 0), 0);
for (size_t i = 0; i < size; i++) { // check that content is unchanged
EXPECT_TRUE(ptr[i] == '.');
}
hbw_free(ptr);
}
/*
* Test HBW_TOUCH_PAGES flag
*/
TEST_F(HbwVerifyFunctionTest,
test_TC_MEMKIND_HBW_TOUCH_PAGES_check_overwritten_content)
{
size_t size = 5 * page_size;
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
memset(ptr, '.', size);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
for (size_t i = 0; i < size;
i++) { // check that content of all pages doesn't change
EXPECT_TRUE(ptr[i] == '.');
}
hbw_free(ptr);
}
/*
* Tests check if number of 64-page blocks are working correctly
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_many_blocks_round)
{
size_t size = 16 * (BLOCK_SIZE * page_size); // exactly 16 * 64-page blocks
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
hbw_free(ptr);
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_many_blocks_not_round)
{
size_t size = (16 * BLOCK_SIZE * page_size) +
(8 * page_size); // 16 * 64-page blocks and 1 * 8-paged block
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
hbw_free(ptr);
}
/*
* Check if 2 blocks and parts of the third block are working correctly.
* With this test we can be sure that different sizes of allocation (not only
* whole blocks) are ok.
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_iterate_not_round)
{
for (size_t i = 1; i < BLOCK_SIZE; i++) {
size_t size = (2 * BLOCK_SIZE * page_size) +
(i * page_size); // 2 * 64-paged blocks and iterate through whole
// 3rd 64-paged block
char *ptr = (char *)hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
hbw_free(ptr);
}
}
/*
* Tests for other kinds and malloc
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_2MBPages_HBW_HUGETLB)
{
HugePageOrganizer huge_page_organizer(16);
size_t size = 2 * 1024 * 1024 * 10; // 10 * 2MB pages
char *ptr = (char *)memkind_malloc(MEMKIND_HBW_HUGETLB, size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);
memkind_free(MEMKIND_HBW_HUGETLB, ptr);
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_DEFAULT)
{
size_t size = page_size * 1024;
char *ptr = (char *)memkind_malloc(MEMKIND_DEFAULT, size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), -1);
memkind_free(MEMKIND_DEFAULT, ptr);
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_malloc)
{
size_t size = page_size * 1024;
char *ptr = (char *)malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), -1);
free(ptr);
}
/*
* Group of negative tests
*/
TEST_F(HbwVerifyFunctionTest,
test_TC_MEMKIND_Negative_size_0_and_SET_MEMORY_flag)
{
void *ptr = hbw_malloc(page_size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, 0, HBW_TOUCH_PAGES), EINVAL);
hbw_free(ptr);
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Negative_size_0_without_flag)
{
void *ptr = hbw_malloc(page_size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, 0, 0), EINVAL);
hbw_free(ptr);
}
TEST_F(HbwVerifyFunctionTest,
test_TC_MEMKIND_Negative_Uninitialized_Memory_without_flag)
{
void *ptr = NULL;
EXPECT_EQ(hbw_verify_memory_region(ptr, page_size * 1024, 0), EINVAL);
}
TEST_F(HbwVerifyFunctionTest,
test_TC_MEMKIND_Negative_Uninitialized_Memory_and_SET_MEMORY_flag)
{
void *ptr = NULL;
EXPECT_EQ(hbw_verify_memory_region(ptr, page_size * 1024, HBW_TOUCH_PAGES),
EINVAL);
}
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Negative_Without_Memset)
{
size_t size = page_size * 1024;
void *ptr = hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
EXPECT_EQ(hbw_verify_memory_region(ptr, size, 0), -1);
hbw_free(ptr);
}
/*
* Corner cases: tests for half of pages
* + HBM memory
* # HBM memory and verified
* - not HBM memory, but allocated
* = not HBM memory and verified
*/
/* 3 pages:
* |++##|####|##++|
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages)
{
size_t size = 3 * page_size;
nodemask_t nodemask;
struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};
// function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when
// we will have replacement in new API.
memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp,
hbw_nodemask.size);
char *ptr = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
ASSERT_FALSE(ptr == NULL);
// all pages should fall on HBM
mbind(ptr, size, MPOL_BIND, hbw_nodemask.maskp, NUMA_NUM_NODES, 0);
// verified are: half of the first page, second page and half of the third
// page
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size / 2),
size - page_size, HBW_TOUCH_PAGES),
0);
EXPECT_EQ(munmap(ptr, size), 0);
}
/* 3 pages:
* |####|####|----|
* |++##|####|==--|
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages_1_and_2_page)
{
size_t size = 3 * page_size;
nodemask_t nodemask;
struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};
// function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when
// we will have replacement in new API.
memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp,
hbw_nodemask.size);
char *ptr = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
ASSERT_FALSE(ptr == NULL);
// first and second page should fall on HBM
mbind(ptr, size - page_size, MPOL_BIND, hbw_nodemask.maskp, NUMA_NUM_NODES,
0);
// check if mbind is successful |####|####|----|
EXPECT_EQ(hbw_verify_memory_region(ptr, size - page_size, HBW_TOUCH_PAGES),
0);
// verified are: half of the first page, second page and half of the third
// page |++##|####|==--|
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size / 2),
size - page_size, HBW_TOUCH_PAGES),
-1);
EXPECT_EQ(munmap(ptr, size), 0);
}
/* 3 pages:
* |----|####|####|
* |--==|####|##++|
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages_2_and_3_page)
{
size_t size = 3 * page_size;
nodemask_t nodemask;
struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};
// function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when
// we will have replacement in new API.
memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp,
hbw_nodemask.size);
char *ptr = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
ASSERT_FALSE(ptr == NULL);
// second and third page should fall on HBM
mbind(SHIFT_BYTES(ptr, page_size), size - page_size, MPOL_BIND,
hbw_nodemask.maskp, NUMA_NUM_NODES, 0);
// check if mbind is successful |----|####|####|
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size),
size - page_size, HBW_TOUCH_PAGES),
0);
// verified are: half of the first page, second page and half of the third
// page
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size / 2),
size - page_size, HBW_TOUCH_PAGES),
-1);
EXPECT_EQ(munmap(ptr, size), 0);
}
/* 3 pages:
* |####|----|++++| and |++++|----|####|
* |++##|====|##++|
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages_1_and_3_page)
{
size_t size = 3 * page_size;
nodemask_t nodemask;
struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};
// function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when
// we will have replacement in new API.
memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp,
hbw_nodemask.size);
char *ptr = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
ASSERT_FALSE(ptr == NULL);
// first and third page should fall on HBM
mbind(ptr, page_size, MPOL_BIND, hbw_nodemask.maskp, NUMA_NUM_NODES, 0);
mbind(SHIFT_BYTES(ptr, 2 * page_size), page_size, MPOL_BIND,
hbw_nodemask.maskp, NUMA_NUM_NODES, 0);
// check if mbind is successful |####|----|++++| and |++++|----|####|
EXPECT_EQ(
hbw_verify_memory_region(ptr, size - 2 * page_size, HBW_TOUCH_PAGES),
0);
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, 2 * page_size),
size - 2 * page_size, HBW_TOUCH_PAGES),
0);
// verified are: half of the first page, second page and half of the third
// page
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size / 2),
size - page_size, HBW_TOUCH_PAGES),
-1);
EXPECT_EQ(munmap(ptr, size), 0);
}
/* 5 pages:
* |----|+###|####|###+|----|
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Boundaries_CornerCase)
{
size_t size = 5 * page_size;
nodemask_t nodemask;
struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};
// function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when
// we will have replacement in new API.
memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp,
hbw_nodemask.size);
char *ptr = (char *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
ASSERT_FALSE(ptr == NULL);
size_t tested_pages_size = size - 2 * page_size;
// second, third and fourth page should fall on HBM
mbind(SHIFT_BYTES(ptr, page_size), tested_pages_size, MPOL_BIND,
hbw_nodemask.maskp, NUMA_NUM_NODES, 0);
// verified are: second byte of the second page to last byte -1 of fourth
// page
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size + 1),
tested_pages_size - 2, HBW_TOUCH_PAGES),
0);
EXPECT_EQ(munmap(ptr, size), 0);
}
/* 5 pages:
* |-###|####|####|####|###-|
*/
TEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_partial_verification)
{
size_t size = 5 * page_size;
// all pages should fall on HBM
void *ptr = hbw_malloc(size);
ASSERT_FALSE(ptr == NULL);
// but only second byte of the first page to last byte -1 of the last page
// are touched
memset(SHIFT_BYTES(ptr, 1), 0, size - 2);
// verified are: second byte of the first page to last byte -1 of last page
EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, 1), size - 2, 0), 0);
hbw_free(ptr);
}
|