1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 Allen Samuels <allen.samuels@sandisk.com>
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.
15 #include "include/mempool.h"
16 #include "include/demangle.h"
19 // default to debug_mode off
20 bool mempool::debug_mode = false;
22 // --------------------------------------------------------------
24 mempool::pool_t& mempool::get_pool(mempool::pool_index_t ix)
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];
33 const char *mempool::get_pool_name(mempool::pool_index_t ix) {
35 static const char *names[num_pools] = {
36 DEFINE_MEMORY_POOLS_HELPER(P)
42 void mempool::dump(ceph::Formatter *f)
45 for (size_t i = 0; i < num_pools; ++i) {
46 const pool_t &pool = mempool::get_pool((pool_index_t)i);
47 f->open_object_section(get_pool_name((pool_index_t)i));
51 f->dump_object("total", total);
54 void mempool::set_debug_mode(bool d)
59 // --------------------------------------------------------------
62 size_t mempool::pool_t::allocated_bytes() const
65 for (size_t i = 0; i < num_shards; ++i) {
66 result += shard[i].bytes;
69 return (size_t) result;
72 size_t mempool::pool_t::allocated_items() const
75 for (size_t i = 0; i < num_shards; ++i) {
76 result += shard[i].items;
79 return (size_t) result;
82 void mempool::pool_t::adjust_count(ssize_t items, ssize_t bytes)
84 shard_t *shard = pick_a_shard();
85 shard->items += items;
86 shard->bytes += bytes;
89 void mempool::pool_t::get_stats(
91 std::map<std::string, stats_t> *by_type) const
93 for (size_t i = 0; i < num_shards; ++i) {
94 total->items += shard[i].items;
95 total->bytes += shard[i].bytes;
98 std::unique_lock<std::mutex> shard_lock(lock);
99 for (auto &p : type_map) {
100 std::string n = ceph_demangle(p.second.type_name);
101 stats_t &s = (*by_type)[n];
102 s.bytes = p.second.items * p.second.item_size;
103 s.items = p.second.items;
108 void mempool::pool_t::dump(ceph::Formatter *f, stats_t *ptotal) const
111 std::map<std::string, stats_t> by_type;
112 get_stats(&total, &by_type);
117 if (!by_type.empty()) {
118 f->open_object_section("by_type");
119 for (auto &i : by_type) {
120 f->open_object_section(i.first.c_str());