[sheepdog] [PATCH v4 9/9] sbd: use kmem_cache for sheep aiocb and request
Liu Yuan
namei.unix at gmail.com
Mon May 26 08:52:50 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 | 14 +++++++++++---
3 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/sbd/sbd.h b/sbd/sbd.h
index e2940f8..6762369 100644
--- a/sbd/sbd.h
+++ b/sbd/sbd.h
@@ -102,6 +102,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..acd5b08 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_request_pool);
+}
diff --git a/sbd/sheep_block_device.c b/sbd/sheep_block_device.c
index a34ad7e..1067386 100644
--- a/sbd/sheep_block_device.c
+++ b/sbd/sheep_block_device.c
@@ -207,7 +207,7 @@ static ssize_t sbd_add(struct bus_type *bus, const char *buf,
if (sscanf(buf, "%s %d %s", dev->vdi.ip, &dev->vdi.port,
dev->vdi.name) != 3) {
ret = -EINVAL;
- goto err_put;
+ goto err_free_dev;
}
spin_lock_init(&dev->queue_lock);
@@ -393,11 +393,18 @@ int __init sbd_init(void)
ret = sbd_sysfs_init();
if (ret < 0)
- goto err;
+ goto err_unreg_blkdev;
+
+ ret = sheep_slab_create();
+ if (ret < 0)
+ goto err_sysfs_cleanup;
pr_info("%s: Sheepdog block device loaded\n", DRV_NAME);
return 0;
-err:
+
+err_sysfs_cleanup:
+ sbd_sysfs_cleanup();
+err_unreg_blkdev:
unregister_blkdev(sbd_major, DRV_NAME);
return ret;
}
@@ -406,6 +413,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