[PATCH] delayacct: track delays from ksm cow

cgel.zte
2022-03-16T13:35:00Z

From: Yang Yang <yang.yang29@zte.com.cn>

Delay accounting does not track the delay of ksm cow. When tasks
have many ksm pages, it may spend a amount of time waiting for ksm
cow.

To get the impact of tasks in ksm cow, measure the delay when ksm
cow happens. This could help users to decide whether to user 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
KSM count delay total delay average
3635 271567604 0ms

Signed-off-by: Yang Yang <yang.yang29@zte.com.cn>
---
include/linux/delayacct.h | 28 ++++++++++++++++++++++++++++
include/uapi/linux/taskstats.h | 6 +++++-
kernel/delayacct.c | 16 ++++++++++++++++
mm/memory.c | 25 ++++++++++++++++++++++---
tools/accounting/getdelays.c | 8 +++++++-
5 files changed, 78 insertions(+), 5 deletions(-)

diff --git a/include/linux/delayacct.h b/include/linux/delayacct.h
index 6b16a6930a19..0fbe2cb25c23 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 ksm_start;
+ u64 ksm_delay; /* wait for ksm 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 ksm_count; /* total count of ksm 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_ksm_start(void);
+extern void __delayacct_ksm_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_ksm_start(void)
+{
+ if (!static_branch_unlikely(&delayacct_key))
+ return;
+
+ if (current->delays)
+ __delayacct_ksm_start();
+}
+
+static inline void delayacct_ksm_end(void)
+{
+ if (!static_branch_unlikely(&delayacct_key))
+ return;
+
+ if (current->delays)
+ __delayacct_ksm_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_ksm_start(void)
+{}
+static inline void delayacct_ksm_end(void)
+{}

#endif /* CONFIG_TASK_DELAY_ACCT */

diff --git a/include/uapi/linux/taskstats.h b/include/uapi/linux/taskstats.h
index 12327d32378f..a851c032dfb8 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 ksm cow */
+ __u64 ksm_count;
+ __u64 ksm_delay_total;
};

diff --git a/kernel/delayacct.c b/kernel/delayacct.c
index 2c1e18f7c5cf..11accef0c2bd 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->ksm_delay_total + tsk->delays->ksm_delay;
+ d->ksm_delay_total = (tmp < d->ksm_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->ksm_count += tsk->delays->ksm_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_ksm_start(void)
+{
+ current->delays->ksm_start = local_clock();
+}
+
+void __delayacct_ksm_end(void)
+{
+ delayacct_end(&current->delays->lock,
+ &current->delays->ksm_start,
+ &current->delays->ksm_delay,
+ &current->delays->ksm_count);
+}
diff --git a/mm/memory.c b/mm/memory.c
index 4499cf09c21f..23dfc3b24d28 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3249,6 +3249,8 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)
__releases(vmf->ptl)
{
struct vm_area_struct *vma = vmf->vma;
+ vm_fault_t ret = 0;
+ bool delayacct = false;

if (userfaultfd_pte_wp(vma, *vmf->pte)) {
pte_unmap_unlock(vmf->pte, vmf->ptl);
@@ -3294,7 +3296,11 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)
*
* PageKsm() doesn't necessarily raise the page refcount.
*/
- if (PageKsm(page) || page_count(page) > 3)
+ if (PageKsm(page)) {
+ delayacct = true;
+ goto copy;
+ }
+ if (page_count(page) > 3)
goto copy;
if (!PageLRU(page))
/*
@@ -3308,7 +3314,12 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)
goto copy;
if (PageSwapCache(page))
try_to_free_swap(page);
- if (PageKsm(page) || page_count(page) != 1) {
+ if (PageKsm(page)) {
+ delayacct = true;
+ unlock_page(page);
+ goto copy;
+ }
+ if (page_count(page) != 1) {
unlock_page(page);
goto copy;
}
@@ -3328,10 +3339,18 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)
/*
* Ok, we need to copy. Oh, well..
*/
+ if (delayacct)
+ delayacct_ksm_start();
+
get_page(vmf->page);

pte_unmap_unlock(vmf->pte, vmf->ptl);
- return wp_page_copy(vmf);
+ ret = wp_page_copy(vmf);
+
+ if (delayacct)
+ delayacct_ksm_end();
+
+ return ret;
}

static void unmap_mapping_range_vma(struct vm_area_struct *vma,
diff --git a/tools/accounting/getdelays.c b/tools/accounting/getdelays.c
index 11e86739456d..3e77c9ff7fcf 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"
+ "KSM %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->ksm_count,
+ (unsigned long long)t->ksm_delay_total,
+ average_ms(t->ksm_delay_total, t->ksm_count));
}

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