[Devel] [PATCH RH8 2/8] ve/page_alloc, kstat: account allocation latencies per-task

Andrey Zhadchenko andrey.zhadchenko at virtuozzo.com
Tue May 18 20:48:26 MSK 2021


From: Andrey Ryabinin <aryabinin at virtuozzo.com>

Vstorage wants per-process allocation latencies:

 - total accumulated latency (total time spent inside the kernel allocator)
 - total alloc attempts (so that average latency can be calculated)

This adds /proc/<pid>/vz_latency file which outputs the numbers:

Type                   Total_lat                Calls
allocatomic:                    0                 1334
alloc:                    8000000                36643
allocmp:                        0                  919

https://jira.sw.ru/browse/PSBM-81395
Signed-off-by: Andrey Ryabinin <aryabinin at virtuozzo.com>

Cc: Pavel Borzenkov <pborzenkov at virtuozzo.com>

(cherry-picked from 6d9a9210395e1f7573f5da6be18a3362f52eea8c)
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 fs/proc/base.c        | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/sched.h |  5 +++++
 mm/page_alloc.c       |  4 ++++
 3 files changed, 60 insertions(+)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index f931b1c..c8dc2e9 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -56,6 +56,7 @@
 #include <linux/stat.h>
 #include <linux/task_io_accounting_ops.h>
 #include <linux/init.h>
+#include <linux/kstat.h>
 #include <linux/capability.h>
 #include <linux/file.h>
 #include <linux/fdtable.h>
@@ -524,6 +525,53 @@ static ssize_t lstats_write(struct file *file, const char __user *buf,
 
 #endif
 
+#ifdef CONFIG_VE
+static void lastlat_seq_show(struct seq_file *m,
+		const char *name,
+		struct kstat_lat_snap_struct *snap)
+{
+	seq_printf(m, "%-12s %20Lu %20lu\n", name,
+			snap->totlat, snap->count);
+}
+
+static int vz_lat_show_proc(struct seq_file *m, void *v)
+{
+	int i;
+	struct inode *inode = m->private;
+	struct task_struct *task = get_proc_task(inode);
+	static const char *alloc_descr[] = {
+		"allocatomic:",
+		"alloc:",
+		"allocmp:",
+	};
+	static const int alloc_types[] = {
+		KSTAT_ALLOCSTAT_ATOMIC,
+		KSTAT_ALLOCSTAT_LOW,
+		KSTAT_ALLOCSTAT_LOW_MP,
+	};
+
+	seq_printf(m, "%-11s %20s %20s\n",
+			"Type", "Total_lat", "Calls");
+
+	for (i = 0; i < ARRAY_SIZE(alloc_types); i++)
+		lastlat_seq_show(m, alloc_descr[i],
+				&task->alloc_lat[alloc_types[i]]);
+	return 0;
+}
+
+static int vz_lat_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, vz_lat_show_proc, inode);
+}
+
+static const struct file_operations proc_vz_lat_operations = {
+	.open		= vz_lat_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= single_release,
+};
+#endif
+
 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
 			  struct pid *pid, struct task_struct *task)
 {
@@ -3048,6 +3096,9 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
 #ifdef CONFIG_LIVEPATCH
 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
 #endif
+#ifdef CONFIG_VE
+	REG("vz_latency", S_IRUGO, proc_vz_lat_operations),
+#endif
 };
 
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 76cc397..72b3f40 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -32,6 +32,7 @@
 #include <linux/posix-timers.h>
 #include <linux/rseq.h>
 #include <linux/rh_kabi.h>
+#include <linux/kstat.h>
 
 /* task_struct member predeclarations (sorted alphabetically): */
 struct audit_context;
@@ -1124,6 +1125,10 @@ struct task_struct {
 
 	struct tlbflush_unmap_batch	tlb_ubc;
 
+#ifdef CONFIG_VE
+	struct kstat_lat_snap_struct alloc_lat[KSTAT_ALLOCSTAT_NR];
+#endif
+
 	RH_KABI_REPLACE(struct rcu_head rcu, union {
 		refcount_t		rcu_users;
 		struct rcu_head		rcu;
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 2f5c6d1..2d8365e 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -4536,6 +4536,10 @@ static void __alloc_collect_stats(gfp_t gfp_mask, unsigned int order,
 	local_irq_save(flags);
 	cpu = smp_processor_id();
 	KSTAT_LAT_PCPU_ADD(&kstat_glob.alloc_lat[ind], time);
+
+	current->alloc_lat[ind].totlat += time;
+	current->alloc_lat[ind].count++;
+
 	if (!page)
 		kstat_glob.alloc_fails[cpu][ind]++;
 	local_irq_restore(flags);
-- 
1.8.3.1



More information about the Devel mailing list