[PATCH v2 2/2] delayacct: add a proc file to dump the delay info

From: brookxu
Date: Mon Apr 19 2021 - 10:37:21 EST


From: Chunguang Xu <brookxu@xxxxxxxxxxx>

Many distributions do not install the getdelay tool by
default, similar to task_io_accounting, adding a proc
file to make access easier.

v2: Fix some errors prompted by the kernel test robot.

Signed-off-by: Chunguang Xu <brookxu@xxxxxxxxxxx>
Reported-by: kernel test robot <lkp@xxxxxxxxx>
---
fs/proc/base.c | 7 +++++++
kernel/delayacct.c | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index b3422cd..4de261a 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -96,6 +96,7 @@
#include <linux/posix-timers.h>
#include <linux/time_namespace.h>
#include <linux/resctrl.h>
+#include <linux/delayacct.h>
#include <trace/events/oom.h>
#include "internal.h"
#include "fd.h"
@@ -3244,6 +3245,9 @@ static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
#ifdef CONFIG_TASK_IO_ACCOUNTING
ONE("io", S_IRUSR, proc_tgid_io_accounting),
#endif
+#ifdef CONFIG_TASK_DELAY_ACCT
+ ONE("delays", S_IRUSR, proc_delayacct_show),
+#endif
#ifdef CONFIG_USER_NS
REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
@@ -3583,6 +3587,9 @@ static int proc_tid_comm_permission(struct inode *inode, int mask)
#ifdef CONFIG_TASK_IO_ACCOUNTING
ONE("io", S_IRUSR, proc_tid_io_accounting),
#endif
+#ifdef CONFIG_TASK_DELAY_ACCT
+ ONE("delays", S_IRUSR, proc_delayacct_show),
+#endif
#ifdef CONFIG_USER_NS
REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
diff --git a/kernel/delayacct.c b/kernel/delayacct.c
index ec580cb..87d091a 100644
--- a/kernel/delayacct.c
+++ b/kernel/delayacct.c
@@ -14,6 +14,7 @@
#include <linux/sysctl.h>
#include <linux/delayacct.h>
#include <linux/module.h>
+#include <linux/seq_file.h>

int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */
EXPORT_SYMBOL_GPL(delayacct_on);
@@ -26,6 +27,18 @@ static int __init delayacct_setup_disable(char *str)
}
__setup("nodelayacct", delayacct_setup_disable);

+struct delayacct_stat {
+ const char *name;
+ unsigned int idx;
+};
+
+static struct delayacct_stat delayacct_stats[] = {
+ {"blkio", DELAYACCT_BLKIO},
+ {"swapin", DELAYACCT_SWAPIN},
+ {"pagecache_thrashing", DELAYACCT_THRASHING},
+ {"mem_reclaim", DELAYACCT_FREEPAGES}
+};
+
void delayacct_init(void)
{
delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC|SLAB_ACCOUNT);
@@ -126,3 +139,31 @@ u64 __delayacct_blkio_ticks(struct task_struct *tsk)
return ret;
}

+#define K(x) ({ u64 _tmp = x; do_div(_tmp, 1000); _tmp; })
+
+int proc_delayacct_show(struct seq_file *m, struct pid_namespace *ns,
+ struct pid *pid, struct task_struct *task)
+{
+ struct delayacct_count *delays;
+ int idx;
+
+ if (!task->delays)
+ return 0;
+
+ delays = task->delays->delays;
+ for (idx = 0; idx < ARRAY_SIZE(delayacct_stats); idx++) {
+ u32 item = delayacct_stats[idx].idx;
+ u64 mean = delays[item].delay;
+
+ if (delays[item].count)
+ do_div(mean, delays[item].count);
+
+ seq_printf(m, "%s %llu %llu %u %llu\n",
+ delayacct_stats[idx].name,
+ K(mean),
+ K(delays[item].max),
+ delays[item].count,
+ K(delays[item].delay));
+ }
+ return 0;
+}
--
1.8.3.1