From cd567c063ec25b280d3fb5686a69b068c2d6d6df Mon Sep 17 00:00:00 2001 From: shishir gowda Date: Wed, 10 Aug 2011 16:58:38 +0530 Subject: mem-pool: Make mem-pool ptr avialable in ptr The header of the ptr returned from mem-pool will now store the mem-pool ptr it belongs to. mem_put will now take only the pointer to be freed. Also, changing MALLOC call to GF_CALLOC in mem_get when we run out of entries in mem-pool. This also will have the header information saved. Change-Id: I3de182663a7f5b49c9e9425e9531775b70bdff67 BUG: 3390 Reviewed-on: http://review.gluster.com/205 Reviewed-by: Amar Tumballi Tested-by: Gluster Build System Reviewed-by: Vijay Bellur --- libglusterfs/src/call-stub.c | 2 +- libglusterfs/src/fd.c | 4 ++-- libglusterfs/src/inode.c | 8 +++---- libglusterfs/src/mem-pool.c | 42 +++++++++++++++++++++++++--------- libglusterfs/src/mem-pool.h | 2 +- libglusterfs/src/rbthash.c | 6 ++--- libglusterfs/src/stack.h | 4 ++-- rpc/rpc-lib/src/rpc-clnt.c | 12 +++++----- rpc/rpc-lib/src/rpcsvc.c | 2 +- rpc/rpc-transport/rdma/src/rdma.c | 8 +++---- xlators/features/quiesce/src/quiesce.c | 2 +- xlators/nfs/server/src/nfs-fops.c | 2 +- xlators/nfs/server/src/nfs3.c | 2 +- 13 files changed, 58 insertions(+), 38 deletions(-) diff --git a/libglusterfs/src/call-stub.c b/libglusterfs/src/call-stub.c index fab368a6d..67543c64a 100644 --- a/libglusterfs/src/call-stub.c +++ b/libglusterfs/src/call-stub.c @@ -3835,7 +3835,7 @@ call_stub_destroy (call_stub_t *stub) } stub->stub_mem_pool = NULL; - mem_put (tmp_pool, stub); + mem_put (stub); out: tmp_pool = NULL; diff --git a/libglusterfs/src/fd.c b/libglusterfs/src/fd.c index 72df5e1d3..d6c4806a1 100644 --- a/libglusterfs/src/fd.c +++ b/libglusterfs/src/fd.c @@ -428,7 +428,7 @@ fd_destroy (fd_t *fd) GF_FREE (fd->_ctx); inode_unref (fd->inode); fd->inode = (inode_t *)0xaaaaaaaa; - mem_put (tmp_pool,fd); + mem_put (fd); tmp_pool = NULL; out: return; @@ -500,7 +500,7 @@ fd_create (inode_t *inode, pid_t pid) fd->_ctx = GF_CALLOC (1, (sizeof (struct _fd_ctx) * fd->xl_count), gf_common_mt_fd_ctx); if (!fd->_ctx) { - mem_put (inode->table->fd_mem_pool, fd); + mem_put (fd); fd = NULL; goto out; } diff --git a/libglusterfs/src/inode.c b/libglusterfs/src/inode.c index a3709cdb3..1b4320311 100644 --- a/libglusterfs/src/inode.c +++ b/libglusterfs/src/inode.c @@ -154,7 +154,7 @@ __dentry_unset (dentry_t *dentry) dentry->parent = NULL; } - mem_put (tmp_pool, dentry); + mem_put (dentry); tmp_pool = NULL; } @@ -336,7 +336,7 @@ __inode_destroy (inode_t *inode) noctx: LOCK_DESTROY (&inode->lock); // memset (inode, 0xb, sizeof (*inode)); - mem_put (tmp_pool, inode); + mem_put (inode); tmp_pool = NULL; } @@ -501,7 +501,7 @@ __dentry_create (inode_t *inode, inode_t *parent, const char *name) newd->name = gf_strdup (name); if (newd->name == NULL) { - mem_put (parent->table->dentry_pool, newd); + mem_put (newd); newd = NULL; goto out; } @@ -547,7 +547,7 @@ __inode_create (inode_table_t *table) if (newi->_ctx == NULL) { LOCK_DESTROY (&newi->lock); - mem_put (table->inode_pool, newi); + mem_put (newi); newi = NULL; goto out; } diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c index 5fdd5e5f2..f3dfc2149 100644 --- a/libglusterfs/src/mem-pool.c +++ b/libglusterfs/src/mem-pool.c @@ -24,10 +24,12 @@ #include #define GF_MEM_POOL_LIST_BOUNDARY (sizeof(struct list_head)) -#define GF_MEM_POOL_PAD_BOUNDARY (GF_MEM_POOL_LIST_BOUNDARY + sizeof(int)) +#define GF_MEM_POOL_PTR (sizeof(struct mem_pool*)) +#define GF_MEM_POOL_PAD_BOUNDARY (GF_MEM_POOL_LIST_BOUNDARY + GF_MEM_POOL_PTR + sizeof(int)) #define mem_pool_chunkhead2ptr(head) ((head) + GF_MEM_POOL_PAD_BOUNDARY) #define mem_pool_ptr2chunkhead(ptr) ((ptr) - GF_MEM_POOL_PAD_BOUNDARY) #define is_mem_chunk_in_use(ptr) (*ptr == 1) +#define mem_pool_from_ptr(ptr) ((ptr) + GF_MEM_POOL_LIST_BOUNDARY) #define GF_MEM_HEADER_SIZE (4 + sizeof (size_t) + sizeof (xlator_t *) + 4 + 8) #define GF_MEM_TRAILER_SIZE 8 @@ -379,6 +381,7 @@ mem_get (struct mem_pool *mem_pool) struct list_head *list = NULL; void *ptr = NULL; int *in_use = NULL; + struct mem_pool **pool_ptr = NULL; if (!mem_pool) { gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); @@ -395,7 +398,8 @@ mem_get (struct mem_pool *mem_pool) mem_pool->cold_count--; ptr = list; - in_use = (ptr + GF_MEM_POOL_LIST_BOUNDARY); + in_use = (ptr + GF_MEM_POOL_LIST_BOUNDARY + + GF_MEM_POOL_PTR); *in_use = 1; goto fwd_addr_out; @@ -421,17 +425,20 @@ mem_get (struct mem_pool *mem_pool) * because it is too much work knowing that a better slab * allocator is coming RSN. */ - ptr = MALLOC (mem_pool->real_sizeof_type); + ptr = GF_CALLOC (1, mem_pool->padded_sizeof_type, + gf_common_mt_mem_pool); + gf_log_callingfn ("mem-pool", GF_LOG_DEBUG, "Mem pool is full. " + "Callocing mem"); /* Memory coming from the heap need not be transformed from a * chunkhead to a usable pointer since it is not coming from * the pool. */ - goto unlocked_out; } fwd_addr_out: + pool_ptr = mem_pool_from_ptr (ptr); + *pool_ptr = (struct mem_pool *)mem_pool; ptr = mem_pool_chunkhead2ptr (ptr); -unlocked_out: UNLOCK (&mem_pool->lock); return ptr; @@ -458,25 +465,39 @@ __is_member (struct mem_pool *pool, void *ptr) void -mem_put (struct mem_pool *pool, void *ptr) +mem_put (void *ptr) { struct list_head *list = NULL; int *in_use = NULL; void *head = NULL; + struct mem_pool **tmp = NULL; + struct mem_pool *pool = NULL; - if (!pool || !ptr) { + if (!ptr) { gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); return; } + list = head = mem_pool_ptr2chunkhead (ptr); + tmp = mem_pool_from_ptr (head); + if (!tmp) { + gf_log ("mem-pool", GF_LOG_ERROR, "ptr header is corrupted"); + return; + } + + pool = *tmp; + if (!pool) { + gf_log ("mem-pool", GF_LOG_ERROR, "mem-pool ptr is NULL"); + return; + } LOCK (&pool->lock); { switch (__is_member (pool, ptr)) { case 1: - list = head = mem_pool_ptr2chunkhead (ptr); - in_use = (head + GF_MEM_POOL_LIST_BOUNDARY); + in_use = (head + GF_MEM_POOL_LIST_BOUNDARY + + GF_MEM_POOL_PTR); if (!is_mem_chunk_in_use(in_use)) { gf_log_callingfn ("mem-pool", GF_LOG_CRITICAL, "mem_put called on freed ptr %p of mem " @@ -506,7 +527,7 @@ mem_put (struct mem_pool *pool, void *ptr) * not have enough info to distinguish between the two * situations. */ - FREE (ptr); + GF_FREE (list); break; default: /* log error */ @@ -516,7 +537,6 @@ mem_put (struct mem_pool *pool, void *ptr) UNLOCK (&pool->lock); } - void mem_pool_destroy (struct mem_pool *pool) { diff --git a/libglusterfs/src/mem-pool.h b/libglusterfs/src/mem-pool.h index 7092ece30..98454c5be 100644 --- a/libglusterfs/src/mem-pool.h +++ b/libglusterfs/src/mem-pool.h @@ -152,7 +152,7 @@ mem_pool_new_fn (unsigned long sizeof_type, unsigned long count); #define mem_pool_new(type,count) mem_pool_new_fn (sizeof(type), count) -void mem_put (struct mem_pool *pool, void *ptr); +void mem_put (void *ptr); void *mem_get (struct mem_pool *pool); void *mem_get0 (struct mem_pool *pool); diff --git a/libglusterfs/src/rbthash.c b/libglusterfs/src/rbthash.c index c134c3a00..beda672d0 100644 --- a/libglusterfs/src/rbthash.c +++ b/libglusterfs/src/rbthash.c @@ -201,7 +201,7 @@ rbthash_init_entry (rbthash_table_t *tbl, void *data, void *key, int keylen) ret = 0; free_entry: if (ret == -1) { - mem_put (tbl->entrypool, entry); + mem_put (entry); entry = NULL; } @@ -230,7 +230,7 @@ rbthash_deinit_entry (rbthash_table_t *tbl, rbthash_entry_t *entry) } UNLOCK (&tbl->tablelock); - mem_put (tbl->entrypool, entry); + mem_put (entry); } return; @@ -398,7 +398,7 @@ rbthash_remove (rbthash_table_t *tbl, void *key, int keylen) } UNLOCK (&tbl->tablelock); - mem_put (tbl->entrypool, entry); + mem_put (entry); return dataref; } diff --git a/libglusterfs/src/stack.h b/libglusterfs/src/stack.h index 87c723112..455a360be 100644 --- a/libglusterfs/src/stack.h +++ b/libglusterfs/src/stack.h @@ -149,7 +149,7 @@ FRAME_DESTROY (call_frame_t *frame) } LOCK_DESTROY (&frame->lock); - mem_put (frame->root->pool->frame_mem_pool, frame); + mem_put (frame); if (local) GF_FREE (local); @@ -178,7 +178,7 @@ STACK_DESTROY (call_stack_t *stack) while (stack->frames.next) { FRAME_DESTROY (stack->frames.next); } - mem_put (stack->pool->stack_mem_pool, stack); + mem_put (stack); if (local) GF_FREE (local); diff --git a/rpc/rpc-lib/src/rpc-clnt.c b/rpc/rpc-lib/src/rpc-clnt.c index 5545f7e2e..acc4b0e57 100644 --- a/rpc/rpc-lib/src/rpc-clnt.c +++ b/rpc/rpc-lib/src/rpc-clnt.c @@ -117,7 +117,7 @@ saved_frames_delete (struct saved_frame *saved_frame, conn->rpc_clnt->reqpool); } - mem_put (conn->rpc_clnt->saved_frames_pool, saved_frame); + mem_put (saved_frame); out: return; } @@ -201,7 +201,7 @@ call_bail (void *data) rpc_clnt_reply_deinit (trav->rpcreq, clnt->reqpool); list_del_init (&trav->list); - mem_put (conn->rpc_clnt->saved_frames_pool, trav); + mem_put (trav); } out: return; @@ -347,7 +347,7 @@ saved_frames_unwind (struct saved_frames *saved_frames) trav->rpcreq->conn->rpc_clnt->reqpool); list_del_init (&trav->list); - mem_put (saved_frames_pool, trav); + mem_put (trav); } } @@ -601,7 +601,7 @@ rpc_clnt_reply_deinit (struct rpc_req *req, struct mem_pool *pool) iobref_unref (req->rsp_iobref); } - mem_put (pool, req); + mem_put (req); out: return; } @@ -752,7 +752,7 @@ rpc_clnt_handle_reply (struct rpc_clnt *clnt, rpc_transport_pollin_t *pollin) out: if (saved_frame) { - mem_put (conn->rpc_clnt->saved_frames_pool, saved_frame); + mem_put (saved_frame); } clnt = rpc_clnt_unref (clnt); @@ -1461,7 +1461,7 @@ out: if (rpcreq) { rpcreq->rpc_status = -1; cbkfn (rpcreq, NULL, 0, frame); - mem_put (rpc->reqpool, rpcreq); + mem_put (rpcreq); } } return ret; diff --git a/rpc/rpc-lib/src/rpcsvc.c b/rpc/rpc-lib/src/rpcsvc.c index a8d3e84c9..245b41cab 100644 --- a/rpc/rpc-lib/src/rpcsvc.c +++ b/rpc/rpc-lib/src/rpcsvc.c @@ -276,7 +276,7 @@ rpcsvc_request_destroy (rpcsvc_request_t *req) rpc_transport_unref (req->trans); - mem_put (req->svc->rxpool, req); + mem_put (req); out: return; diff --git a/rpc/rpc-transport/rdma/src/rdma.c b/rpc/rpc-transport/rdma/src/rdma.c index d170c0d66..e88148460 100644 --- a/rpc/rpc-transport/rdma/src/rdma.c +++ b/rpc/rpc-transport/rdma/src/rdma.c @@ -300,7 +300,7 @@ __rdma_ioq_entry_free (rdma_ioq_t *entry) entry->msg.request.rsp_iobref = NULL; } /* TODO: use mem-pool */ - mem_put (entry->pool, entry); + mem_put (entry); } @@ -795,7 +795,7 @@ __rdma_request_context_destroy (rdma_request_context_t *context) gf_log ("rdma", GF_LOG_DEBUG, "failed to send " "message"); - mem_put (context->pool, context); + mem_put (context); __rdma_disconnect (peer->trans); goto out; } @@ -811,7 +811,7 @@ __rdma_request_context_destroy (rdma_request_context_t *context) context->rsp_iobref = NULL; } - mem_put (context->pool, context); + mem_put (context); out: return; @@ -1602,7 +1602,7 @@ rdma_reply_info_destroy (rdma_reply_info_t *reply_info) reply_info->wc_array = NULL; } - mem_put (reply_info->pool, reply_info); + mem_put (reply_info); out: return; } diff --git a/xlators/features/quiesce/src/quiesce.c b/xlators/features/quiesce/src/quiesce.c index 6c76a0bf5..7fd16ceb9 100644 --- a/xlators/features/quiesce/src/quiesce.c +++ b/xlators/features/quiesce/src/quiesce.c @@ -56,7 +56,7 @@ gf_quiesce_local_wipe (xlator_t *this, quiesce_local_t *local) if (local->vector) GF_FREE (local->vector); - mem_put (priv->local_pool, local); + mem_put (local); } call_stub_t * diff --git a/xlators/nfs/server/src/nfs-fops.c b/xlators/nfs/server/src/nfs-fops.c index 5280139cb..236c411e2 100644 --- a/xlators/nfs/server/src/nfs-fops.c +++ b/xlators/nfs/server/src/nfs-fops.c @@ -74,7 +74,7 @@ nfs_fop_local_wipe (xlator_t *nfsx, struct nfs_fop_local *l) if (l->dictgfid) dict_unref (l->dictgfid); - mem_put (nfs->foppool, l); + mem_put (l); return; } diff --git a/xlators/nfs/server/src/nfs3.c b/xlators/nfs/server/src/nfs3.c index 6c0d1e2ef..013f6d80e 100644 --- a/xlators/nfs/server/src/nfs3.c +++ b/xlators/nfs/server/src/nfs3.c @@ -429,7 +429,7 @@ nfs3_call_state_wipe (nfs3_call_state_t *cs) if (cs->iobref) iobref_unref (cs->iobref); memset (cs, 0, sizeof (*cs)); - mem_put (nfs3->localpool, cs); + mem_put (cs); /* Already refd by fd_lookup, so no need to ref again. */ } -- cgit