]> git.proxmox.com Git - ceph.git/blob - ceph/src/include/mempool.h
update sources to v12.2.1
[ceph.git] / ceph / src / include / mempool.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2016 Allen Samuels <allen.samuels@sandisk.com>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef _CEPH_INCLUDE_MEMPOOL_H
16 #define _CEPH_INCLUDE_MEMPOOL_H
17
18 #include <cstddef>
19 #include <map>
20 #include <unordered_map>
21 #include <set>
22 #include <vector>
23 #include <list>
24 #include <mutex>
25 #include <atomic>
26 #include <typeinfo>
27
28 #include <common/Formatter.h>
29 #include "include/assert.h"
30
31
32 /*
33
34 Memory Pools
35 ============
36
37 A memory pool is a method for accounting the consumption of memory of
38 a set of containers.
39
40 Memory pools are statically declared (see pool_index_t).
41
42 Each memory pool tracks the number of bytes and items it contains.
43
44 Allocators can be declared and associated with a type so that they are
45 tracked independently of the pool total. This additional accounting
46 is optional and only incurs an overhead if the debugging is enabled at
47 runtime. This allows developers to see what types are consuming the
48 pool resources.
49
50
51 Declaring
52 ---------
53
54 Using memory pools is very easy.
55
56 To create a new memory pool, simply add a new name into the list of
57 memory pools that's defined in "DEFINE_MEMORY_POOLS_HELPER". That's
58 it. :)
59
60 For each memory pool that's created a C++ namespace is also
61 automatically created (name is same as in DEFINE_MEMORY_POOLS_HELPER).
62 That namespace contains a set of common STL containers that are predefined
63 with the appropriate allocators.
64
65 Thus for mempool "osd" we have automatically available to us:
66
67 mempool::osd::map
68 mempool::osd::multimap
69 mempool::osd::set
70 mempool::osd::multiset
71 mempool::osd::list
72 mempool::osd::vector
73 mempool::osd::unordered_map
74
75
76 Putting objects in a mempool
77 ----------------------------
78
79 In order to use a memory pool with a particular type, a few additional
80 declarations are needed.
81
82 For a class:
83
84 struct Foo {
85 MEMPOOL_CLASS_HELPERS();
86 ...
87 };
88
89 Then, in an appropriate .cc file,
90
91 MEMPOOL_DEFINE_OBJECT_FACTORY(Foo, foo, osd);
92
93 The second argument can generally be identical to the first, except
94 when the type contains a nested scope. For example, for
95 BlueStore::Onode, we need to do
96
97 MEMPOOL_DEFINE_OBJECT_FACTORY(BlueStore::Onode, bluestore_onode,
98 bluestore_meta);
99
100 (This is just because we need to name some static variables and we
101 can't use :: in a variable name.)
102
103 XXX Note: the new operator hard-codes the allocation size to the size of the
104 object given in MEMPOOL_DEFINE_OBJECT_FACTORY. For this reason, you cannot
105 incorporate mempools into a base class without also defining a helper/factory
106 for the child class as well (as the base class is usually smaller than the
107 child class).
108
109 In order to use the STL containers, simply use the namespaced variant
110 of the container type. For example,
111
112 mempool::osd::map<int> myvec;
113
114 Introspection
115 -------------
116
117 The simplest way to interrogate the process is with
118
119 Formater *f = ...
120 mempool::dump(f);
121
122 This will dump information about *all* memory pools. When debug mode
123 is enabled, the runtime complexity of dump is O(num_shards *
124 num_types). When debug name is disabled it is O(num_shards).
125
126 You can also interrogate a specific pool programmatically with
127
128 size_t bytes = mempool::unittest_2::allocated_bytes();
129 size_t items = mempool::unittest_2::allocated_items();
130
131 The runtime complexity is O(num_shards).
132
133 Note that you cannot easily query per-type, primarily because debug
134 mode is optional and you should not rely on that information being
135 available.
136
137 */
138
139 namespace mempool {
140
141 // --------------------------------------------------------------
142 // define memory pools
143
144 #define DEFINE_MEMORY_POOLS_HELPER(f) \
145 f(bloom_filter) \
146 f(bluestore_alloc) \
147 f(bluestore_cache_data) \
148 f(bluestore_cache_onode) \
149 f(bluestore_cache_other) \
150 f(bluestore_fsck) \
151 f(bluestore_txc) \
152 f(bluestore_writing_deferred) \
153 f(bluestore_writing) \
154 f(bluefs) \
155 f(buffer_anon) \
156 f(buffer_meta) \
157 f(osd) \
158 f(osd_mapbl) \
159 f(osd_pglog) \
160 f(osdmap) \
161 f(osdmap_mapping) \
162 f(pgmap) \
163 f(mds_co) \
164 f(unittest_1) \
165 f(unittest_2)
166
167
168 // give them integer ids
169 #define P(x) mempool_##x,
170 enum pool_index_t {
171 DEFINE_MEMORY_POOLS_HELPER(P)
172 num_pools // Must be last.
173 };
174 #undef P
175
176 extern bool debug_mode;
177 extern void set_debug_mode(bool d);
178
179 // --------------------------------------------------------------
180 class pool_t;
181
182 // we shard pool stats across many shard_t's to reduce the amount
183 // of cacheline ping pong.
184 enum {
185 num_shard_bits = 5
186 };
187 enum {
188 num_shards = 1 << num_shard_bits
189 };
190
191 // align shard to a cacheline
192 struct shard_t {
193 std::atomic<size_t> bytes = {0};
194 std::atomic<size_t> items = {0};
195 char __padding[128 - sizeof(std::atomic<size_t>)*2];
196 } __attribute__ ((aligned (128)));
197
198 static_assert(sizeof(shard_t) == 128, "shard_t should be cacheline-sized");
199
200 struct stats_t {
201 ssize_t items = 0;
202 ssize_t bytes = 0;
203 void dump(ceph::Formatter *f) const {
204 f->dump_int("items", items);
205 f->dump_int("bytes", bytes);
206 }
207
208 stats_t& operator+=(const stats_t& o) {
209 items += o.items;
210 bytes += o.bytes;
211 return *this;
212 }
213 };
214
215 pool_t& get_pool(pool_index_t ix);
216 const char *get_pool_name(pool_index_t ix);
217
218 struct type_t {
219 const char *type_name;
220 size_t item_size;
221 std::atomic<ssize_t> items = {0}; // signed
222 };
223
224 struct type_info_hash {
225 std::size_t operator()(const std::type_info& k) const {
226 return k.hash_code();
227 }
228 };
229
230 class pool_t {
231 shard_t shard[num_shards];
232
233 mutable std::mutex lock; // only used for types list
234 std::unordered_map<const char *, type_t> type_map;
235
236 public:
237 //
238 // How much this pool consumes. O(<num_shards>)
239 //
240 size_t allocated_bytes() const;
241 size_t allocated_items() const;
242
243 void adjust_count(ssize_t items, ssize_t bytes);
244
245 shard_t* pick_a_shard() {
246 // Dirt cheap, see:
247 // http://fossies.org/dox/glibc-2.24/pthread__self_8c_source.html
248 size_t me = (size_t)pthread_self();
249 size_t i = (me >> 3) & ((1 << num_shard_bits) - 1);
250 return &shard[i];
251 }
252
253 type_t *get_type(const std::type_info& ti, size_t size) {
254 std::lock_guard<std::mutex> l(lock);
255 auto p = type_map.find(ti.name());
256 if (p != type_map.end()) {
257 return &p->second;
258 }
259 type_t &t = type_map[ti.name()];
260 t.type_name = ti.name();
261 t.item_size = size;
262 return &t;
263 }
264
265 // get pool stats. by_type is not populated if !debug
266 void get_stats(stats_t *total,
267 std::map<std::string, stats_t> *by_type) const;
268
269 void dump(ceph::Formatter *f, stats_t *ptotal=0) const;
270 };
271
272 void dump(ceph::Formatter *f);
273
274
275 // STL allocator for use with containers. All actual state
276 // is stored in the static pool_allocator_base_t, which saves us from
277 // passing the allocator to container constructors.
278
279 template<pool_index_t pool_ix, typename T>
280 class pool_allocator {
281 pool_t *pool;
282 type_t *type = nullptr;
283
284 public:
285 typedef pool_allocator<pool_ix, T> allocator_type;
286 typedef T value_type;
287 typedef value_type *pointer;
288 typedef const value_type * const_pointer;
289 typedef value_type& reference;
290 typedef const value_type& const_reference;
291 typedef std::size_t size_type;
292 typedef std::ptrdiff_t difference_type;
293
294 template<typename U> struct rebind {
295 typedef pool_allocator<pool_ix,U> other;
296 };
297
298 void init(bool force_register) {
299 pool = &get_pool(pool_ix);
300 if (debug_mode || force_register) {
301 type = pool->get_type(typeid(T), sizeof(T));
302 }
303 }
304
305 pool_allocator(bool force_register=false) {
306 init(force_register);
307 }
308 template<typename U>
309 pool_allocator(const pool_allocator<pool_ix,U>&) {
310 init(false);
311 }
312
313 T* allocate(size_t n, void *p = nullptr) {
314 size_t total = sizeof(T) * n;
315 shard_t *shard = pool->pick_a_shard();
316 shard->bytes += total;
317 shard->items += n;
318 if (type) {
319 type->items += n;
320 }
321 T* r = reinterpret_cast<T*>(new char[total]);
322 return r;
323 }
324
325 void deallocate(T* p, size_t n) {
326 size_t total = sizeof(T) * n;
327 shard_t *shard = pool->pick_a_shard();
328 shard->bytes -= total;
329 shard->items -= n;
330 if (type) {
331 type->items -= n;
332 }
333 delete[] reinterpret_cast<char*>(p);
334 }
335
336 T* allocate_aligned(size_t n, size_t align, void *p = nullptr) {
337 size_t total = sizeof(T) * n;
338 shard_t *shard = pool->pick_a_shard();
339 shard->bytes += total;
340 shard->items += n;
341 if (type) {
342 type->items += n;
343 }
344 char *ptr;
345 int rc = ::posix_memalign((void**)(void*)&ptr, align, total);
346 if (rc)
347 throw std::bad_alloc();
348 T* r = reinterpret_cast<T*>(ptr);
349 return r;
350 }
351
352 void deallocate_aligned(T* p, size_t n) {
353 size_t total = sizeof(T) * n;
354 shard_t *shard = pool->pick_a_shard();
355 shard->bytes -= total;
356 shard->items -= n;
357 if (type) {
358 type->items -= n;
359 }
360 ::free(p);
361 }
362
363 void destroy(T* p) {
364 p->~T();
365 }
366
367 template<class U>
368 void destroy(U *p) {
369 p->~U();
370 }
371
372 void construct(T* p, const T& val) {
373 ::new ((void *)p) T(val);
374 }
375
376 template<class U, class... Args> void construct(U* p,Args&&... args) {
377 ::new((void *)p) U(std::forward<Args>(args)...);
378 }
379
380 bool operator==(const pool_allocator&) const { return true; }
381 bool operator!=(const pool_allocator&) const { return false; }
382 };
383
384
385 // Namespace mempool
386
387 #define P(x) \
388 namespace x { \
389 static const mempool::pool_index_t id = mempool::mempool_##x; \
390 template<typename v> \
391 using pool_allocator = mempool::pool_allocator<id,v>; \
392 \
393 using string = std::basic_string<char,std::char_traits<char>, \
394 pool_allocator<char>>; \
395 \
396 template<typename k,typename v, typename cmp = std::less<k> > \
397 using map = std::map<k, v, cmp, \
398 pool_allocator<std::pair<const k,v>>>; \
399 \
400 template<typename k,typename v, typename cmp = std::less<k> > \
401 using multimap = std::multimap<k,v,cmp, \
402 pool_allocator<std::pair<const k, \
403 v>>>; \
404 \
405 template<typename k, typename cmp = std::less<k> > \
406 using set = std::set<k,cmp,pool_allocator<k>>; \
407 \
408 template<typename v> \
409 using list = std::list<v,pool_allocator<v>>; \
410 \
411 template<typename v> \
412 using vector = std::vector<v,pool_allocator<v>>; \
413 \
414 template<typename k, typename v, \
415 typename h=std::hash<k>, \
416 typename eq = std::equal_to<k>> \
417 using unordered_map = \
418 std::unordered_map<k,v,h,eq,pool_allocator<std::pair<const k,v>>>;\
419 \
420 inline size_t allocated_bytes() { \
421 return mempool::get_pool(id).allocated_bytes(); \
422 } \
423 inline size_t allocated_items() { \
424 return mempool::get_pool(id).allocated_items(); \
425 } \
426 };
427
428 DEFINE_MEMORY_POOLS_HELPER(P)
429
430 #undef P
431
432 };
433
434
435
436 // Use this for any type that is contained by a container (unless it
437 // is a class you defined; see below).
438 #define MEMPOOL_DECLARE_FACTORY(obj, factoryname, pool) \
439 namespace mempool { \
440 namespace pool { \
441 extern pool_allocator<obj> alloc_##factoryname; \
442 } \
443 }
444
445 #define MEMPOOL_DEFINE_FACTORY(obj, factoryname, pool) \
446 namespace mempool { \
447 namespace pool { \
448 pool_allocator<obj> alloc_##factoryname = {true}; \
449 } \
450 }
451
452 // Use this for each class that belongs to a mempool. For example,
453 //
454 // class T {
455 // MEMPOOL_CLASS_HELPERS();
456 // ...
457 // };
458 //
459 #define MEMPOOL_CLASS_HELPERS() \
460 void *operator new(size_t size); \
461 void *operator new[](size_t size) noexcept { \
462 assert(0 == "no array new"); \
463 return nullptr; } \
464 void operator delete(void *); \
465 void operator delete[](void *) { assert(0 == "no array delete"); }
466
467
468 // Use this in some particular .cc file to match each class with a
469 // MEMPOOL_CLASS_HELPERS().
470 #define MEMPOOL_DEFINE_OBJECT_FACTORY(obj,factoryname,pool) \
471 MEMPOOL_DEFINE_FACTORY(obj, factoryname, pool) \
472 void *obj::operator new(size_t size) { \
473 return mempool::pool::alloc_##factoryname.allocate(1); \
474 } \
475 void obj::operator delete(void *p) { \
476 return mempool::pool::alloc_##factoryname.deallocate((obj*)p, 1); \
477 }
478
479 #endif