[sheepdog] [PATCH v2 9/9] sbd: use kmem_cache for sheep aiocb and request

Liu Yuan namei.unix at gmail.com
Sun May 25 09:53:21 CEST 2014


From: Liu Yuan <tailai.ly at taobao.com>

Though it won't help the performance much, but let's do our best for performance

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 sbd/sbd.h                |  2 ++
 sbd/sheep.c              | 38 +++++++++++++++++++++++++++++++-------
 sbd/sheep_block_device.c | 13 ++++++++++---
 3 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/sbd/sbd.h b/sbd/sbd.h
index 5625867..ee8f07d 100644
--- a/sbd/sbd.h
+++ b/sbd/sbd.h
@@ -108,6 +108,8 @@ int sheep_setup_vdi(struct sbd_device *dev);
 struct sheep_aiocb *sheep_aiocb_setup(struct request *req);
 int sheep_aiocb_submit(struct sheep_aiocb *aiocb);
 int sheep_handle_reply(struct sbd_device *dev);
+int sheep_slab_create(void);
+void sheep_slab_destroy(void);
 
 static inline int sbd_dev_id_to_minor(int id)
 {
diff --git a/sbd/sheep.c b/sbd/sheep.c
index 9644907..e4d34e8 100644
--- a/sbd/sheep.c
+++ b/sbd/sheep.c
@@ -12,6 +12,8 @@
 #include "sbd.h"
 
 static DEFINE_MUTEX(socket_mutex);
+static struct kmem_cache *sheep_aiocb_pool;
+static struct kmem_cache *sheep_request_pool;
 
 void socket_shutdown(struct socket *sock)
 {
@@ -312,7 +314,7 @@ err:
 static inline void free_sheep_aiocb(struct sheep_aiocb *aiocb)
 {
 	vfree(aiocb->buf);
-	kfree(aiocb);
+	kmem_cache_free(sheep_aiocb_pool, aiocb);
 }
 
 static void aio_write_done(struct sheep_aiocb *aiocb)
@@ -349,7 +351,8 @@ static void aio_read_done(struct sheep_aiocb *aiocb)
 
 struct sheep_aiocb *sheep_aiocb_setup(struct request *req)
 {
-	struct sheep_aiocb *aiocb = kmalloc(sizeof(*aiocb), SBD_GFP_FLAGS);
+	struct sheep_aiocb *aiocb = kmem_cache_alloc(sheep_aiocb_pool,
+						     SBD_GFP_FLAGS);
 	struct req_iterator iter;
 	struct bio_vec *bvec;
 	int len = 0;
@@ -366,7 +369,7 @@ struct sheep_aiocb *sheep_aiocb_setup(struct request *req)
 	atomic_set(&aiocb->nr_requests, 0);
 
 	if (!aiocb->buf) {
-		kfree(aiocb);
+		kmem_cache_free(sheep_aiocb_pool, aiocb);
 		return ERR_PTR(-ENOMEM);
 	}
 
@@ -401,7 +404,8 @@ static struct sheep_request *alloc_sheep_request(struct sheep_aiocb *aiocb,
 						 u64 oid, int len,
 						 int offset)
 {
-	struct sheep_request *req = kmalloc(sizeof(*req), SBD_GFP_FLAGS);
+	struct sheep_request *req = kmem_cache_alloc(sheep_request_pool,
+						     SBD_GFP_FLAGS);
 	struct sbd_device *dev = sheep_aiocb_to_device(aiocb);
 
 	if (!req)
@@ -425,7 +429,7 @@ static struct sheep_request *alloc_sheep_request(struct sheep_aiocb *aiocb,
 	default:
 		/* impossible case */
 		WARN_ON(1);
-		kfree(req);
+		kmem_cache_free(sheep_request_pool, req);
 		return ERR_PTR(-EINVAL);
 	}
 
@@ -445,7 +449,7 @@ static void end_sheep_request(struct sheep_request *req)
 	if (atomic_dec_return(&aiocb->nr_requests) <= 0)
 		aiocb->aio_done_func(aiocb);
 	BUG_ON(!list_empty(&req->list));
-	kfree(req);
+	kmem_cache_free(sheep_request_pool, req);
 }
 
 static struct sheep_request *find_inflight_request_oid(struct sbd_device *dev,
@@ -617,7 +621,7 @@ int sheep_handle_reply(struct sbd_device *dev)
 	switch (req->type) {
 	case SHEEP_CREATE:
 		/* We need to update inode for create */
-		new = kmalloc(sizeof(*new), SBD_GFP_FLAGS);
+		new = kmem_cache_alloc(sheep_request_pool, SBD_GFP_FLAGS);
 		if (!new) {
 			ret = -ENOMEM;
 			req->aiocb->ret = EIO;
@@ -654,3 +658,23 @@ end_request:
 err:
 	return ret;
 }
+
+int sheep_slab_create(void)
+{
+	sheep_aiocb_pool = KMEM_CACHE(sheep_aiocb, 0);
+	if (!sheep_aiocb_pool)
+		return -ENOMEM;
+
+	sheep_request_pool = KMEM_CACHE(sheep_request, 0);
+	if (!sheep_request_pool) {
+		kmem_cache_destroy(sheep_aiocb_pool);
+		return -ENOMEM;
+	}
+	return 0;
+}
+
+void sheep_slab_destroy(void)
+{
+	kmem_cache_destroy(sheep_aiocb_pool);
+	kmem_cache_destroy(sheep_aiocb_pool);
+}
diff --git a/sbd/sheep_block_device.c b/sbd/sheep_block_device.c
index 3e81de5..a155cf3 100644
--- a/sbd/sheep_block_device.c
+++ b/sbd/sheep_block_device.c
@@ -204,10 +204,14 @@ static ssize_t sbd_add(struct bus_type *bus, const char *buf,
 		goto err_put;
 	}
 
+	ret = sheep_slab_create();
+	if (ret < 0)
+		goto err_free_dev;
+
 	if (sscanf(buf, "%s %d %s", dev->vdi.ip, &dev->vdi.port,
 		   dev->vdi.name) != 3) {
 		ret = -EINVAL;
-		goto err_put;
+		goto err_free_slab;
 	}
 
 	spin_lock_init(&dev->queue_lock);
@@ -229,7 +233,7 @@ static ssize_t sbd_add(struct bus_type *bus, const char *buf,
 
 	ret = sheep_setup_vdi(dev);
 	if (ret < 0)
-		goto err_free_dev;
+		goto err_free_slab;
 
 	dev->id = new_id;
 	snprintf(name, DEV_NAME_LEN, DRV_NAME "%d", dev->id);
@@ -237,7 +241,7 @@ static ssize_t sbd_add(struct bus_type *bus, const char *buf,
 	dev->minor = sbd_dev_id_to_minor(dev->id);
 	dev->reaper = kthread_run(sbd_request_reaper, dev, "sbd_reaper");
 	if (IS_ERR(dev->reaper))
-		goto err_free_dev;
+		goto err_free_slab;
 	dev->submiter = kthread_run(sbd_request_submiter, dev, "sbd_submiter");
 	if (IS_ERR(dev->submiter))
 		goto err_stop_reaper;
@@ -259,6 +263,8 @@ err_stop_kthreads:
 err_stop_reaper:
 	kthread_stop(dev->reaper);
 	wake_up(&dev->reaper_wq);
+err_free_slab:
+	sheep_slab_destroy();
 err_free_dev:
 	free_sbd_device(dev);
 err_put:
@@ -406,6 +412,7 @@ void __exit sbd_exit(void)
 {
 	sbd_sysfs_cleanup();
 	unregister_blkdev(sbd_major, DRV_NAME);
+	sheep_slab_destroy();
 	pr_info("%s: Sheepdog block device unloaded\n", DRV_NAME);
 }
 
-- 
1.8.1.2




More information about the sheepdog mailing list