[sheepdog] [PATCH v2 3/4] sheep: use refcnt_t in various data structures

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Tue Jul 9 09:42:02 CEST 2013


From: Hitoshi Mitake <mitake.hitoshi at gmail.com>

This patch lets various data structures employ refcnt_t for its
reference counting of cache entries. Changed structs are: vnode_info,
client_info, request, and object_cache_entry.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---

v2:
 - eliminate main_refcnt_t

 include/sheep.h      |    3 ++-
 sheep/group.c        |   10 +++++-----
 sheep/object_cache.c |    8 ++++----
 sheep/request.c      |   16 ++++++++--------
 sheep/sheep_priv.h   |    4 ++--
 sheep/vdi.c          |    2 +-
 6 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/include/sheep.h b/include/sheep.h
index 0d3fae4..1aecb32 100644
--- a/include/sheep.h
+++ b/include/sheep.h
@@ -17,6 +17,7 @@
 #include "list.h"
 #include "net.h"
 #include "logger.h"
+#include "work.h"
 
 struct sd_vnode {
 	struct node_id  nid;
@@ -33,7 +34,7 @@ struct vnode_info {
 	int nr_nodes;
 
 	int nr_zones;
-	int refcnt;
+	refcnt_t refcnt;
 };
 
 #define TRACE_GRAPH_ENTRY  0x01
diff --git a/sheep/group.c b/sheep/group.c
index 6e01a8d..fc15c44 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -107,9 +107,9 @@ static int get_node_idx(struct vnode_info *vnode_info, struct sd_node *ent)
  */
 struct vnode_info *grab_vnode_info(struct vnode_info *vnode_info)
 {
-	assert(uatomic_read(&vnode_info->refcnt) > 0);
+	assert(refcnt_read(&vnode_info->refcnt) > 0);
 
-	uatomic_inc(&vnode_info->refcnt);
+	refcnt_inc(&vnode_info->refcnt);
 	return vnode_info;
 }
 
@@ -132,9 +132,9 @@ struct vnode_info *get_vnode_info(void)
 void put_vnode_info(struct vnode_info *vnode_info)
 {
 	if (vnode_info) {
-		assert(uatomic_read(&vnode_info->refcnt) > 0);
+		assert(refcnt_read(&vnode_info->refcnt) > 0);
 
-		if (uatomic_sub_return(&vnode_info->refcnt, 1) == 0)
+		if (refcnt_dec_return(&vnode_info->refcnt) == 0)
 			free(vnode_info);
 	}
 }
@@ -155,7 +155,7 @@ struct vnode_info *alloc_vnode_info(const struct sd_node *nodes,
 	vnode_info->nr_vnodes = nodes_to_vnodes(vnode_info->nodes, nr_nodes,
 						vnode_info->vnodes);
 	vnode_info->nr_zones = get_zones_nr_from(nodes, nr_nodes);
-	uatomic_set(&vnode_info->refcnt, 1);
+	refcnt_set(&vnode_info->refcnt, 1);
 	return vnode_info;
 }
 
diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index 0aaf882..2825743 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -42,7 +42,7 @@ struct global_cache {
 
 struct object_cache_entry {
 	uint32_t idx; /* Index of this entry */
-	int refcnt; /* Reference count of this entry */
+	refcnt_t refcnt; /* Reference count of this entry */
 	uint64_t bmap; /* Each bit represents one dirty block in object */
 	struct object_cache *oc; /* Object cache this entry belongs to */
 	struct rb_node node; /* For lru tree of object cache */
@@ -133,17 +133,17 @@ static uint64_t calc_object_bmap(size_t len, off_t offset)
 
 static inline void get_cache_entry(struct object_cache_entry *entry)
 {
-	uatomic_inc(&entry->refcnt);
+	refcnt_inc(&entry->refcnt);
 }
 
 static inline void put_cache_entry(struct object_cache_entry *entry)
 {
-	uatomic_dec(&entry->refcnt);
+	refcnt_dec(&entry->refcnt);
 }
 
 static inline bool entry_in_use(struct object_cache_entry *entry)
 {
-	return uatomic_read(&entry->refcnt) > 0;
+	return refcnt_read(&entry->refcnt) > 0;
 }
 
 /*
diff --git a/sheep/request.c b/sheep/request.c
index c0ed943..a25a0df 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -487,7 +487,7 @@ static struct request *alloc_request(struct client_info *ci, int data_length)
 		return NULL;
 
 	req->ci = ci;
-	ci->refcnt++;
+	refcnt_inc(&ci->refcnt);
 	if (data_length) {
 		req->data_length = data_length;
 		req->data = valloc(data_length);
@@ -498,7 +498,7 @@ static struct request *alloc_request(struct client_info *ci, int data_length)
 	}
 
 	INIT_LIST_HEAD(&req->request_list);
-	uatomic_set(&req->refcnt, 1);
+	refcnt_set(&req->refcnt, 1);
 
 	uatomic_inc(&sys->nr_outstanding_reqs);
 
@@ -509,7 +509,7 @@ static void free_request(struct request *req)
 {
 	uatomic_dec(&sys->nr_outstanding_reqs);
 
-	req->ci->refcnt--;
+	refcnt_dec(&req->ci->refcnt);
 	put_vnode_info(req->vinfo);
 	free(req->data);
 	free(req);
@@ -520,7 +520,7 @@ void put_request(struct request *req)
 	struct client_info *ci = req->ci;
 	eventfd_t value = 1;
 
-	if (uatomic_sub_return(&req->refcnt, 1) > 0)
+	if (refcnt_dec_return(&req->refcnt) > 0)
 		return;
 
 	if (req->local)
@@ -748,10 +748,10 @@ static void clear_client_info(struct client_info *ci)
 
 	unregister_event(ci->conn.fd);
 
-	sd_dprintf("refcnt:%d, fd:%d, %s:%d", ci->refcnt, ci->conn.fd,
-		   ci->conn.ipstr, ci->conn.port);
+	sd_dprintf("refcnt:%d, fd:%d, %s:%d", refcnt_read(&ci->refcnt),
+		ci->conn.fd, ci->conn.ipstr, ci->conn.port);
 
-	if (ci->refcnt)
+	if (refcnt_read(&ci->refcnt))
 		return;
 
 	destroy_client(ci);
@@ -785,7 +785,7 @@ static struct client_info *create_client(int fd, struct cluster_info *cluster)
 
 	ci->conn.fd = fd;
 	ci->conn.events = EPOLLIN;
-	ci->refcnt = 0;
+	refcnt_set(&ci->refcnt, 0);
 
 	INIT_LIST_HEAD(&ci->done_reqs);
 
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 382d246..d08df95 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -52,7 +52,7 @@ struct client_info {
 
 	struct list_head done_reqs;
 
-	int refcnt;
+	refcnt_t refcnt;
 };
 
 struct request {
@@ -68,7 +68,7 @@ struct request {
 	struct list_head request_list;
 	struct list_head pending_list;
 
-	int refcnt;
+	refcnt_t refcnt;
 	bool local;
 	int local_req_efd;
 
diff --git a/sheep/vdi.c b/sheep/vdi.c
index 0e38ba3..255fd70 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -856,7 +856,7 @@ static int start_deletion(struct request *req, uint32_t vid)
 	if (dw->count == 0)
 		goto out;
 
-	uatomic_inc(&req->refcnt);
+	refcnt_inc(&req->refcnt);
 
 	if (list_empty(&deletion_work_list)) {
 		list_add_tail(&dw->list, &deletion_work_list);
-- 
1.7.10.4




More information about the sheepdog mailing list