OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PMemAllocator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <filesystem>
18 #include "memkind.h"
19 
20 #include "PMemAllocator.h"
21 
22 extern std::string g_pmem_path;
23 extern size_t g_pmem_size;
24 
25 PMemArena::PMemArena(size_t min_block_size, size_t size_limit)
26  : size_limit_(size_limit), size_(0) {
27  std::error_code ec;
28  auto pmem_space_info = std::filesystem::space(g_pmem_path.c_str(), ec);
29  CHECK(!ec) << "Failed to get pmem space info for path: " << g_pmem_path
30  << "error code: " << ec << "\n";
31  size_t capacity = pmem_space_info.capacity;
32  CHECK_EQ(memkind_check_dax_path(g_pmem_path.c_str()), 0)
33  << g_pmem_path << " is not recognized as a direct-access pmem path.";
34  CHECK_GE(capacity, g_pmem_size)
35  << g_pmem_path << " is not large enough for the requested PMem space";
36  CHECK_EQ(memkind_create_pmem(g_pmem_path.c_str(), capacity, &pmem_kind_), 0)
37  << "Failed to create PMEM memory.";
38  LOG(INFO) << "Created Pmem direct-access allocator at " << g_pmem_path
39  << " with capacity " << capacity << "\n";
40 }
41 
43  for (auto [ptr, size] : allocations_) {
44  memkind_free(pmem_kind_, ptr);
45  }
46 }
47 
48 void* PMemArena::allocate(const size_t num_bytes) {
49  if (size_limit_ != 0 && size_ + num_bytes > size_limit_) {
50  throw OutOfHostMemory(num_bytes);
51  }
52  auto ret = memkind_malloc(pmem_kind_, num_bytes);
53  size_ += num_bytes;
54  allocations_.push_back({ret, num_bytes});
55  return ret;
56 }
57 
58 void* PMemArena::allocateAndZero(const size_t num_bytes) {
59  auto ret = allocate(num_bytes);
60  std::memset(ret, 0, num_bytes);
61  return ret;
62 }
63 
64 size_t PMemArena::bytesUsed() const {
65  return size_;
66 }
PMemArena(size_t min_block_size=1ULL<< 32, size_t size_limit=0)
#define CHECK_EQ(x, y)
Definition: Logger.h:217
size_t g_pmem_size
void * allocate(const size_t num_bytes) override
size_t size_limit_
Definition: PMemAllocator.h:39
#define LOG(tag)
Definition: Logger.h:203
~PMemArena() override
#define CHECK_GE(x, y)
Definition: Logger.h:222
size_t bytesUsed() const override
std::vector< std::pair< void *, size_t > > allocations_
Definition: PMemAllocator.h:41
std::string g_pmem_path
size_t size_
Definition: PMemAllocator.h:40
struct memkind * pmem_kind_
Definition: PMemAllocator.h:42
void * allocateAndZero(const size_t num_bytes) override
#define CHECK(condition)
Definition: Logger.h:209