887953
From c8e58e3a577e70a64df77fe885847285f682d9fb Mon Sep 17 00:00:00 2001
887953
From: Niels de Vos <ndevos@redhat.com>
887953
Date: Tue, 29 Aug 2017 00:16:22 +0200
887953
Subject: [PATCH 492/493] mem-pool: track glusterfs_ctx_t in struct mem_pool
887953
887953
In order to generate statedumps per glusterfs_ctx_t, it is needed to
887953
place all the memory pools in a structure that the context can reach.
887953
The 'struct mem_pool' has been extended with a 'list_head owner' that is
887953
linked with the glusterfs_ctx_t->mempool_list.
887953
887953
All callers of mem_pool_new() have been updated to pass the current
887953
glusterfs_ctx_t along. This context is needed to add the new memory pool
887953
to the list and for grabbing the ctx->lock while updating the
887953
glusterfs_ctx_t->mempool_list.
887953
887953
> Updates: #307
887953
> Change-Id: Ia9384424d8d1630ef3efc9d5d523bf739c356c6e
887953
> Signed-off-by: Niels de Vos <ndevos@redhat.com>
887953
> Reviewed-on: https://review.gluster.org/18075
887953
> Smoke: Gluster Build System <jenkins@build.gluster.org>
887953
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
887953
> Reviewed-by: Jeff Darcy <jeff@pl.atyp.us>
887953
887953
Change-Id: Id3b193f366f7c46f91b77bced8729a4eb538837b
887953
BUG: 1648893
887953
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
887953
Reviewed-on: https://code.engineering.redhat.com/gerrit/158937
887953
Tested-by: RHGS Build Bot <nigelb@redhat.com>
887953
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
887953
---
887953
 libglusterfs/src/ctx.c                      |  2 --
887953
 libglusterfs/src/glusterfs.h                |  2 --
887953
 libglusterfs/src/mem-pool.c                 | 18 +++++++++++++++++-
887953
 libglusterfs/src/mem-pool.h                 | 16 +++++++++++++---
887953
 libglusterfs/src/rbthash.c                  |  5 +++--
887953
 libglusterfs/src/rbthash.h                  |  2 +-
887953
 libglusterfs/src/statedump.c                | 16 +++++++++++++++-
887953
 xlators/cluster/ec/src/ec-method.c          |  2 +-
887953
 xlators/nfs/server/src/nfs3.c               |  3 ++-
887953
 xlators/performance/io-cache/src/io-cache.c |  2 +-
887953
 10 files changed, 53 insertions(+), 15 deletions(-)
887953
887953
diff --git a/libglusterfs/src/ctx.c b/libglusterfs/src/ctx.c
887953
index 94c56ac..90480d0 100644
887953
--- a/libglusterfs/src/ctx.c
887953
+++ b/libglusterfs/src/ctx.c
887953
@@ -31,9 +31,7 @@ glusterfs_ctx_new ()
887953
         ctx->mem_acct_enable = gf_global_mem_acct_enable_get();
887953
 
887953
         INIT_LIST_HEAD (&ctx->graphs);
887953
-#if defined(OLD_MEM_POOLS)
887953
         INIT_LIST_HEAD (&ctx->mempool_list);
887953
-#endif
887953
         INIT_LIST_HEAD (&ctx->volfile_list);
887953
 
887953
 	ctx->daemon_pipe[0] = -1;
887953
diff --git a/libglusterfs/src/glusterfs.h b/libglusterfs/src/glusterfs.h
887953
index c12e94e..157437c 100644
887953
--- a/libglusterfs/src/glusterfs.h
887953
+++ b/libglusterfs/src/glusterfs.h
887953
@@ -526,11 +526,9 @@ struct _glusterfs_ctx {
887953
         int                process_mode; /*mode in which process is runninng*/
887953
         struct syncenv    *env;          /* The env pointer to the synctasks */
887953
 
887953
-#if defined(OLD_MEM_POOLS)
887953
         struct list_head   mempool_list; /* used to keep a global list of
887953
                                             mempools, used to log details of
887953
                                             mempool in statedump */
887953
-#endif
887953
         char              *statedump_path;
887953
 
887953
         struct mem_pool   *dict_pool;
887953
diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c
887953
index a8a9347..999a83f 100644
887953
--- a/libglusterfs/src/mem-pool.c
887953
+++ b/libglusterfs/src/mem-pool.c
887953
@@ -676,7 +676,7 @@ void mem_pools_fini (void) {}
887953
 #endif
887953
 
887953
 struct mem_pool *
887953
-mem_pool_new_fn (unsigned long sizeof_type,
887953
+mem_pool_new_fn (glusterfs_ctx_t *ctx, unsigned long sizeof_type,
887953
                  unsigned long count, char *name)
887953
 {
887953
         unsigned int            i;
887953
@@ -706,10 +706,18 @@ mem_pool_new_fn (unsigned long sizeof_type,
887953
         if (!new)
887953
                 return NULL;
887953
 
887953
+        new->ctx = ctx;
887953
         new->sizeof_type = sizeof_type;
887953
         new->count = count;
887953
         new->name = name;
887953
         new->pool = pool;
887953
+        INIT_LIST_HEAD (&new->owner);
887953
+
887953
+        LOCK (&ctx->lock);
887953
+        {
887953
+                list_add (&new->owner, &ctx->mempool_list);
887953
+        }
887953
+        UNLOCK (&ctx->lock);
887953
 
887953
         return new;
887953
 }
887953
@@ -905,6 +913,14 @@ mem_put (void *ptr)
887953
 void
887953
 mem_pool_destroy (struct mem_pool *pool)
887953
 {
887953
+        /* remove this pool from the owner (glusterfs_ctx_t) */
887953
+        LOCK (&pool->ctx->lock);
887953
+        {
887953
+                list_del (&pool->owner);
887953
+        }
887953
+        UNLOCK (&pool->ctx->lock);
887953
+
887953
+        /* free this pool, but keep the mem_pool_shared */
887953
         GF_FREE (pool);
887953
 
887953
         /*
887953
diff --git a/libglusterfs/src/mem-pool.h b/libglusterfs/src/mem-pool.h
887953
index 057d957..0ebb63b 100644
887953
--- a/libglusterfs/src/mem-pool.h
887953
+++ b/libglusterfs/src/mem-pool.h
887953
@@ -16,6 +16,7 @@
887953
 #include "atomic.h"
887953
 #include "logging.h"
887953
 #include "mem-types.h"
887953
+#include "glusterfs.h" /* for glusterfs_ctx_t */
887953
 #include <stdlib.h>
887953
 #include <inttypes.h>
887953
 #include <string.h>
887953
@@ -207,11 +208,15 @@ out:
887953
 /* kind of 'header' for the actual mem_pool_shared structure, this might make
887953
  * it possible to dump some more details in a statedump */
887953
 struct mem_pool {
887953
+        /* object size, without pooled_obj_hdr_t */
887953
         unsigned long           sizeof_type;
887953
         unsigned long           count;
887953
         char                    *name;
887953
 
887953
-        struct mem_pool_shared  *pool;
887953
+        struct list_head        owner;  /* glusterfs_ctx_t->mempool_list */
887953
+        glusterfs_ctx_t         *ctx;   /* take ctx->lock when updating owner */
887953
+
887953
+        struct mem_pool_shared  *pool;  /* the initial pool that was returned */
887953
 };
887953
 
887953
 typedef struct pooled_obj_hdr {
887953
@@ -276,9 +281,14 @@ void mem_pools_init_late (void);   /* start the pool_sweeper thread */
887953
 void mem_pools_fini (void);        /* cleanup memory pools */
887953
 
887953
 struct mem_pool *
887953
-mem_pool_new_fn (unsigned long sizeof_type, unsigned long count, char *name);
887953
+mem_pool_new_fn (glusterfs_ctx_t *ctx, unsigned long sizeof_type, unsigned long
887953
+                 count, char *name);
887953
+
887953
+#define mem_pool_new(type, count)  (mem_pool_new_fn (THIS->ctx, \
887953
+                                    sizeof(type), count, #type))
887953
 
887953
-#define mem_pool_new(type,count) mem_pool_new_fn (sizeof(type), count, #type)
887953
+#define mem_pool_new_ctx(ctx, type, count)  (mem_pool_new_fn (ctx, \
887953
+                                             sizeof(type), count, #type))
887953
 
887953
 void mem_put (void *ptr);
887953
 void *mem_get (struct mem_pool *pool);
887953
diff --git a/libglusterfs/src/rbthash.c b/libglusterfs/src/rbthash.c
887953
index 52d8a15..06fc7ee 100644
887953
--- a/libglusterfs/src/rbthash.c
887953
+++ b/libglusterfs/src/rbthash.c
887953
@@ -83,7 +83,7 @@ err:
887953
  */
887953
 
887953
 rbthash_table_t *
887953
-rbthash_table_init (int buckets, rbt_hasher_t hfunc,
887953
+rbthash_table_init (glusterfs_ctx_t *ctx, int buckets, rbt_hasher_t hfunc,
887953
                     rbt_data_destroyer_t dfunc,
887953
                     unsigned long expected_entries,
887953
                     struct mem_pool *entrypool)
887953
@@ -123,7 +123,8 @@ rbthash_table_init (int buckets, rbt_hasher_t hfunc,
887953
 
887953
         if (expected_entries) {
887953
                 newtab->entrypool =
887953
-                        mem_pool_new (rbthash_entry_t, expected_entries);
887953
+                        mem_pool_new_ctx (ctx, rbthash_entry_t,
887953
+                                          expected_entries);
887953
                 if (!newtab->entrypool) {
887953
                         goto free_buckets;
887953
                 }
887953
diff --git a/libglusterfs/src/rbthash.h b/libglusterfs/src/rbthash.h
887953
index b093ce9..949b88a 100644
887953
--- a/libglusterfs/src/rbthash.h
887953
+++ b/libglusterfs/src/rbthash.h
887953
@@ -52,7 +52,7 @@ typedef struct rbthash_table {
887953
 } rbthash_table_t;
887953
 
887953
 extern rbthash_table_t *
887953
-rbthash_table_init (int buckets, rbt_hasher_t hfunc,
887953
+rbthash_table_init (glusterfs_ctx_t *ctx, int buckets, rbt_hasher_t hfunc,
887953
                     rbt_data_destroyer_t dfunc, unsigned long expected_entries,
887953
                     struct mem_pool *entrypool);
887953
 
887953
diff --git a/libglusterfs/src/statedump.c b/libglusterfs/src/statedump.c
887953
index a4635f3..4aad014 100644
887953
--- a/libglusterfs/src/statedump.c
887953
+++ b/libglusterfs/src/statedump.c
887953
@@ -377,11 +377,11 @@ gf_proc_dump_mem_info_to_dict (dict_t *dict)
887953
 void
887953
 gf_proc_dump_mempool_info (glusterfs_ctx_t *ctx)
887953
 {
887953
-#if defined(OLD_MEM_POOLS)
887953
         struct mem_pool *pool = NULL;
887953
 
887953
         gf_proc_dump_add_section ("mempool");
887953
 
887953
+#if defined(OLD_MEM_POOLS)
887953
         list_for_each_entry (pool, &ctx->mempool_list, global_list) {
887953
                 gf_proc_dump_write ("-----", "-----");
887953
                 gf_proc_dump_write ("pool-name", "%s", pool->name);
887953
@@ -396,6 +396,20 @@ gf_proc_dump_mempool_info (glusterfs_ctx_t *ctx)
887953
                 gf_proc_dump_write ("cur-stdalloc", "%d", pool->curr_stdalloc);
887953
                 gf_proc_dump_write ("max-stdalloc", "%d", pool->max_stdalloc);
887953
         }
887953
+#else
887953
+        LOCK (&ctx->lock);
887953
+        {
887953
+                list_for_each_entry (pool, &ctx->mempool_list, owner) {
887953
+                        gf_proc_dump_write ("-----", "-----");
887953
+                        gf_proc_dump_write ("pool-name", "%s", pool->name);
887953
+                        gf_proc_dump_write ("sizeof-type", "%lu", pool->sizeof_type);
887953
+                        gf_proc_dump_write ("padded-sizeof", "%d", 1 << pool->pool->power_of_two);
887953
+                        gf_proc_dump_write ("shared-pool", "%p", pool->pool);
887953
+                }
887953
+        }
887953
+        UNLOCK (&ctx->lock);
887953
+
887953
+        /* TODO: details of (struct mem_pool_shared) pool->pool */
887953
 #endif
887953
 }
887953
 
887953
diff --git a/xlators/cluster/ec/src/ec-method.c b/xlators/cluster/ec/src/ec-method.c
887953
index e0dd8e7..a2dd2bd 100644
887953
--- a/xlators/cluster/ec/src/ec-method.c
887953
+++ b/xlators/cluster/ec/src/ec-method.c
887953
@@ -310,7 +310,7 @@ ec_method_init(xlator_t *xl, ec_matrix_list_t *list, uint32_t columns,
887953
     INIT_LIST_HEAD(&list->lru);
887953
     int32_t err;
887953
 
887953
-    list->pool = mem_pool_new_fn(sizeof(ec_matrix_t) +
887953
+    list->pool = mem_pool_new_fn(xl->ctx, sizeof(ec_matrix_t) +
887953
                                  sizeof(ec_matrix_row_t) * columns +
887953
                                  sizeof(uint32_t) * columns * columns,
887953
                                  128, "ec_matrix_t");
887953
diff --git a/xlators/nfs/server/src/nfs3.c b/xlators/nfs/server/src/nfs3.c
887953
index 040d316..b053eb3 100644
887953
--- a/xlators/nfs/server/src/nfs3.c
887953
+++ b/xlators/nfs/server/src/nfs3.c
887953
@@ -5786,7 +5786,8 @@ nfs3_init_state (xlator_t *nfsx)
887953
 
887953
         localpool = nfs->memfactor * GF_NFS_CONCURRENT_OPS_MULT;
887953
         gf_msg_trace (GF_NFS3, 0, "local pool: %d", localpool);
887953
-        nfs3->localpool = mem_pool_new (nfs3_call_state_t, localpool);
887953
+        nfs3->localpool = mem_pool_new_ctx (nfsx->ctx, nfs3_call_state_t,
887953
+                                            localpool);
887953
         if (!nfs3->localpool) {
887953
                 gf_msg (GF_NFS3, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
887953
                         "local mempool creation failed");
887953
diff --git a/xlators/performance/io-cache/src/io-cache.c b/xlators/performance/io-cache/src/io-cache.c
887953
index de44ad2..d7b3b37 100644
887953
--- a/xlators/performance/io-cache/src/io-cache.c
887953
+++ b/xlators/performance/io-cache/src/io-cache.c
887953
@@ -1146,7 +1146,7 @@ ioc_readv (call_frame_t *frame, xlator_t *this, fd_t *fd,
887953
                 if (!ioc_inode->cache.page_table) {
887953
                         ioc_inode->cache.page_table
887953
                                 = rbthash_table_init
887953
-                                (IOC_PAGE_TABLE_BUCKET_COUNT,
887953
+                                (this->ctx, IOC_PAGE_TABLE_BUCKET_COUNT,
887953
                                  ioc_hashfn, NULL, 0,
887953
                                  table->mem_pool);
887953
 
887953
-- 
887953
1.8.3.1
887953