Skip to content

Commit

Permalink
mempool: use function static for pool table
Browse files Browse the repository at this point in the history
The compiler/linker guarantee this is initialized before any invocation
of this function... even if it is by a ctor in another compilation unit
that is initialized by the mempool.cc compilation unit.

Suggested by Bartłomiej Święcki <[email protected]>

Signed-off-by: Allen Samuels <[email protected]>
  • Loading branch information
liewegas committed Nov 2, 2016
1 parent ebf0527 commit 9cecff8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
32 changes: 13 additions & 19 deletions src/global/mempool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,29 @@ bool mempool::debug_mode = false;

// --------------------------------------------------------------

// We rely on this array of pointers being zeroed when the process
// starts *before* the ctors from *any* linked modules are executed.
// That way, regardless of link order, pool_t's are allocated and
// instantiated on demand.
static mempool::pool_t *pools[mempool::num_pools];

mempool::pool_t& mempool::get_pool(mempool::pool_index_t ix)
{
if (pools[ix]) {
return *pools[ix];
}
// We rely on this array being initialized before any invocation of
// this function, even if it is called by ctors in other compilation
// units that are being initialized before this compilation unit.
static mempool::pool_t table[num_pools];
return table[ix];
}

switch (ix) {
#define P(x) \
case mempool_##x: pools[ix] = new mempool::pool_t(#x); break;
DEFINE_MEMORY_POOLS_HELPER(P);
const char *mempool::get_pool_name(mempool::pool_index_t ix) {
#define P(x) #x,
static const char *names[num_pools] = {
DEFINE_MEMORY_POOLS_HELPER(P)
};
#undef P
default: assert(0);
}
return *pools[ix];
return names[ix];
}

void mempool::dump(ceph::Formatter *f, size_t skip)
{
for (size_t i = skip; i < num_pools; ++i) {
if (!pools[i])
continue;
const pool_t &pool = mempool::get_pool((pool_index_t)i);
f->open_object_section(pool.get_name().c_str());
f->open_object_section(get_pool_name((pool_index_t)i));
pool.dump(f);
f->close_section();
}
Expand Down
10 changes: 1 addition & 9 deletions src/include/mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ struct stats_t {
};

pool_t& get_pool(pool_index_t ix);
const char *get_pool_name(pool_index_t ix);

struct type_t {
const char *type_name;
Expand All @@ -196,27 +197,18 @@ struct type_info_hash {
};

class pool_t {
std::string name;
shard_t shard[num_shards];

mutable std::mutex lock; // only used for types list
std::unordered_map<const char *, type_t> type_map;

public:
pool_t(const std::string& n)
: name(n) {
}

//
// How much this pool consumes. O(<num_shards>)
//
size_t allocated_bytes() const;
size_t allocated_items() const;

const std::string& get_name() const {
return name;
}

shard_t* pick_a_shard() {
// Dirt cheap, see:
// http://fossies.org/dox/glibc-2.24/pthread__self_8c_source.html
Expand Down

0 comments on commit 9cecff8

Please sign in to comment.