[PATCH] delayacct: track delays from COW

From: cgel . zte
Date: Tue Mar 22 2022 - 07:06:51 EST


From: Yang Yang <yang.yang29@xxxxxxxxxx>

Delay accounting does not track the delay of COW. When tasks trigger
much COW, it may spend a amount of time waiting for it. To get the
impact of tasks in COW, measure the delay when it happens. This
could help users to do tunnings, such as decide whether to use
ksm or not.

Also update tools/accounting/getdelays.c:

/ # ./getdelays -dl -p 231
print delayacct stats ON
listen forever
PID 231

CPU count real total virtual total delay total delay average
6247 1859000000 2154070021 1674255063 0.268ms
IO count delay total delay average
0 0 0ms
SWAP count delay total delay average
0 0 0ms
RECLAIM count delay total delay average
0 0 0ms
THRASHING count delay total delay average
0 0 0ms
COMPACT count delay total delay average
3 72758 0ms
COW count delay total delay average
3635 271567604 0ms

Signed-off-by: Yang Yang <yang.yang29@xxxxxxxxxx>
Reviewed-by: Ran Xiaokai <ran.xiaokai@xxxxxxxxxx>
---
include/linux/delayacct.h | 28 ++++++++++++++++++++++++++++
include/uapi/linux/taskstats.h | 6 +++++-
kernel/delayacct.c | 16 ++++++++++++++++
mm/memory.c | 8 ++++++++
tools/accounting/getdelays.c | 8 +++++++-
5 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/include/linux/delayacct.h b/include/linux/delayacct.h
index 6b16a6930a19..89b202c695d3 100644
--- a/include/linux/delayacct.h
+++ b/include/linux/delayacct.h
@@ -45,9 +45,13 @@ struct task_delay_info {
u64 compact_start;
u64 compact_delay; /* wait for memory compact */

+ u64 cow_start;
+ u64 cow_delay; /* wait for cow */
+
u32 freepages_count; /* total count of memory reclaim */
u32 thrashing_count; /* total count of thrash waits */
u32 compact_count; /* total count of memory compact */
+ u32 cow_count; /* total count of cow */
};
#endif

@@ -75,6 +79,8 @@ extern void __delayacct_swapin_start(void);
extern void __delayacct_swapin_end(void);
extern void __delayacct_compact_start(void);
extern void __delayacct_compact_end(void);
+extern void __delayacct_cow_start(void);
+extern void __delayacct_cow_end(void);

static inline void delayacct_tsk_init(struct task_struct *tsk)
{
@@ -191,6 +197,24 @@ static inline void delayacct_compact_end(void)
__delayacct_compact_end();
}

+static inline void delayacct_cow_start(void)
+{
+ if (!static_branch_unlikely(&delayacct_key))
+ return;
+
+ if (current->delays)
+ __delayacct_cow_start();
+}
+
+static inline void delayacct_cow_end(void)
+{
+ if (!static_branch_unlikely(&delayacct_key))
+ return;
+
+ if (current->delays)
+ __delayacct_cow_end();
+}
+
#else
static inline void delayacct_init(void)
{}
@@ -225,6 +249,10 @@ static inline void delayacct_compact_start(void)
{}
static inline void delayacct_compact_end(void)
{}
+static inline void delayacct_cow_start(void)
+{}
+static inline void delayacct_cow_end(void)
+{}

#endif /* CONFIG_TASK_DELAY_ACCT */

diff --git a/include/uapi/linux/taskstats.h b/include/uapi/linux/taskstats.h
index 12327d32378f..cfb9a2e0e95a 100644
--- a/include/uapi/linux/taskstats.h
+++ b/include/uapi/linux/taskstats.h
@@ -34,7 +34,7 @@
*/


-#define TASKSTATS_VERSION 11
+#define TASKSTATS_VERSION 12
#define TS_COMM_LEN 32 /* should be >= TASK_COMM_LEN
* in linux/sched.h */

@@ -176,6 +176,10 @@ struct taskstats {
/* Delay waiting for memory compact */
__u64 compact_count;
__u64 compact_delay_total;
+
+ /* Delay waiting for cow */
+ __u64 cow_count;
+ __u64 cow_delay_total;
};


diff --git a/kernel/delayacct.c b/kernel/delayacct.c
index 2c1e18f7c5cf..c9a833270fe0 100644
--- a/kernel/delayacct.c
+++ b/kernel/delayacct.c
@@ -177,11 +177,14 @@ int delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
d->thrashing_delay_total = (tmp < d->thrashing_delay_total) ? 0 : tmp;
tmp = d->compact_delay_total + tsk->delays->compact_delay;
d->compact_delay_total = (tmp < d->compact_delay_total) ? 0 : tmp;
+ tmp = d->cow_delay_total + tsk->delays->cow_delay;
+ d->cow_delay_total = (tmp < d->cow_delay_total) ? 0 : tmp;
d->blkio_count += tsk->delays->blkio_count;
d->swapin_count += tsk->delays->swapin_count;
d->freepages_count += tsk->delays->freepages_count;
d->thrashing_count += tsk->delays->thrashing_count;
d->compact_count += tsk->delays->compact_count;
+ d->cow_count += tsk->delays->cow_count;
raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);

return 0;
@@ -249,3 +252,16 @@ void __delayacct_compact_end(void)
&current->delays->compact_delay,
&current->delays->compact_count);
}
+
+void __delayacct_cow_start(void)
+{
+ current->delays->cow_start = local_clock();
+}
+
+void __delayacct_cow_end(void)
+{
+ delayacct_end(&current->delays->lock,
+ &current->delays->cow_start,
+ &current->delays->cow_delay,
+ &current->delays->cow_count);
+}
diff --git a/mm/memory.c b/mm/memory.c
index 4499cf09c21f..4111f97c91a0 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3006,6 +3006,8 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
int page_copied = 0;
struct mmu_notifier_range range;

+ delayacct_cow_start();
+
if (unlikely(anon_vma_prepare(vma)))
goto oom;

@@ -3030,6 +3032,8 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
put_page(new_page);
if (old_page)
put_page(old_page);
+
+ delayacct_cow_end();
return 0;
}
}
@@ -3128,12 +3132,16 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
free_swap_cache(old_page);
put_page(old_page);
}
+
+ delayacct_cow_end();
return page_copied ? VM_FAULT_WRITE : 0;
oom_free_new:
put_page(new_page);
oom:
if (old_page)
put_page(old_page);
+
+ delayacct_cow_end();
return VM_FAULT_OOM;
}

diff --git a/tools/accounting/getdelays.c b/tools/accounting/getdelays.c
index 11e86739456d..0f6e4831a77b 100644
--- a/tools/accounting/getdelays.c
+++ b/tools/accounting/getdelays.c
@@ -207,6 +207,8 @@ static void print_delayacct(struct taskstats *t)
"THRASHING%12s%15s%15s\n"
" %15llu%15llu%15llums\n"
"COMPACT %12s%15s%15s\n"
+ " %15llu%15llu%15llums\n"
+ "COW %15s%15s%15s\n"
" %15llu%15llu%15llums\n",
"count", "real total", "virtual total",
"delay total", "delay average",
@@ -234,7 +236,11 @@ static void print_delayacct(struct taskstats *t)
"count", "delay total", "delay average",
(unsigned long long)t->compact_count,
(unsigned long long)t->compact_delay_total,
- average_ms(t->compact_delay_total, t->compact_count));
+ average_ms(t->compact_delay_total, t->compact_count),
+ "count", "delay total", "delay average",
+ (unsigned long long)t->cow_count,
+ (unsigned long long)t->cow_delay_total,
+ average_ms(t->cow_delay_total, t->cow_count));
}

static void task_context_switch_counts(struct taskstats *t)
--
2.25.1