mm: workingset reporting
Subsystems (10)
Yuanchu Xie
reviewercover mm: workingset reporting
Changes from RFC v3 -> PATCH v1:
- Updated selftest to use ksft_print_msg instead of fprintf(stderr, ...) (Muhammad Usama Anjum)
- Included more detail in patch skipping pmd_young with force_scan (Huang, Ying)
- Deferred reaccess histogram as a followup
- Removed per-memcg page age interval configs for simplicity
Changes from RFC v2 -> RFC v3:
- Update to v6.8
- Added an aging kernel thread (gated behind config)
- Added basic selftests for sysfs interface files
- Track swapped out pages for reaccesses
- Refactoring and cleanup
- Dropped the virtio-balloon extension to make things manageable
Changes from RFC v1 -> RFC v2:
- Refactored the patchs into smaller pieces
- Renamed interfaces and functions from wss to wsr (Working Set Reporting)
- Fixed build errors when CONFIG_WSR is not set
- Changed working_set_num_bins to u8 for virtio-balloon
- Added support for per-NUMA node reporting for virtio-balloon
[rfc v1]
https://lore.kernel.org/linux-mm/[email protected]/
[rfc v2]
https://lore.kernel.org/linux-mm/[email protected]/
[rfc v3]
https://lore.kernel.org/linux-mm/[email protected]/
This patch series provides workingset reporting of user pages in lruvecs, of which coldness can be tracked by accessed bits and fd references. However, the concept of workingset applies generically to all types of memory, which could be kernel slab caches, discardable userspace caches (databases), or CXL.mem. Therefore, data sources might come from slab shrinkers, device drivers, or the userspace. IMO, the kernel should provide a set of workingset interfaces that should be generic enough to accommodate the various use cases, and be extensible to potential future use cases. The current proposed interfaces are not sufficient in that regard, but I would like to start somewhere, solicit feedback, and iterate.
Use cases
==========
Job scheduling
On overcommitted hosts, workingset information allows the job scheduler to right-size each job and land more jobs on the same host or NUMA node, and in the case of a job with increasing workingset, policy decisions can be made to migrate other jobs off the host/NUMA node, or oom-kill the misbehaving job. If the job shape is very different from the machine shape, knowing the workingset per-node can also help inform page allocation policies.
Proactive reclaim
Workingset information allows the a container manager to proactively reclaim memory while not impacting a job's performance. While PSI may provide a reactive measure of when a proactive reclaim has reclaimed too much, workingset reporting allows the policy to be more accurate and flexible.
Ballooning (similar to proactive reclaim)
While this patch series does not extend the virtio-balloon device, balloon policies benefit from workingset to more precisely determine the size of the memory balloon. On desktops/laptops/mobile devices where memory is scarce and overcommitted, the balloon sizing in multiple VMs running on the same device can be orchestrated with workingset reports from each one.
Promotion/Demotion
Similar to proactive reclaim, a workingset report enables demotion to a slower tier of memory.
For promotion, the workingset report interfaces need to be extended to report hotness and gather hotness information from the devices[1].
[1]
https://www.opencompute.org/documents/ocp-cms-hotness-tracking-requirements-white-paper-pdf-1
Sysfs and Cgroup Interfaces
==========
The interfaces are detailed in the patches that introduce them. The main idea here is we break down the workingset per-node per-memcg into time intervals (ms), e.g.
1000 anon=137368 file=24530
20000 anon=34342 file=0
30000 anon=353232 file=333608
40000 anon=407198 file=206052
9223372036854775807 anon=4925624 file=892892
I realize this does not generalize well to hotness information, but I lack the intuition for an abstraction that presents hotness in a useful way. Please advise.
Implementation
==========
Currently, the reporting of user pages is based off of MGLRU, and therefore requires CONFIG_LRU_GEN=y. We would benefit from more MGLRU generations for a more fine-grained workingset report. I will make the generation count configurable in the next version. The workingset reporting mechanism is gated behind CONFIG_WORKINGSET_REPORT, and the aging thread is behind CONFIG_WORKINGSET_REPORT_AGING.
Yuanchu Xie (7):
mm: multi-gen LRU: ignore non-leaf pmd_young for force_scan=true
mm: aggregate working set information into histograms
mm: use refresh interval to rate-limit workingset report aggregation
mm: report workingset during memory pressure driven scanning
mm: extend working set reporting to memcgs
mm: add kernel aging thread for workingset reporting
selftest: test system-wide workingset reporting
drivers/base/node.c | 6 + include/linux/memcontrol.h | 5 + include/linux/mmzone.h | 9 + include/linux/workingset_report.h | 97 ++++ mm/Kconfig | 15 + mm/Makefile | 2 + mm/internal.h | 17 + mm/memcontrol.c | 184 +++++- mm/mm_init.c | 2 + mm/mmzone.c | 2 + mm/vmscan.c | 85 ++- mm/workingset_report.c | 545 ++++++++++++++++++ mm/workingset_report_aging.c | 127 ++++ tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + .../testing/selftests/mm/workingset_report.c | 317 ++++++++++ .../testing/selftests/mm/workingset_report.h | 39 ++ .../selftests/mm/workingset_report_test.c | 332 +++++++++++ 18 files changed, 1786 insertions(+), 2 deletions(-) create mode 100644 include/linux/workingset_report.h create mode 100644 mm/workingset_report.c create mode 100644 mm/workingset_report_aging.c create mode 100644 tools/testing/selftests/mm/workingset_report.c create mode 100644 tools/testing/selftests/mm/workingset_report.h create mode 100644 tools/testing/selftests/mm/workingset_report_test.c
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Yuanchu Xie
reviewerPATCH 1/7 mm: multi-gen LRU: ignore non-leaf pmd_young for force_scan=true
When non-leaf pmd accessed bits are available, MGLRU page table walks can clear the non-leaf pmd accessed bit and ignore the accessed bit on the pte if it's on a different node, skipping a generation update as well. If another scan occurrs on the same node as said skipped pte. the non-leaf pmd accessed bit might remain cleared and the pte accessed bits won't be checked. While this is sufficient for reclaim-driven aging, where the goal is to select a reasonably cold page, the access can be missed when aging proactively for workingset estimation of a of a node/memcg.
In more detail, get_pfn_folio returns NULL if the folio's nid != node under scanning, so the page table walk skips processing of said pte. Now the pmd_young flag on this pmd is cleared, and if none of the pte's are accessed before another scan occurrs on the folio's node, the pmd_young check fails and the pte accessed bit is skipped.
Since force_scan disables various other optimizations, we check force_scan to ignore the non-leaf pmd accessed bit.
--- mm/vmscan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmscan.c +1 -1
@@ -3522,7 +3522,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end, walk->mm_stats[MM_NONLEAF_TOTAL ]++; -if (should_clear_pmd_young ()) { +if (!walk->force_scan &&should_clear_pmd_young ()) {if (!pmd_young (val))continue ;
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Yuanchu Xie
reviewerPATCH 2/7 mm: aggregate working set information into histograms
In reply to Yuanchu XieHierarchically aggregate all memcgs' MGLRU generations and their page counts into working set page age histograms.
The histograms break down the system's working set per-node, per-anon/file.
The sysfs interfaces are as follows:/sys/devices/system/node/nodeX/page_age
A per-node page age histogram, showing an aggregate of the node's lruvecs. The information is extracted from MGLRU's per-generation page counters. Reading this file causes a hierarchical aging of all lruvecs, scanning pages and creates a new generation in each lruvec.
For example:
1000 anon=0 file=0
2000 anon=0 file=0
100000 anon=5533696 file=5566464 18446744073709551615 anon=0 file=0
/sys/devices/system/node/nodeX/page_age_interval
A comma separated list of time in milliseconds that configures what the page age histogram uses for aggregation.
--- drivers/base/node.c | 6 + include/linux/mmzone.h | 9 + include/linux/workingset_report.h | 79 ++++++ mm/Kconfig | 9 + mm/Makefile | 1 + mm/internal.h | 9 + mm/memcontrol.c | 2 + mm/mm_init.c | 2 + mm/mmzone.c | 2 + mm/vmscan.c | 32 +++ mm/workingset_report.c | 438 ++++++++++++++++++++++++++++++ 11 files changed, 589 insertions(+) create mode 100644 include/linux/workingset_report.h create mode 100644 mm/workingset_report.c
drivers/base/node.c +6 -0
@@ -20,6 +20,8 @@#include <linux/pm_runtime.h> #include <linux/swap.h> #include <linux/slab.h> +#include <linux/memcontrol.h> +#include <linux/workingset_report.h> static const struct bus_type node_subsys = { .name ="node" ,
@@ -625,6 +627,7 @@ static int register_node(struct node *node, int num) }else {hugetlb_register_node (node);compaction_register_node (node); +wsr_init_sysfs (node); }return error;
@@ -641,6 +644,9 @@ void unregister_node(struct node *node) {hugetlb_unregister_node (node);compaction_unregister_node (node); +wsr_remove_sysfs (node); +wsr_destroy_lruvec (mem_cgroup_lruvec (NULL ,NODE_DATA (node->dev.id))); +wsr_destroy_pgdat (NODE_DATA (node->dev.id));node_remove_accesses (node);node_remove_caches (node);device_unregister (&node->dev);
include/linux/mmzone.h +9 -0
@@ -24,6 +24,7 @@#include <linux/local_lock.h> #include <linux/zswap.h> #include <asm/page.h> +#include <linux/workingset_report.h> /* Free memory management - zoned buddy allocator. */ #ifndef CONFIG_ARCH_FORCE_MAX_ORDER
@@ -625,6 +626,9 @@ struct lruvec {struct lru_gen_mm_state mm_state;#endif #endif /* CONFIG_LRU_GEN */ +#ifdef CONFIG_WORKINGSET_REPORT +struct wsr_state wsr; +#endif /* CONFIG_WORKINGSET_REPORT */ #ifdef CONFIG_MEMCG struct pglist_data *pgdat;#endif
@@ -1398,6 +1402,11 @@ typedef struct pglist_data {struct lru_gen_memcg memcg_lru;#endif +#ifdef CONFIG_WORKINGSET_REPORT +struct mutex wsr_update_mutex; +struct wsr_report_bins __rcu *wsr_page_age_bins; +#endif +CACHELINE_PADDING (_pad2_);/* Per-node vmstats */
include/linux/workingset_report.h new file +79 -0
79 lines not on this page · raw message
mm/Kconfig +9 -0
@@ -1261,6 +1261,15 @@ config LOCK_MM_AND_FIND_VMA config IOMMU_MM_DATA bool +config WORKINGSET_REPORT + bool "Working set reporting" + depends on LRU_GEN && SYSFS + help + Report system and per-memcg working set to userspace. + + This option exports stats and events giving the user more insight + into its memory working set. + source "mm/damon/Kconfig" endmenu
mm/Makefile +1 -0
@@ -92,6 +92,7 @@ obj-$(CONFIG_DEVICE_MIGRATION) += migrate_device.o obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o obj-$(CONFIG_PAGE_COUNTER) += page_counter.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o +obj-$(CONFIG_WORKINGSET_REPORT) += workingset_report.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) += swap_cgroup.o endif
mm/internal.h +9 -0
@@ -198,12 +198,21 @@ extern unsigned long highest_memmap_pfn;/* * in mm/vmscan.c: */ +struct scan_control ;bool isolate_lru_page (struct page *page);bool folio_isolate_lru (struct folio *folio);void putback_lru_page (struct page *page);void folio_putback_lru (struct folio *folio);extern void reclaim_throttle (pg_data_t *pgdat,enum vmscan_throttle_state reason); +#ifdef CONFIG_WORKINGSET_REPORT +/* +* in mm/wsr.c +*/ +/* Requires wsr->page_age_lock held */ +void wsr_refresh_scan (struct lruvec *lruvec); +#endif +/* * in mm/rmap.c: */
mm/memcontrol.c +2 -0
@@ -65,6 +65,7 @@#include <linux/seq_buf.h> #include <linux/sched/isolation.h> #include <linux/kmemleak.h> +#include <linux/workingset_report.h> #include "internal.h" #include <net/sock.h> #include <net/ip.h>
@@ -5457,6 +5458,7 @@ static void free_mem_cgroup_per_node_info(struct mem_cgroup *memcg, int node)if (!pn)return ; +wsr_destroy_lruvec (&pn->lruvec);free_percpu (pn->lruvec_stats_percpu);kfree (pn); }
mm/mm_init.c +2 -0
@@ -27,6 +27,7 @@#include <linux/swap.h> #include <linux/cma.h> #include <linux/crash_dump.h> +#include <linux/workingset_report.h> #include "internal.h" #include "slab.h" #include "shuffle.h"
@@ -1368,6 +1369,7 @@ static void __meminit pgdat_init_internals(struct pglist_data *pgdat)pgdat_page_ext_init (pgdat);lruvec_init (&pgdat->__lruvec); +wsr_init_pgdat (pgdat); } static void __meminit zone_init_internals(struct zone *zone, enum zone_type idx, int nid,
mm/mmzone.c +2 -0
@@ -90,6 +90,8 @@ void lruvec_init(struct lruvec *lruvec)*/ list_del (&lruvec->lists[LRU_UNEVICTABLE ]); +wsr_init_lruvec (lruvec); +lru_gen_init_lruvec (lruvec); }
mm/vmscan.c +32 -0
@@ -56,6 +56,7 @@#include <linux/khugepaged.h> #include <linux/rculist_nulls.h> #include <linux/random.h> +#include <linux/workingset_report.h> #include <asm/tlbflush.h> #include <asm/div64.h>
@@ -5606,6 +5607,8 @@ static int __init init_lru_gen(void)if (sysfs_create_group (mm_kobj, &lru_gen_attr_group))pr_err ("lru_gen: failed to create sysfs group\n" ); +wsr_init_sysfs (NULL ); +debugfs_create_file ("lru_gen" ,0644 ,NULL ,NULL , &lru_gen_rw_fops);debugfs_create_file ("lru_gen_full" ,0444 ,NULL ,NULL , &lru_gen_ro_fops);
@@ -5613,6 +5616,35 @@ static int __init init_lru_gen(void) };late_initcall (init_lru_gen); +/****************************************************************************** +* workingset reporting +******************************************************************************/ +#ifdef CONFIG_WORKINGSET_REPORT +void wsr_refresh_scan (struct lruvec *lruvec) +{ +DEFINE_MAX_SEQ (lruvec); +struct scan_control sc = { + .may_writepage = true, + .may_unmap = true, + .may_swap = true, + .proactive = true, + .reclaim_idx =MAX_NR_ZONES -1 , + .gfp_mask =GFP_KERNEL , + }; +unsigned int flags; + +set_task_reclaim_state (current, &sc.reclaim_state); + flags =memalloc_noreclaim_save (); +/* +* setting can_swap=true and force_scan=true ensures +* proper workingset stats when the system cannot swap. +*/ +try_to_inc_max_seq (lruvec, max_seq, &sc, true, true); +memalloc_noreclaim_restore (flags); +set_task_reclaim_state (current,NULL ); +} +#endif /* CONFIG_WORKINGSET_REPORT */ +#else /* !CONFIG_LRU_GEN */ static void lru_gen_age_node (struct pglist_data *pgdat,struct scan_control *sc)
mm/workingset_report.c new file +438 -0
438 lines not on this page · raw message
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Yuanchu Xie
reviewerPATCH 3/7 mm: use refresh interval to rate-limit workingset report aggregation
In reply to Yuanchu XieThe refresh interval is a rate limiting factor to workingset page age histogram reads. When a workingset report is generated, a timestamp is noted, and the same report will be read until it expires beyond the refresh interval, at which point a new report is generated.
Sysfs interface /sys/devices/system/node/nodeX/workingset_report/refresh_interval
time in milliseconds specifying how long the report is valid for
--- include/linux/workingset_report.h | 1 + mm/internal.h | 2 +- mm/vmscan.c | 27 +++++++---- mm/workingset_report.c | 81 +++++++++++++++++++++++++------ 4 files changed, 85 insertions(+), 26 deletions(-)
include/linux/workingset_report.h +1 -0
@@ -37,6 +37,7 @@ struct wsr_page_age_histo { };struct wsr_state { +unsigned long refresh_interval;/* breakdown of workingset by page age */ struct mutex page_age_lock;struct wsr_page_age_histo *page_age;
mm/internal.h +1 -1
@@ -210,7 +210,7 @@ extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason* in mm/wsr.c */ /* Requires wsr->page_age_lock held */ -void wsr_refresh_scan (struct lruvec *lruvec); +void wsr_refresh_scan (struct lruvec *lruvec,unsigned long refresh_interval);#endif /*
mm/vmscan.c +17 -10
@@ -5620,7 +5620,7 @@ late_initcall(init_lru_gen);* workingset reporting ******************************************************************************/ #ifdef CONFIG_WORKINGSET_REPORT -void wsr_refresh_scan(struct lruvec *lruvec) +void wsr_refresh_scan(struct lruvec *lruvec, unsigned long refresh_interval) { DEFINE_MAX_SEQ(lruvec); struct scan_control sc = {
@@ -5633,15 +5633,22 @@ void wsr_refresh_scan(struct lruvec *lruvec) };unsigned int flags; -set_task_reclaim_state (current, &sc.reclaim_state); - flags =memalloc_noreclaim_save (); -/* -* setting can_swap=true and force_scan=true ensures -* proper workingset stats when the system cannot swap. -*/ -try_to_inc_max_seq (lruvec, max_seq, &sc, true, true); -memalloc_noreclaim_restore (flags); -set_task_reclaim_state (current,NULL ); +if (refresh_interval) { +int gen =lru_gen_from_seq (max_seq); +unsigned long birth =READ_ONCE (lruvec->lrugen.timestamps[gen]); + +if (time_is_before_jiffies (birth + refresh_interval)) { +set_task_reclaim_state (current, &sc.reclaim_state); + flags =memalloc_noreclaim_save (); +/* +* setting can_swap=true and force_scan=true ensures +* proper workingset stats when the system cannot swap. +*/ +try_to_inc_max_seq (lruvec, max_seq, &sc, true, true); +memalloc_noreclaim_restore (flags); +set_task_reclaim_state (current,NULL ); + } + } }#endif /* CONFIG_WORKINGSET_REPORT */
mm/workingset_report.c +66 -15
123 lines not on this page · raw message
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Yuanchu Xie
reviewerPATCH 4/7 mm: report workingset during memory pressure driven scanning
In reply to Yuanchu XieWhen a node reaches its low watermarks and wakes up kswapd, notify all userspace programs waiting on the workingset page age histogram of the memory pressure, so a userspace agent can read the workingset report in time and make policy decisions, such as logging, oom-killing, or migration.
Sysfs interface:/sys/devices/system/node/nodeX/workingset_report/report_threshold
time in milliseconds that specifies how often the userspace agent can be notified for node memory pressure.
--- include/linux/workingset_report.h | 4 +++ mm/internal.h | 6 +++++ mm/vmscan.c | 44 +++++++++++++++++++++++++++++++ mm/workingset_report.c | 43 +++++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 1 deletion(-)
include/linux/workingset_report.h +4 -0
@@ -37,7 +37,11 @@ struct wsr_page_age_histo { };struct wsr_state { +unsigned long report_threshold;unsigned long refresh_interval; + +struct kernfs_node *page_age_sys_file; +/* breakdown of workingset by page age */ struct mutex page_age_lock;struct wsr_page_age_histo *page_age;
mm/internal.h +6 -0
@@ -209,8 +209,14 @@ extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason/* * in mm/wsr.c */ +void notify_workingset (struct mem_cgroup *memcg,struct pglist_data *pgdat);/* Requires wsr->page_age_lock held */ void wsr_refresh_scan (struct lruvec *lruvec,unsigned long refresh_interval); +#else +static inline void notify_workingset (struct mem_cgroup *memcg, +struct pglist_data *pgdat) +{ +}#endif /*
mm/vmscan.c +44 -0
68 lines not on this page · raw message
mm/workingset_report.c +42 -1
68 lines not on this page · raw message
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Yuanchu Xie
reviewerPATCH 5/7 mm: extend working set reporting to memcgs
In reply to Yuanchu XieBreak down the system-wide working set reporting into per-memcg reports, which aggregages its children hierarchically. The per-node working set reporting histograms and refresh/report threshold files are presented as memcg files, showing a report containing all the nodes.
The per-node page age interval is configurable in sysfs and not available per-memcg, while the refresh interval and report threshold are configured per-memcg.
Memcg interface:/sys/fs/cgroup/.../memory.workingset.page_age
The memcg equivalent of the sysfs workingset page age histogram, breaks down the workingset of this memcg and its children into page age intervals. Each node is prefixed with a node header and a newline. Non-proactive direct reclaim on this memcg can also wake up userspace agents that are waiting on this file. e.g. N0 1000 anon=0 file=0 2000 anon=0 file=0 3000 anon=0 file=0 4000 anon=0 file=0 5000 anon=0 file=0 18446744073709551615 anon=0 file=0
/sys/fs/cgroup/.../memory.workingset.refresh_interval
The memcg equivalent of the sysfs refresh interval. A per-node number of how much time a page age histogram is valid for, in milliseconds.
e.g.
echo N0=2000 > memory.workingset.refresh_interval
/sys/fs/cgroup/.../memory.workingset.report_threshold
The memcg equivalent of the sysfs report threshold. A per-node number of how often userspace agent waiting on the page age histogram can be woken up, in milliseconds.
e.g.
echo N0=1000 > memory.workingset.report_threshold
--- include/linux/memcontrol.h | 5 + include/linux/workingset_report.h | 6 +- mm/internal.h | 2 + mm/memcontrol.c | 178 +++++++++++++++++++++++++++++- mm/workingset_report.c | 12 +- 5 files changed, 198 insertions(+), 5 deletions(-)
include/linux/memcontrol.h +5 -0
@@ -335,6 +335,11 @@ struct mem_cgroup {struct lru_gen_mm_list mm_list;#endif +#ifdef CONFIG_WORKINGSET_REPORT +/* memory.workingset.page_age file */ +struct cgroup_file workingset_page_age_file; +#endif +struct mem_cgroup_per_node *nodeinfo[]; };
include/linux/workingset_report.h +5 -1
@@ -9,6 +9,7 @@ struct mem_cgroup;struct pglist_data ;struct node ;struct lruvec ; +struct cgroup_file ;#ifdef CONFIG_WORKINGSET_REPORT
@@ -40,7 +41,10 @@ struct wsr_state {unsigned long report_threshold;unsigned long refresh_interval; -struct kernfs_node *page_age_sys_file; +union { +struct kernfs_node *page_age_sys_file; +struct cgroup_file *page_age_cgroup_file; + };/* breakdown of workingset by page age */ struct mutex page_age_lock;
mm/internal.h +2 -0
@@ -212,6 +212,8 @@ extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reasonvoid notify_workingset (struct mem_cgroup *memcg,struct pglist_data *pgdat);/* Requires wsr->page_age_lock held */ void wsr_refresh_scan (struct lruvec *lruvec,unsigned long refresh_interval); +int workingset_report_intervals_parse (char *src, +struct wsr_report_bins *bins);#else static inline void notify_workingset (struct mem_cgroup *memcg,struct pglist_data *pgdat)
mm/memcontrol.c +177 -1
190 lines not on this page · raw message
mm/workingset_report.c +9 -3
@@ -37,9 +37,12 @@ void wsr_destroy_pgdat(struct pglist_data *pgdat)void wsr_init_lruvec (struct lruvec *lruvec) {struct wsr_state *wsr = &lruvec->wsr; +struct mem_cgroup *memcg =lruvec_memcg (lruvec);memset (wsr,0 ,sizeof (*wsr));mutex_init (&wsr->page_age_lock); +if (memcg && !mem_cgroup_is_root (memcg)) + wsr->page_age_cgroup_file = &memcg->workingset_page_age_file; }void wsr_destroy_lruvec (struct lruvec *lruvec)
@@ -51,8 +54,8 @@ void wsr_destroy_lruvec(struct lruvec *lruvec)memset (wsr,0 ,sizeof (*wsr)); } -static int workingset_report_intervals_parse (char *src, -struct wsr_report_bins *bins) +int workingset_report_intervals_parse (char *src, +struct wsr_report_bins *bins) {int err =0 , i =0 ;char *cur, *next =strim (src);
@@ -526,5 +529,8 @@ void notify_workingset(struct mem_cgroup *memcg, struct pglist_data *pgdat) {struct wsr_state *wsr = &mem_cgroup_lruvec (memcg, pgdat)->wsr; -kernfs_notify (wsr->page_age_sys_file); +if (mem_cgroup_is_root (memcg)) +kernfs_notify (wsr->page_age_sys_file); +else +cgroup_file_notify (wsr->page_age_cgroup_file); }
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Yuanchu Xie
reviewerPATCH 6/7 mm: add kernel aging thread for workingset reporting
In reply to Yuanchu XieFor reliable and timely aging on memcgs, one has to read the page age histograms on time. A kernel thread makes it easier by aging memcgs with valid refresh_interval when they can be refreshed, and also reduces the latency of any userspace consumers of the page age histogram.
The kerne aging thread is gated behind CONFIG_WORKINGSET_REPORT_AGING. Debugging stats may be added in the future for when aging cannot keep up with the configured refresh_interval.
--- include/linux/workingset_report.h | 11 ++- mm/Kconfig | 6 ++ mm/Makefile | 1 + mm/memcontrol.c | 8 +- mm/workingset_report.c | 15 +++- mm/workingset_report_aging.c | 127 ++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 mm/workingset_report_aging.c
include/linux/workingset_report.h +10 -1
@@ -63,7 +63,16 @@ void wsr_remove_sysfs(struct node *node);* The next refresh time is stored in refresh_time. */ bool wsr_refresh_report (struct wsr_state *wsr,struct mem_cgroup *root, -struct pglist_data *pgdat); +struct pglist_data *pgdat,unsigned long *refresh_time); + +#ifdef CONFIG_WORKINGSET_REPORT_AGING +void wsr_wakeup_aging_thread (void ); +#else /* CONFIG_WORKINGSET_REPORT_AGING */ +static inline void wsr_wakeup_aging_thread (void ) +{ +} +#endif /* CONFIG_WORKINGSET_REPORT_AGING */ +#else static inline void wsr_init_lruvec (struct lruvec *lruvec) {
mm/Kconfig +6 -0
@@ -1270,6 +1270,12 @@ config WORKINGSET_REPORT This option exports stats and events giving the user more insight into its memory working set. +config WORKINGSET_REPORT_AGING + bool "Workingset report kernel aging thread" + depends on WORKINGSET_REPORT + help + Performs aging on memcgs with their configured refresh intervals. + source "mm/damon/Kconfig" endmenu
mm/Makefile +1 -0
@@ -93,6 +93,7 @@ obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.o khugepaged.o obj-$(CONFIG_PAGE_COUNTER) += page_counter.o obj-$(CONFIG_MEMCG) += memcontrol.o vmpressure.o obj-$(CONFIG_WORKINGSET_REPORT) += workingset_report.o +obj-$(CONFIG_WORKINGSET_REPORT_AGING) += workingset_report_aging.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) += swap_cgroup.o endif
mm/memcontrol.c +6 -2
@@ -7060,12 +7060,12 @@ static ssize_t memory_ws_refresh_interval_write(struct kernfs_open_file *of, {unsigned int nid, msecs;struct wsr_state *wsr; +unsigned long old_interval;struct mem_cgroup *memcg =mem_cgroup_from_css (of_css (of));ssize_t ret =memory_wsr_threshold_parse (buf, nbytes, &nid, &msecs);if (ret <0 )return ret; - wsr = &mem_cgroup_lruvec (memcg,NODE_DATA (nid))->wsr;mutex_lock (&wsr->page_age_lock);
@@ -7084,9 +7084,13 @@ static ssize_t memory_ws_refresh_interval_write(struct kernfs_open_file *of, wsr->page_age =NULL ; } + old_interval =READ_ONCE (wsr->refresh_interval);WRITE_ONCE (wsr->refresh_interval,msecs_to_jiffies (msecs)); unlock:mutex_unlock (&wsr->page_age_lock); +if (ret >0 && msecs && + (!old_interval ||jiffies_to_msecs (old_interval) > msecs)) +wsr_wakeup_aging_thread ();return ret; }
@@ -7137,7 +7141,7 @@ static int memory_ws_page_age_show(struct seq_file *m, void *v)if (!READ_ONCE (wsr->page_age))continue ; -wsr_refresh_report (wsr, memcg,NODE_DATA (nid)); +wsr_refresh_report (wsr, memcg,NODE_DATA (nid),NULL );mutex_lock (&wsr->page_age_lock);if (!wsr->page_age) goto unlock;
mm/workingset_report.c +12 -3
@@ -258,7 +258,7 @@ static void copy_node_bins(struct pglist_data *pgdat, }bool wsr_refresh_report (struct wsr_state *wsr,struct mem_cgroup *root, -struct pglist_data *pgdat) +struct pglist_data *pgdat,unsigned long *refresh_time) {struct wsr_page_age_histo *page_age;unsigned long refresh_interval =READ_ONCE (wsr->refresh_interval);
@@ -275,10 +275,14 @@ bool wsr_refresh_report(struct wsr_state *wsr, struct mem_cgroup *root, goto unlock;if (page_age->timestamp &&time_is_after_jiffies (page_age->timestamp + refresh_interval)) - goto unlock; + goto time;refresh_scan (wsr, root, pgdat, refresh_interval);copy_node_bins (pgdat, page_age);refresh_aggregate (page_age, root, pgdat); + +time: +if (refresh_time) + *refresh_time = page_age->timestamp + refresh_interval; unlock:mutex_unlock (&wsr->page_age_lock);return !!page_age;
@@ -341,6 +345,7 @@ static ssize_t refresh_interval_store(struct kobject *kobj,unsigned int interval;int err;struct wsr_state *wsr =kobj_to_wsr (kobj); +unsigned long old_interval =0 ; err =kstrtouint (buf,0 , &interval);if (err)
@@ -362,9 +367,13 @@ static ssize_t refresh_interval_store(struct kobject *kobj, wsr->page_age =NULL ; } + old_interval =READ_ONCE (wsr->refresh_interval);WRITE_ONCE (wsr->refresh_interval,msecs_to_jiffies (interval)); unlock:mutex_unlock (&wsr->page_age_lock); +if (!err && interval && + (!old_interval ||jiffies_to_msecs (old_interval) > interval)) +wsr_wakeup_aging_thread ();return err ?: len; }
@@ -454,7 +463,7 @@ static ssize_t page_age_show(struct kobject *kobj, struct kobj_attribute *attr,int ret =0 ;struct wsr_state *wsr =kobj_to_wsr (kobj); -wsr_refresh_report (wsr,NULL ,kobj_to_pgdat (kobj)); +wsr_refresh_report (wsr,NULL ,kobj_to_pgdat (kobj),NULL );mutex_lock (&wsr->page_age_lock);if (!wsr->page_age)
mm/workingset_report_aging.c new file +127 -0
127 lines not on this page · raw message
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Yuanchu Xie
reviewerPATCH 7/7 selftest: test system-wide workingset reporting
In reply to Yuanchu XieA basic test that verifies the working set size of a simple memory accessor. It should work with or without the aging thread.
Question: I don't know how to best test file memory in selftests. Is there a place where I should put the temporary file? /tmp can be tmpfs mounted in many distros.
--- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + .../testing/selftests/mm/workingset_report.c | 317 +++++++++++++++++ .../testing/selftests/mm/workingset_report.h | 39 ++ .../selftests/mm/workingset_report_test.c | 332 ++++++++++++++++++ 5 files changed, 692 insertions(+) create mode 100644 tools/testing/selftests/mm/workingset_report.c create mode 100644 tools/testing/selftests/mm/workingset_report.h create mode 100644 tools/testing/selftests/mm/workingset_report_test.c
tools/testing/selftests/mm/.gitignore +1 -0
@@ -46,3 +46,4 @@ gup_longterm mkdirty va_high_addr_switch hugetlb_fault_after_madv +workingset_report_test
tools/testing/selftests/mm/Makefile +3 -0
@@ -70,6 +70,7 @@ TEST_GEN_FILES += ksm_tests TEST_GEN_FILES += ksm_functional_tests TEST_GEN_FILES += mdwe_test TEST_GEN_FILES += hugetlb_fault_after_madv +TEST_GEN_FILES += workingset_report_test ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
@@ -123,6 +124,8 @@ $(TEST_GEN_FILES): vm_util.c thp_settings.c $(OUTPUT)/uffd-stress: uffd-common.c $(OUTPUT)/uffd-unit-tests: uffd-common.c +$(OUTPUT)/workingset_report_test: workingset_report.c + ifeq ($(ARCH),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))
tools/testing/selftests/mm/workingset_report.c new file +317 -0
317 lines not on this page · raw message
tools/testing/selftests/mm/workingset_report.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef WORKINGSET_REPORT_H_ +#define WORKINGSET_REPORT_H_ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <fcntl.h> +#include <sys/stat.h> +#include <errno.h> +#include <stdint.h> +#include <sys/types.h> + +#define PAGETYPE_ANON 0 +#define PAGETYPE_FILE 1 +#define ANON_AND_FILE 2 + +int get_nr_nodes (void ); +int drop_pagecache (void ); + +long sysfs_get_refresh_interval (int nid); +int sysfs_set_refresh_interval (int nid,long interval); + +int sysfs_get_page_age_intervals_str (int nid,char *buf,int len); +int sysfs_set_page_age_intervals_str (int nid,const char *buf,int len); + +int sysfs_set_page_age_intervals (int nid,const char *const intervals[], +int nr_intervals); + +char *page_age_split_node (char *buf,int nid,char **next); +ssize_t sysfs_page_age_read (int nid,char *buf,size_t len); +ssize_t page_age_read (const char *buf,const char *interval,int pagetype); + +int alloc_file_workingset (void *arg); +void cleanup_file_workingset (void ); +int alloc_anon_workingset (void *arg); + +#endif /* WORKINGSET_REPORT_H_ */
tools/testing/selftests/mm/workingset_report_test.c new file +332 -0
332 lines not on this page · raw message
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
Re: [PATCH v1 7/7] selftest: test system-wide workingset reporting
Quoted 7 lines · Yuanchu Xie
A basic test that verifies the working set size of a simple memory accessor. It should work with or without the aging thread.
Question: I don't know how to best test file memory in selftests. Is there a place where I should put the temporary file? /tmp can be tmpfs mounted in many distros.
Quoted 7 lines
Signed-off-by Yuanchu Xie <[email protected]>--- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + .../testing/selftests/mm/workingset_report.c | 317 +++++++++++++++++ .../testing/selftests/mm/workingset_report.h | 39 ++ .../selftests/mm/workingset_report_test.c | 332 ++++++++++++++++++
Add this test to run_vmtests.sh as all the tests are run from this script.
Quoted 152 lines
5 files changed, 692 insertions(+) create mode 100644 tools/testing/selftests/mm/workingset_report.c create mode 100644 tools/testing/selftests/mm/workingset_report.h create mode 100644 tools/testing/selftests/mm/workingset_report_test.ctools/testing/selftests/mm/.gitignore +1
-0@@ -46,3 +46,4 @@ gup_longterm mkdirty va_high_addr_switch hugetlb_fault_after_madv +workingset_report_testtools/testing/selftests/mm/Makefile +3
-0@@ -70,6 +70,7 @@ TEST_GEN_FILES += ksm_tests TEST_GEN_FILES += ksm_functional_tests TEST_GEN_FILES += mdwe_test TEST_GEN_FILES += hugetlb_fault_after_madv +TEST_GEN_FILES += workingset_report_test ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty@@ -123,6 +124,8 @@ $(TEST_GEN_FILES): vm_util.c thp_settings.c $(OUTPUT)/uffd-stress: uffd-common.c $(OUTPUT)/uffd-unit-tests: uffd-common.c +$(OUTPUT)/workingset_report_test: workingset_report.c + ifeq ($(ARCH),x86_64) BINARIES_32 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_32)) BINARIES_64 := $(patsubst %,$(OUTPUT)/%,$(BINARIES_64))tools/testing/selftests/mm/workingset_report.c new file +111
-0@@ -0,0 +1,317 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "workingset_report.h" + +#include <stddef.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdbool.h> +#include <unistd.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/wait.h> + +#include "../kselftest.h" + +#define SYSFS_NODE_ONLINE "/sys/devices/system/node/online" +#define PROC_DROP_CACHES "/proc/sys/vm/drop_caches" + +/* Returns read len on success, or -errno on failure. */ +static ssize_t read_text (const char *path,char *buf,size_t max_len) +{ +ssize_t len; +int fd, err; +size_t bytes_read =0 ; + +if (!max_len) +return -EINVAL ; + + fd =open (path,O_RDONLY ); +if (fd <0 ) +return -errno; + +while (bytes_read < max_len -1 ) { + len =read (fd, buf + bytes_read, max_len -1 - bytes_read); + +if (len <=0 ) +break ; + bytes_read += len; + } + + buf[bytes_read] ='\0' ; + + err = -errno; +close (fd); +return len <0 ? err : bytes_read; +} + +/* Returns written len on success, or -errno on failure. */ +static ssize_t write_text (const char *path,const char *buf,ssize_t max_len) +{ +int fd, len, err; +size_t bytes_written =0 ; + + fd =open (path,O_WRONLY |O_APPEND ); +if (fd <0 ) +return -errno; + +while (bytes_written < max_len) { + len =write (fd, buf + bytes_written, max_len - bytes_written); + +if (len <0 ) +break ; + bytes_written += len; + } + + err = -errno; +close (fd); +return len <0 ? err : bytes_written; +} + +static long read_num (const char *path) +{ +char buf[21 ]; + +if (read_text (path, buf,sizeof (buf)) <=0 ) +return -1 ; +return (long )strtoul (buf,NULL ,10 ); +} + +static int write_num (const char *path,unsigned long n) +{ +char buf[21 ]; + +sprintf (buf,"%lu" , n); +if (write_text (path, buf,strlen (buf)) <0 ) +return -1 ; +return 0 ; +} + +long sysfs_get_refresh_interval (int nid) +{ +char file[128 ]; + +snprintf ( + file, +sizeof (file), +"/sys/devices/system/node/node%d/workingset_report/refresh_interval" , + nid); +return read_num (file); +} + +int sysfs_set_refresh_interval (int nid,long interval) +{ +char file[128 ]; + +snprintf ( + file, +sizeof (file), +"/sys/devices/system/node/node%d/workingset_report/refresh_interval" , + nid); +return write_num (file, interval); +}
tools/testing/selftests/mm/workingset_report.c line 121
Quoted 10 lines
tools/testing/selftests/mm/workingset_report.c new file +10
-0+ +int sysfs_get_page_age_intervals_str (int nid,char *buf,int len) +{ +char path[128 ]; + +snprintf ( + path, +sizeof (path), +"/sys/devices/system/node/node%d/workingset_report/page_age_intervals" , + nid);
Not sure if checkpatch reports this or not, write arguments directly after the snprintf instead of using so many spaces.
Quoted 566 lines
tools/testing/selftests/mm/workingset_report.c new file +196
-0+return read_text (path, buf, len); + +} + +int sysfs_set_page_age_intervals_str (int nid,const char *buf,int len) +{ +char path[128 ]; + +snprintf ( + path, +sizeof (path), +"/sys/devices/system/node/node%d/workingset_report/page_age_intervals" , + nid); +return write_text (path, buf, len); +} + +int sysfs_set_page_age_intervals (int nid,const char *const intervals[], +int nr_intervals) +{ +char file[128 ]; +char buf[1024 ]; +int i; +int err, len =0 ; + +for (i =0 ; i < nr_intervals; ++i) { + err =snprintf (buf + len,sizeof (buf) - len,"%s" , intervals[i]); + +if (err <0 ) +return err; + len += err; + +if (i < nr_intervals -1 ) { + err =snprintf (buf + len,sizeof (buf) - len,"," ); +if (err <0 ) +return err; + len += err; + } + } + +snprintf ( + file, +sizeof (file), +"/sys/devices/system/node/node%d/workingset_report/page_age_intervals" , + nid); +return write_text (file, buf, len); +} + +int get_nr_nodes (void ) +{ +char buf[22 ]; +char *found; + +if (read_text (SYSFS_NODE_ONLINE , buf,sizeof (buf)) <=0 ) +return -1 ; + found =strstr (buf,"-" ); +if (found) +return (int )strtoul (found +1 ,NULL ,10 ) +1 ; +return (long )strtoul (buf,NULL ,10 ) +1 ; +} + +int drop_pagecache (void ) +{ +return write_num (PROC_DROP_CACHES ,1 ); +} + +ssize_t sysfs_page_age_read (int nid,char *buf,size_t len) + +{ +char file[128 ]; + +snprintf (file, +sizeof (file), +"/sys/devices/system/node/node%d/workingset_report/page_age" , + nid); +return read_text (file, buf, len); +} + +/* +* Finds the first occurrence of "N<nid>\n" +* Modifies buf to terminate before the next occurrence of "N". +* Returns a substring of buf starting after "N<nid>\n" +*/ +char *page_age_split_node (char *buf,int nid,char **next) +{ +char node_str[5 ]; +char *found; +int node_str_len; + + node_str_len =snprintf (node_str,sizeof (node_str),"N%u\n" , nid); + +/* find the node prefix first */ + found =strstr (buf, node_str); +if (!found) { +ksft_print_msg ("cannot find '%s' in page_idle_age" , node_str); +return NULL ; + } + found += node_str_len; + + *next =strchr (found,'N' ); +if (*next) + *(*next -1 ) ='\0' ; + +return found; +} + +ssize_t page_age_read (const char *buf,const char *interval,int pagetype) +{ +static const char *const type[ANON_AND_FILE ] = {"anon=" ,"file=" }; +char *found; + + found =strstr (buf, interval); +if (!found) { +ksft_print_msg ("cannot find %s in page_age" , interval); +return -1 ; + } + found =strstr (found, type[pagetype]); +if (!found) { +ksft_print_msg ("cannot find %s in page_age" , type[pagetype]); +return -1 ; + } + found +=strlen (type[pagetype]); +return (long )strtoul (found,NULL ,10 ); +} + +static const char *TEMP_FILE ="/tmp/workingset_selftest" ; +void cleanup_file_workingset (void ) +{ +remove (TEMP_FILE ); +} + +int alloc_file_workingset (void *arg) +{ +int err =0 ; +char *ptr; +int fd; +int ppid; +char *mapped; +size_t size = (size_t )arg; +size_t page_size =getpagesize (); + + ppid =getppid (); + + fd =open (TEMP_FILE ,O_RDWR |O_CREAT ); +if (fd <0 ) { + err = -errno; +ksft_perror ("failed to open temp file\n" ); + goto cleanup; + } + +if (fallocate (fd,0 ,0 , size) <0 ) { + err = -errno; +ksft_perror ("fallocate" ); + goto cleanup; + } + + mapped = (char *)mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED , + fd,0 ); +if (mapped ==NULL ) { + err = -errno; +ksft_perror ("mmap" ); + goto cleanup; + } + +while (getppid () == ppid) { +sync (); +for (ptr = mapped; ptr < mapped + size; ptr += page_size) + *ptr = *ptr ^0xFF ; + } + +cleanup: +cleanup_file_workingset (); +return err; +} + +int alloc_anon_workingset (void *arg) +{ +char *buf, *ptr; +int ppid =getppid (); +size_t size = (size_t )arg; +size_t page_size =getpagesize (); + + buf =malloc (size); + +if (!buf) { +ksft_print_msg ("cannot allocate anon workingset" ); +exit (1 ); + } + +while (getppid () == ppid) { +for (ptr = buf; ptr < buf + size; ptr += page_size) + *ptr = *ptr ^0xFF ; + } + +free (buf); +return 0 ; +}tools/testing/selftests/mm/workingset_report.h new file +39
-0@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef WORKINGSET_REPORT_H_ +#define WORKINGSET_REPORT_H_ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <fcntl.h> +#include <sys/stat.h> +#include <errno.h> +#include <stdint.h> +#include <sys/types.h> + +#define PAGETYPE_ANON 0 +#define PAGETYPE_FILE 1 +#define ANON_AND_FILE 2 + +int get_nr_nodes (void ); +int drop_pagecache (void ); + +long sysfs_get_refresh_interval (int nid); +int sysfs_set_refresh_interval (int nid,long interval); + +int sysfs_get_page_age_intervals_str (int nid,char *buf,int len); +int sysfs_set_page_age_intervals_str (int nid,const char *buf,int len); + +int sysfs_set_page_age_intervals (int nid,const char *const intervals[], +int nr_intervals); + +char *page_age_split_node (char *buf,int nid,char **next); +ssize_t sysfs_page_age_read (int nid,char *buf,size_t len); +ssize_t page_age_read (const char *buf,const char *interval,int pagetype); + +int alloc_file_workingset (void *arg); +void cleanup_file_workingset (void ); +int alloc_anon_workingset (void *arg); + +#endif /* WORKINGSET_REPORT_H_ */ tools/testing/selftests/mm/workingset_report_test.c new file +319
-0@@ -0,0 +1,332 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "workingset_report.h" + +#include <stdlib.h> +#include <stdio.h> +#include <signal.h> +#include <time.h> + +#include "../clone3/clone3_selftests.h" + +#define REFRESH_INTERVAL 5000 +#define MB (x) (x << 20) + +static void sleep_ms (int milliseconds) +{ +struct timespec ts; + + ts.tv_sec = milliseconds /1000 ; + ts.tv_nsec = (milliseconds %1000 ) *1000000 ; +nanosleep (&ts,NULL ); +} + +/* +* Checks if two given values differ by less than err% of their sum. +*/ +static inline int values_close (long a,long b,int err) +{ +return labs (a - b) <= (a + b) /100 * err; +} + +static const char *const PAGE_AGE_INTERVALS [] = { +"6000" ,"10000" ,"15000" ,"18446744073709551615" , +}; +#define NR_PAGE_AGE_INTERVALS (ARRAY_SIZE(PAGE_AGE_INTERVALS)) + +static int set_page_age_intervals_all_nodes (const char *intervals,int nr_nodes) +{ +int i; + +for (i =0 ; i < nr_nodes; ++i) { +int err =sysfs_set_page_age_intervals_str ( + i, &intervals[i *1024 ],strlen (&intervals[i *1024 ])); + +if (err <0 ) +return err; + } +return 0 ; +} + +static int get_page_age_intervals_all_nodes (char *intervals,int nr_nodes) +{ +int i; + +for (i =0 ; i < nr_nodes; ++i) { +int err =sysfs_get_page_age_intervals_str ( + i, &intervals[i *1024 ],1024 ); + +if (err <0 ) +return err; + } +return 0 ; +} + +static int set_refresh_interval_all_nodes (const long *interval,int nr_nodes) +{ +int i; + +for (i =0 ; i < nr_nodes; ++i) { +int err =sysfs_set_refresh_interval (i, interval[i]); + +if (err <0 ) +return err; + } +return 0 ; +} + +static int get_refresh_interval_all_nodes (long *interval,int nr_nodes) +{ +int i; + +for (i =0 ; i < nr_nodes; ++i) { +long val =sysfs_get_refresh_interval (i); + +if (val <0 ) +return val; + interval[i] = val; + } +return 0 ; +} + +static pid_t clone_and_run (int fn (void *arg),void *arg) +{ +pid_t pid; + +struct __clone_args args = { + .exit_signal =SIGCHLD , + }; + + pid =sys_clone3 (&args,sizeof (struct __clone_args )); + +if (pid ==0 ) +exit (fn (arg)); + +return pid; +} + +static int read_workingset (int pagetype,int nid, +unsigned long page_age[NR_PAGE_AGE_INTERVALS ]) +{ +int i, err; +char buf[4096 ]; + + err =sysfs_page_age_read (nid, buf,sizeof (buf)); +if (err <0 ) +return err; + +for (i =0 ; i <NR_PAGE_AGE_INTERVALS ; ++i) { + err =page_age_read (buf,PAGE_AGE_INTERVALS [i], pagetype); +if (err <0 ) +return err; + page_age[i] = err; + } + +return 0 ; +} + +static ssize_t read_interval_all_nodes (int pagetype,int interval) +{ +int i, err; +unsigned long page_age[NR_PAGE_AGE_INTERVALS ]; +ssize_t ret =0 ; +int nr_nodes =get_nr_nodes (); + +for (i =0 ; i < nr_nodes; ++i) { + err =read_workingset (pagetype, i, page_age); +if (err <0 ) +return err; + + ret += page_age[interval]; + } + +return ret; +} + +#define TEST_SIZE MB(500l) + +static int run_test (int f (void )) +{ +int i, err, test_result; +long *old_refresh_intervals; +long *new_refresh_intervals; +char *old_page_age_intervals; +int nr_nodes =get_nr_nodes (); + +if (nr_nodes <=0 ) { +ksft_print_msg ("failed to get nr_nodes\n" ); +return KSFT_FAIL ; + } + + old_refresh_intervals =calloc (nr_nodes,sizeof (long )); + new_refresh_intervals =calloc (nr_nodes,sizeof (long )); + old_page_age_intervals =calloc (nr_nodes,1024 ); + +if (!(old_refresh_intervals && new_refresh_intervals && + old_page_age_intervals)) { +ksft_print_msg ("failed to allocate memory for intervals\n" ); +return KSFT_FAIL ; + } + + err =get_refresh_interval_all_nodes (old_refresh_intervals, nr_nodes); +if (err <0 ) { +ksft_print_msg ("failed to read refresh interval\n" ); +return KSFT_FAIL ; + } + + err =get_page_age_intervals_all_nodes (old_page_age_intervals, nr_nodes); +if (err <0 ) { +ksft_print_msg ("failed to read page age interval\n" ); +return KSFT_FAIL ; + } + +for (i =0 ; i < nr_nodes; ++i) + new_refresh_intervals[i] =REFRESH_INTERVAL ; + +for (i =0 ; i < nr_nodes; ++i) { + err =sysfs_set_page_age_intervals (i,PAGE_AGE_INTERVALS , +NR_PAGE_AGE_INTERVALS -1 ); +if (err <0 ) { +ksft_print_msg ("failed to set page age interval\n" ); + test_result =KSFT_FAIL ; + goto fail; + } + } + + err =set_refresh_interval_all_nodes (new_refresh_intervals, nr_nodes); +if (err <0 ) { +ksft_print_msg ("failed to set refresh interval\n" ); + test_result =KSFT_FAIL ; + goto fail; + } + +sync (); +drop_pagecache (); + + test_result =f (); + +fail: + err =set_refresh_interval_all_nodes (old_refresh_intervals, nr_nodes); +if (err <0 ) { +ksft_print_msg ("failed to restore refresh interval\n" ); + test_result =KSFT_FAIL ; + } + err =set_page_age_intervals_all_nodes (old_page_age_intervals, nr_nodes); +if (err <0 ) { +ksft_print_msg ("failed to restore page age interval\n" ); + test_result =KSFT_FAIL ; + } +return test_result; +} + +static int test_file (void ) +{ +ssize_t ws_size_ref, ws_size_test; +int ret =KSFT_FAIL , i; +pid_t pid =0 ; + + ws_size_ref =read_interval_all_nodes (PAGETYPE_FILE ,0 ); +if (ws_size_ref <0 ) + goto cleanup; + + pid =clone_and_run (alloc_file_workingset, (void *)TEST_SIZE ); +if (pid <0 ) + goto cleanup; + +read_interval_all_nodes (PAGETYPE_FILE ,0 ); +sleep_ms (REFRESH_INTERVAL ); + +for (i =0 ; i <3 ; ++i) { +sleep_ms (REFRESH_INTERVAL ); + ws_size_test =read_interval_all_nodes (PAGETYPE_FILE ,0 ); + ws_size_test +=read_interval_all_nodes (PAGETYPE_FILE ,1 ); +if (ws_size_test <0 ) + goto cleanup; + +if (!values_close (ws_size_test - ws_size_ref,TEST_SIZE ,10 )) { +ksft_print_msg ( +"file working set size difference too large: actual=%ld, expected=%ld\n" , + ws_size_test - ws_size_ref,TEST_SIZE ); + goto cleanup; + } + } + ret =KSFT_PASS ; + +cleanup: +if (pid >0 ) +kill (pid,SIGKILL ); +cleanup_file_workingset (); +return ret; +} + +static int test_anon (void ) +{ +ssize_t ws_size_ref, ws_size_test; +pid_t pid =0 ; +int ret =KSFT_FAIL , i; + + ws_size_ref =read_interval_all_nodes (PAGETYPE_ANON ,0 ); +if (ws_size_ref <0 ) + goto cleanup; + + pid =clone_and_run (alloc_anon_workingset, (void *)TEST_SIZE ); +if (pid <0 ) + goto cleanup; + +sleep_ms (REFRESH_INTERVAL ); +read_interval_all_nodes (PAGETYPE_ANON ,0 ); + +for (i =0 ; i <5 ; ++i) { +sleep_ms (REFRESH_INTERVAL ); + ws_size_test =read_interval_all_nodes (PAGETYPE_ANON ,0 ); + ws_size_test +=read_interval_all_nodes (PAGETYPE_ANON ,1 ); +if (ws_size_test <0 ) + goto cleanup; + +if (!values_close (ws_size_test - ws_size_ref,TEST_SIZE ,10 )) { +ksft_print_msg ( +"anon working set size difference too large: actual=%ld, expected=%ld\n" , + ws_size_test - ws_size_ref,TEST_SIZE ); + goto cleanup; + } + } + ret =KSFT_PASS ; + +cleanup: +if (pid >0 ) +kill (pid,SIGKILL ); +return ret; +} + + +#define T (x) { x, #x } +struct workingset_test { +int (*fn)(void ); +const char *name; +} tests[] = { +T (test_anon), +T (test_file), +}; +#undef T + +int main (int argc,char **argv) +{ +int ret =EXIT_SUCCESS , i, err; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { + err =run_test (tests[i].fn); +switch (err) { +case KSFT_PASS : +ksft_test_result_pass ("%s\n" , tests[i].name);
tools/testing/selftests/mm/workingset_report_test.c line 329
Quoted 10 lines
tools/testing/selftests/mm/workingset_report_test.c new file +10
-0+break ; +case KSFT_SKIP : +ksft_test_result_skip ("%s\n" , tests[i].name); +break ; +default : + ret =EXIT_FAILURE ; +ksft_test_result_fail ("%s with error %d\n" , + tests[i].name, err); +break ; + }
Replace this switch-case with ksft_test_result*().
Quoted 3 lines
tools/testing/selftests/mm/workingset_report_test.c new file +3
-0+ } +return ret; +}
Signature · 2 lines
BR, Muhammad Usama Anjum
Hi, Yuanchu,
Quoted 68 lines · Yuanchu Xie
Changes from RFC v3 -> PATCH v1:
- Updated selftest to useksft_print_msginstead offprintf(stderr, ...)(Muhammad Usama Anjum)
- Included more detail in patch skippingpmd_youngwithforce_scan(Huang, Ying)
- Deferred reaccess histogram as a followup
- Removed per-memcg page age interval configs for simplicityChanges from RFC v2 -> RFC v3:
- Update to v6.8
- Added an aging kernel thread (gated behind config)
- Added basic selftests for sysfs interface files
- Track swapped out pages for reaccesses
- Refactoring and cleanup
- Dropped the virtio-balloon extension to make things manageableChanges from RFC v1 -> RFC v2:
- Refactored the patchs into smaller pieces
- Renamed interfaces and functions from wss to wsr (Working Set Reporting)
- Fixed build errors whenCONFIG_WSRis not set
- Changedworking_set_num_binsto u8 for virtio-balloon
- Added support for per-NUMA node reporting for virtio-balloon[rfc v1]
https://lore.kernel.org/linux-mm/[email protected]/
[rfc v2]
https://lore.kernel.org/linux-mm/[email protected]/
[rfc v3]
https://lore.kernel.org/linux-mm/[email protected]/This patch series provides workingset reporting of user pages in lruvecs, of which coldness can be tracked by accessed bits and fd references. However, the concept of workingset applies generically to all types of memory, which could be kernel slab caches, discardable userspace caches (databases), or CXL.mem. Therefore, data sources might come from slab shrinkers, device drivers, or the userspace. IMO, the kernel should provide a set of workingset interfaces that should be generic enough to accommodate the various use cases, and be extensible to potential future use cases. The current proposed interfaces are not sufficient in that regard, but I would like to start somewhere, solicit feedback, and iterate.
Use cases
==========
Job scheduling
On overcommitted hosts, workingset information allows the job scheduler to right-size each job and land more jobs on the same host or NUMA node, and in the case of a job with increasing workingset, policy decisions can be made to migrate other jobs off the host/NUMA node, or oom-kill the misbehaving job. If the job shape is very different from the machine shape, knowing the workingset per-node can also help inform page allocation policies.Proactive reclaim
Workingset information allows the a container manager to proactively reclaim memory while not impacting a job's performance. While PSI may provide a reactive measure of when a proactive reclaim has reclaimed too much, workingset reporting allows the policy to be more accurate and flexible.Ballooning (similar to proactive reclaim)
While this patch series does not extend the virtio-balloon device, balloon policies benefit from workingset to more precisely determine the size of the memory balloon. On desktops/laptops/mobile devices where memory is scarce and overcommitted, the balloon sizing in multiple VMs running on the same device can be orchestrated with workingset reports from each one.
Quoted 8 lines
Promotion/Demotion
Similar to proactive reclaim, a workingset report enables demotion to a slower tier of memory.
For promotion, the workingset report interfaces need to be extended to report hotness and gather hotness information from the devices[1].[1]
https://www.opencompute.org/documents/ocp-cms-hotness-tracking-requirements-white-paper-pdf-1
This mechanism can also provide a way to control the pages ping-pong caused by promotion and demotion. If different methods are used for promotion and demotion, some pages may be migrated between the fast and slow memory back and forth. Because there's no connection between promotion side and demotion side. For example, if we use NUMA balancing based promotion and MGLRU based demotion.
If we use workingset reporting on the fast memory nodes, then we can establish the connection between demotion and promotion with it. For example, the promotion hot pages threshold (determined as accessed every N seconds) should be set to make 80% of the fast memory pages pass the threshold. This can be calculated with the workingset reporting statistics.
Quoted 51 lines
Sysfs and Cgroup Interfaces
==========
The interfaces are detailed in the patches that introduce them. The main idea here is we break down the workingset per-node per-memcg into time intervals (ms), e.g.1000 anon=137368 file=24530
20000 anon=34342 file=0
30000 anon=353232 file=333608
40000 anon=407198 file=206052
9223372036854775807 anon=4925624 file=892892I realize this does not generalize well to hotness information, but I lack the intuition for an abstraction that presents hotness in a useful way. Please advise.
Implementation
==========
Currently, the reporting of user pages is based off of MGLRU, and therefore requiresCONFIG_LRU_GEN=y. We would benefit from more MGLRU generations for a more fine-grained workingset report. I will make the generation count configurable in the next version. The workingset reporting mechanism is gated behindCONFIG_WORKINGSET_REPORT, and the aging thread is behindCONFIG_WORKINGSET_REPORT_AGING.Yuanchu Xie (7):
mm: multi-gen LRU: ignore non-leafpmd_youngfor force_scan=true
mm: aggregate working set information into histograms
mm: use refresh interval to rate-limit workingset report aggregation
mm: report workingset during memory pressure driven scanning
mm: extend working set reporting to memcgs
mm: add kernel aging thread for workingset reporting
selftest: test system-wide workingset reportingdrivers/base/node.c | 6 + include/linux/memcontrol.h | 5 + include/linux/mmzone.h | 9 + include/linux/workingset_report.h | 97 ++++ mm/Kconfig | 15 + mm/Makefile | 2 + mm/internal.h | 17 + mm/memcontrol.c | 184 +++++- mm/mm_init.c | 2 + mm/mmzone.c | 2 + mm/vmscan.c | 85 ++- mm/workingset_report.c | 545 ++++++++++++++++++ mm/workingset_report_aging.c | 127 ++++ tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 3 + .../testing/selftests/mm/workingset_report.c | 317 ++++++++++ .../testing/selftests/mm/workingset_report.h | 39 ++
Quoted 8 lines
.../selftests/mm/workingset_report_test.c | 332 +++++++++++ 18 files changed, 1786 insertions(+), 2 deletions(-) create mode 100644 include/linux/workingset_report.h create mode 100644 mm/workingset_report.c create mode 100644 mm/workingset_report_aging.c create mode 100644 tools/testing/selftests/mm/workingset_report.c create mode 100644 tools/testing/selftests/mm/workingset_report.h create mode 100644 tools/testing/selftests/mm/workingset_report_test.c
--
Best Regards,
Huang, Ying