]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mempool: use function static for pool table 11331/head
authorSage Weil <sage@redhat.com>
Thu, 20 Oct 2016 15:10:08 +0000 (10:10 -0500)
committerSage Weil <sage@redhat.com>
Wed, 2 Nov 2016 17:48:49 +0000 (13:48 -0400)
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 <bartlomiej.swiecki@corp.ovh.com>

Signed-off-by: Allen Samuels <allen.samuels@sandisk.com>
src/global/mempool.cc
src/include/mempool.h

index f8f9927111f034e6f199cf3635ba2c62e0a54d4e..33ea7beb4d8e798474c8470a4cee41b1399410c4 100644 (file)
@@ -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();
   }
index d801801bb5af299e1ba9ed6af9dfe73f8e8b9cd9..da017bb9143f70aecb08358664ddaecda259612f 100644 (file)
@@ -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;
@@ -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