Skip to content

Commit

Permalink
btrfs: Add the capability of getting commit stats in BTRFS
Browse files Browse the repository at this point in the history
First we add  "struct btrfs_commit_stats" data structure under "fs_info"
to store the commit stats for BTRFS that will be exposed through sysfs

The stats exposed are: 1) The number of commits so far, 2) The duration of
the last commit in ms, 3) The maximum commit duration seen so far in ms
and 4) The total duration for all commits so far in ms.

The update of the commit stats occurs after the commit thread has gone
through all the logic that checks if there is another thread committing
at the same time. This means that we only account for actual commit work
in the commit stats we report and not the time the thread spends waiting
until it is ready to do the commit work.

Signed-off-by: Ioannis Angelakopoulos <iangelak@fb.com>
  • Loading branch information
Ioannis Angelakopoulos authored and intel-lab-lkp committed Jun 10, 2022
1 parent 4077c8c commit 8f66d4d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
14 changes: 14 additions & 0 deletions fs/btrfs/ctree.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,18 @@ enum btrfs_exclusive_operation {
BTRFS_EXCLOP_SWAP_ACTIVATE,
};

/* Storing data about btrfs commits. The data are accessible over sysfs */
struct btrfs_commit_stats {
/* Total number of commits */
u64 commit_counter;
/* The maximum commit duration so far*/
u64 max_commit_dur;
/* The last commit duration */
u64 last_commit_dur;
/* The total commit duration */
u64 total_commit_dur;
};

struct btrfs_fs_info {
u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
unsigned long flags;
Expand Down Expand Up @@ -1082,6 +1094,8 @@ struct btrfs_fs_info {
spinlock_t eb_leak_lock;
struct list_head allocated_ebs;
#endif

struct btrfs_commit_stats *commit_stats;
};

static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
Expand Down
6 changes: 6 additions & 0 deletions fs/btrfs/disk-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,7 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
btrfs_free_ref_cache(fs_info);
kfree(fs_info->balance_ctl);
kfree(fs_info->delayed_root);
kfree(fs_info->commit_stats);
free_global_roots(fs_info);
btrfs_put_root(fs_info->tree_root);
btrfs_put_root(fs_info->chunk_root);
Expand Down Expand Up @@ -3174,6 +3175,11 @@ static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block
return -ENOMEM;
btrfs_init_delayed_root(fs_info->delayed_root);

fs_info->commit_stats = kzalloc(sizeof(struct btrfs_commit_stats),
GFP_KERNEL);
if (!fs_info->commit_stats)
return -ENOMEM;

if (sb_rdonly(sb))
set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);

Expand Down
38 changes: 37 additions & 1 deletion fs/btrfs/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <linux/pagemap.h>
#include <linux/blkdev.h>
#include <linux/uuid.h>
#include <linux/timekeeping.h>
#include "misc.h"
#include "ctree.h"
#include "disk-io.h"
Expand Down Expand Up @@ -674,7 +675,7 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
* and then we deadlock with somebody doing a freeze.
*
* If we are ATTACH, it means we just want to catch the current
* transaction and commit it, so we needn't do sb_start_intwrite().
* transaction and commit it, so we needn't do sb_start_intwrite().
*/
if (type & __TRANS_FREEZABLE)
sb_start_intwrite(fs_info->sb);
Expand Down Expand Up @@ -2084,12 +2085,30 @@ static void add_pending_snapshot(struct btrfs_trans_handle *trans)
list_add(&trans->pending_snapshot->list, &cur_trans->pending_snapshots);
}

static void update_commit_stats(struct btrfs_fs_info *fs_info,
ktime_t interval)
{
/* Increase the successful commits counter */
fs_info->commit_stats->commit_counter += 1;
/* Update the last commit duration */
fs_info->commit_stats->last_commit_dur = interval / 1000000;
/* Update the maximum commit duration */
fs_info->commit_stats->max_commit_dur =
fs_info->commit_stats->max_commit_dur > interval / 1000000 ?
fs_info->commit_stats->max_commit_dur : interval / 1000000;
/* Update the total commit duration */
fs_info->commit_stats->total_commit_dur += interval / 1000000;
}

int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
{
struct btrfs_fs_info *fs_info = trans->fs_info;
struct btrfs_transaction *cur_trans = trans->transaction;
struct btrfs_transaction *prev_trans = NULL;
int ret;
ktime_t start_time;
ktime_t end_time;
ktime_t interval;

ASSERT(refcount_read(&trans->use_count) == 1);

Expand Down Expand Up @@ -2214,6 +2233,12 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
}
}

/*
* Get the time spent on the work done by the commit thread and not
* the time spent on a previous commit
*/
start_time = ktime_get_ns();

extwriter_counter_dec(cur_trans, trans->type);

ret = btrfs_start_delalloc_flush(fs_info);
Expand Down Expand Up @@ -2462,6 +2487,17 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)

kmem_cache_free(btrfs_trans_handle_cachep, trans);

end_time = ktime_get_ns();
interval = end_time - start_time;

/*
* Protect the commit stats updates from concurrent updates through
* sysfs.
*/
spin_lock(&fs_info->trans_lock);
update_commit_stats(fs_info, interval);
spin_unlock(&fs_info->trans_lock);

return ret;

unlock_reloc:
Expand Down

0 comments on commit 8f66d4d

Please sign in to comment.