diff mbox series

[v3,2/4] mm/ksm: add sysfs knobs for advisor

Message ID 20231204234906.1237478-3-shr@devkernel.io (mailing list archive)
State New
Headers show
Series mm/ksm: Add ksm advisor | expand

Commit Message

Stefan Roesch Dec. 4, 2023, 11:49 p.m. UTC
This adds four new knobs for the KSM advisor to influence its behaviour.

The knobs are:
- advisor_mode:
    none:      no advisor (default)
    scan-time: scan time advisor
- 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 <shr@devkernel.io>
---
 mm/ksm.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 127 insertions(+)

Comments

David Hildenbrand Dec. 12, 2023, 1:45 p.m. UTC | #1
On 05.12.23 00:49, Stefan Roesch wrote:
> This adds four new knobs for the KSM advisor to influence its behaviour.
> 
> The knobs are:
> - advisor_mode:
>      none:      no advisor (default)
>      scan-time: scan time advisor
> - 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 <shr@devkernel.io>
> ---
>   mm/ksm.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 127 insertions(+)
> 
> diff --git a/mm/ksm.c b/mm/ksm.c
> index b27010fa2e946..18b7185bbc65b 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -3735,6 +3735,128 @@ 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)
> +{
> +	const char *output;
> +
> +	if (ksm_advisor == KSM_ADVISOR_NONE)
> +		output = "[none] scan-time";
> +	else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME)
> +		output = "none [scan-time]";
> +
> +	return sysfs_emit(buf, "%s\n", output);
> +}
> +
> +static ssize_t advisor_mode_store(struct kobject *kobj,
> +				  struct kobj_attribute *attr, const char *buf,
> +				  size_t count)
> +{
> +	if (sysfs_streq("scan-time", buf))
> +		ksm_advisor = KSM_ADVISOR_SCAN_TIME;
> +	else if (sysfs_streq("none", buf))
> +		ksm_advisor = KSM_ADVISOR_NONE;
> +	else
> +		return -EINVAL;
> +
> +	/* Set advisor default values */
> +	init_advisor();

Is the "init_advisor()" really required?

> +	set_advisor_defaults();

That function should go to this patch.
Stefan Roesch Dec. 12, 2023, 6:02 p.m. UTC | #2
On Tue, Dec 12, 2023, at 5:45 AM, David Hildenbrand wrote:
> On 05.12.23 00:49, Stefan Roesch wrote:
>> This adds four new knobs for the KSM advisor to influence its behaviour.
>> 
>> The knobs are:
>> - advisor_mode:
>>      none:      no advisor (default)
>>      scan-time: scan time advisor
>> - 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 <shr@devkernel.io>
>> ---
>>   mm/ksm.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 127 insertions(+)
>> 
>> diff --git a/mm/ksm.c b/mm/ksm.c
>> index b27010fa2e946..18b7185bbc65b 100644
>> --- a/mm/ksm.c
>> +++ b/mm/ksm.c
>> @@ -3735,6 +3735,128 @@ 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)
>> +{
>> +	const char *output;
>> +
>> +	if (ksm_advisor == KSM_ADVISOR_NONE)
>> +		output = "[none] scan-time";
>> +	else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME)
>> +		output = "none [scan-time]";
>> +
>> +	return sysfs_emit(buf, "%s\n", output);
>> +}
>> +
>> +static ssize_t advisor_mode_store(struct kobject *kobj,
>> +				  struct kobj_attribute *attr, const char *buf,
>> +				  size_t count)
>> +{
>> +	if (sysfs_streq("scan-time", buf))
>> +		ksm_advisor = KSM_ADVISOR_SCAN_TIME;
>> +	else if (sysfs_streq("none", buf))
>> +		ksm_advisor = KSM_ADVISOR_NONE;
>> +	else
>> +		return -EINVAL;
>> +
>> +	/* Set advisor default values */
>> +	init_advisor();
>
> Is the "init_advisor()" really required?
>

The init_advisor is required for the following scenario:
- advisor is used
- advisor is turned off
- pages_to_scan is used and run for some time
- advisor is turned on again
   ==> Advisor would start with two high values for the first calculation

>> +	set_advisor_defaults();
>
> That function should go to this patch.
>
>
>

I'll move it to this patch. I'll also look if I can should move init_advisor() to this
patch.

> -- 
> Cheers,
>
> David / dhildenb
David Hildenbrand Dec. 12, 2023, 6:07 p.m. UTC | #3
On 12.12.23 19:02, Stefan Roesch wrote:
> 
> 
> On Tue, Dec 12, 2023, at 5:45 AM, David Hildenbrand wrote:
>> On 05.12.23 00:49, Stefan Roesch wrote:
>>> This adds four new knobs for the KSM advisor to influence its behaviour.
>>>
>>> The knobs are:
>>> - advisor_mode:
>>>       none:      no advisor (default)
>>>       scan-time: scan time advisor
>>> - 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 <shr@devkernel.io>
>>> ---
>>>    mm/ksm.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>    1 file changed, 127 insertions(+)
>>>
>>> diff --git a/mm/ksm.c b/mm/ksm.c
>>> index b27010fa2e946..18b7185bbc65b 100644
>>> --- a/mm/ksm.c
>>> +++ b/mm/ksm.c
>>> @@ -3735,6 +3735,128 @@ 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)
>>> +{
>>> +	const char *output;
>>> +
>>> +	if (ksm_advisor == KSM_ADVISOR_NONE)
>>> +		output = "[none] scan-time";
>>> +	else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME)
>>> +		output = "none [scan-time]";
>>> +
>>> +	return sysfs_emit(buf, "%s\n", output);
>>> +}
>>> +
>>> +static ssize_t advisor_mode_store(struct kobject *kobj,
>>> +				  struct kobj_attribute *attr, const char *buf,
>>> +				  size_t count)
>>> +{
>>> +	if (sysfs_streq("scan-time", buf))
>>> +		ksm_advisor = KSM_ADVISOR_SCAN_TIME;
>>> +	else if (sysfs_streq("none", buf))
>>> +		ksm_advisor = KSM_ADVISOR_NONE;
>>> +	else
>>> +		return -EINVAL;
>>> +
>>> +	/* Set advisor default values */
>>> +	init_advisor();
>>
>> Is the "init_advisor()" really required?
>>
> 
> The init_advisor is required for the following scenario:
> - advisor is used
> - advisor is turned off
> - pages_to_scan is used and run for some time
> - advisor is turned on again
>     ==> Advisor would start with two high values for the first calculation

Can't set_advisor_defaults() handle that?
Stefan Roesch Dec. 12, 2023, 6:27 p.m. UTC | #4
On Tue, Dec 12, 2023, at 10:07 AM, David Hildenbrand wrote:
> On 12.12.23 19:02, Stefan Roesch wrote:
>> 
>> 
>> On Tue, Dec 12, 2023, at 5:45 AM, David Hildenbrand wrote:
>>> On 05.12.23 00:49, Stefan Roesch wrote:
>>>> This adds four new knobs for the KSM advisor to influence its behaviour.
>>>>
>>>> The knobs are:
>>>> - advisor_mode:
>>>>       none:      no advisor (default)
>>>>       scan-time: scan time advisor
>>>> - 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 <shr@devkernel.io>
>>>> ---
>>>>    mm/ksm.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>>    1 file changed, 127 insertions(+)
>>>>
>>>> diff --git a/mm/ksm.c b/mm/ksm.c
>>>> index b27010fa2e946..18b7185bbc65b 100644
>>>> --- a/mm/ksm.c
>>>> +++ b/mm/ksm.c
>>>> @@ -3735,6 +3735,128 @@ 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)
>>>> +{
>>>> +	const char *output;
>>>> +
>>>> +	if (ksm_advisor == KSM_ADVISOR_NONE)
>>>> +		output = "[none] scan-time";
>>>> +	else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME)
>>>> +		output = "none [scan-time]";
>>>> +
>>>> +	return sysfs_emit(buf, "%s\n", output);
>>>> +}
>>>> +
>>>> +static ssize_t advisor_mode_store(struct kobject *kobj,
>>>> +				  struct kobj_attribute *attr, const char *buf,
>>>> +				  size_t count)
>>>> +{
>>>> +	if (sysfs_streq("scan-time", buf))
>>>> +		ksm_advisor = KSM_ADVISOR_SCAN_TIME;
>>>> +	else if (sysfs_streq("none", buf))
>>>> +		ksm_advisor = KSM_ADVISOR_NONE;
>>>> +	else
>>>> +		return -EINVAL;
>>>> +
>>>> +	/* Set advisor default values */
>>>> +	init_advisor();
>>>
>>> Is the "init_advisor()" really required?
>>>
>> 
>> The init_advisor is required for the following scenario:
>> - advisor is used
>> - advisor is turned off
>> - pages_to_scan is used and run for some time
>> - advisor is turned on again
>>     ==> Advisor would start with two high values for the first calculation
>
> Can't set_advisor_defaults() handle that?
>

Yes, I can move the

advisor_ctx = (const struct advisor_ctx){ 0 };

into set_advisor_defaults().

> -- 
> Cheers,
>
> David / dhildenb
diff mbox series

Patch

diff --git a/mm/ksm.c b/mm/ksm.c
index b27010fa2e946..18b7185bbc65b 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -3735,6 +3735,128 @@  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)
+{
+	const char *output;
+
+	if (ksm_advisor == KSM_ADVISOR_NONE)
+		output = "[none] scan-time";
+	else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME)
+		output = "none [scan-time]";
+
+	return sysfs_emit(buf, "%s\n", output);
+}
+
+static ssize_t advisor_mode_store(struct kobject *kobj,
+				  struct kobj_attribute *attr, const char *buf,
+				  size_t count)
+{
+	if (sysfs_streq("scan-time", buf))
+		ksm_advisor = KSM_ADVISOR_SCAN_TIME;
+	else if (sysfs_streq("none", buf))
+		ksm_advisor = KSM_ADVISOR_NONE;
+	else
+		return -EINVAL;
+
+	/* Set advisor default values */
+	init_advisor();
+	set_advisor_defaults();
+
+	return count;
+}
+KSM_ATTR(advisor_mode);
+
+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,
@@ -3757,6 +3879,11 @@  static struct attribute *ksm_attrs[] = {
 	&use_zero_pages_attr.attr,
 	&general_profit_attr.attr,
 	&smart_scan_attr.attr,
+	&advisor_mode_attr.attr,
+	&advisor_max_cpu_attr.attr,
+	&advisor_min_pages_attr.attr,
+	&advisor_max_pages_attr.attr,
+	&advisor_target_scan_time_attr.attr,
 	NULL,
 };