Patchwork [v3,1/8] ext4: add percpu counters and related functions to account io types.

login
register
mail settings
Submitter Zheng Liu
Date Nov. 28, 2011, 12:20 p.m.
Message ID <1322482828-5529-1-git-send-email-wenqing.lz@taobao.com>
Download mbox | patch
Permalink /patch/127970/
State New
Headers show

Comments

Zheng Liu - Nov. 28, 2011, 12:20 p.m.
From: Zheng Liu <wenqing.lz@taobao.com>

Add percpu counters in ext4_sb_info and related functions to save the result of
io types. IO types in ext4 are splitted as follows:

* Metadata:
 - super block
 - group descriptor
 - inode bitmap
 - block bitmap
 - inode table
 - extent block
 - indirect block
 - dir index and entry
 - extended attribute
* Data:
 - regular data block

CC: Jens Axboe <axboe@kernel.dk>
CC: Steven Whitehouse <swhiteho@redhat.com>
CC: Aditya Kali <adityakali@google.com>
Signed-off-by: Wang Shaoyan <wangshaoyan.pt@taobao.com>
Signed-off-by: Zheng Liu <wenqing.lz@taobao.com>
---
 fs/ext4/ext4.h  |   37 +++++++++++++++++++++++++++++++++
 fs/ext4/super.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 0 deletions(-)

Patch

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 5b0e26a..cd9c7f9 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1108,6 +1108,23 @@  struct ext4_super_block {
 #define EXT4_MF_FS_ABORTED	0x0002	/* Fatal error detected */
 
 /*
+ * ext4 io types
+ */
+enum {
+	EXT4_IOS_SUPER_BLOCK = 0,
+	EXT4_IOS_GROUP_DESC,
+	EXT4_IOS_INODE_BITMAP,
+	EXT4_IOS_BLOCK_BITMAP,
+	EXT4_IOS_INODE_TABLE,
+	EXT4_IOS_EXTENT_BLOCK,
+	EXT4_IOS_INDIRECT_BLOCK,
+	EXT4_IOS_DIR_ENTRY,
+	EXT4_IOS_EXTENDED_ATTR,
+	EXT4_IOS_REGULAR_DATA,
+	EXT4_IOS_TYPE_END,
+};
+
+/*
  * fourth extended-fs super-block data in memory
  */
 struct ext4_sb_info {
@@ -1249,6 +1266,9 @@  struct ext4_sb_info {
 
 	/* record the last minlen when FITRIM is called. */
 	atomic_t s_last_trim_minblks;
+
+	/* for io type accouting */
+	struct percpu_counter s_ios_counters[EXT4_IOS_TYPE_END][2];
 };
 
 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
@@ -1284,6 +1304,11 @@  static inline void ext4_set_io_unwritten_flag(struct inode *inode,
 	}
 }
 
+static inline unsigned int ext4_blocks_per_page(struct inode *inode)
+{
+	return PAGE_CACHE_SIZE >> inode->i_blkbits;
+}
+
 /*
  * Inode dynamic state flags
  */
@@ -1926,6 +1951,18 @@  extern int ext4_group_extend(struct super_block *sb,
 				ext4_fsblk_t n_blocks_count);
 
 /* super.c */
+extern void __ext4_io_stat(struct super_block *sb, int rw,
+			   int type, unsigned long count);
+extern void ext4_ios_write(struct super_block *sb, handle_t *handle,
+			   struct buffer_head *bh, int type,
+			   unsigned long count);
+
+static inline void ext4_ios_read(struct super_block *sb, int type,
+				 unsigned long count)
+{
+	__ext4_io_stat(sb, READ, type, count);
+}
+
 extern void *ext4_kvmalloc(size_t size, gfp_t flags);
 extern void *ext4_kvzalloc(size_t size, gfp_t flags);
 extern void ext4_kvfree(void *ptr);
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 3858767..3ef3e03b 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -842,6 +842,10 @@  static void ext4_put_super(struct super_block *sb)
 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
 	percpu_counter_destroy(&sbi->s_dirs_counter);
 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
+	for (i = 0; i < EXT4_IOS_TYPE_END; i++) {
+		percpu_counter_destroy(&sbi->s_ios_counters[i][READ]);
+		percpu_counter_destroy(&sbi->s_ios_counters[i][WRITE]);
+	}
 	brelse(sbi->s_sbh);
 #ifdef CONFIG_QUOTA
 	for (i = 0; i < MAXQUOTAS; i++)
@@ -3157,6 +3161,16 @@  static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 	}
 
 	/*
+	 * Note: s_ios_counters must be initialized as soon as possible becuase
+	 *       ext4 io type accouting depends on it
+	 */
+	for (i = 0; i < EXT4_IOS_TYPE_END; i++) {
+		if (percpu_counter_init(&sbi->s_ios_counters[i][READ], 0))
+			goto out_init_ios;
+		if (percpu_counter_init(&sbi->s_ios_counters[i][WRITE], 0))
+			goto out_init_ios;
+	}
+	/*
 	 * The ext4 superblock will not be buffer aligned for other than 1kB
 	 * block sizes.  We need to calculate the offset from buffer start.
 	 */
@@ -3875,6 +3889,11 @@  out_fail:
 	sb->s_fs_info = NULL;
 	kfree(sbi->s_blockgroup_lock);
 	kfree(sbi);
+out_init_ios:
+	for (i = 0; i < EXT4_IOS_TYPE_END; i++) {
+		percpu_counter_destroy(&sbi->s_ios_counters[i][READ]);
+		percpu_counter_destroy(&sbi->s_ios_counters[i][WRITE]);
+	}
 out_free_orig:
 	kfree(orig_data);
 	return ret;
@@ -4940,6 +4959,47 @@  out:
 
 #endif
 
+static inline u64 ext4_get_ios_counter(struct ext4_sb_info *sbi,
+					int rw, int type)
+{
+	return percpu_counter_sum(&sbi->s_ios_counters[type][rw]);
+}
+
+static inline void ext4_reset_ios_counter(struct ext4_sb_info *sbi)
+{
+	int i;
+
+	for (i = 0; i < EXT4_IOS_TYPE_END; i++) {
+		percpu_counter_set(&sbi->s_ios_counters[i][READ], 0);
+		percpu_counter_set(&sbi->s_ios_counters[i][WRITE], 0);
+	}
+}
+
+void __ext4_io_stat(struct super_block *sb, int rw,
+		    int type, unsigned long count)
+{
+	struct ext4_sb_info *sbi = EXT4_SB(sb);
+
+	BUG_ON(type < 0 || type >= EXT4_IOS_TYPE_END);
+	percpu_counter_add(&sbi->s_ios_counters[type][rw], count);
+}
+
+void ext4_ios_write(struct super_block *sb, handle_t *handle,
+		    struct buffer_head *bh, int type, unsigned long count)
+{
+	if (!bh)
+		goto count;
+	if (!handle || !ext4_handle_valid(handle)) {
+		if (buffer_dirty(bh))
+			return;
+	} else {
+		if (buffer_jbddirty(bh))
+			return;
+	}
+count:
+	__ext4_io_stat(sb, WRITE, type, count);
+}
+
 static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
 		       const char *dev_name, void *data)
 {