>From c3e97f5daf99bcd54383eaab466c477dbb743dd9 Mon Sep 17 00:00:00 2001 From: Johannes Weiner Date: Mon, 5 Jun 2017 16:07:22 -0400 Subject: [PATCH 3/3] mm/sched: memdelay: memory health interface for systems and workloads 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//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 --- 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/cgroup.c | 3 +- kernel/fork.c | 4 + kernel/sched/Makefile | 2 +- kernel/sched/core.c | 27 +++++ kernel/sched/memdelay.c | 118 +++++++++++++++++++ mm/Makefile | 2 +- mm/compaction.c | 4 + mm/filemap.c | 11 ++ mm/memcontrol.c | 25 ++++ mm/memdelay.c | 285 +++++++++++++++++++++++++++++++++++++++++++++ mm/page_alloc.c | 11 +- mm/vmscan.c | 9 ++ 18 files changed, 712 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 88c355574aa0..00e0e9aa3e70 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -611,6 +611,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 719c2e943ea1..19f194940c80 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2916,6 +2916,7 @@ static const struct pid_entry tgid_base_stuff[] = { 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), @@ -3307,6 +3308,7 @@ static const struct pid_entry tid_base_stuff[] = { 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 aa2b89071630..7ab706c316b8 100644 --- a/fs/proc/internal.h +++ b/fs/proc/internal.h @@ -146,6 +146,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 9b15a4bcfa77..1f720d3090f7 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -30,6 +30,7 @@ #include #include #include +#include struct mem_cgroup; struct page; @@ -183,6 +184,9 @@ struct mem_cgroup { unsigned long soft_limit; + /* Memory delay measurement domain */ + struct memdelay_domain *memdelay_domain; + /* vmpressure notifications */ struct vmpressure vmpressure; @@ -728,6 +732,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) { @@ -740,6 +749,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 000000000000..08ed4e4baedf --- /dev/null +++ b/include/linux/memdelay.h @@ -0,0 +1,182 @@ +#ifndef _LINUX_MEMDELAY_H +#define _LINUX_MEMDELAY_H + +#include +#include + +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 c05ac5f5aa03..de15e3c8c43a 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -651,6 +651,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 requiring atomic access. */ @@ -871,6 +872,12 @@ struct task_struct { struct io_context *io_context; + u64 memdelay_start; + unsigned long memdelay_total; +#ifdef CONFIG_DEBUG_VM + int memdelay_state; +#endif + /* Ptrace state: */ unsigned long ptrace_message; siginfo_t *last_siginfo; @@ -1274,6 +1281,7 @@ extern struct pid *cad_pid; #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/cgroup.c b/kernel/cgroup/cgroup.c index df2e0f14a95d..930aaef50396 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -699,7 +699,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 b7e9e57b71ea..96dd35393be9 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1208,6 +1208,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 53f0164ed362..84390fc42f60 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 -obj-y += wait.o wait_bit.o swait.o completion.o idle.o +obj-y += wait.o wait_bit.o swait.o completion.o idle.o memdelay.o obj-$(CONFIG_SMP) += cpupri.o cpudeadline.o topology.o stop_task.o obj-$(CONFIG_SCHED_AUTOGROUP) += autogroup.o obj-$(CONFIG_SCHEDSTATS) += stats.o diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 0869b20fba81..bf105c870da6 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -759,6 +760,14 @@ static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags) 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); } @@ -770,6 +779,11 @@ static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) 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); } @@ -2044,7 +2058,16 @@ try_to_wake_up(struct task_struct *p, unsigned int state, 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); } @@ -3326,6 +3349,8 @@ static void __sched notrace __schedule(bool preempt) rq->curr = next; ++*switch_count; + memdelay_schedule(prev, next); + trace_sched_switch(preempt, prev, next); /* Also unlocks the rq: */ @@ -5919,6 +5944,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 000000000000..1d4813cd018a --- /dev/null +++ b/kernel/sched/memdelay.c @@ -0,0 +1,118 @@ +/* + * 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 +#include +#include + +#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_flags rf; + 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(); + rq_lock(rq, &rf); + + current->flags |= PF_MEMDELAY; + memdelay_task_change(current, MTS_RUNNABLE, MTS_DELAYED_ACTIVE); + + rq_unlock(rq, &rf); + 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_flags rf; + 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(); + rq_lock(rq, &rf); + + current->flags &= ~PF_MEMDELAY; + memdelay_task_change(current, MTS_DELAYED_ACTIVE, MTS_RUNNABLE); + + rq_unlock(rq, &rf); + 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 411bd24d4a7c..c9bdbc5627e5 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -39,7 +39,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 swap_slots.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 fb548e4c7bd4..adf67de23fee 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -2040,11 +2040,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 da55a5693da9..648418694405 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "internal.h" #define CREATE_TRACE_POINTS @@ -961,8 +962,15 @@ static inline int wait_on_page_bit_common(wait_queue_head_t *q, { struct wait_page_queue wait_page; wait_queue_entry_t *wait = &wait_page.wait; + unsigned long mdflags; + bool refault = false; int ret = 0; + if (bit_nr == PG_locked && !PageUptodate(page) && PageWorkingset(page)) { + memdelay_enter(&mdflags); + refault = true; + } + init_wait(wait); wait->flags = lock ? WQ_FLAG_EXCLUSIVE : 0; wait->func = wake_page_function; @@ -1001,6 +1009,9 @@ static inline int wait_on_page_bit_common(wait_queue_head_t *q, finish_wait(q, wait); + if (refault) + memdelay_leave(&mdflags); + /* * A signal could leave PageWaiters set. Clearing it here if * !waitqueue_active would be possible (by open-coding finish_wait), diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 93b2eb063afd..102f0f4d3f5c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -65,6 +65,7 @@ #include #include #include +#include #include "internal.h" #include #include @@ -3926,6 +3927,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", @@ -3993,6 +3996,10 @@ static struct cftype mem_cgroup_legacy_files[] = { { .name = "pressure_level", }, + { + .name = "memdelay", + .seq_show = memory_memdelay_show, + }, #ifdef CONFIG_NUMA { .name = "numa_stat", @@ -4170,6 +4177,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); } @@ -4275,10 +4283,15 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) /* 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; @@ -5282,6 +5295,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", @@ -5317,6 +5337,11 @@ static struct cftype memory_files[] = { .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 000000000000..c43d6f7ba22a --- /dev/null +++ b/mm/memdelay.c @@ -0,0 +1,285 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 1423da8dd16f..d8d01e9df982 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -67,6 +67,7 @@ #include #include #include +#include #include #include @@ -3364,16 +3365,19 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, unsigned int alloc_flags, const struct alloc_context *ac, enum compact_priority prio, enum compact_result *compact_result) { - struct page *page; unsigned int noreclaim_flag; + unsigned long mdflags; + struct page *page; if (!order) return NULL; + memdelay_enter(&mdflags); noreclaim_flag = memalloc_noreclaim_save(); *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac, prio); memalloc_noreclaim_restore(noreclaim_flag); + memdelay_leave(&mdflags); if (*compact_result <= COMPACT_INACTIVE) return NULL; @@ -3519,13 +3523,15 @@ __perform_reclaim(gfp_t gfp_mask, unsigned int order, const struct alloc_context *ac) { struct reclaim_state reclaim_state; - int progress; unsigned int noreclaim_flag; + unsigned long mdflags; + int progress; cond_resched(); /* We now go into synchronous reclaim */ cpuset_memory_pressure_bump(); + memdelay_enter(&mdflags); noreclaim_flag = memalloc_noreclaim_save(); lockdep_set_current_reclaim_state(gfp_mask); reclaim_state.reclaimed_slab = 0; @@ -3537,6 +3543,7 @@ __perform_reclaim(gfp_t gfp_mask, unsigned int order, current->reclaim_state = NULL; lockdep_clear_current_reclaim_state(); memalloc_noreclaim_restore(noreclaim_flag); + memdelay_leave(&mdflags); cond_resched(); diff --git a/mm/vmscan.c b/mm/vmscan.c index 60357cd84c67..1029305b9b3a 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include @@ -3098,6 +3099,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; unsigned int noreclaim_flag; struct scan_control sc = { @@ -3126,9 +3128,11 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, sc.gfp_mask, sc.reclaim_idx); + memdelay_enter(&mdflags); noreclaim_flag = memalloc_noreclaim_save(); nr_reclaimed = do_try_to_free_pages(zonelist, &sc); memalloc_noreclaim_restore(noreclaim_flag); + memdelay_leave(&mdflags); trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); @@ -3550,6 +3554,7 @@ static int kswapd(void *p) pgdat->kswapd_order = 0; pgdat->kswapd_classzone_idx = MAX_NR_ZONES; for ( ; ; ) { + unsigned long mdflags; bool ret; alloc_order = reclaim_order = pgdat->kswapd_order; @@ -3586,7 +3591,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; } -- 2.14.1