]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | // Copyright (c) 2019 Intel Corporation | |
3 | // This source code is licensed under both the GPLv2 (found in the | |
4 | // COPYING file in the root directory) and Apache 2.0 License | |
5 | // (found in the LICENSE.Apache file in the root directory). | |
6 | ||
7 | #include <cstdio> | |
8 | ||
9 | #ifdef MEMKIND | |
10 | #include "memkind_kmem_allocator.h" | |
11 | #include "rocksdb/cache.h" | |
12 | #include "rocksdb/db.h" | |
13 | #include "rocksdb/options.h" | |
14 | #include "table/block_based/block_based_table_factory.h" | |
15 | #include "test_util/testharness.h" | |
16 | ||
17 | namespace rocksdb { | |
18 | TEST(MemkindKmemAllocatorTest, Allocate) { | |
19 | MemkindKmemAllocator allocator; | |
20 | void* p; | |
21 | try { | |
22 | p = allocator.Allocate(1024); | |
23 | } catch (const std::bad_alloc& e) { | |
24 | return; | |
25 | } | |
26 | ASSERT_NE(p, nullptr); | |
27 | size_t size = allocator.UsableSize(p, 1024); | |
28 | ASSERT_GE(size, 1024); | |
29 | allocator.Deallocate(p); | |
30 | } | |
31 | ||
32 | TEST(MemkindKmemAllocatorTest, DatabaseBlockCache) { | |
33 | // Check if a memory node is available for allocation | |
34 | try { | |
35 | MemkindKmemAllocator allocator; | |
36 | allocator.Allocate(1024); | |
37 | } catch (const std::bad_alloc& e) { | |
38 | return; // if no node available, skip the test | |
39 | } | |
40 | ||
41 | // Create database with block cache using MemkindKmemAllocator | |
42 | Options options; | |
43 | std::string dbname = test::PerThreadDBPath("memkind_kmem_allocator_test"); | |
44 | ASSERT_OK(DestroyDB(dbname, options)); | |
45 | ||
46 | options.create_if_missing = true; | |
47 | std::shared_ptr<Cache> cache = NewLRUCache( | |
48 | 1024 * 1024, 6, false, false, std::make_shared<MemkindKmemAllocator>()); | |
49 | BlockBasedTableOptions table_options; | |
50 | table_options.block_cache = cache; | |
51 | options.table_factory.reset(NewBlockBasedTableFactory(table_options)); | |
52 | ||
53 | DB* db = nullptr; | |
54 | Status s = DB::Open(options, dbname, &db); | |
55 | ASSERT_OK(s); | |
56 | ASSERT_NE(db, nullptr); | |
57 | ASSERT_EQ(cache->GetUsage(), 0); | |
58 | ||
59 | // Write 2kB (200 values, each 10 bytes) | |
60 | int num_keys = 200; | |
61 | WriteOptions wo; | |
62 | std::string val = "0123456789"; | |
63 | for (int i = 0; i < num_keys; i++) { | |
64 | std::string key = std::to_string(i); | |
65 | s = db->Put(wo, Slice(key), Slice(val)); | |
66 | ASSERT_OK(s); | |
67 | } | |
68 | ASSERT_OK(db->Flush(FlushOptions())); // Flush all data from memtable so that | |
69 | // reads are from block cache | |
70 | ||
71 | // Read and check block cache usage | |
72 | ReadOptions ro; | |
73 | std::string result; | |
74 | for (int i = 0; i < num_keys; i++) { | |
75 | std::string key = std::to_string(i); | |
76 | s = db->Get(ro, key, &result); | |
77 | ASSERT_OK(s); | |
78 | ASSERT_EQ(result, val); | |
79 | } | |
80 | ASSERT_GT(cache->GetUsage(), 2000); | |
81 | ||
82 | // Close database | |
83 | s = db->Close(); | |
84 | ASSERT_OK(s); | |
85 | ASSERT_OK(DestroyDB(dbname, options)); | |
86 | } | |
87 | } // namespace rocksdb | |
88 | ||
89 | int main(int argc, char** argv) { | |
90 | ::testing::InitGoogleTest(&argc, argv); | |
91 | return RUN_ALL_TESTS(); | |
92 | } | |
93 | ||
94 | #else | |
95 | ||
96 | int main(int /*argc*/, char** /*argv*/) { | |
97 | printf( | |
98 | "Skip memkind_kmem_allocator_test as the required library memkind is " | |
99 | "missing."); | |
100 | } | |
101 | ||
102 | #endif // MEMKIND |