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