With config MOUNT_IO_STATS, add an array of counters to struct mnt_pcp that will be used to collect I/O statistics. Signed-off-by: Amir Goldstein <amir73il@xxxxxxxxx> --- Please note that the dependency on SMP is just for the RFC. Thanks, Amir. fs/Kconfig | 9 +++++++++ fs/mount.h | 32 ++++++++++++++++++++++++++++++++ fs/namespace.c | 17 +++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/fs/Kconfig b/fs/Kconfig index aa4c12282301..7473bdf4bbfb 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -15,6 +15,15 @@ config VALIDATE_FS_PARSER Enable this to perform validation of the parameter description for a filesystem when it is registered. +config FS_MOUNT_STATS + bool "Enable per-mount I/O statistics" + depends on SMP + help + Enable this to allow collecting per-mount I/O statistics and display + them in /proc/<pid>/mountstats. + + Say N if unsure. + if BLOCK config FS_IOMAP diff --git a/fs/mount.h b/fs/mount.h index ce6c376e0bc2..2bf0df64ded5 100644 --- a/fs/mount.h +++ b/fs/mount.h @@ -24,9 +24,25 @@ struct mnt_namespace { unsigned int pending_mounts; } __randomize_layout; +/* Similar to task_io_accounting members */ +enum { + MNTIOS_CHARS_RD, /* bytes read via syscalls */ + MNTIOS_CHARS_WR, /* bytes written via syscalls */ + MNTIOS_SYSCALLS_RD, /* # of read syscalls */ + MNTIOS_SYSCALLS_WR, /* # of write syscalls */ + _MNTIOS_COUNTERS_NUM +}; + +struct mnt_iostats { + s64 counter[_MNTIOS_COUNTERS_NUM]; +}; + struct mnt_pcp { int mnt_count; int mnt_writers; +#ifdef CONFIG_FS_MOUNT_STATS + struct mnt_iostats iostats; +#endif }; struct mountpoint { @@ -158,3 +174,19 @@ static inline bool is_anon_ns(struct mnt_namespace *ns) } extern void mnt_cursor_del(struct mnt_namespace *ns, struct mount *cursor); + +static inline void mnt_iostats_counter_inc(struct mount *mnt, int id) +{ +#ifdef CONFIG_FS_MOUNT_STATS + this_cpu_inc(mnt->mnt_pcp->iostats.counter[id]); +#endif +} + +static inline void mnt_iostats_counter_add(struct mount *mnt, int id, s64 n) +{ +#ifdef CONFIG_FS_MOUNT_STATS + this_cpu_add(mnt->mnt_pcp->iostats.counter[id], n); +#endif +} + +extern s64 mnt_iostats_counter_read(struct mount *mnt, int id); diff --git a/fs/namespace.c b/fs/namespace.c index d2db7dfe232b..04b35dfcc71f 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -283,6 +283,23 @@ static unsigned int mnt_get_writers(struct mount *mnt) #endif } +s64 mnt_iostats_counter_read(struct mount *mnt, int id) +{ + s64 count = 0; +#ifdef CONFIG_FS_MOUNT_STATS + /* + * MOUNT_STATS depends on SMP. + * Should be trivial to implement for !SMP if anyone cares... + */ + int cpu; + + for_each_possible_cpu(cpu) { + count += per_cpu_ptr(mnt->mnt_pcp, cpu)->iostats.counter[id]; + } +#endif + return count; +} + static int mnt_is_readonly(struct vfsmount *mnt) { if (mnt->mnt_sb->s_readonly_remount) -- 2.25.1