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
|
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2018-2020, Intel Corporation */
/*
* concurrent_hash_map_reorder.cpp -- pmem::obj::concurrent_hash_map test
*
*/
#include "unittest.hpp"
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <future>
#include <iostream>
#include <libpmemobj++/container/concurrent_hash_map.hpp>
#define LAYOUT "persistent_concurrent_hash_map"
namespace nvobj = pmem::obj;
namespace
{
typedef nvobj::concurrent_hash_map<nvobj::p<int>, nvobj::p<int>>
persistent_map_type;
typedef persistent_map_type::value_type value_type;
struct root {
nvobj::persistent_ptr<persistent_map_type> cons;
};
/*
* All elements will go to the same bucket.
* After rehash, element 0, 1, 4 will remain in the original bucket, rest
* will go to the new one.
*/
static constexpr int elements[] = {1, 257, 1281, 513, 1025, 769};
static constexpr int elements_size = sizeof(elements) / sizeof(elements[0]);
static constexpr int test_element = 1793;
static constexpr int hash_map_size = 255;
/*
* Insert test elements.
*/
void
insert(nvobj::pool<root> &pop)
{
auto persistent_map = pop.root()->cons;
persistent_map->runtime_initialize();
auto TEST_KEY = 10000;
/* Prepare hash_map, so that adding elements[] will cause rehash */
for (int i = 0; i < static_cast<int>(hash_map_size - elements_size);
i++) {
auto ret = persistent_map->insert(
value_type(TEST_KEY + i, TEST_KEY + i));
UT_ASSERT(ret == true);
}
for (int i = 0; i < elements_size; i++) {
auto ret = persistent_map->insert(
value_type(elements[i], elements[i]));
UT_ASSERT(ret == true);
}
}
void
rehash(nvobj::pool<root> &pop)
{
auto persistent_map = pop.root()->cons;
persistent_map->runtime_initialize();
/* Force rehash */
auto ret = persistent_map->count(test_element);
/* there is no element test_element */
UT_ASSERT(ret == false);
}
void
check_consistency(nvobj::pool<root> &pop)
{
auto persistent_map = pop.root()->cons;
persistent_map->runtime_initialize();
auto ret = persistent_map->count(test_element);
/* there is no element test_element */
UT_ASSERT(ret == false);
auto size = static_cast<typename persistent_map_type::difference_type>(
persistent_map->size());
UT_ASSERTeq(
std::distance(persistent_map->begin(), persistent_map->end()),
hash_map_size);
UT_ASSERTeq(size, hash_map_size);
for (int i = 0; i < elements_size; i++) {
UT_ASSERTeq(persistent_map->count(elements[i]), 1);
typename persistent_map_type::accessor accessor;
UT_ASSERTeq(persistent_map->find(accessor, elements[i]), true);
UT_ASSERTeq(accessor->first, elements[i]);
UT_ASSERTeq(accessor->second, elements[i]);
}
}
}
static void
test(int argc, char *argv[])
{
if (argc != 3 || strchr("cbo", argv[1][0]) == nullptr)
UT_FATAL("usage: %s <c|b|o> file-name", argv[0]);
const char *path = argv[2];
nvobj::pool<root> pop;
try {
if (argv[1][0] == 'c') {
pop = nvobj::pool<root>::create(path, LAYOUT,
PMEMOBJ_MIN_POOL * 20,
S_IWUSR | S_IRUSR);
pmem::obj::transaction::run(pop, [&] {
pop.root()->cons = nvobj::make_persistent<
persistent_map_type>();
});
insert(pop);
} else if (argv[1][0] == 'b') {
pop = nvobj::pool<root>::open(path, LAYOUT);
rehash(pop);
} else if (argv[1][0] == 'o') {
pop = nvobj::pool<root>::open(path, LAYOUT);
check_consistency(pop);
}
} catch (pmem::pool_error &pe) {
UT_FATAL("!pool::create: %s %s", pe.what(), path);
}
pop.close();
}
int
main(int argc, char *argv[])
{
return run_test([&] { test(argc, argv); });
}
|