[PATCH 2/2] mm/sched: memdelay: memory health interface for systems and workloads
From: Johannes Weiner
Date: Wed Sep 27 2017 - 13:01:41 EST
Linux doesn't have a useful metric to describe the memory health of a
system, a cgroup container, or individual tasks.
When workloads are bigger than available memory, they spend a certain
amount of their time inside page reclaim, waiting on thrashing cache,
and swapping in. This has impact on latency, and depending on the CPU
capacity in the system can also translate to a decrease in throughput.
While Linux exports some stats and counters for these events, it does
not quantify the true impact they have on throughput and latency. How
much of the execution time is spent unproductively? This is important
to know when sizing workloads to systems and containers. It also comes
in handy when evaluating the effectiveness and efficiency of the
kernel's memory management policies and heuristics.
This patch implements a metric that quantifies memory pressure in a
unit that matters most to applications and does not rely on hardware
aspects to be meaningful: wallclock time lost while waiting on memory.
Whenever a task is blocked on refaults, swapins, or direct reclaim,
the time it spends is accounted on the task level and aggregated into
a domain state along with other tasks on the system and cgroup level.
Each task has a /proc/<pid>/memdelay file that lists the microseconds
the task has been delayed since it's been forked. That file can be
sampled periodically for recent delays, or before and after certain
operations to measure their memory-related latencies.
On the system and cgroup-level, there are /proc/memdelay and
memory.memdelay, respectively, and their format is as such:
$ cat /proc/memdelay
2489084
41.61 47.28 29.66
0.00 0.00 0.00
The first line shows the cumulative delay times of all tasks in the
domain - in this case, all tasks in the system cumulatively lost 2.49
seconds due to memory delays.
The second and third line show percentages spent in aggregate states
for the domain - system or cgroup - in a load average type format as
decaying averages over the last 1m, 5m, and 15m:
The second line indicates the share of wall-time the domain spends in
a state where SOME tasks are delayed by memory while others are still
productive (runnable or iowait). This indicates a latency problem for
individual tasks, but since the CPU/IO capacity is still used, adding
more memory might not necessarily improve the domain's throughput.
The third line indicates the share of wall-time the domain spends in a
state where ALL non-idle tasks are delayed by memory. In this state,
the domain is entirely unproductive due to a lack of memory.
v2:
- fix active-delay condition when only other runnables, no iowait
- drop private lock from sched path, we can use the rq lock
- fix refault vs. simple lockwait detection
- drop ktime, we can use cpu_clock()
XXX:
- eliminate redundant cgroup hierarchy walks in the scheduler
Signed-off-by: Johannes Weiner <hannes@xxxxxxxxxxx>
Conflicts:
include/linux/sched.h
kernel/cgroup/cgroup.c
kernel/sched/Makefile
kernel/sched/core.c
mm/filemap.c
mm/page_alloc.c
mm/vmscan.c
Change-Id: I48f7910eb5d6a029fbb884b64deb8d90ec12ddcb
---
fs/proc/array.c | 8 ++
fs/proc/base.c | 2 +
fs/proc/internal.h | 2 +
include/linux/memcontrol.h | 14 +++
include/linux/memdelay.h | 182 +++++++++++++++++++++++++++++
include/linux/sched.h | 8 ++
kernel/cgroup.c | 3 +-
kernel/fork.c | 4 +
kernel/sched/Makefile | 2 +-
kernel/sched/core.c | 27 +++++
kernel/sched/memdelay.c | 116 ++++++++++++++++++
mm/Makefile | 2 +-
mm/compaction.c | 4 +
mm/filemap.c | 40 ++++++-
mm/memcontrol.c | 25 ++++
mm/memdelay.c | 286 +++++++++++++++++++++++++++++++++++++++++++++
mm/page_alloc.c | 7 ++
mm/vmscan.c | 9 ++
18 files changed, 736 insertions(+), 5 deletions(-)
create mode 100644 include/linux/memdelay.h
create mode 100644 kernel/sched/memdelay.c
create mode 100644 mm/memdelay.c
diff --git a/fs/proc/array.c b/fs/proc/array.c
index 81818ad..1725b51 100644
--- a/fs/proc/array.c
+++ b/fs/proc/array.c
@@ -606,6 +606,14 @@ int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
return 0;
}
+int proc_pid_memdelay(struct seq_file *m, struct pid_namespace *ns,
+ struct pid *pid, struct task_struct *task)
+{
+ seq_put_decimal_ull(m, "", task->memdelay_total);
+ seq_putc(m, '\n');
+ return 0;
+}
+
#ifdef CONFIG_PROC_CHILDREN
static struct pid *
get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 0ccab57..e7f9bcc 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -3004,6 +3004,7 @@ static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
ONE("stat", S_IRUGO, proc_tgid_stat),
ONE("statm", S_IRUGO, proc_pid_statm),
+ ONE("memdelay", S_IRUGO, proc_pid_memdelay),
REG("maps", S_IRUGO, proc_pid_maps_operations),
#ifdef CONFIG_NUMA
REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
@@ -3397,6 +3398,7 @@ static int proc_tid_comm_permission(struct inode *inode, int mask)
REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
ONE("stat", S_IRUGO, proc_tid_stat),
ONE("statm", S_IRUGO, proc_pid_statm),
+ ONE("memdelay", S_IRUGO, proc_pid_memdelay),
REG("maps", S_IRUGO, proc_tid_maps_operations),
#ifdef CONFIG_PROC_CHILDREN
REG("children", S_IRUGO, proc_tid_children_operations),
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 5378441..c5bbbee 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -155,6 +155,8 @@ extern int proc_pid_status(struct seq_file *, struct pid_namespace *,
struct pid *, struct task_struct *);
extern int proc_pid_statm(struct seq_file *, struct pid_namespace *,
struct pid *, struct task_struct *);
+extern int proc_pid_memdelay(struct seq_file *, struct pid_namespace *,
+ struct pid *, struct task_struct *);
/*
* base.c
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 8b35bdb..239d3ad 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -29,6 +29,7 @@
#include <linux/mmzone.h>
#include <linux/writeback.h>
#include <linux/page-flags.h>
+#include <linux/memdelay.h>
struct mem_cgroup;
struct page;
@@ -194,6 +195,9 @@ struct mem_cgroup {
unsigned long soft_limit;
+ /* Memory delay measurement domain */
+ struct memdelay_domain *memdelay_domain;
+
/* vmpressure notifications */
struct vmpressure vmpressure;
@@ -632,6 +636,11 @@ static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
return &pgdat->lruvec;
}
+static inline struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
+{
+ return NULL;
+}
+
static inline bool mm_match_cgroup(struct mm_struct *mm,
struct mem_cgroup *memcg)
{
@@ -644,6 +653,11 @@ static inline bool task_in_mem_cgroup(struct task_struct *task,
return true;
}
+static inline struct mem_cgroup *mem_cgroup_from_task(struct task_struct *task)
+{
+ return NULL;
+}
+
static inline struct mem_cgroup *
mem_cgroup_iter(struct mem_cgroup *root,
struct mem_cgroup *prev,
diff --git a/include/linux/memdelay.h b/include/linux/memdelay.h
new file mode 100644
index 0000000..08ed4e4
--- /dev/null
+++ b/include/linux/memdelay.h
@@ -0,0 +1,182 @@
+#ifndef _LINUX_MEMDELAY_H
+#define _LINUX_MEMDELAY_H
+
+#include <linux/spinlock_types.h>
+#include <linux/sched.h>
+
+struct seq_file;
+struct css_set;
+
+/*
+ * Task productivity states tracked by the scheduler
+ */
+enum memdelay_task_state {
+ MTS_NONE, /* Idle/unqueued/untracked */
+ MTS_IOWAIT, /* Waiting for IO, not memory delayed */
+ MTS_RUNNABLE, /* On the runqueue, not memory delayed */
+ MTS_DELAYED, /* Memory delayed, not running */
+ MTS_DELAYED_ACTIVE, /* Memory delayed, actively running */
+ NR_MEMDELAY_TASK_STATES,
+};
+
+/*
+ * System/cgroup delay state tracked by the VM, composed of the
+ * productivity states of all tasks inside the domain.
+ */
+enum memdelay_domain_state {
+ MDS_NONE, /* No delayed tasks */
+ MDS_SOME, /* Delayed tasks, working tasks */
+ MDS_FULL, /* Delayed tasks, no working tasks */
+ NR_MEMDELAY_DOMAIN_STATES,
+};
+
+struct memdelay_domain_cpu {
+ /* Task states of the domain on this CPU */
+ int tasks[NR_MEMDELAY_TASK_STATES];
+
+ /* Delay state of the domain on this CPU */
+ enum memdelay_domain_state state;
+
+ /* Time of last state change */
+ u64 state_start;
+};
+
+struct memdelay_domain {
+ /* Aggregate delayed time of all domain tasks */
+ unsigned long aggregate;
+
+ /* Per-CPU delay states in the domain */
+ struct memdelay_domain_cpu __percpu *mdcs;
+
+ /* Cumulative state times from all CPUs */
+ unsigned long times[NR_MEMDELAY_DOMAIN_STATES];
+
+ /* Decaying state time averages over 1m, 5m, 15m */
+ unsigned long period_expires;
+ unsigned long avg_full[3];
+ unsigned long avg_some[3];
+};
+
+/* mm/memdelay.c */
+extern struct memdelay_domain memdelay_global_domain;
+void memdelay_init(void);
+void memdelay_task_change(struct task_struct *task,
+ enum memdelay_task_state old,
+ enum memdelay_task_state new);
+struct memdelay_domain *memdelay_domain_alloc(void);
+void memdelay_domain_free(struct memdelay_domain *md);
+int memdelay_domain_show(struct seq_file *s, struct memdelay_domain *md);
+
+/* kernel/sched/memdelay.c */
+void memdelay_enter(unsigned long *flags);
+void memdelay_leave(unsigned long *flags);
+
+/**
+ * memdelay_schedule - note a context switch
+ * @prev: task scheduling out
+ * @next: task scheduling in
+ *
+ * A task switch doesn't affect the balance between delayed and
+ * productive tasks, but we have to update whether the delay is
+ * actively using the CPU or not.
+ */
+static inline void memdelay_schedule(struct task_struct *prev,
+ struct task_struct *next)
+{
+ if (prev->flags & PF_MEMDELAY)
+ memdelay_task_change(prev, MTS_DELAYED_ACTIVE, MTS_DELAYED);
+
+ if (next->flags & PF_MEMDELAY)
+ memdelay_task_change(next, MTS_DELAYED, MTS_DELAYED_ACTIVE);
+}
+
+/**
+ * memdelay_wakeup - note a task waking up
+ * @task: the task
+ *
+ * Notes an idle task becoming productive. Delayed tasks remain
+ * delayed even when they become runnable.
+ */
+static inline void memdelay_wakeup(struct task_struct *task)
+{
+ if (task->flags & PF_MEMDELAY)
+ return;
+
+ if (task->in_iowait)
+ memdelay_task_change(task, MTS_IOWAIT, MTS_RUNNABLE);
+ else
+ memdelay_task_change(task, MTS_NONE, MTS_RUNNABLE);
+}
+
+/**
+ * memdelay_wakeup - note a task going to sleep
+ * @task: the task
+ *
+ * Notes a working tasks becoming unproductive. Delayed tasks remain
+ * delayed.
+ */
+static inline void memdelay_sleep(struct task_struct *task)
+{
+ if (task->flags & PF_MEMDELAY)
+ return;
+
+ if (task->in_iowait)
+ memdelay_task_change(task, MTS_RUNNABLE, MTS_IOWAIT);
+ else
+ memdelay_task_change(task, MTS_RUNNABLE, MTS_NONE);
+}
+
+/**
+ * memdelay_del_add - track task movement between runqueues
+ * @task: the task
+ * @runnable: a runnable task is moved if %true, unqueued otherwise
+ * @add: task is being added if %true, removed otherwise
+ *
+ * Update the memdelay domain per-cpu states as tasks are being moved
+ * around the runqueues.
+ */
+static inline void memdelay_del_add(struct task_struct *task,
+ bool runnable, bool add)
+{
+ int state;
+
+ if (task->flags & PF_MEMDELAY)
+ state = MTS_DELAYED;
+ else if (runnable)
+ state = MTS_RUNNABLE;
+ else if (task->in_iowait)
+ state = MTS_IOWAIT;
+ else
+ return; /* already MTS_NONE */
+
+ if (add)
+ memdelay_task_change(task, MTS_NONE, state);
+ else
+ memdelay_task_change(task, state, MTS_NONE);
+}
+
+static inline void memdelay_del_runnable(struct task_struct *task)
+{
+ memdelay_del_add(task, true, false);
+}
+
+static inline void memdelay_add_runnable(struct task_struct *task)
+{
+ memdelay_del_add(task, true, true);
+}
+
+static inline void memdelay_del_sleeping(struct task_struct *task)
+{
+ memdelay_del_add(task, false, false);
+}
+
+static inline void memdelay_add_sleeping(struct task_struct *task)
+{
+ memdelay_del_add(task, false, true);
+}
+
+#ifdef CONFIG_CGROUPS
+void cgroup_move_task(struct task_struct *task, struct css_set *to);
+#endif
+
+#endif /* _LINUX_MEMDELAY_H */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 9fcfa40..072c244 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1589,6 +1589,7 @@ struct task_struct {
/* disallow userland-initiated cgroup migration */
unsigned no_cgroup_migration:1;
#endif
+ unsigned memdelay_migrate_enqueue:1;
unsigned long atomic_flags; /* Flags needing atomic access. */
@@ -1783,6 +1784,12 @@ struct task_struct {
struct io_context *io_context;
+ u64 memdelay_start;
+ unsigned long memdelay_total;
+#ifdef CONFIG_DEBUG_VM
+ int memdelay_state;
+#endif
+
unsigned long ptrace_message;
siginfo_t *last_siginfo; /* For ptrace use. */
struct task_io_accounting ioac;
@@ -2295,6 +2302,7 @@ static inline cputime_t task_gtime(struct task_struct *t)
#define PF_KTHREAD 0x00200000 /* I am a kernel thread */
#define PF_RANDOMIZE 0x00400000 /* randomize virtual address space */
#define PF_SWAPWRITE 0x00800000 /* Allowed to write to swap */
+#define PF_MEMDELAY 0x01000000 /* Delayed due to lack of memory */
#define PF_NO_SETAFFINITY 0x04000000 /* Userland is not allowed to meddle with cpus_allowed */
#define PF_MCE_EARLY 0x08000000 /* Early kill for mce process policy */
#define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index a3d2aad..d427077 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -781,7 +781,8 @@ static void css_set_move_task(struct task_struct *task,
*/
WARN_ON_ONCE(task->flags & PF_EXITING);
- rcu_assign_pointer(task->cgroups, to_cset);
+ cgroup_move_task(task, to_cset);
+
list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
&to_cset->tasks);
}
diff --git a/kernel/fork.c b/kernel/fork.c
index 98897eb..4535bf5 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1159,6 +1159,10 @@ static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
int retval;
tsk->min_flt = tsk->maj_flt = 0;
+ tsk->memdelay_total = 0;
+#ifdef CONFIG_DEBUG_VM
+ tsk->memdelay_state = 0;
+#endif
tsk->nvcsw = tsk->nivcsw = 0;
#ifdef CONFIG_DETECT_HUNG_TASK
tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 5e59b83..8d405e9 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -17,7 +17,7 @@ endif
obj-y += core.o loadavg.o clock.o cputime.o
obj-y += idle_task.o fair.o rt.o deadline.o stop_task.o
-obj-y += wait.o swait.o completion.o idle.o
+obj-y += wait.o swait.o completion.o idle.o memdelay.o
obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o
obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
obj-$(CONFIG_SCHEDSTATS) += stats.o
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index d63376e..d3e6b06 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -60,6 +60,7 @@
#include <linux/sysctl.h>
#include <linux/syscalls.h>
#include <linux/times.h>
+#include <linux/memdelay.h>
#include <linux/tsacct_kern.h>
#include <linux/kprobes.h>
#include <linux/delayacct.h>
@@ -757,6 +758,14 @@ static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
update_rq_clock(rq);
if (!(flags & ENQUEUE_RESTORE))
sched_info_queued(rq, p);
+ WARN_ON_ONCE(!(flags & ENQUEUE_WAKEUP) && p->memdelay_migrate_enqueue);
+ if (!(flags & ENQUEUE_WAKEUP) || p->memdelay_migrate_enqueue) {
+ memdelay_add_runnable(p);
+ p->memdelay_migrate_enqueue = 0;
+ } else {
+ memdelay_wakeup(p);
+ }
+
p->sched_class->enqueue_task(rq, p, flags);
}
@@ -765,6 +774,11 @@ static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
update_rq_clock(rq);
if (!(flags & DEQUEUE_SAVE))
sched_info_dequeued(rq, p);
+ if (!(flags & DEQUEUE_SLEEP))
+ memdelay_del_runnable(p);
+ else
+ memdelay_sleep(p);
+
p->sched_class->dequeue_task(rq, p, flags);
}
@@ -2087,7 +2101,16 @@ static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
if (task_cpu(p) != cpu) {
+ struct rq_flags rf;
+ struct rq *rq;
+
wake_flags |= WF_MIGRATED;
+
+ rq = __task_rq_lock(p, &rf);
+ memdelay_del_sleeping(p);
+ __task_rq_unlock(rq, &rf);
+ p->memdelay_migrate_enqueue = 1;
+
set_task_cpu(p, cpu);
}
#endif /* CONFIG_SMP */
@@ -3398,6 +3421,8 @@ static void __sched notrace __schedule(bool preempt)
rq->curr = next;
++*switch_count;
+ memdelay_schedule(prev, next);
+
trace_sched_switch(preempt, prev, next);
rq = context_switch(rq, prev, next, cookie); /* unlocks the rq */
} else {
@@ -7692,6 +7717,8 @@ void __init sched_init(void)
init_schedstats();
+ memdelay_init();
+
scheduler_running = 1;
}
diff --git a/kernel/sched/memdelay.c b/kernel/sched/memdelay.c
new file mode 100644
index 0000000..2cc274b
--- /dev/null
+++ b/kernel/sched/memdelay.c
@@ -0,0 +1,116 @@
+/*
+ * Memory delay metric
+ *
+ * Copyright (c) 2017 Facebook, Johannes Weiner
+ *
+ * This code quantifies and reports to userspace the wall-time impact
+ * of memory pressure on the system and memory-controlled cgroups.
+ */
+
+#include <linux/memdelay.h>
+#include <linux/cgroup.h>
+#include <linux/sched.h>
+
+#include "sched.h"
+
+/**
+ * memdelay_enter - mark the beginning of a memory delay section
+ * @flags: flags to handle nested memdelay sections
+ *
+ * Marks the calling task as being delayed due to a lack of memory,
+ * such as waiting for a workingset refault or performing reclaim.
+ */
+void memdelay_enter(unsigned long *flags)
+{
+ struct rq *rq;
+
+ *flags = current->flags & PF_MEMDELAY;
+ if (*flags)
+ return;
+ /*
+ * PF_MEMDELAY & accounting needs to be atomic wrt changes to
+ * the task's scheduling state and its domain association.
+ * Otherwise we could race with CPU or cgroup migration and
+ * misaccount.
+ */
+ local_irq_disable();
+ rq = this_rq();
+ raw_spin_lock(&rq->lock);
+
+ current->flags |= PF_MEMDELAY;
+ memdelay_task_change(current, MTS_RUNNABLE, MTS_DELAYED_ACTIVE);
+
+ raw_spin_unlock(&rq->lock);
+ local_irq_enable();
+}
+
+/**
+ * memdelay_leave - mark the end of a memory delay section
+ * @flags: flags to handle nested memdelay sections
+ *
+ * Marks the calling task as no longer delayed due to memory.
+ */
+void memdelay_leave(unsigned long *flags)
+{
+ struct rq *rq;
+
+ if (*flags)
+ return;
+ /*
+ * PF_MEMDELAY & accounting needs to be atomic wrt changes to
+ * the task's scheduling state and its domain association.
+ * Otherwise we could race with CPU or cgroup migration and
+ * misaccount.
+ */
+ local_irq_disable();
+ rq = this_rq();
+ raw_spin_lock(&rq->lock);
+
+ current->flags &= ~PF_MEMDELAY;
+ memdelay_task_change(current, MTS_DELAYED_ACTIVE, MTS_RUNNABLE);
+
+ raw_spin_unlock(&rq->lock);
+ local_irq_enable();
+}
+
+#ifdef CONFIG_CGROUPS
+/**
+ * cgroup_move_task - move task to a different cgroup
+ * @task: the task
+ * @to: the target css_set
+ *
+ * Move task to a new cgroup and safely migrate its associated
+ * delayed/working state between the different domains.
+ *
+ * This function acquires the task's rq lock to lock out concurrent
+ * changes to the task's scheduling state and - in case the task is
+ * running - concurrent changes to its delay state.
+ */
+void cgroup_move_task(struct task_struct *task, struct css_set *to)
+{
+ struct rq_flags rf;
+ struct rq *rq;
+ int state;
+
+ rq = task_rq_lock(task, &rf);
+
+ if (task->flags & PF_MEMDELAY)
+ state = MTS_DELAYED + task_current(rq, task);
+ else if (task_on_rq_queued(task))
+ state = MTS_RUNNABLE;
+ else if (task->in_iowait)
+ state = MTS_IOWAIT;
+ else
+ state = MTS_NONE;
+
+ /*
+ * Lame to do this here, but the scheduler cannot be locked
+ * from the outside, so we move cgroups from inside sched/.
+ */
+ memdelay_task_change(task, state, MTS_NONE);
+ rcu_assign_pointer(task->cgroups, to);
+ memdelay_task_change(task, MTS_NONE, state);
+
+ task_rq_unlock(rq, task, &rf);
+}
+#endif /* CONFIG_CGROUPS */
diff --git a/mm/Makefile b/mm/Makefile
index 4d9c99b..c00ab42 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -37,7 +37,7 @@ obj-y := filemap.o mempool.o oom_kill.o \
mm_init.o mmu_context.o percpu.o slab_common.o \
compaction.o vmacache.o \
interval_tree.o list_lru.o workingset.o \
- debug.o $(mmu-y)
+ memdelay.o debug.o $(mmu-y)
obj-y += init-mm.o
diff --git a/mm/compaction.c b/mm/compaction.c
index 70e6bec..e00941f 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -1973,11 +1973,15 @@ static int kcompactd(void *p)
pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
while (!kthread_should_stop()) {
+ unsigned long mdflags;
+
trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
wait_event_freezable(pgdat->kcompactd_wait,
kcompactd_work_requested(pgdat));
+ memdelay_enter(&mdflags);
kcompactd_do_work(pgdat);
+ memdelay_leave(&mdflags);
}
return 0;
diff --git a/mm/filemap.c b/mm/filemap.c
index 59fe831..aa98d3f 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -794,34 +794,70 @@ wait_queue_head_t *page_waitqueue(struct page *page)
void wait_on_page_bit(struct page *page, int bit_nr)
{
DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
+ unsigned long mdflags;
+ bool refault = false;
+
+ if (bit_nr == PG_locked && !PageUptodate(page) && PageWorkingset(page)) {
+ memdelay_enter(&mdflags);
+ refault = true;
+ }
if (test_bit(bit_nr, &page->flags))
__wait_on_bit(page_waitqueue(page), &wait, bit_wait_io,
TASK_UNINTERRUPTIBLE);
+ if (refault)
+ memdelay_leave(&mdflags);
}
EXPORT_SYMBOL(wait_on_page_bit);
int wait_on_page_bit_killable(struct page *page, int bit_nr)
{
DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
+ int res;
+ unsigned long mdflags;
+ bool refault = false;
+
+ if (bit_nr == PG_locked && !PageUptodate(page) && PageWorkingset(page)) {
+ memdelay_enter(&mdflags);
+ refault = true;
+ }
if (!test_bit(bit_nr, &page->flags))
return 0;
- return __wait_on_bit(page_waitqueue(page), &wait,
+ res = __wait_on_bit(page_waitqueue(page), &wait,
bit_wait_io, TASK_KILLABLE);
+
+ if (refault)
+ memdelay_leave(&mdflags);
+
+ return res;
}
int wait_on_page_bit_killable_timeout(struct page *page,
int bit_nr, unsigned long timeout)
{
DEFINE_WAIT_BIT(wait, &page->flags, bit_nr);
+ int res;
+ unsigned long mdflags;
+ bool refault = false;
+
+ if (bit_nr == PG_locked && !PageUptodate(page) && PageWorkingset(page)) {
+ memdelay_enter(&mdflags);
+ refault = true;
+ }
wait.key.timeout = jiffies + timeout;
if (!test_bit(bit_nr, &page->flags))
return 0;
- return __wait_on_bit(page_waitqueue(page), &wait,
+ res = __wait_on_bit(page_waitqueue(page), &wait,
bit_wait_io_timeout, TASK_KILLABLE);
+
+ if (refault)
+ memdelay_leave(&mdflags);
+
+ return res;
+
}
EXPORT_SYMBOL_GPL(wait_on_page_bit_killable_timeout);
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index d636bd6..ee4617b 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -63,6 +63,7 @@
#include <linux/lockdep.h>
#include <linux/file.h>
#include <linux/tracehook.h>
+#include <linux/memdelay.h>
#include "internal.h"
#include <net/sock.h>
#include <net/ip.h>
@@ -3912,6 +3913,8 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
return ret;
}
+static int memory_memdelay_show(struct seq_file *m, void *v);
+
static struct cftype mem_cgroup_legacy_files[] = {
{
.name = "usage_in_bytes",
@@ -3979,6 +3982,10 @@ static ssize_t memcg_write_event_control(struct kernfs_open_file *of,
{
.name = "pressure_level",
},
+ {
+ .name = "memdelay",
+ .seq_show = memory_memdelay_show,
+ },
#ifdef CONFIG_NUMA
{
.name = "numa_stat",
@@ -4147,6 +4154,7 @@ static void __mem_cgroup_free(struct mem_cgroup *memcg)
for_each_node(node)
free_mem_cgroup_per_node_info(memcg, node);
+ memdelay_domain_free(memcg->memdelay_domain);
free_percpu(memcg->stat);
kfree(memcg);
}
@@ -4252,10 +4260,15 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
/* The following stuff does not apply to the root */
if (!parent) {
+ memcg->memdelay_domain = &memdelay_global_domain;
root_mem_cgroup = memcg;
return &memcg->css;
}
+ memcg->memdelay_domain = memdelay_domain_alloc();
+ if (!memcg->memdelay_domain)
+ goto fail;
+
error = memcg_online_kmem(memcg);
if (error)
goto fail;
@@ -5245,6 +5258,13 @@ static int memory_stat_show(struct seq_file *m, void *v)
return 0;
}
+static int memory_memdelay_show(struct seq_file *m, void *v)
+{
+ struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
+
+ return memdelay_domain_show(m, memcg->memdelay_domain);
+}
+
static struct cftype memory_files[] = {
{
.name = "current",
@@ -5280,6 +5300,11 @@ static int memory_stat_show(struct seq_file *m, void *v)
.flags = CFTYPE_NOT_ON_ROOT,
.seq_show = memory_stat_show,
},
+ {
+ .name = "memdelay",
+ .flags = CFTYPE_NOT_ON_ROOT,
+ .seq_show = memory_memdelay_show,
+ },
{ } /* terminate */
};
diff --git a/mm/memdelay.c b/mm/memdelay.c
new file mode 100644
index 0000000..66e2b63
--- /dev/null
+++ b/mm/memdelay.c
@@ -0,0 +1,286 @@
+/*
+ * Memory delay metric
+ *
+ * Copyright (c) 2017 Facebook, Johannes Weiner
+ *
+ * This code quantifies and reports to userspace the wall-time impact
+ * of memory pressure on the system and memory-controlled cgroups.
+ */
+
+#include <linux/memcontrol.h>
+#include <linux/memdelay.h>
+#include <linux/seq_file.h>
+#include <linux/proc_fs.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+
+#define LOAD_INT(x) ((x) >> FSHIFT)
+#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
+
+static DEFINE_PER_CPU(struct memdelay_domain_cpu, global_domain_cpus);
+
+/* System-level keeping of memory delay statistics */
+struct memdelay_domain memdelay_global_domain = {
+ .mdcs = &global_domain_cpus,
+};
+
+static void domain_init(struct memdelay_domain *md)
+{
+ md->period_expires = jiffies + LOAD_FREQ;
+}
+
+/**
+ * memdelay_init - initialize the memdelay subsystem
+ *
+ * This needs to run before the scheduler starts queuing and
+ * scheduling tasks.
+ */
+void __init memdelay_init(void)
+{
+ domain_init(&memdelay_global_domain);
+}
+
+static void domain_move_clock(struct memdelay_domain *md)
+{
+ unsigned long expires = READ_ONCE(md->period_expires);
+ unsigned long none, some, full;
+ int missed_periods;
+ unsigned long next;
+ int i;
+
+ if (time_before(jiffies, expires))
+ return;
+
+ missed_periods = 1 + (jiffies - expires) / LOAD_FREQ;
+ next = expires + (missed_periods * LOAD_FREQ);
+
+ if (cmpxchg(&md->period_expires, expires, next) != expires)
+ return;
+
+ none = xchg(&md->times[MDS_NONE], 0);
+ some = xchg(&md->times[MDS_SOME], 0);
+ full = xchg(&md->times[MDS_FULL], 0);
+
+ for (i = 0; i < missed_periods; i++) {
+ unsigned long pct;
+
+ pct = some * 100 / max(none + some + full, 1UL);
+ pct *= FIXED_1;
+ CALC_LOAD(md->avg_some[0], EXP_1, pct);
+ CALC_LOAD(md->avg_some[1], EXP_5, pct);
+ CALC_LOAD(md->avg_some[2], EXP_15, pct);
+
+ pct = full * 100 / max(none + some + full, 1UL);
+ pct *= FIXED_1;
+ CALC_LOAD(md->avg_full[0], EXP_1, pct);
+ CALC_LOAD(md->avg_full[1], EXP_5, pct);
+ CALC_LOAD(md->avg_full[2], EXP_15, pct);
+
+ none = some = full = 0;
+ }
+}
+
+static void domain_cpu_update(struct memdelay_domain *md, int cpu,
+ enum memdelay_task_state old,
+ enum memdelay_task_state new)
+{
+ enum memdelay_domain_state state;
+ struct memdelay_domain_cpu *mdc;
+ unsigned long delta;
+ u64 now;
+
+ mdc = per_cpu_ptr(md->mdcs, cpu);
+
+ if (old) {
+ WARN_ONCE(!mdc->tasks[old], "cpu=%d old=%d new=%d counter=%d\n",
+ cpu, old, new, mdc->tasks[old]);
+ mdc->tasks[old] -= 1;
+ }
+ if (new)
+ mdc->tasks[new] += 1;
+
+ /*
+ * The domain is somewhat delayed when a number of tasks are
+ * delayed but there are still others running the workload.
+ *
+ * The domain is fully delayed when all non-idle tasks on the
+ * CPU are delayed, or when a delayed task is actively running
+ * and preventing productive tasks from making headway.
+ *
+ * The state times then add up over all CPUs in the domain: if
+ * the domain is fully blocked on one CPU and there is another
+ * one running the workload, the domain is considered fully
+ * blocked 50% of the time.
+ */
+ if (mdc->tasks[MTS_DELAYED_ACTIVE] && !mdc->tasks[MTS_IOWAIT])
+ state = MDS_FULL;
+ else if (mdc->tasks[MTS_DELAYED])
+ state = (mdc->tasks[MTS_RUNNABLE] || mdc->tasks[MTS_IOWAIT]) ?
+ MDS_SOME : MDS_FULL;
+ else
+ state = MDS_NONE;
+
+ if (mdc->state == state)
+ return;
+
+ now = cpu_clock(cpu);
+ delta = (now - mdc->state_start) / NSEC_PER_USEC;
+
+ domain_move_clock(md);
+ md->times[mdc->state] += delta;
+
+ mdc->state = state;
+ mdc->state_start = now;
+}
+
+static struct memdelay_domain *memcg_domain(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_MEMCG
+ if (!mem_cgroup_disabled())
+ return memcg->memdelay_domain;
+#endif
+ return &memdelay_global_domain;
+}
+
+/**
+ * memdelay_task_change - note a task changing its delay/work state
+ * @task: the task changing state
+ * @old: old task state
+ * @new: new task state
+ *
+ * Updates the task's domain counters to reflect a change in the
+ * task's delayed/working state.
+ */
+void memdelay_task_change(struct task_struct *task,
+ enum memdelay_task_state old,
+ enum memdelay_task_state new)
+{
+ int cpu = task_cpu(task);
+ struct mem_cgroup *memcg;
+ unsigned long delay = 0;
+
+#ifdef CONFIG_DEBUG_VM
+ WARN_ONCE(task->memdelay_state != old,
+ "cpu=%d task=%p state=%d (in_iowait=%d PF_MEMDELAYED=%d) old=%d new=%d\n",
+ cpu, task, task->memdelay_state, task->in_iowait,
+ !!(task->flags & PF_MEMDELAY), old, new);
+ task->memdelay_state = new;
+#endif
+
+ /* Account when tasks are entering and leaving delays */
+ if (old < MTS_DELAYED && new >= MTS_DELAYED) {
+ task->memdelay_start = cpu_clock(cpu);
+ } else if (old >= MTS_DELAYED && new < MTS_DELAYED) {
+ delay = (cpu_clock(cpu) - task->memdelay_start) / NSEC_PER_USEC;
+ task->memdelay_total += delay;
+ }
+
+ /* Account domain state changes */
+ rcu_read_lock();
+ memcg = mem_cgroup_from_task(task);
+ do {
+ struct memdelay_domain *md;
+
+ md = memcg_domain(memcg);
+ md->aggregate += delay;
+ domain_cpu_update(md, cpu, old, new);
+ } while (memcg && (memcg = parent_mem_cgroup(memcg)));
+ rcu_read_unlock();
+};
+
+/**
+ * memdelay_domain_alloc - allocate a cgroup memory delay domain
+ */
+struct memdelay_domain *memdelay_domain_alloc(void)
+{
+ struct memdelay_domain *md;
+
+ md = kzalloc(sizeof(*md), GFP_KERNEL);
+ if (!md)
+ return NULL;
+ md->mdcs = alloc_percpu(struct memdelay_domain_cpu);
+ if (!md->mdcs) {
+ kfree(md);
+ return NULL;
+ }
+ domain_init(md);
+ return md;
+}
+
+/**
+ * memdelay_domain_free - free a cgroup memory delay domain
+ */
+void memdelay_domain_free(struct memdelay_domain *md)
+{
+ if (md) {
+ free_percpu(md->mdcs);
+ kfree(md);
+ }
+}
+
+/**
+ * memdelay_domain_show - format memory delay domain stats to a seq_file
+ * @s: the seq_file
+ * @md: the memory domain
+ */
+int memdelay_domain_show(struct seq_file *s, struct memdelay_domain *md)
+{
+ domain_move_clock(md);
+
+ seq_printf(s, "%lu\n", md->aggregate);
+
+ seq_printf(s, "%lu.%02lu %lu.%02lu %lu.%02lu\n",
+ LOAD_INT(md->avg_some[0]), LOAD_FRAC(md->avg_some[0]),
+ LOAD_INT(md->avg_some[1]), LOAD_FRAC(md->avg_some[1]),
+ LOAD_INT(md->avg_some[2]), LOAD_FRAC(md->avg_some[2]));
+
+ seq_printf(s, "%lu.%02lu %lu.%02lu %lu.%02lu\n",
+ LOAD_INT(md->avg_full[0]), LOAD_FRAC(md->avg_full[0]),
+ LOAD_INT(md->avg_full[1]), LOAD_FRAC(md->avg_full[1]),
+ LOAD_INT(md->avg_full[2]), LOAD_FRAC(md->avg_full[2]));
+
+#ifdef CONFIG_DEBUG_VM
+ {
+ int cpu;
+
+ for_each_online_cpu(cpu) {
+ struct memdelay_domain_cpu *mdc;
+
+ mdc = per_cpu_ptr(md->mdcs, cpu);
+ seq_printf(s, "%d %d %d %d\n",
+ mdc->tasks[MTS_IOWAIT],
+ mdc->tasks[MTS_RUNNABLE],
+ mdc->tasks[MTS_DELAYED],
+ mdc->tasks[MTS_DELAYED_ACTIVE]);
+ }
+ }
+#endif
+
+ return 0;
+}
+
+static int memdelay_show(struct seq_file *m, void *v)
+{
+ return memdelay_domain_show(m, &memdelay_global_domain);
+}
+
+static int memdelay_open(struct inode *inode, struct file *file)
+{
+ return single_open(file, memdelay_show, NULL);
+}
+
+static const struct file_operations memdelay_fops = {
+ .open = memdelay_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+
+static int __init memdelay_proc_init(void)
+{
+ proc_create("memdelay", 0, NULL, &memdelay_fops);
+ return 0;
+}
+module_init(memdelay_proc_init);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index e5b159b..103cd4a 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -64,6 +64,7 @@
#include <linux/page_owner.h>
#include <linux/kthread.h>
#include <linux/memcontrol.h>
+#include <linux/memdelay.h>
#include <asm/sections.h>
#include <asm/tlbflush.h>
@@ -3124,15 +3125,18 @@ void warn_alloc(gfp_t gfp_mask, const char *fmt, ...)
unsigned int alloc_flags, const struct alloc_context *ac,
enum compact_priority prio, enum compact_result *compact_result)
{
+ unsigned long mdflags;
struct page *page;
if (!order)
return NULL;
+ memdelay_enter(&mdflags);
current->flags |= PF_MEMALLOC;
*compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac,
prio);
current->flags &= ~PF_MEMALLOC;
+ memdelay_leave(&mdflags);
if (*compact_result <= COMPACT_INACTIVE)
return NULL;
@@ -3268,12 +3272,14 @@ void warn_alloc(gfp_t gfp_mask, const char *fmt, ...)
const struct alloc_context *ac)
{
struct reclaim_state reclaim_state;
+ unsigned long mdflags;
int progress;
cond_resched();
/* We now go into synchronous reclaim */
cpuset_memory_pressure_bump();
+ memdelay_enter(&mdflags);
current->flags |= PF_MEMALLOC;
lockdep_set_current_reclaim_state(gfp_mask);
reclaim_state.reclaimed_slab = 0;
@@ -3285,6 +3291,7 @@ void warn_alloc(gfp_t gfp_mask, const char *fmt, ...)
current->reclaim_state = NULL;
lockdep_clear_current_reclaim_state();
current->flags &= ~PF_MEMALLOC;
+ memdelay_leave(&mdflags);
cond_resched();
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 6361461..7024ba6 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -47,6 +47,7 @@
#include <linux/prefetch.h>
#include <linux/printk.h>
#include <linux/dax.h>
+#include <linux/memdelay.h>
#include <asm/tlbflush.h>
#include <asm/div64.h>
@@ -3031,6 +3032,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
{
struct zonelist *zonelist;
unsigned long nr_reclaimed;
+ unsigned long mdflags;
int nid;
struct scan_control sc = {
.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
@@ -3058,9 +3060,11 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
sc.gfp_mask,
sc.reclaim_idx);
+ memdelay_enter(&mdflags);
current->flags |= PF_MEMALLOC;
nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
current->flags &= ~PF_MEMALLOC;
+ memdelay_leave(&mdflags);
trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
@@ -3445,6 +3449,7 @@ static int kswapd(void *p)
pgdat->kswapd_order = alloc_order = reclaim_order = 0;
pgdat->kswapd_classzone_idx = classzone_idx = 0;
for ( ; ; ) {
+ unsigned long mdflags;
bool ret;
kswapd_try_sleep:
@@ -3478,7 +3483,11 @@ static int kswapd(void *p)
*/
trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
alloc_order);
+
+ memdelay_enter(&mdflags);
reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
+ memdelay_leave(&mdflags);
+
if (reclaim_order < alloc_order)
goto kswapd_try_sleep;
--
1.9.1
--------------9B27BB3AED4106EF45BCD875
Content-Type: text/x-patch;
name="0001-mm-workingset-tell-cache-transitions-from-workingset.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename*0="0001-mm-workingset-tell-cache-transitions-from-workingset.pa";
filename*1="tch"