]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/mempool.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / mempool.cc
CommitLineData
7c673cae
FG
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#include "include/mempool.h"
16#include "include/demangle.h"
17
18
19// default to debug_mode off
20bool mempool::debug_mode = false;
21
22// --------------------------------------------------------------
23
24mempool::pool_t& mempool::get_pool(mempool::pool_index_t ix)
25{
26 // We rely on this array being initialized before any invocation of
27 // this function, even if it is called by ctors in other compilation
28 // units that are being initialized before this compilation unit.
29 static mempool::pool_t table[num_pools];
30 return table[ix];
31}
32
33const char *mempool::get_pool_name(mempool::pool_index_t ix) {
34#define P(x) #x,
35 static const char *names[num_pools] = {
36 DEFINE_MEMORY_POOLS_HELPER(P)
37 };
38#undef P
39 return names[ix];
40}
41
42void mempool::dump(ceph::Formatter *f)
43{
44 for (size_t i = 0; i < num_pools; ++i) {
45 const pool_t &pool = mempool::get_pool((pool_index_t)i);
46 f->open_object_section(get_pool_name((pool_index_t)i));
47 pool.dump(f);
48 f->close_section();
49 }
50}
51
52void mempool::set_debug_mode(bool d)
53{
54 debug_mode = d;
55}
56
57// --------------------------------------------------------------
58// pool_t
59
60size_t mempool::pool_t::allocated_bytes() const
61{
62 ssize_t result = 0;
63 for (size_t i = 0; i < num_shards; ++i) {
64 result += shard[i].bytes;
65 }
66 assert(result >= 0);
67 return (size_t) result;
68}
69
70size_t mempool::pool_t::allocated_items() const
71{
72 ssize_t result = 0;
73 for (size_t i = 0; i < num_shards; ++i) {
74 result += shard[i].items;
75 }
76 assert(result >= 0);
77 return (size_t) result;
78}
79
80void mempool::pool_t::get_stats(
81 stats_t *total,
82 std::map<std::string, stats_t> *by_type) const
83{
84 for (size_t i = 0; i < num_shards; ++i) {
85 total->items += shard[i].items;
86 total->bytes += shard[i].bytes;
87 }
88 if (debug_mode) {
89 std::unique_lock<std::mutex> shard_lock(lock);
90 for (auto &p : type_map) {
91 std::string n = ceph_demangle(p.second.type_name);
92 stats_t &s = (*by_type)[n];
93 s.bytes = p.second.items * p.second.item_size;
94 s.items = p.second.items;
95 }
96 }
97}
98
99void mempool::pool_t::dump(ceph::Formatter *f) const
100{
101 stats_t total;
102 std::map<std::string, stats_t> by_type;
103 get_stats(&total, &by_type);
104 f->dump_object("total", total);
105 if (!by_type.empty()) {
106 for (auto &i : by_type) {
107 f->open_object_section(i.first.c_str());
108 i.second.dump(f);
109 f->close_section();
110 }
111 }
112}