[Devel] [PATCH RH8 7/8] /proc/<pid>/vz_latency: Add scheduling stats

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


From: Andrey Ryabinin <aryabinin at virtuozzo.com>

Add schdeuling latncies to /proc/<pid>/vz_latency.
They are the same as alloc latencies - total cumulative
latency, number of schedule events, and latency maximum
in the last 2 minutes.

The sysctl kernel.sched_schedstats must be enabled to
see these stats.

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

Cc: Pavel Borzenkov <Pavel.Borzenkov at acronis.com>
Reviewed-by: Denis V. Lunev <den at openvz.org>

Rebase to vz8:
 - Add new argument to update_sched_lat. It was reworked during previous ports to have only
time delta, which is unsufficient for update_maxlat.

(cherry-picked from 0ed8df6d0d18e079369cd004ff47caf119c86edb)
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 fs/proc/base.c               | 18 ++++++++++++++++--
 include/linux/kstat.h        |  5 +++++
 include/linux/sched.h        |  2 +-
 include/linux/sched/signal.h |  2 +-
 kernel/exit.c                |  6 ++----
 kernel/sched/fair.c          | 13 +++++++++----
 6 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index cf8afe1..4860efc 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -538,13 +538,18 @@ static void lastlat_seq_show(struct seq_file *m,
 	"allocatomic:",
 	"alloc:",
 	"allocmp:",
+	"scheduling:",
 };
 static const int alloc_types[] = {
 	KSTAT_ALLOCSTAT_ATOMIC,
 	KSTAT_ALLOCSTAT_LOW,
 	KSTAT_ALLOCSTAT_LOW_MP,
+	KSTAT_SCHED,
 };
 
+extern struct static_key sched_schedstats;
+# define schedstat_enabled()		static_key_false(&sched_schedstats)
+
 static int proc_tid_vz_lat(struct seq_file *m, struct pid_namespace *ns,
 			struct pid *pid, struct task_struct *task)
 {
@@ -553,9 +558,13 @@ static int proc_tid_vz_lat(struct seq_file *m, struct pid_namespace *ns,
 	seq_printf(m, "%-12s %20s %20s %20s\n",
 			"Type", "Total_lat", "Calls", "Max (2min)");
 
-	for (i = 0; i < ARRAY_SIZE(alloc_types); i++)
+	for (i = 0; i < ARRAY_SIZE(alloc_types); i++) {
+		if (alloc_types[i] == KSTAT_SCHED && !schedstat_enabled())
+			continue;
 		lastlat_seq_show(m, alloc_descr[i],
 				&task->alloc_lat[alloc_types[i]]);
+	}
+
 	return 0;
 }
 
@@ -595,15 +604,20 @@ static int proc_tgid_vz_lat(struct seq_file *m, struct pid_namespace *ns,
 				maxlats[i] = maxlat;
 
 		}
+
 		unlock_task_sighand(task, &flags);
 	}
 
 	seq_printf(m, "%-12s %20s %20s %20s\n",
 			"Type", "Total_lat", "Calls", "Max (2min)");
 
-	for (i = 0; i < ARRAY_SIZE(alloc_types); i++)
+	for (i = 0; i < ARRAY_SIZE(alloc_types); i++) {
+		if (alloc_types[i] == KSTAT_SCHED && !schedstat_enabled())
+			continue;
+
 		seq_printf(m, "%-12s %20Lu %20Lu %20Lu\n", alloc_descr[i],
 			lat[i], count[i], maxlats[i]);
+	}
 
 	return 0;
 }
diff --git a/include/linux/kstat.h b/include/linux/kstat.h
index 4a49337..97a0f9b 100644
--- a/include/linux/kstat.h
+++ b/include/linux/kstat.h
@@ -8,6 +8,8 @@ enum {
 	KSTAT_ALLOCSTAT_LOW_MP,
 	KSTAT_ALLOCSTAT_HIGH_MP,
 	KSTAT_ALLOCSTAT_NR,
+	KSTAT_SCHED = KSTAT_ALLOCSTAT_NR,
+	KSTAT_NR,
 };
 
 struct kstat_perf_snap_struct {
@@ -46,4 +48,7 @@ struct kstat_lat_pcpu_struct {
 	u64 avg[3];
 };
 
+void update_maxlat(struct kstat_lat_snap_struct *alloc_lat,
+				u64 lat, unsigned long time);
+
 #endif
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 72b3f40..f8a0e14 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1126,7 +1126,7 @@ struct task_struct {
 	struct tlbflush_unmap_batch	tlb_ubc;
 
 #ifdef CONFIG_VE
-	struct kstat_lat_snap_struct alloc_lat[KSTAT_ALLOCSTAT_NR];
+	struct kstat_lat_snap_struct alloc_lat[KSTAT_NR];
 #endif
 
 	RH_KABI_REPLACE(struct rcu_head rcu, union {
diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h
index adfe9cc..1f4a0c5 100644
--- a/include/linux/sched/signal.h
+++ b/include/linux/sched/signal.h
@@ -212,7 +212,7 @@ struct signal_struct {
 	struct taskstats *stats;
 #endif
 #ifdef CONFIG_VE
-	struct kstat_lat_snap_struct alloc_lat[KSTAT_ALLOCSTAT_NR];
+	struct kstat_lat_snap_struct alloc_lat[KSTAT_NR];
 #endif
 #ifdef CONFIG_AUDIT
 	unsigned audit_tty;
diff --git a/kernel/exit.c b/kernel/exit.c
index d105044..55c1e50 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -723,16 +723,13 @@ static void check_stack_usage(void)
 static inline void check_stack_usage(void) {}
 #endif
 
-void update_maxlat(struct kstat_lat_snap_struct *alloc_lat,
-				u64 lat, unsigned long time);
-
 void kstat_add_dying(struct task_struct *tsk)
 {
 #ifdef CONFIG_VE
 	int i;
 
 	spin_lock_irq(&tsk->sighand->siglock);
-	for (i = 0; i < KSTAT_ALLOCSTAT_NR; i++) {
+	for (i = 0; i < KSTAT_NR; i++) {
 		int j;
 
 		tsk->signal->alloc_lat[i].totlat += tsk->alloc_lat[i].totlat;
@@ -746,6 +743,7 @@ void kstat_add_dying(struct task_struct *tsk)
 			}
 		}
 	}
+
 	spin_unlock_irq(&tsk->sighand->siglock);
 #endif
 }
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 8f22066..da2e976 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -947,11 +947,15 @@ static void update_curr_fair(struct rq *rq)
 	__schedstat_set(se->statistics.wait_start, wait_start);
 }
 
-static inline void update_sched_lat(struct task_struct *t, u64 delta)
+static inline void update_sched_lat(struct task_struct *t, u64 delta, u64 now)
 {
 #ifdef CONFIG_VE
 	KSTAT_LAT_PCPU_ADD(&kstat_glob.sched_lat, delta);
 	KSTAT_LAT_PCPU_ADD(&t->task_ve->sched_lat_ve, delta);
+
+	t->alloc_lat[KSTAT_SCHED].totlat += delta;
+	t->alloc_lat[KSTAT_SCHED].count++;
+	update_maxlat(&t->alloc_lat[KSTAT_SCHED], delta, now);
 #endif
 }
 
@@ -959,12 +963,13 @@ static inline void update_sched_lat(struct task_struct *t, u64 delta)
 update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
 	struct task_struct *p;
-	u64 delta;
+	u64 delta, now;
 
 	if (!schedstat_enabled())
 		return;
 
-	delta = rq_clock(rq_of(cfs_rq)) - schedstat_val(se->statistics.wait_start);
+	now = rq_clock(rq_of(cfs_rq));
+	delta = now - schedstat_val(se->statistics.wait_start);
 
 	if (entity_is_task(se)) {
 		p = task_of(se);
@@ -978,7 +983,7 @@ static inline void update_sched_lat(struct task_struct *t, u64 delta)
 			return;
 		}
 		trace_sched_stat_wait(p, delta);
-		update_sched_lat(p, delta);
+		update_sched_lat(p, delta, now);
 	}
 
 	__schedstat_set(se->statistics.wait_max,
-- 
1.8.3.1



More information about the Devel mailing list