1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph distributed storage system
6 * Copyright (C) 2016 Western Digital Corporation
8 * Author: Allen Samuels <allen.samuels@sandisk.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
19 #include "global/global_init.h"
20 #include "common/ceph_argparse.h"
21 #include "global/global_context.h"
22 #include "gtest/gtest.h"
23 #include "include/mempool.h"
25 void check_usage(mempool::pool_index_t ix)
27 mempool::pool_t *pool = &mempool::get_pool(ix);
28 mempool::stats_t total;
29 map<std::string,mempool::stats_t> m;
30 pool->get_stats(&total, &m);
31 size_t usage = pool->allocated_bytes();
34 sum += p.second.bytes;
37 ceph::TableFormatter jf;
41 EXPECT_EQ(sum, usage);
44 template<typename A, typename B>
45 void eq_elements(const A& a, const B& b)
49 while (lhs != a.end()) {
54 EXPECT_EQ(rhs,b.end());
57 template<typename A, typename B>
58 void eq_pairs(const A& a, const B& b)
62 while (lhs != a.end()) {
63 EXPECT_EQ(lhs->first,rhs->first);
64 EXPECT_EQ(lhs->second,rhs->second);
68 EXPECT_EQ(rhs,b.end());
71 #define MAKE_INSERTER(inserter) \
72 template<typename A,typename B> \
73 void do_##inserter(A& a, B& b, int count, int base) { \
74 for (int i = 0; i < count; ++i) { \
75 a.inserter(base + i); \
76 b.inserter(base + i); \
80 MAKE_INSERTER(push_back);
81 MAKE_INSERTER(insert);
83 template<typename A,typename B>
84 void do_insert_key(A& a, B& b, int count, int base)
86 for (int i = 0; i < count; ++i) {
87 a.insert(make_pair(base+i,base+i));
88 b.insert(make_pair(base+i,base+i));
89 check_usage(mempool::osd::id);
93 TEST(mempool, vector_context)
95 check_usage(mempool::osd::id);
96 EXPECT_EQ(mempool::osd::allocated_bytes(), 0u);
97 EXPECT_EQ(mempool::osd::allocated_items(), 0u);
98 for (unsigned i = 0; i < 10; ++i) {
100 mempool::osd::vector<int> b,c;
102 do_push_back(a,b,i,i);
104 check_usage(mempool::osd::id);
106 mempool::stats_t total;
107 map<std::string,mempool::stats_t> by_type;
108 mempool::get_pool(mempool::osd::id).get_stats(&total, &by_type);
109 EXPECT_GE(mempool::osd::allocated_bytes(), i * 4u);
110 EXPECT_GE(mempool::osd::allocated_items(), i);
114 check_usage(mempool::osd::id);
121 TEST(mempool, list_context)
123 for (unsigned i = 1; i < 10; ++i) {
125 mempool::osd::list<int> b,c;
127 do_push_back(a,b,i,i);
137 do_push_back(a,b,i,i);
138 c.splice(c.begin(),b,b.begin(),b.end());
140 mempool::stats_t total;
141 map<std::string,mempool::stats_t> by_type;
142 mempool::get_pool(mempool::osd::id).get_stats(&total, &by_type);
143 EXPECT_GE(mempool::osd::allocated_bytes(), i * 4u);
144 EXPECT_EQ(mempool::osd::allocated_items(), i);
147 check_usage(mempool::osd::id);
151 TEST(mempool, set_context)
153 for (int i = 0; i < 10; ++i) {
155 mempool::osd::set<int> b;
158 check_usage(mempool::osd::id);
161 for (int i = 1; i < 10; ++i) {
163 mempool::osd::set<int> b;
165 EXPECT_NE(a.find(i/2),a.end());
166 EXPECT_NE(b.find(i/2),b.end());
167 a.erase(a.find(i/2));
168 b.erase(b.find(i/2));
170 check_usage(mempool::osd::id);
175 MEMPOOL_CLASS_HELPERS();
178 obj() : a(1), b(1) {}
179 obj(int _a) : a(_a), b(2) {}
180 obj(int _a,int _b) : a(_a), b(_b) {}
181 friend inline bool operator<(const obj& l, const obj& r) {
185 MEMPOOL_DEFINE_OBJECT_FACTORY(obj, obj, osdmap);
187 TEST(mempool, test_factory)
190 obj *o2 = new obj(10);
191 obj *o3 = new obj(20,30);
192 check_usage(mempool::osdmap::id);
193 EXPECT_NE(o1,nullptr);
204 check_usage(mempool::osdmap::id);
207 TEST(mempool, vector)
210 mempool::osd::vector<int> v;
215 mempool::osdmap::vector<obj> v;
223 mempool::osd::set<int> set_int;
226 mempool::osdmap::set<obj> set_obj;
227 set_obj.insert(obj());
228 set_obj.insert(obj(1));
229 set_obj.insert(obj(1, 2));
235 mempool::osd::map<int,int> v;
240 mempool::osdmap::map<int,obj> v;
250 mempool::osd::list<int> v;
255 mempool::osdmap::list<obj> v;
261 TEST(mempool, unordered_map)
263 mempool::osdmap::unordered_map<int,obj> h;
268 TEST(mempool, string_test)
270 mempool::osdmap::string s;
272 EXPECT_GE(mempool::osdmap::allocated_items(), s.capacity() + 1u); // +1 for zero-byte termination :
273 for (size_t i = 0; i < 10; ++i) {
276 EXPECT_GE(mempool::osdmap::allocated_items(), s.capacity() + 1u);
280 TEST(mempool, bufferlist)
284 size_t before = mempool::buffer_anon::allocated_bytes();
285 cout << "before " << before << std::endl;
286 bl.append(buffer::create_aligned(len, 4096));
287 size_t after = mempool::buffer_anon::allocated_bytes();
288 cout << "after " << after << std::endl;
289 ASSERT_GE(after, before + len);
292 TEST(mempool, bufferlist_reassign)
295 size_t items_before = mempool::buffer_anon::allocated_items();
296 size_t bytes_before = mempool::buffer_anon::allocated_bytes();
298 ASSERT_EQ(items_before + 1, mempool::buffer_anon::allocated_items());
299 ASSERT_LT(bytes_before, mempool::buffer_anon::allocated_bytes());
302 bl.reassign_to_mempool(mempool::mempool_osd);
303 ASSERT_EQ(items_before, mempool::buffer_anon::allocated_items());
304 ASSERT_EQ(bytes_before, mempool::buffer_anon::allocated_bytes());
306 // additional appends should go to the same pool
307 items_before = mempool::osd::allocated_items();
308 bytes_before = mempool::osd::allocated_bytes();
309 cout << "anon b " << mempool::buffer_anon::allocated_bytes() << std::endl;
310 for (unsigned i = 0; i < 1000; ++i) {
311 bl.append("asdfddddddddddddddddddddddasfdasdfasdfasdfasdfasdf");
313 cout << "anon a " << mempool::buffer_anon::allocated_bytes() << std::endl;
314 ASSERT_LT(items_before, mempool::osd::allocated_items());
315 ASSERT_LT(bytes_before, mempool::osd::allocated_bytes());
318 items_before = mempool::osd::allocated_items();
319 bytes_before = mempool::osd::allocated_bytes();
320 bl.try_assign_to_mempool(mempool::mempool_bloom_filter);
321 ASSERT_EQ(items_before, mempool::osd::allocated_items());
322 ASSERT_EQ(bytes_before, mempool::osd::allocated_bytes());
325 int main(int argc, char **argv)
327 vector<const char*> args;
328 argv_to_vec(argc, (const char **)argv, args);
330 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
331 CODE_ENVIRONMENT_UTILITY, 0);
332 common_init_finish(g_ceph_context);
334 // enable debug mode for the tests
335 mempool::set_debug_mode(true);
337 ::testing::InitGoogleTest(&argc, argv);
338 return RUN_ALL_TESTS();
344 * compile-command: "cd ../../build ; make -j4 &&
345 * make unittest_mempool &&
346 * valgrind --tool=memcheck ./unittest_mempool --gtest_filter=*.*"