-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide an allocator for new memory type to be used with RocksDB block cache #6214
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. | ||
// Copyright (c) 2019 Intel Corporation | ||
// This source code is licensed under both the GPLv2 (found in the | ||
// COPYING file in the root directory) and Apache 2.0 License | ||
// (found in the LICENSE.Apache file in the root directory). | ||
|
||
#ifdef MEMKIND | ||
|
||
#include "memkind_kmem_allocator.h" | ||
|
||
namespace rocksdb { | ||
|
||
void* MemkindKmemAllocator::Allocate(size_t size) { | ||
void* p = memkind_malloc(MEMKIND_DAX_KMEM, size); | ||
if (p == NULL) { | ||
throw std::bad_alloc(); | ||
} | ||
return p; | ||
} | ||
|
||
void MemkindKmemAllocator::Deallocate(void* p) { | ||
memkind_free(MEMKIND_DAX_KMEM, p); | ||
} | ||
|
||
#ifdef ROCKSDB_MALLOC_USABLE_SIZE | ||
size_t MemkindKmemAllocator::UsableSize(void* p, size_t /*allocation_size*/) const { | ||
return memkind_malloc_usable_size(MEMKIND_DAX_KMEM, p); | ||
} | ||
#endif // ROCKSDB_MALLOC_USABLE_SIZE | ||
|
||
} // namespace rocksdb | ||
#endif // MEMKIND |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. | ||
// Copyright (c) 2019 Intel Corporation | ||
// This source code is licensed under both the GPLv2 (found in the | ||
// COPYING file in the root directory) and Apache 2.0 License | ||
// (found in the LICENSE.Apache file in the root directory). | ||
|
||
#pragma once | ||
|
||
#ifdef MEMKIND | ||
|
||
#include <memkind.h> | ||
#include "rocksdb/memory_allocator.h" | ||
|
||
namespace rocksdb { | ||
|
||
class MemkindKmemAllocator : public MemoryAllocator { | ||
public: | ||
const char* Name() const override { return "MemkindKmemAllocator"; }; | ||
void* Allocate(size_t size) override; | ||
void Deallocate(void* p) override; | ||
#ifdef ROCKSDB_MALLOC_USABLE_SIZE | ||
size_t UsableSize(void* p, size_t /*allocation_size*/) const override; | ||
#endif | ||
}; | ||
|
||
} // namespace rocksdb | ||
#endif // MEMKIND | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. | ||
// Copyright (c) 2019 Intel Corporation | ||
// This source code is licensed under both the GPLv2 (found in the | ||
// COPYING file in the root directory) and Apache 2.0 License | ||
// (found in the LICENSE.Apache file in the root directory). | ||
|
||
#include <cstdio> | ||
|
||
#ifdef MEMKIND | ||
#include "memkind_kmem_allocator.h" | ||
#include "test_util/testharness.h" | ||
#include "rocksdb/cache.h" | ||
#include "rocksdb/db.h" | ||
#include "rocksdb/options.h" | ||
#include "table/block_based/block_based_table_factory.h" | ||
|
||
namespace rocksdb { | ||
TEST(MemkindKmemAllocatorTest, Allocate) { | ||
MemkindKmemAllocator allocator; | ||
void* p; | ||
try { | ||
p = allocator.Allocate(1024); | ||
} catch (const std::bad_alloc& e) { | ||
return; | ||
} | ||
ASSERT_NE(p, nullptr); | ||
size_t size = allocator.UsableSize(p, 1024); | ||
ASSERT_GE(size, 1024); | ||
allocator.Deallocate(p); | ||
} | ||
|
||
TEST(MemkindKmemAllocatorTest, DatabaseBlockCache) { | ||
// Check if a memory node is available for allocation | ||
try { | ||
MemkindKmemAllocator allocator; | ||
allocator.Allocate(1024); | ||
} catch (const std::bad_alloc& e) { | ||
return; // if no node available, skip the test | ||
} | ||
|
||
// Create database with block cache using MemkindKmemAllocator | ||
Options options; | ||
std::string dbname = test::PerThreadDBPath("memkind_kmem_allocator_test"); | ||
ASSERT_OK(DestroyDB(dbname, options)); | ||
|
||
options.create_if_missing = true; | ||
std::shared_ptr<Cache> cache = NewLRUCache(1024 * 1024, 6, false, false, | ||
std::make_shared<MemkindKmemAllocator>()); | ||
BlockBasedTableOptions table_options; | ||
table_options.block_cache = cache; | ||
options.table_factory.reset(NewBlockBasedTableFactory(table_options)); | ||
|
||
DB* db = nullptr; | ||
Status s = DB::Open(options, dbname, &db); | ||
ASSERT_OK(s); | ||
ASSERT_NE(db, nullptr); | ||
ASSERT_EQ(cache->GetUsage(), 0); | ||
|
||
// Write 2kB (200 values, each 10 bytes) | ||
int num_keys = 200; | ||
WriteOptions wo; | ||
std::string val = "0123456789"; | ||
for (int i = 0; i < num_keys; i++) { | ||
std::string key = std::to_string(i); | ||
s = db->Put(wo, Slice(key), Slice(val)); | ||
ASSERT_OK(s); | ||
} | ||
ASSERT_OK(db->Flush(FlushOptions())); // Flush all data from memtable so that reads are from block cache | ||
|
||
// Read and check block cache usage | ||
ReadOptions ro; | ||
std::string result; | ||
for (int i = 0; i < num_keys; i++) { | ||
std::string key = std::to_string(i); | ||
s = db->Get(ro, key, &result); | ||
ASSERT_OK(s); | ||
ASSERT_EQ(result, val); | ||
} | ||
ASSERT_GT(cache->GetUsage(), 2000); | ||
|
||
// Close database | ||
s = db->Close(); | ||
ASSERT_OK(s); | ||
ASSERT_OK(DestroyDB(dbname, options)); | ||
} | ||
} // namespace rocksdb | ||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} | ||
|
||
#else | ||
|
||
int main(int /*argc*/, char** /*argv*/) { | ||
printf("Skip memkind_kmem_allocator_test as the required library memkind is missing."); | ||
} | ||
|
||
#endif // MEMKIND |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be interesting to add a test of full DB with block cache to allocate from this allocator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a full DB test using the new allocator. The database is populated and a flush is triggered, so the block cache is used for subsequent reads.