Btrfs: use a slab for ordered extents allocation
authorMiao Xie <miaox@cn.fujitsu.com>
Thu, 6 Sep 2012 10:01:51 +0000 (04:01 -0600)
committerChris Mason <chris.mason@fusionio.com>
Mon, 1 Oct 2012 19:19:11 +0000 (15:19 -0400)
The ordered extent allocation is in the fast path of the IO, so use a slab
to improve the speed of the allocation.

 "Size of the struct is 280, so this will fall into the size-512 bucket,
  giving 8 objects per page, while own slab will pack 14 objects into a page.

  Another benefit I see is to check for leaked objects when the module is
  removed (and the cache destroy takes place)."
-- David Sterba

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
fs/btrfs/ordered-data.c
fs/btrfs/ordered-data.h
fs/btrfs/super.c

index cd8ecb73c05c6519981193069a3dc0d034fc7662..e2b3d994ec01b69726947cb341b8ca5525d23fdc 100644 (file)
@@ -25,6 +25,8 @@
 #include "btrfs_inode.h"
 #include "extent_io.h"
 
+static struct kmem_cache *btrfs_ordered_extent_cache;
+
 static u64 entry_end(struct btrfs_ordered_extent *entry)
 {
        if (entry->file_offset + entry->len < entry->file_offset)
@@ -187,7 +189,7 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
        struct btrfs_ordered_extent *entry;
 
        tree = &BTRFS_I(inode)->ordered_tree;
-       entry = kzalloc(sizeof(*entry), GFP_NOFS);
+       entry = kmem_cache_zalloc(btrfs_ordered_extent_cache, GFP_NOFS);
        if (!entry)
                return -ENOMEM;
 
@@ -421,7 +423,7 @@ void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
                        list_del(&sum->list);
                        kfree(sum);
                }
-               kfree(entry);
+               kmem_cache_free(btrfs_ordered_extent_cache, entry);
        }
 }
 
@@ -958,3 +960,20 @@ void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
        }
        spin_unlock(&root->fs_info->ordered_extent_lock);
 }
+
+int __init ordered_data_init(void)
+{
+       btrfs_ordered_extent_cache = kmem_cache_create("btrfs_ordered_extent",
+                                    sizeof(struct btrfs_ordered_extent), 0,
+                                    SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
+                                    NULL);
+       if (!btrfs_ordered_extent_cache)
+               return -ENOMEM;
+       return 0;
+}
+
+void ordered_data_exit(void)
+{
+       if (btrfs_ordered_extent_cache)
+               kmem_cache_destroy(btrfs_ordered_extent_cache);
+}
index c2443a431ca5212bb4a83a7fd2b67b2a06192da5..d1ddaeff13562d9693b61f9982f664d017f00f09 100644 (file)
@@ -192,4 +192,6 @@ void btrfs_add_ordered_operation(struct btrfs_trans_handle *trans,
                                 struct inode *inode);
 void btrfs_wait_ordered_extents(struct btrfs_root *root,
                                int nocow_only, int delay_iput);
+int __init ordered_data_init(void);
+void ordered_data_exit(void);
 #endif
index 83d6f9f9c2209861efdec86dec9ad54d629deeb9..06ff1dd0f9b7a30bbab3571487e70d93e2334e82 100644 (file)
@@ -1620,10 +1620,14 @@ static int __init init_btrfs_fs(void)
        if (err)
                goto free_extent_io;
 
-       err = btrfs_delayed_inode_init();
+       err = ordered_data_init();
        if (err)
                goto free_extent_map;
 
+       err = btrfs_delayed_inode_init();
+       if (err)
+               goto free_ordered_data;
+
        err = btrfs_interface_init();
        if (err)
                goto free_delayed_inode;
@@ -1641,6 +1645,8 @@ unregister_ioctl:
        btrfs_interface_exit();
 free_delayed_inode:
        btrfs_delayed_inode_exit();
+free_ordered_data:
+       ordered_data_exit();
 free_extent_map:
        extent_map_exit();
 free_extent_io:
@@ -1657,6 +1663,7 @@ static void __exit exit_btrfs_fs(void)
 {
        btrfs_destroy_cachep();
        btrfs_delayed_inode_exit();
+       ordered_data_exit();
        extent_map_exit();
        extent_io_exit();
        btrfs_interface_exit();