mm/ksm: Add ksm advisor
Subsystems (5)
v2 cover mm/ksm: Add ksm advisor
What is the KSM advisor?
=========================
The ksm advisor automatically manages the pages_to_scan setting to achieve a target scan time. The target scan time defines how many seconds it should take to scan all the candidate KSM pages. In other words the pages_to_scan rate is changed by the advisor to achieve the target scan time.
Why do we need a KSM advisor?
==============================
The number of candidate pages for KSM is dynamic. It can often be observed that during the startup of an application more candidate pages need to be processed. Without an advisor the pages_to_scan parameter needs to be sized for the maximum number of candidate pages. With the scan time advisor the pages_to_scan parameter based can be changed based on demand.
Algorithm
==========
The algorithm calculates the change value based on the target scan time and the previous scan time. To avoid pertubations an exponentially weighted moving average is applied.
The algorithm has a max and min
value to:
- guarantee responsiveness to changes
- to avoid to spend too much CPU
Parameters to influence the KSM scan advisor
=============================================
The respective parameters are:
- ksm_advisor_mode
0: None (default), 1: scan time advisor
- ksm_advisor_target_scan_time
how many seconds a scan should of all candidate pages take
- ksm_advisor_min_cpu
lower limit for the cpu usage in percent of the ksmd background thread
- ksm_advisor_max_cpu
upper limit for the cpu usage in percent of the ksmd background thread
The initial value and the max value for the pages_to_scan parameter can be limited with:
- ksm_advisor_min_pages
minimum value for pages_to_scan per batch
- ksm_advisor_max_pages
maximum value for pages_to_scan per batch
The default settings for the above two parameters should be suitable for most workloads.
The parameters are exposed as knobs in /sys/kernel/mm/ksm. By default the scan time advisor is disabled.
Currently there are two advisors:
- none and
- scan time.
Resource savings
=================
Tests with various workloads have shown considerable CPU savings. Most of the workloads I have investigated have more candidate pages during startup. Once the workload is stable in terms of memory, the number of candidate pages is reduced. Without the advisor, the pages_to_scan needs to be sized for the maximum number of candidate pages. So having this advisor definitely helps in reducing CPU consumption.
For the instagram workload, the advisor achieves a 25% CPU reduction. Once the memory is stable, the pages_to_scan parameter gets reduced to about 40% of its max value.
The new advisor works especially well if the smart scan feature is also enabled.
How is defining a target scan time better?
===========================================
For an administrator it is more logical to set a target scan time.. The administrator can determine how many pages are scanned on each scan. Therefore setting a target scan time makes more sense.
In addition the administrator might have a good idea about the memory sizing of its respective workloads.
Setting cpu limits is easier than setting The pages_to_scan parameter. The pages_to_scan parameter is per batch. For the administrator it is difficult to set the pages_to_scan parameter.
Tracing
=======
A new tracing event has been added for the scan time advisor. The new trace event is called ksm_advisor. It reports the scan time, the new pages_to_scan setting and the cpu usage of the ksmd background thread.
Other approaches
=================
Approach 1: Adapt pages_to_scan after processing each batch. If KSM
merges pages, increase the scan rate, if less KSM pages, reduce the the pages_to_scan rate. This doesn't work too well. While it increases the pages_to_scan for a short period, but generally it ends up with a too low pages_to_scan rate.
Approach 2: Adapt pages_to_scan after each scan. The problem with that
approach is that the calculated scan rate tends to be high. The more aggressive KSM scans, the more pages it can de-duplicate.
There have been earlier attempts at an advisor:
propose auto-run mode of ksm and its tests
(https://marc.info/?l=linux-mm&m=166029880214485&w=2)
Changes:
========
V2:
- Use functions for long long calculations to support 32 bit platforms
- Use cpu min and cpu max settings for the advisor instead of the pages min and max parameters.
- pages min and max values are now used for the initial and max values. Generally they are not required to be changed.
- Add cpu percent usage value to tracepoint definition
- Update documentation for cpu min and cpu max values
- Update commit messages for the above changes
Stefan Roesch (4):
mm/ksm: add ksm advisor
mm/ksm: add sysfs knobs for advisor mm/ksm: add tracepoint for ksm advisor mm/ksm: document ksm advisor and its sysfs knobs
Documentation/admin-guide/mm/ksm.rst | 66 ++++++ include/trace/events/ksm.h | 33 +++ mm/ksm.c | 314 ++++++++++++++++++++++++++- 3 files changed, 412 insertions(+), 1 deletion(-)
base-commit: 12d04a7bf0da67321229d2bc8b1a7074d65415a9
Signature · 1 line
2.39.3
PATCH v2 1/4 mm/ksm: add ksm advisor
This adds the ksm advisor. The ksm advisor automatically manages the pages_to_scan setting to achieve a target scan time. The target scan time defines how many seconds it should take to scan all the candidate KSM pages. In other words the pages_to_scan rate is changed by the advisor to achieve the target scan time. The algorithm has a max and min value to:
- guarantee responsiveness to changes
- to avoid to spend too much CPU
The respective parameters are:
- ksm_advisor_target_scan_time (how many seconds a scan should take)
- ksm_advisor_min_cpu (minimum value for cpu percent usage)
- ksm_advisor_max_cpu (maximum value for cpu percent usage)
- ksm_advisor_min_pages (minimum value for pages_to_scan per batch)
- ksm_advisor_max_pages (maximum value for pages_to_scan per batch)
The algorithm calculates the change value based on the target scan time and the previous scan time. To avoid pertubations an exponentially weighted moving average is applied.
The advisor is managed by three main parameters: target scan time, cpu min time and cpu max time for the ksmd background thread. These parameters determine how aggresive ksmd scans.
In addition there are min and max values for the pages_to_scan parameter to make sure that its initial and max values are not set too low or too high. This ensures that it is able to react to changes quickly enough.
The default values are:
- target scan time: 200 secs
- min cpu: 15%
- max cpu: 70%
- min pages: 500
- max pages: 30000
By default the advisor is disabled. Currently there are two advisors:
none and scan_time.
Tests with various workloads have shown considerable CPU savings. Most of the workloads I have investigated have more candidate pages during startup, once the workload is stable in terms of memory, the number of candidate pages is reduced. Without the advisor, the pages_to_scan needs to be sized for the maximum number of candidate pages. So having this advisor definitely helps in reducing CPU consumption.
For the instagram workload, the advisor achieves a 25% CPU reduction. Once the memory is stable, the pages_to_scan parameter gets reduced to about 40% of its max value.
--- mm/ksm.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-)
mm/ksm.c +158 -1
@@ -21,6 +21,7 @@#include <linux/sched.h> #include <linux/sched/mm.h> #include <linux/sched/coredump.h> +#include <linux/sched/cputime.h> #include <linux/rwsem.h> #include <linux/pagemap.h> #include <linux/rmap.h>
@@ -248,6 +249,9 @@ static struct kmem_cache *rmap_item_cache;static struct kmem_cache *stable_node_cache;static struct kmem_cache *mm_slot_cache; +/* Default number of pages to scan per batch */ +#define DEFAULT_PAGES_TO_SCAN 100 +/* The number of pages scanned */ static unsigned long ksm_pages_scanned;
@@ -276,7 +280,7 @@ static unsigned int ksm_stable_node_chains_prune_millisecs = 2000;static int ksm_max_page_sharing =256 ;/* Number of pages ksmd should scan in one batch */ -static unsigned int ksm_thread_pages_to_scan =100 ; +static unsigned int ksm_thread_pages_to_scan =DEFAULT_PAGES_TO_SCAN ;/* Milliseconds ksmd should sleep between batches */ static unsigned int ksm_thread_sleep_millisecs =20 ;
@@ -297,6 +301,155 @@ unsigned long ksm_zero_pages;/* The number of pages that have been skipped due to "smart scanning" */ static unsigned long ksm_pages_skipped; +/* Don't scan more than max pages per batch. */ +static unsigned long ksm_advisor_max_pages =30000 ; + +/* At least scan this many pages per batch. */ +static unsigned long ksm_advisor_min_pages =500 ; + +/* Min CPU for scanning pages per scan */ +static unsigned int ksm_advisor_min_cpu =15 ; + +/* Max CPU for scanning pages per scan */ +static unsigned int ksm_advisor_max_cpu =70 ; + +/* Target scan time in seconds to analyze all KSM candidate pages. */ +static unsigned long ksm_advisor_target_scan_time =200 ; + +/* Exponentially weighted moving average. */ +#define EWMA_WEIGHT 30 + +/** +* struct advisor_ctx - metadata for KSM advisor +* @start_scan: start time of the current scan +* @scan_time: scan time of previous scan +* @change: change in percent to pages_to_scan parameter +* @cpu_percent: average cpu percent usage of the ksmd thread for the last scan +*/ +struct advisor_ctx { +ktime_t start_scan; +unsigned long scan_time; +unsigned long change; +unsigned long long cpu_time; +}; +static struct advisor_ctx advisor_ctx; + +/* Define different advisor's */ +enum ksm_advisor_type { +KSM_ADVISOR_NONE , +KSM_ADVISOR_FIRST =KSM_ADVISOR_NONE , +KSM_ADVISOR_SCAN_TIME , +KSM_ADVISOR_LAST =KSM_ADVISOR_SCAN_TIME +}; +static enum ksm_advisor_type ksm_advisor; + +static void init_advisor (void ) +{ + advisor_ctx.start_scan =0 ; + advisor_ctx.scan_time =0 ; + advisor_ctx.change =0 ; + advisor_ctx.cpu_time =0 ; +} + +/* +* Use previous scan time if available, otherwise use current scan time as an +* approximation for the previous scan time. +*/ +static inline unsigned long prev_scan_time (struct advisor_ctx *ctx, +unsigned long scan_time) +{ +return ctx->scan_time ? ctx->scan_time : scan_time; +} + +/* Calculate exponential weighted moving average */ +static unsigned long ewma (unsigned long prev,unsigned long curr) +{ +return ((100 -EWMA_WEIGHT ) * prev +EWMA_WEIGHT * curr) /100 ; +} + +/* +* The scan time advisor is based on the current scan rate and the target +* scan rate. +* +* new_pages_to_scan = pages_to_scan * (scan_time / target_scan_time) +* +* To avoid pertubations it calculates a change factor of previous changes. +* A new change factor is calculated for each iteration and it uses an +* exponentially weighted moving average. The new pages_to_scan value is +* multiplied with that change factor: +* +* new_pages_to_scan *= change facor +* +* In addition the new pages_to_scan value is capped by the max and min +* limits. +*/ +static void scan_time_advisor (unsigned long scan_time) +{ +unsigned int cpu_percent; +unsigned long cpu_time; +unsigned long cpu_time_diff; +unsigned long cpu_time_diff_ms; +unsigned long pages; +unsigned long per_page_cost; +unsigned long factor; +unsigned long change; +unsigned long last_scan_time; + + cpu_time =task_sched_runtime (current); + cpu_time_diff = cpu_time - advisor_ctx.cpu_time; + cpu_time_diff_ms = cpu_time_diff /1000 /1000 ; + + cpu_percent = (cpu_time_diff_ms *100 ) / (scan_time *1000 ); + cpu_percent = cpu_percent ? cpu_percent :1 ; + last_scan_time =prev_scan_time (&advisor_ctx, scan_time); + +/* Calculate scan time as percentage of target scan time */ + factor = ksm_advisor_target_scan_time *100 / scan_time; + factor = factor ? factor :1 ; + +/* +* Calculate scan time as percentage of last scan time and use +* exponentially weighted average to smooth it +*/ + change = scan_time *100 / last_scan_time; + change = change ? change :1 ; + change =ewma (advisor_ctx.change, change); + +/* Calculate new scan rate based on target scan rate. */ + pages = ksm_thread_pages_to_scan *100 / factor; +/* Update pages_to_scan by weighted change percentage. */ + pages = pages * change /100 ; + +/* Cap new pages_to_scan value */ + per_page_cost = ksm_thread_pages_to_scan / cpu_percent; + per_page_cost = per_page_cost ? per_page_cost :1 ; + + pages =min (pages, per_page_cost * ksm_advisor_max_cpu); + pages =max (pages, per_page_cost * ksm_advisor_min_cpu); + pages =min (pages, ksm_advisor_max_pages); + +/* Update advisor context */ + advisor_ctx.change = change; + advisor_ctx.scan_time = scan_time; + advisor_ctx.cpu_time = cpu_time; + + ksm_thread_pages_to_scan = pages; +} + +static void run_advisor (void ) +{ +if (ksm_advisor ==KSM_ADVISOR_SCAN_TIME ) { +s64 scan_time; + +/* Convert scan time to seconds */ + scan_time =ktime_ms_delta (ktime_get (), advisor_ctx.start_scan); + scan_time =div_s64 (scan_time,MSEC_PER_SEC ); + scan_time = scan_time ? scan_time :1 ; + +scan_time_advisor ((unsigned long )scan_time); + } +} +#ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes =1 ;
@@ -2401,6 +2554,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) mm_slot = ksm_scan.mm_slot;if (mm_slot == &ksm_mm_head) { + advisor_ctx.start_scan =ktime_get ();trace_ksm_start_scan (ksm_scan.seqnr, ksm_rmap_items);/*
@@ -2558,6 +2712,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)if (mm_slot != &ksm_mm_head) goto next_mm; +run_advisor (); +trace_ksm_stop_scan (ksm_scan.seqnr, ksm_rmap_items); ksm_scan.seqnr++;return NULL ;
@@ -3603,6 +3759,7 @@ static int __init ksm_init(void) zero_checksum =calc_checksum (ZERO_PAGE (0 ));/* Default to false for backwards compatibility */ ksm_use_zero_pages = false; +init_advisor (); err =ksm_slab_init ();if (err)
Signature · 1 line
2.39.3
kernel test robot
build botRe: [PATCH v2 1/4] mm/ksm: add ksm advisor
kernel test robot noticed the following build warnings:
Hi Stefan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 12d04a7bf0da67321229d2bc8b1a7074d65415a9]
url: https://github.com/intel-lab-lkp/linux/commits/Stefan-Roesch/mm-ksm-add-ksm-advisor/20231028-081347 base: 12d04a7bf0da67321229d2bc8b1a7074d65415a9 patch link: https://lore.kernel.org/r/20231028000945.2428830-2-shr%40devkernel.io patch subject: [PATCH v2 1/4] mm/ksm: add ksm advisor config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231028/[email protected]/config) compiler: m68k-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231028/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> mm/ksm.c:334: warning: Function parameter or member 'cpu_time' not described in 'advisor_ctx'
vim +334 mm/ksm.c
321
322 /**
323 * struct advisor_ctx - metadata for KSM advisor
324 * @start_scan: start time of the current scan
325 * @scan_time: scan time of previous scan
326 * @change: change in percent to pages_to_scan parameter
327 * @cpu_percent: average cpu percent usage of the ksmd thread for the last scan
328 */
329 struct advisor_ctx {
330 ktime_t start_scan;
331 unsigned long scan_time;
332 unsigned long change;
333 unsigned long long cpu_time;
> 334 };
335 static struct advisor_ctx advisor_ctx;
336
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v2 1/4] mm/ksm: add ksm advisor
In reply to Stefan RoeschQuoted 29 lines · Stefan Roesch
This adds the ksm advisor. The ksm advisor automatically manages the
pages_to_scansetting to achieve a target scan time. The target scan time defines how many seconds it should take to scan all the candidate KSM pages. In other words thepages_to_scanrate is changed by the advisor to achieve the target scan time. The algorithm has a max and min value to:
- guarantee responsiveness to changes
- to avoid to spend too much CPUThe respective parameters are:
-ksm_advisor_target_scan_time(how many seconds a scan should take)
-ksm_advisor_min_cpu(minimum value for cpu percent usage)
-ksm_advisor_max_cpu(maximum value for cpu percent usage)-
ksm_advisor_min_pages(minimum value forpages_to_scanper batch)
-ksm_advisor_max_pages(maximum value forpages_to_scanper batch)The algorithm calculates the change value based on the target scan time and the previous scan time. To avoid pertubations an exponentially weighted moving average is applied.
The advisor is managed by three main parameters: target scan time, cpu min time and cpu max time for the ksmd background thread. These parameters determine how aggresive ksmd scans.
In addition there are min and max values for the
pages_to_scanparameter to make sure that its initial and max values are not set too low or too high. This ensures that it is able to react to changes quickly enough.
Quoted 6 lines
The default values are:
- target scan time: 200 secs
- min cpu: 15%
- max cpu: 70%
- min pages: 500
- max pages: 30000
Do we really need the min cpu load? The target scan time combined with the max CPU load should be sufficient, no?
Internally, we might want some sane default/min start value, but exposing that to the user is questionable.
For example, if I have exactly two possible KSM pages in the system, why should my cpu dedicate 15% to scanning nothing after merging them? :)
[...]
Quoted 9 lines
mm/ksm.c +9
-0+/** +* struct advisor_ctx - metadata for KSM advisor +* @start_scan: start time of the current scan +* @scan_time: scan time of previous scan +* @change: change in percent to pages_to_scan parameter +* @cpu_percent: average cpu percent usage of the ksmd thread for the last scan +*/ +struct advisor_ctx { +ktime_t start_scan;
mm/ksm.c unsigned long ksm_zero_pages; · line 340
Quoted 10 lines
mm/ksm.c +10
-0+unsigned long scan_time; +unsigned long change; +unsigned long long cpu_time; +}; +static struct advisor_ctx advisor_ctx; + +/* Define different advisor's */ +enum ksm_advisor_type { +KSM_ADVISOR_NONE , +KSM_ADVISOR_FIRST =KSM_ADVISOR_NONE ,
Unused, better drop it. 0 is the implicit first one.
mm/ksm.c unsigned long ksm_zero_pages; · line 342
Quoted 2 lines
mm/ksm.c +2
-0+KSM_ADVISOR_SCAN_TIME , +KSM_ADVISOR_LAST =KSM_ADVISOR_SCAN_TIME
Instead of "_LAST", maybe use "_COUNT" and use that when checking for valid values.
But: we likely want to store "strings" instead of magic numbers from user space instead.
mm/ksm.c unsigned long ksm_zero_pages; · line 352
Quoted 10 lines
mm/ksm.c +10
-0+}; +static enum ksm_advisor_type ksm_advisor; + +static void init_advisor (void ) +{ + advisor_ctx.start_scan =0 ; + advisor_ctx.scan_time =0 ; + advisor_ctx.change =0 ; + advisor_ctx.cpu_time =0 ; +}
That should likely not be required. The values are all 0.
If other values are ever required, they could be initialized right with the variable:
static struct advisor_ctx advisor_ctx = {
.start_scan = 0,
...
};Quoted 74 lines
mm/ksm.c +74
-0+ +/* +* Use previous scan time if available, otherwise use current scan time as an +* approximation for the previous scan time. +*/ +static inline unsigned long prev_scan_time (struct advisor_ctx *ctx, +unsigned long scan_time) +{ +return ctx->scan_time ? ctx->scan_time : scan_time; +} + +/* Calculate exponential weighted moving average */ +static unsigned long ewma (unsigned long prev,unsigned long curr) +{ +return ((100 -EWMA_WEIGHT ) * prev +EWMA_WEIGHT * curr) /100 ; +} + +/* +* The scan time advisor is based on the current scan rate and the target +* scan rate. +* +* new_pages_to_scan = pages_to_scan * (scan_time / target_scan_time) +* +* To avoid pertubations it calculates a change factor of previous changes. +* A new change factor is calculated for each iteration and it uses an +* exponentially weighted moving average. The new pages_to_scan value is +* multiplied with that change factor: +* +* new_pages_to_scan *= change facor +* +* In addition the new pages_to_scan value is capped by the max and min +* limits. +*/ +static void scan_time_advisor (unsigned long scan_time) +{ +unsigned int cpu_percent; +unsigned long cpu_time; +unsigned long cpu_time_diff; +unsigned long cpu_time_diff_ms; +unsigned long pages; +unsigned long per_page_cost; +unsigned long factor; +unsigned long change; +unsigned long last_scan_time; + + cpu_time =task_sched_runtime (current); + cpu_time_diff = cpu_time - advisor_ctx.cpu_time; + cpu_time_diff_ms = cpu_time_diff /1000 /1000 ; + + cpu_percent = (cpu_time_diff_ms *100 ) / (scan_time *1000 ); + cpu_percent = cpu_percent ? cpu_percent :1 ; + last_scan_time =prev_scan_time (&advisor_ctx, scan_time); + +/* Calculate scan time as percentage of target scan time */ + factor = ksm_advisor_target_scan_time *100 / scan_time; + factor = factor ? factor :1 ; + +/* +* Calculate scan time as percentage of last scan time and use +* exponentially weighted average to smooth it +*/ + change = scan_time *100 / last_scan_time; + change = change ? change :1 ; + change =ewma (advisor_ctx.change, change); + +/* Calculate new scan rate based on target scan rate. */ + pages = ksm_thread_pages_to_scan *100 / factor; +/* Update pages_to_scan by weighted change percentage. */ + pages = pages * change /100 ; + +/* Cap new pages_to_scan value */ + per_page_cost = ksm_thread_pages_to_scan / cpu_percent; + per_page_cost = per_page_cost ? per_page_cost :1 ; +
mm/ksm.c unsigned long ksm_zero_pages; · line 436
Quoted 10 lines
mm/ksm.c +10
-0+ pages =min (pages, per_page_cost * ksm_advisor_max_cpu); + pages =max (pages, per_page_cost * ksm_advisor_min_cpu); + pages =min (pages, ksm_advisor_max_pages); + +/* Update advisor context */ + advisor_ctx.change = change; + advisor_ctx.scan_time = scan_time; + advisor_ctx.cpu_time = cpu_time; + + ksm_thread_pages_to_scan = pages;
While that advisor is active, we should likely disallow changing ksm_thread_pages_to_scan using other means.
Quoted 4 lines
mm/ksm.c +4
-0+} + +static void run_advisor (void ) +{
mm/ksm.c unsigned long ksm_zero_pages; · line 450
Quoted 10 lines
mm/ksm.c +10
-0+if (ksm_advisor ==KSM_ADVISOR_SCAN_TIME ) { +s64 scan_time; + +/* Convert scan time to seconds */ + scan_time =ktime_ms_delta (ktime_get (), advisor_ctx.start_scan); + scan_time =div_s64 (scan_time,MSEC_PER_SEC ); + scan_time = scan_time ? scan_time :1 ; + +scan_time_advisor ((unsigned long )scan_time); + }
We could have rescheduled in the meantime, right? Doesn't that mean that our CPU load consumption might be wrong in some cases?
mm/ksm.c static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) · line 2557
Quoted 10 lines
mm/ksm.c +3
-0+} +#ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes =1 ;@@ -2401,6 +2554,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) mm_slot = ksm_scan.mm_slot;if (mm_slot == &ksm_mm_head) { + advisor_ctx.start_scan =ktime_get ();
Why do that even without KSM_ADVISOR_SCAN_TIME?
You should probably have two functions:
ksm_advisor_start_scan() [this code, fenced by KSM_ADVISOR_SCAN_TIME] ksm_advisor_stop_scan() [previous run_advisor]
Quoted 20 lines
mm/ksm.c +3
-0trace_ksm_start_scan (ksm_scan.seqnr, ksm_rmap_items);/* @@ -2558,6 +2712,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)if (mm_slot != &ksm_mm_head) goto next_mm; +run_advisor (); +trace_ksm_stop_scan (ksm_scan.seqnr, ksm_rmap_items); ksm_scan.seqnr++;return NULL ;@@ -3603,6 +3759,7 @@ static int __init ksm_init(void) zero_checksum =calc_checksum (ZERO_PAGE (0 ));/* Default to false for backwards compatibility */ ksm_use_zero_pages = false; +init_advisor (); err =ksm_slab_init ();if (err)
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 1/4] mm/ksm: add ksm advisor
Quoted 34 lines · David Hildenbrand
Quoted 21 lines · Stefan Roesch
This adds the ksm advisor. The ksm advisor automatically manages the
pages_to_scansetting to achieve a target scan time. The target scan time defines how many seconds it should take to scan all the candidate KSM pages. In other words thepages_to_scanrate is changed by the advisor to achieve the target scan time. The algorithm has a max and min value to:
- guarantee responsiveness to changes
- to avoid to spend too much CPU
The respective parameters are:
-ksm_advisor_target_scan_time(how many seconds a scan should take)
-ksm_advisor_min_cpu(minimum value for cpu percent usage)
-ksm_advisor_max_cpu(maximum value for cpu percent usage)
-ksm_advisor_min_pages(minimum value forpages_to_scanper batch)
-ksm_advisor_max_pages(maximum value forpages_to_scanper batch) The algorithm calculates the change value based on the target scan time and the previous scan time. To avoid pertubations an exponentially weighted moving average is applied.
The advisor is managed by three main parameters: target scan time, cpu min time and cpu max time for the ksmd background thread. These parameters determine how aggresive ksmd scans.
In addition there are min and max values for thepages_to_scanparameterQuoted 8 lines
to make sure that its initial and max values are not set too low or too high. This ensures that it is able to react to changes quickly enough. The default values are:
- target scan time: 200 secs
- min cpu: 15%
- max cpu: 70%
- min pages: 500
- max pages: 30000Do we really need the min cpu load? The target scan time combined with the max CPU load should be sufficient, no?
Quoted 8 lines
Internally, we might want some sane default/min start value, but exposing that to the user is questionable.
For example, if I have exactly two possible KSM pages in the system, why should my cpu dedicate 15% to scanning nothing after merging them? :)
[...]
The min cpu case is to make sure that we scan fast enough to be able to react fast enough to the changes in the number of pages. This helps in determining in how quick we want to react to changes. This helps especially with the startup phase of applications.
We can certainly only set a default value, that is not exposed in sysfs.
Quoted 13 lines
Quoted 13 lines
mm/ksm.c +13
-0+/** +* struct advisor_ctx - metadata for KSM advisor +* @start_scan: start time of the current scan +* @scan_time: scan time of previous scan +* @change: change in percent to pages_to_scan parameter +* @cpu_percent: average cpu percent usage of the ksmd thread for the last scan +*/ +struct advisor_ctx { +ktime_t start_scan; +unsigned long scan_time; +unsigned long change; +unsigned long long cpu_time; +};
Quoted 9 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 340
Quoted 6 lines
mm/ksm.c +6
-0+static struct advisor_ctx advisor_ctx; + +/* Define different advisor's */ +enum ksm_advisor_type { +KSM_ADVISOR_NONE , +KSM_ADVISOR_FIRST =KSM_ADVISOR_NONE ,Unused, better drop it. 0 is the implicit first one.
Will change it accordingly.
Quoted 9 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 342
Quoted 2 lines
mm/ksm.c +2
-0+KSM_ADVISOR_SCAN_TIME , +KSM_ADVISOR_LAST =KSM_ADVISOR_SCAN_TIME Instead of "
_LAST", maybe use "_COUNT" and use that when checking for valid values.But: we likely want to store "strings" instead of magic numbers from user space instead.
Any recommendation for the naming of the parameters when I switch to strings?
Quoted 13 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 352
Quoted 10 lines
mm/ksm.c +10
-0+}; +static enum ksm_advisor_type ksm_advisor; + +static void init_advisor (void ) +{ + advisor_ctx.start_scan =0 ; + advisor_ctx.scan_time =0 ; + advisor_ctx.change =0 ; + advisor_ctx.cpu_time =0 ; +}That should likely not be required. The values are all 0.
Quoted 8 lines
If other values are ever required, they could be initialized right with the variable:
static struct advisor_ctx advisor_ctx = { .start_scan = 0, ... };
ok
Quoted 79 lines
Quoted 79 lines
mm/ksm.c +79
-0+ +/* +* Use previous scan time if available, otherwise use current scan time as an +* approximation for the previous scan time. +*/ +static inline unsigned long prev_scan_time (struct advisor_ctx *ctx, +unsigned long scan_time) +{ +return ctx->scan_time ? ctx->scan_time : scan_time; +} + +/* Calculate exponential weighted moving average */ +static unsigned long ewma (unsigned long prev,unsigned long curr) +{ +return ((100 -EWMA_WEIGHT ) * prev +EWMA_WEIGHT * curr) /100 ; +} + +/* +* The scan time advisor is based on the current scan rate and the target +* scan rate. +* +* new_pages_to_scan = pages_to_scan * (scan_time / target_scan_time) +* +* To avoid pertubations it calculates a change factor of previous changes. +* A new change factor is calculated for each iteration and it uses an +* exponentially weighted moving average. The new pages_to_scan value is +* multiplied with that change factor: +* +* new_pages_to_scan *= change facor +* +* In addition the new pages_to_scan value is capped by the max and min +* limits. +*/ +static void scan_time_advisor (unsigned long scan_time) +{ +unsigned int cpu_percent; +unsigned long cpu_time; +unsigned long cpu_time_diff; +unsigned long cpu_time_diff_ms; +unsigned long pages; +unsigned long per_page_cost; +unsigned long factor; +unsigned long change; +unsigned long last_scan_time; + + cpu_time =task_sched_runtime (current); + cpu_time_diff = cpu_time - advisor_ctx.cpu_time; + cpu_time_diff_ms = cpu_time_diff /1000 /1000 ; + + cpu_percent = (cpu_time_diff_ms *100 ) / (scan_time *1000 ); + cpu_percent = cpu_percent ? cpu_percent :1 ; + last_scan_time =prev_scan_time (&advisor_ctx, scan_time); + +/* Calculate scan time as percentage of target scan time */ + factor = ksm_advisor_target_scan_time *100 / scan_time; + factor = factor ? factor :1 ; + +/* +* Calculate scan time as percentage of last scan time and use +* exponentially weighted average to smooth it +*/ + change = scan_time *100 / last_scan_time; + change = change ? change :1 ; + change =ewma (advisor_ctx.change, change); + +/* Calculate new scan rate based on target scan rate. */ + pages = ksm_thread_pages_to_scan *100 / factor; +/* Update pages_to_scan by weighted change percentage. */ + pages = pages * change /100 ; + +/* Cap new pages_to_scan value */ + per_page_cost = ksm_thread_pages_to_scan / cpu_percent; + per_page_cost = per_page_cost ? per_page_cost :1 ; + + pages =min (pages, per_page_cost * ksm_advisor_max_cpu); + pages =max (pages, per_page_cost * ksm_advisor_min_cpu); + pages =min (pages, ksm_advisor_max_pages); + +/* Update advisor context */
Quoted 9 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 436
Quoted 5 lines
mm/ksm.c +5
-0+ advisor_ctx.change = change; + advisor_ctx.scan_time = scan_time; + advisor_ctx.cpu_time = cpu_time; + + ksm_thread_pages_to_scan = pages;While that advisor is active, we should likely disallow changing
ksm_thread_pages_to_scanusing other means.
I'll add a check in the corresponding sysfs function
Quoted 9 lines
Quoted 9 lines
mm/ksm.c +9
-0+} + +static void run_advisor (void ) +{ +if (ksm_advisor ==KSM_ADVISOR_SCAN_TIME ) { +s64 scan_time; + +/* Convert scan time to seconds */ + scan_time =ktime_ms_delta (ktime_get (), advisor_ctx.start_scan);
Quoted 9 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 450
Quoted 5 lines
mm/ksm.c +5
-0+ scan_time =div_s64 (scan_time,MSEC_PER_SEC ); + scan_time = scan_time ? scan_time :1 ; + +scan_time_advisor ((unsigned long )scan_time); + }We could have rescheduled in the meantime, right? Doesn't that mean that our CPU load consumption might be wrong in some cases?
Does it matter? I'm interested how long it takes to complete the scan, including any scheduling.
Quoted 12 lines
mm/ksm.c static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) · line 2556
Quoted 9 lines
mm/ksm.c +3
-0+} +#ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes =1 ;@@ -2401,6 +2554,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) mm_slot = ksm_scan.mm_slot;if (mm_slot == &ksm_mm_head) { + advisor_ctx.start_scan =ktime_get ();Why do that even without
KSM_ADVISOR_SCAN_TIME?
Quoted 5 lines
You should probably have two functions:
ksm_advisor_start_scan()[this code, fenced byKSM_ADVISOR_SCAN_TIME]ksm_advisor_stop_scan()[previousrun_advisor]
I'll add the above functions.
Quoted 17 lines
Quoted 17 lines
mm/ksm.c +2
-0trace_ksm_start_scan (ksm_scan.seqnr, ksm_rmap_items);/* @@ -2558,6 +2712,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page)if (mm_slot != &ksm_mm_head) goto next_mm; +run_advisor (); +trace_ksm_stop_scan (ksm_scan.seqnr, ksm_rmap_items); ksm_scan.seqnr++;return NULL ;@@ -3603,6 +3759,7 @@ static int __init ksm_init(void) zero_checksum =calc_checksum (ZERO_PAGE (0 ));/* Default to false for backwards compatibility */ ksm_use_zero_pages = false; +init_advisor (); err =ksm_slab_init ();if (err)
Re: [PATCH v2 1/4] mm/ksm: add ksm advisor
Quoted 8 lines
Quoted 1 line
The min cpu case is to make sure that we scan fast enough to be able to react fast enough to the changes in the number of pages. This helps in determining in how quick we want to react to changes. This helps especially with the startup phase of applications.
We can certainly only set a default value, that is not exposed in sysfs.
Less toggles is better. So if we can just use some sane starting default, that would be great.
Quoted 31 lines
Quoted 14 lines
Quoted 14 lines
mm/ksm.c +13
-0+/** +* struct advisor_ctx - metadata for KSM advisor +* @start_scan: start time of the current scan +* @scan_time: scan time of previous scan +* @change: change in percent to pages_to_scan parameter +* @cpu_percent: average cpu percent usage of the ksmd thread for the last scan +*/ +struct advisor_ctx { +ktime_t start_scan; +unsigned long scan_time; +unsigned long change; +unsigned long long cpu_time; +};Quoted 9 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 340
Quoted 6 lines
mm/ksm.c +6
-0+static struct advisor_ctx advisor_ctx; + +/* Define different advisor's */ +enum ksm_advisor_type { +KSM_ADVISOR_NONE , +KSM_ADVISOR_FIRST =KSM_ADVISOR_NONE ,Unused, better drop it. 0 is the implicit first one.
Will change it accordingly.
Quoted 6 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 342
Quoted 2 lines
mm/ksm.c +2
-0+KSM_ADVISOR_SCAN_TIME , +KSM_ADVISOR_LAST =KSM_ADVISOR_SCAN_TIME Instead of "
_LAST", maybe use "_COUNT" and use that when checking for valid values.
Quoted 6 lines
Quoted 3 lines
But: we likely want to store "strings" instead of magic numbers from user space instead.
Any recommendation for the naming of the parameters when I switch to strings?
Probably just "none" and "scan-time" ?
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
mm/ksm.c +9
-0+} + +static void run_advisor (void ) +{ +if (ksm_advisor ==KSM_ADVISOR_SCAN_TIME ) { +s64 scan_time; + +/* Convert scan time to seconds */ + scan_time =ktime_ms_delta (ktime_get (), advisor_ctx.start_scan);Quoted 9 lines
mm/ksm.c unsigned long ksm_zero_pages; · line 450
Quoted 5 lines
mm/ksm.c +5
-0+ scan_time =div_s64 (scan_time,MSEC_PER_SEC ); + scan_time = scan_time ? scan_time :1 ; + +scan_time_advisor ((unsigned long )scan_time); + }We could have rescheduled in the meantime, right? Doesn't that mean that our CPU load consumption might be wrong in some cases?
Quoted 2 lines
Does it matter? I'm interested how long it takes to complete the scan, including any scheduling.
But isn't this also required to compute CPU load, so you can stay between min-load and max-load?
- ksm_advisor_min_cpu (minimum value for cpu percent usage)
- ksm_advisor_max_cpu (maximum value for cpu percent usage)
Likely, you want to exclude any rescheduling from there?
I'll have to recheck the logic.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 1/4] mm/ksm: add ksm advisor
In reply to Stefan RoeschQuoted 186 lines · Stefan Roesch
This adds the ksm advisor. The ksm advisor automatically manages the
pages_to_scansetting to achieve a target scan time. The target scan time defines how many seconds it should take to scan all the candidate KSM pages. In other words thepages_to_scanrate is changed by the advisor to achieve the target scan time. The algorithm has a max and min value to:
- guarantee responsiveness to changes
- to avoid to spend too much CPUThe respective parameters are:
-ksm_advisor_target_scan_time(how many seconds a scan should take)
-ksm_advisor_min_cpu(minimum value for cpu percent usage)
-ksm_advisor_max_cpu(maximum value for cpu percent usage)-
ksm_advisor_min_pages(minimum value forpages_to_scanper batch)
-ksm_advisor_max_pages(maximum value forpages_to_scanper batch)The algorithm calculates the change value based on the target scan time and the previous scan time. To avoid pertubations an exponentially weighted moving average is applied.
The advisor is managed by three main parameters: target scan time, cpu min time and cpu max time for the ksmd background thread. These parameters determine how aggresive ksmd scans.
In addition there are min and max values for the
pages_to_scanparameter to make sure that its initial and max values are not set too low or too high. This ensures that it is able to react to changes quickly enough.The default values are:
- target scan time: 200 secs
- min cpu: 15%
- max cpu: 70%
- min pages: 500
- max pages: 30000By default the advisor is disabled. Currently there are two advisors:
none andscan_time.Tests with various workloads have shown considerable CPU savings. Most of the workloads I have investigated have more candidate pages during startup, once the workload is stable in terms of memory, the number of candidate pages is reduced. Without the advisor, the
pages_to_scanneeds to be sized for the maximum number of candidate pages. So having this advisor definitely helps in reducing CPU consumption.For the instagram workload, the advisor achieves a 25% CPU reduction. Once the memory is stable, the
pages_to_scanparameter gets reduced to about 40% of its max value.Signed-off-by Stefan Roesch <[email protected]>--- mm/ksm.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-)mm/ksm.c +101
-1@@ -21,6 +21,7 @@#include <linux/sched.h> #include <linux/sched/mm.h> #include <linux/sched/coredump.h> +#include <linux/sched/cputime.h> #include <linux/rwsem.h> #include <linux/pagemap.h> #include <linux/rmap.h> @@ -248,6 +249,9 @@ static struct kmem_cache *rmap_item_cache;static struct kmem_cache *stable_node_cache;static struct kmem_cache *mm_slot_cache; +/* Default number of pages to scan per batch */ +#define DEFAULT_PAGES_TO_SCAN 100 +/* The number of pages scanned */ static unsigned long ksm_pages_scanned;@@ -276,7 +280,7 @@ static unsigned int ksm_stable_node_chains_prune_millisecs = 2000;static int ksm_max_page_sharing =256 ;/* Number of pages ksmd should scan in one batch */ -static unsigned int ksm_thread_pages_to_scan =100 ; +static unsigned int ksm_thread_pages_to_scan =DEFAULT_PAGES_TO_SCAN ;/* Milliseconds ksmd should sleep between batches */ static unsigned int ksm_thread_sleep_millisecs =20 ;@@ -297,6 +301,155 @@ unsigned long ksm_zero_pages;/* The number of pages that have been skipped due to "smart scanning" */ static unsigned long ksm_pages_skipped; +/* Don't scan more than max pages per batch. */ +static unsigned long ksm_advisor_max_pages =30000 ; + +/* At least scan this many pages per batch. */ +static unsigned long ksm_advisor_min_pages =500 ; + +/* Min CPU for scanning pages per scan */ +static unsigned int ksm_advisor_min_cpu =15 ; + +/* Max CPU for scanning pages per scan */ +static unsigned int ksm_advisor_max_cpu =70 ; + +/* Target scan time in seconds to analyze all KSM candidate pages. */ +static unsigned long ksm_advisor_target_scan_time =200 ; + +/* Exponentially weighted moving average. */ +#define EWMA_WEIGHT 30 + +/** +* struct advisor_ctx - metadata for KSM advisor +* @start_scan: start time of the current scan +* @scan_time: scan time of previous scan +* @change: change in percent to pages_to_scan parameter +* @cpu_percent: average cpu percent usage of the ksmd thread for the last scan +*/ +struct advisor_ctx { +ktime_t start_scan; +unsigned long scan_time; +unsigned long change; +unsigned long long cpu_time; +}; +static struct advisor_ctx advisor_ctx; + +/* Define different advisor's */ +enum ksm_advisor_type { +KSM_ADVISOR_NONE , +KSM_ADVISOR_FIRST =KSM_ADVISOR_NONE , +KSM_ADVISOR_SCAN_TIME , +KSM_ADVISOR_LAST =KSM_ADVISOR_SCAN_TIME +}; +static enum ksm_advisor_type ksm_advisor; + +static void init_advisor (void ) +{ + advisor_ctx.start_scan =0 ; + advisor_ctx.scan_time =0 ; + advisor_ctx.change =0 ; + advisor_ctx.cpu_time =0 ; +} + +/* +* Use previous scan time if available, otherwise use current scan time as an +* approximation for the previous scan time. +*/ +static inline unsigned long prev_scan_time (struct advisor_ctx *ctx, +unsigned long scan_time) +{ +return ctx->scan_time ? ctx->scan_time : scan_time; +} + +/* Calculate exponential weighted moving average */ +static unsigned long ewma (unsigned long prev,unsigned long curr) +{ +return ((100 -EWMA_WEIGHT ) * prev +EWMA_WEIGHT * curr) /100 ; +} + +/* +* The scan time advisor is based on the current scan rate and the target +* scan rate. +* +* new_pages_to_scan = pages_to_scan * (scan_time / target_scan_time) +* +* To avoid pertubations it calculates a change factor of previous changes. +* A new change factor is calculated for each iteration and it uses an +* exponentially weighted moving average. The new pages_to_scan value is +* multiplied with that change factor: +* +* new_pages_to_scan *= change facor +* +* In addition the new pages_to_scan value is capped by the max and min +* limits. +*/ +static void scan_time_advisor (unsigned long scan_time) +{ +unsigned int cpu_percent; +unsigned long cpu_time; +unsigned long cpu_time_diff; +unsigned long cpu_time_diff_ms; +unsigned long pages; +unsigned long per_page_cost; +unsigned long factor; +unsigned long change; +unsigned long last_scan_time; + + cpu_time =task_sched_runtime (current); + cpu_time_diff = cpu_time - advisor_ctx.cpu_time;
mm/ksm.c unsigned long ksm_zero_pages; · line 409
Quoted 10 lines
mm/ksm.c +10
-0+ cpu_time_diff_ms = cpu_time_diff /1000 /1000 ; + + cpu_percent = (cpu_time_diff_ms *100 ) / (scan_time *1000 ); + cpu_percent = cpu_percent ? cpu_percent :1 ; + last_scan_time =prev_scan_time (&advisor_ctx, scan_time); + +/* Calculate scan time as percentage of target scan time */ + factor = ksm_advisor_target_scan_time *100 / scan_time; + factor = factor ? factor :1 ; +
^ ah, that's what I missed.
BTW, why do we pass in "scan_time" and not simply obtain it here, just like we do with task_sched_runtime() ?
Signature · 3 lines
Cheers, David / dhildenb
PATCH v2 2/4 mm/ksm: add sysfs knobs for advisor
In reply to Stefan RoeschThis adds four new knobs for the KSM advisor to influence its behaviour.
The knobs are:
- advisor_mode:
0: no advisor (default)
1: scan time advisor
- advisor_min_cpu: 15 (default, cpu usage percent)
- advisor_max_cpu: 70 (default, cpu usage percent)
- advisor_min_pages: 500 (default)
- advisor_max_pages: 30000 (default)
- advisor_target_scan_time: 200 (default in seconds)
The new values will take effect on the next scan round.
--- mm/ksm.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+)
mm/ksm.c +154 -0
@@ -351,6 +351,14 @@ static void init_advisor(void) advisor_ctx.cpu_time =0 ; } +static void set_advisor_defaults (void ) +{ +if (ksm_advisor ==KSM_ADVISOR_NONE ) + ksm_thread_pages_to_scan =DEFAULT_PAGES_TO_SCAN ; +else if (ksm_advisor ==KSM_ADVISOR_SCAN_TIME ) + ksm_thread_pages_to_scan = ksm_advisor_min_pages; +} +/* * Use previous scan time if available, otherwise use current scan time as an * approximation for the previous scan time.
@@ -3719,6 +3727,146 @@ static ssize_t smart_scan_store(struct kobject *kobj, }KSM_ATTR (smart_scan); +static ssize_t advisor_mode_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%u\n" , ksm_advisor); +} + +static ssize_t advisor_mode_store (struct kobject *kobj, +struct kobj_attribute *attr,const char *buf, +size_t count) +{ +unsigned int mode; +int err; + + err =kstrtouint (buf,10 , &mode); +if (err) +return -EINVAL ; +if (mode >KSM_ADVISOR_LAST ) +return -EINVAL ; + +/* Set advisor default values */ + ksm_advisor = mode; +init_advisor (); +set_advisor_defaults (); + +return count; +} +KSM_ATTR (advisor_mode); + +static ssize_t advisor_min_cpu_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%u\n" , ksm_advisor_min_cpu); +} + +static ssize_t advisor_min_cpu_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err; +unsigned long value; + + err =kstrtoul (buf,10 , &value); +if (err) +return -EINVAL ; + + ksm_advisor_min_cpu = value; +return count; +} +KSM_ATTR (advisor_min_cpu); + +static ssize_t advisor_max_cpu_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%u\n" , ksm_advisor_max_cpu); +} + +static ssize_t advisor_max_cpu_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err; +unsigned long value; + + err =kstrtoul (buf,10 , &value); +if (err) +return -EINVAL ; + + ksm_advisor_max_cpu = value; +return count; +} +KSM_ATTR (advisor_max_cpu); + +static ssize_t advisor_min_pages_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%lu\n" , ksm_advisor_min_pages); +} + +static ssize_t advisor_min_pages_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err; +unsigned long value; + + err =kstrtoul (buf,10 , &value); +if (err) +return -EINVAL ; + + ksm_advisor_min_pages = value; +return count; +} +KSM_ATTR (advisor_min_pages); + +static ssize_t advisor_max_pages_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%lu\n" , ksm_advisor_max_pages); +} + +static ssize_t advisor_max_pages_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err; +unsigned long value; + + err =kstrtoul (buf,10 , &value); +if (err) +return -EINVAL ; + + ksm_advisor_max_pages = value; +return count; +} +KSM_ATTR (advisor_max_pages); + +static ssize_t advisor_target_scan_time_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%lu\n" , ksm_advisor_target_scan_time); +} + +static ssize_t advisor_target_scan_time_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +int err; +unsigned long value; + + err =kstrtoul (buf,10 , &value); +if (err) +return -EINVAL ; +if (value <1 ) +return -EINVAL ; + + ksm_advisor_target_scan_time = value; +return count; +} +KSM_ATTR (advisor_target_scan_time); +static struct attribute *ksm_attrs[] = { &sleep_millisecs_attr.attr, &pages_to_scan_attr.attr,
@@ -3741,6 +3889,12 @@ static struct attribute *ksm_attrs[] = { &use_zero_pages_attr.attr, &general_profit_attr.attr, &smart_scan_attr.attr, + &advisor_mode_attr.attr, + &advisor_min_cpu_attr.attr, + &advisor_max_cpu_attr.attr, + &advisor_min_pages_attr.attr, + &advisor_max_pages_attr.attr, + &advisor_target_scan_time_attr.attr, NULL, };
Signature · 1 line
2.39.3
Re: [PATCH v2 2/4] mm/ksm: add sysfs knobs for advisor
Quoted 11 lines · Stefan Roesch
This adds four new knobs for the KSM advisor to influence its behaviour.
The knobs are:
-advisor_mode:
0: no advisor (default)
1: scan time advisor
-advisor_min_cpu: 15 (default, cpu usage percent)
-advisor_max_cpu: 70 (default, cpu usage percent)
-advisor_min_pages: 500 (default)
-advisor_max_pages: 30000 (default)
-advisor_target_scan_time: 200 (default in seconds)
Is there a way we can avoid exposing advisor_min_pages/advisor_max_pages and just have this internal e.g., as defines?
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 2/4] mm/ksm: add sysfs knobs for advisor
Quoted 12 lines · David Hildenbrand
Quoted 10 lines · Stefan Roesch
This adds four new knobs for the KSM advisor to influence its behaviour. The knobs are:
-advisor_mode:
0: no advisor (default)
1: scan time advisor
-advisor_min_cpu: 15 (default, cpu usage percent)
-advisor_max_cpu: 70 (default, cpu usage percent)
-advisor_min_pages: 500 (default)
-advisor_max_pages: 30000 (default)
-advisor_target_scan_time: 200 (default in seconds)
Quoted 3 lines
Is there a way we can avoid exposing advisor_min_pages/advisor_max_pages and just have this internal e.g., as defines?
Yes, we can.
Re: [PATCH v2 2/4] mm/ksm: add sysfs knobs for advisor
In reply to David HildenbrandQuoted 12 lines · David Hildenbrand
Quoted 10 lines · Stefan Roesch
This adds four new knobs for the KSM advisor to influence its behaviour. The knobs are:
-advisor_mode:
0: no advisor (default)
1: scan time advisor
-advisor_min_cpu: 15 (default, cpu usage percent)
-advisor_max_cpu: 70 (default, cpu usage percent)
-advisor_min_pages: 500 (default)
-advisor_max_pages: 30000 (default)
-advisor_target_scan_time: 200 (default in seconds)
Quoted 2 lines
Is there a way we can avoid exposing advisor_min_pages/advisor_max_pages and just have this internal e.g., as defines?
Yes, we can.
Re: [PATCH v2 2/4] mm/ksm: add sysfs knobs for advisor
In reply to Stefan RoeschQuoted 26 lines · Stefan Roesch
This adds four new knobs for the KSM advisor to influence its behaviour.
The knobs are:
-advisor_mode:
0: no advisor (default)
1: scan time advisor
-advisor_min_cpu: 15 (default, cpu usage percent)
-advisor_max_cpu: 70 (default, cpu usage percent)
-advisor_min_pages: 500 (default)
-advisor_max_pages: 30000 (default)
-advisor_target_scan_time: 200 (default in seconds)The new values will take effect on the next scan round.
Signed-off-by Stefan Roesch <[email protected]>--- mm/ksm.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+)mm/ksm.c +0
-0@@ -351,6 +351,14 @@ static void init_advisor(void) advisor_ctx.cpu_time =0 ; }
[...]
Quoted 21 lines
* Use previous scan time if available, otherwise use current scan time as an
* approximation for the previous scan time.mm/ksm.c +15
-0@@ -3719,6 +3727,146 @@ static ssize_t smart_scan_store(struct kobject *kobj, }KSM_ATTR (smart_scan); +static ssize_t advisor_mode_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%u\n" , ksm_advisor); +} + +static ssize_t advisor_mode_store (struct kobject *kobj, +struct kobj_attribute *attr,const char *buf, +size_t count) +{ +unsigned int mode; +int err; + + err =kstrtouint (buf,10 , &mode); +if (err)
mm/ksm.c static ssize_t smart_scan_store(struct kobject *kobj, · line 3754
Quoted 10 lines
mm/ksm.c +10
-0+return -EINVAL ; +if (mode >KSM_ADVISOR_LAST ) +return -EINVAL ; + +/* Set advisor default values */ + ksm_advisor = mode; +init_advisor (); +set_advisor_defaults (); + +return count;
Can we instead use human-readable strings?
"none" and "scan-time" should be clearer.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 2/4] mm/ksm: add sysfs knobs for advisor
Quoted 60 lines · David Hildenbrand
Quoted 22 lines · Stefan Roesch
This adds four new knobs for the KSM advisor to influence its behaviour. The knobs are:
-advisor_mode:
0: no advisor (default)
1: scan time advisor
-advisor_min_cpu: 15 (default, cpu usage percent)
-advisor_max_cpu: 70 (default, cpu usage percent)
-advisor_min_pages: 500 (default)
-advisor_max_pages: 30000 (default)
-advisor_target_scan_time: 200 (default in seconds)
The new values will take effect on the next scan round.Signed-off-by Stefan Roesch <[email protected]>--- mm/ksm.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+)mm/ksm.c +0
-0@@ -351,6 +351,14 @@ static void init_advisor(void) advisor_ctx.cpu_time =0 ; }[...]
Quoted 20 lines
* Use previous scan time if available, otherwise use current scan time as an
* approximation for the previous scan time.mm/ksm.c +14
-0@@ -3719,6 +3727,146 @@ static ssize_t smart_scan_store(struct kobject *kobj, }KSM_ATTR (smart_scan); +static ssize_t advisor_mode_show(struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return sysfs_emit (buf,"%u\n" , ksm_advisor); +} + +static ssize_t advisor_mode_store (struct kobject *kobj, +struct kobj_attribute *attr,const char *buf, +size_t count) +{ +unsigned int mode; +int err; + + err =kstrtouint (buf,10 , &mode); +if (err)mm/ksm.c static ssize_t smart_scan_store(struct kobject *kobj, · line 3753
Quoted 10 lines
mm/ksm.c +10
-0+return -EINVAL ; +if (mode >KSM_ADVISOR_LAST ) +return -EINVAL ; + +/* Set advisor default values */ + ksm_advisor = mode; +init_advisor (); +set_advisor_defaults (); + +return count;Can we instead use human-readable strings?
Quoted 2 lines
"none" and "scan-time" should be clearer.
I'll change the interface to use strings instead of numbers.
PATCH v2 3/4 mm/ksm: add tracepoint for ksm advisor
In reply to Stefan RoeschThis adds a new tracepoint for the ksm advisor. It reports the last scan time, the new setting of the pages_to_scan parameter and the average cpu percent usage of the ksmd background thread for the last scan.
--- include/trace/events/ksm.h | 33 +++++++++++++++++++++++++++++++++ mm/ksm.c | 1 + 2 files changed, 34 insertions(+)
include/trace/events/ksm.h +33 -0
@@ -245,6 +245,39 @@ TRACE_EVENT(ksm_remove_rmap_item, __entry->pfn, __entry->rmap_item, __entry->mm) ); +/** +* ksm_advisor - called after the advisor has run +* +* @scan_time: scan time in seconds +* @pages_to_scan: new pages_to_scan value +* @cpu_percent: cpu usage in percent +* +* Allows to trace the ksm advisor. +*/ +TRACE_EVENT (ksm_advisor, + + TP_PROTO(s64 scan_time, unsigned long pages_to_scan, + unsigned int cpu_percent), + +TP_ARGS (scan_time, pages_to_scan, cpu_percent), + +TP_STRUCT__entry ( + __field(s64, scan_time) + __field(unsigned long, pages_to_scan) + __field(unsigned int, cpu_percent) + ), + +TP_fast_assign ( + __entry->scan_time = scan_time; + __entry->pages_to_scan = pages_to_scan; + __entry->cpu_percent = cpu_percent; + ), + +TP_printk ("ksm scan time %lld pages_to_scan %lu cpu percent %u" , + __entry->scan_time, __entry->pages_to_scan, + __entry->cpu_percent) +); +#endif /* _TRACE_KSM_H */ /* This part must be outside protection */
mm/ksm.c +1 -0
@@ -442,6 +442,7 @@ static void scan_time_advisor(unsigned long scan_time) advisor_ctx.cpu_time = cpu_time; ksm_thread_pages_to_scan = pages; +trace_ksm_advisor (scan_time, pages, cpu_percent); }static void run_advisor (void )
Signature · 1 line
2.39.3
PATCH v2 4/4 mm/ksm: document ksm advisor and its sysfs knobs
In reply to Stefan RoeschThis documents the KSM advisor and its new knobs in /sys/fs/kernel/mm.
--- Documentation/admin-guide/mm/ksm.rst | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
Documentation/admin-guide/mm/ksm.rst +66 -0
@@ -164,6 +164,33 @@ smart_scan optimization is enabled. The ``pages_skipped`` metric shows how effective the setting is. +advisor_mode + The ``advisor_mode`` selects the current advisor. Two modes are + supported: 0 (None) and 1 (Scan time). The default is None. By + setting ``advisor_mode`` to 1, the scan time advisor is enabled. + The section about ``advisor`` explains in detail how the scan time + advisor works. + +advisor_min_cpu + specifies the lower limit of the cpu percent usage of the ksmd + background thread. The default is 15. + +adivsor_max_cpu + specifies the upper limit of the cpu percent usage of the ksmd + background thread. The default is 70. + +advisor_target_scan_time + specifies the target scan time in seconds to scan all the candidate + pages. The default value is 200 seconds. + +advisor_min_pages + specifies the lower limit of the ``pages_to_scan`` parameter of the + scan time advisor. The default is 500. + +adivsor_max_pages + specifies the upper limit of the ``pages_to_scan`` parameter of the + scan time advisor. The default is 30000. + The effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``: general_profit
@@ -263,6 +290,45 @@ ksm_swpin_copy note that KSM page might be copied when swapping in because do_swap_page() cannot do all the locking needed to reconstitute a cross-anon_vma KSM page. +Advisor +======= + +The number of candidate pages for KSM is dynamic. It can be often observed +that during the startup of an application more candidate pages need to be +processed. Without an advisor the ``pages_to_scan`` parameter needs to be +sized for the maximum number of candidate pages. The scan time advisor can +changes the ``pages_to_scan`` parameter based on demand. + +The advisor can be enabled, so KSM can automatically adapt to changes in the +number of candidate pages to scan. Two advisors are implemented: 0 (None) and +1 (Scan time). With None no advisor is enabled. The default is None. + +The Scan time advisor changes the ``pages_to_scan`` parameter based on the +observed scan times. The possible values for the ``pages_to_scan`` parameter is +limited by the ``advisor_min_pages`` and ``advisor_max_pages`` parameters. In +addition there is also the ``advisor_target_scan_time`` parameter. This +parameter sets the target time to scan all the KSM candidate pages. The +parameter ``advisor_target_scan_time`` decides how aggressive the scan time +advisor scans candidate pages. Lower values make the scan time advisor to scan +more aggresively. This is the most important parameter for the configuration of +the scan time advisor. + +The Scan time advisor changes the ``pages_to_scan`` parameter based on the +observed scan times. The possible values for the ``pages_to_scan`` parameter is +limited by the ``advisor_min_cpu`` and ``advisor_max_cpu`` parameters. In +addition there is also the ``advisor_target_scan_time`` parameter. This +parameter sets the target time to scan all the KSM candidate pages. The +parameter ``advisor_target_scan_time`` decides how aggressive the scan time +advisor scans candidate pages. Lower values make the scan time advisor to scan +more aggresively. This is the most important parameter for the configuration of +the scan time advisor. + +The initial value and the maximum value can be changed with ``advisor_min_pages`` +and ``advisor_max_pages``. The default values are sufficient for most workloads. + +The ``pages_to_scan`` parameter is re-calculated after a scan has been completed. + + -- Izik Eidus, Hugh Dickins, 17 Nov 2009
Signature · 1 line
2.39.3