mbox series

[RFC,0/2] Hot page promotion optimization for large address space

Message ID 20240327160237.2355-1-bharata@amd.com (mailing list archive)
Headers show
Series Hot page promotion optimization for large address space | expand

Message

Bharata B Rao March 27, 2024, 4:02 p.m. UTC
In order to check how efficiently the existing NUMA balancing
based hot page promotion mechanism can detect hot regions and
promote pages for workloads with large memory footprints, I
wrote and tested a program that allocates huge amount of
memory but routinely touches only small parts of it.

This microbenchmark provisions memory both on DRAM node and CXL node.
It then divides the entire allocated memory into chunks of smaller
size and randomly choses a chunk for generating memory accesses.
Each chunk is then accessed for a fixed number of iterations to
create the notion of hotness. Within each chunk, the individual
pages at 4K granularity are again accessed in random fashion.

When a chunk is taken up for access in this manner, its pages
can either be residing on DRAM or CXL. In the latter case, the NUMA
balancing driven hot page promotion logic is expected to detect and
promote the hot pages that reside on CXL.

The experiment was conducted on a 2P AMD Bergamo system that has
CXL as the 3rd node.

$ numactl -H
available: 3 nodes (0-2)
node 0 cpus: 0-127,256-383
node 0 size: 128054 MB
node 1 cpus: 128-255,384-511
node 1 size: 128880 MB
node 2 cpus:
node 2 size: 129024 MB
node distances:
node   0   1   2 
  0:  10  32  60 
  1:  32  10  50 
  2:  255  255  10

It is seen that number of pages that get promoted is really low and
the reason for it happens to be that the NUMA hint fault latency turns
out to be much higher than the hot threshold most of the times. Here
are a few latency and threshold sample values captured from
should_numa_migrate_memory() routine when the benchmark was run:

latency	threshold (in ms)
20620	1125
56185	1125
98710	1250
148871	1375
182891	1625
369415	1875
630745	2000

The NUMA hint fault latency metric, which is based on absolute time
difference between scanning time and fault time may not be suitable
for applications that have large amounts of memory. If the time
difference between the scan time PTE update and the subsequent access
(hint fault) is more, the existing logic in should_numa_migrate_memory()
to determine if the page needs to be migrated, will exclude more
pages than it selects pages for promotion.

To address this problem, this RFC converts the absolute time based
hint fault latency in to a relative metric. The number of hint faults
that have occurred between the scan time and the page's fault time
is used as the threshold.

This is quite an experimental work and there are things to take
care of still. While more testing needs to be conducted with different
benchmarks, I am posting the patchset here to just get early feedback.

Microbenchmark
==============
Total allocation is 192G which initially occupies full of Node 1 (DRAM)
and half of Node 2 (CXL)
Chunk size is 1G

			Default		Patched

Benchmark score (us)	637,787,351	571,350,410 (-10.41%)
(Lesser is better)

numa_pte_updates	29,834,747	29,275,489
numa_hint_faults	12,512,736	12,080,772
numa_hint_faults_local	0		0
numa_pages_migrated	1,804,583	6,709,580
pgpromote_success	1,804,500	6,709,526
pgpromote_candidate	1,916,720	7,523,345
pgdemote_kswapd		5,358,119	9,438,006
pgdemote_direct		0		0

				Default		Patched
Number of times
should_numa_migrate_memory()
was invoked:			12,512,736	12,080,772

Number of times the migration
request was rejected due to
hint fault latency being
higher than threshold: 		10,595,933	4,557,401

Redis-memtier
=============
memtier_benchmark -t 512 -n 25000 --ratio 1:1 -c 20 -x 1 --key-pattern R:R
--hide-histogram --distinct-client-seed -d 20000 --pipeline=1000

			Default		Patched

Ops/sec			51,921.16	52,694.55
Hits/sec		21,908.72	22,235.03
Misses/sec		4051.86		4112.24
Avg. Latency		867.51710	591.27561 (-31.84%)
p50 Latency		876.54300	708.60700 (-19.15%)
p99 Latency		1044.47900	1044.47900
p99.9 Latency		1048.57500	1048.57500
KB/sec			937,330.19	951,291.76

numa_pte_updates	66,628,064	72,125,512
numa_hint_faults	57,093,369	63,369,538
numa_hint_faults_local	0		0
numa_pages_migrated	799,128		3,634,114
pgpromote_success	798,974		3,633,672
pgpromote_candidate	33,884,196	23,143,552
pgdemote_kswapd		13,321,784	11,948,894
pgdemote_direct		257		57,147

Bharata B Rao (2):
  sched/numa: Fault count based NUMA hint fault latency
  mm: Update hint fault count for pages that are skipped during scanning

 include/linux/mm.h       | 23 ++++---------
 include/linux/mm_types.h |  3 ++
 kernel/sched/debug.c     |  2 +-
 kernel/sched/fair.c      | 73 +++++++++++-----------------------------
 kernel/sched/sched.h     |  1 +
 mm/huge_memory.c         | 10 +++---
 mm/memory.c              |  2 ++
 mm/mprotect.c            | 14 ++++----
 8 files changed, 46 insertions(+), 82 deletions(-)

Comments

Huang, Ying March 28, 2024, 5:35 a.m. UTC | #1
Bharata B Rao <bharata@amd.com> writes:

> In order to check how efficiently the existing NUMA balancing
> based hot page promotion mechanism can detect hot regions and
> promote pages for workloads with large memory footprints, I
> wrote and tested a program that allocates huge amount of
> memory but routinely touches only small parts of it.
>
> This microbenchmark provisions memory both on DRAM node and CXL node.
> It then divides the entire allocated memory into chunks of smaller
> size and randomly choses a chunk for generating memory accesses.
> Each chunk is then accessed for a fixed number of iterations to
> create the notion of hotness. Within each chunk, the individual
> pages at 4K granularity are again accessed in random fashion.
>
> When a chunk is taken up for access in this manner, its pages
> can either be residing on DRAM or CXL. In the latter case, the NUMA
> balancing driven hot page promotion logic is expected to detect and
> promote the hot pages that reside on CXL.
>
> The experiment was conducted on a 2P AMD Bergamo system that has
> CXL as the 3rd node.
>
> $ numactl -H
> available: 3 nodes (0-2)
> node 0 cpus: 0-127,256-383
> node 0 size: 128054 MB
> node 1 cpus: 128-255,384-511
> node 1 size: 128880 MB
> node 2 cpus:
> node 2 size: 129024 MB
> node distances:
> node   0   1   2 
>   0:  10  32  60 
>   1:  32  10  50 
>   2:  255  255  10
>
> It is seen that number of pages that get promoted is really low and
> the reason for it happens to be that the NUMA hint fault latency turns
> out to be much higher than the hot threshold most of the times. Here
> are a few latency and threshold sample values captured from
> should_numa_migrate_memory() routine when the benchmark was run:
>
> latency	threshold (in ms)
> 20620	1125
> 56185	1125
> 98710	1250
> 148871	1375
> 182891	1625
> 369415	1875
> 630745	2000

The access latency of your workload is 20s to 630s, which appears too
long.  Can you try to increase the range of threshold to deal with that?
For example,

echo 100000 > /sys/kernel/debug/sched/numa_balancing/hot_threshold_ms

[snip]

--
Best Regards,
Huang, Ying
Bharata B Rao March 28, 2024, 5:49 a.m. UTC | #2
On 28-Mar-24 11:05 AM, Huang, Ying wrote:
> Bharata B Rao <bharata@amd.com> writes:
> 
>> In order to check how efficiently the existing NUMA balancing
>> based hot page promotion mechanism can detect hot regions and
>> promote pages for workloads with large memory footprints, I
>> wrote and tested a program that allocates huge amount of
>> memory but routinely touches only small parts of it.
>>
>> This microbenchmark provisions memory both on DRAM node and CXL node.
>> It then divides the entire allocated memory into chunks of smaller
>> size and randomly choses a chunk for generating memory accesses.
>> Each chunk is then accessed for a fixed number of iterations to
>> create the notion of hotness. Within each chunk, the individual
>> pages at 4K granularity are again accessed in random fashion.
>>
>> When a chunk is taken up for access in this manner, its pages
>> can either be residing on DRAM or CXL. In the latter case, the NUMA
>> balancing driven hot page promotion logic is expected to detect and
>> promote the hot pages that reside on CXL.
>>
>> The experiment was conducted on a 2P AMD Bergamo system that has
>> CXL as the 3rd node.
>>
>> $ numactl -H
>> available: 3 nodes (0-2)
>> node 0 cpus: 0-127,256-383
>> node 0 size: 128054 MB
>> node 1 cpus: 128-255,384-511
>> node 1 size: 128880 MB
>> node 2 cpus:
>> node 2 size: 129024 MB
>> node distances:
>> node   0   1   2 
>>   0:  10  32  60 
>>   1:  32  10  50 
>>   2:  255  255  10
>>
>> It is seen that number of pages that get promoted is really low and
>> the reason for it happens to be that the NUMA hint fault latency turns
>> out to be much higher than the hot threshold most of the times. Here
>> are a few latency and threshold sample values captured from
>> should_numa_migrate_memory() routine when the benchmark was run:
>>
>> latency	threshold (in ms)
>> 20620	1125
>> 56185	1125
>> 98710	1250
>> 148871	1375
>> 182891	1625
>> 369415	1875
>> 630745	2000
> 
> The access latency of your workload is 20s to 630s, which appears too
> long.  Can you try to increase the range of threshold to deal with that?
> For example,
> 
> echo 100000 > /sys/kernel/debug/sched/numa_balancing/hot_threshold_ms

That of course should help. But I was exploring alternatives where the
notion of hotness can be de-linked from the absolute scanning time to
the extent possible. For large memory workloads where only parts of memory
get accessed at once, the scanning time can lag from the actual access
time significantly as the data above shows. Wondering if such cases can
be addressed without having to be workload-specific.

Regards,
Bharata.
Huang, Ying March 28, 2024, 6:03 a.m. UTC | #3
Bharata B Rao <bharata@amd.com> writes:

> On 28-Mar-24 11:05 AM, Huang, Ying wrote:
>> Bharata B Rao <bharata@amd.com> writes:
>> 
>>> In order to check how efficiently the existing NUMA balancing
>>> based hot page promotion mechanism can detect hot regions and
>>> promote pages for workloads with large memory footprints, I
>>> wrote and tested a program that allocates huge amount of
>>> memory but routinely touches only small parts of it.
>>>
>>> This microbenchmark provisions memory both on DRAM node and CXL node.
>>> It then divides the entire allocated memory into chunks of smaller
>>> size and randomly choses a chunk for generating memory accesses.
>>> Each chunk is then accessed for a fixed number of iterations to
>>> create the notion of hotness. Within each chunk, the individual
>>> pages at 4K granularity are again accessed in random fashion.
>>>
>>> When a chunk is taken up for access in this manner, its pages
>>> can either be residing on DRAM or CXL. In the latter case, the NUMA
>>> balancing driven hot page promotion logic is expected to detect and
>>> promote the hot pages that reside on CXL.
>>>
>>> The experiment was conducted on a 2P AMD Bergamo system that has
>>> CXL as the 3rd node.
>>>
>>> $ numactl -H
>>> available: 3 nodes (0-2)
>>> node 0 cpus: 0-127,256-383
>>> node 0 size: 128054 MB
>>> node 1 cpus: 128-255,384-511
>>> node 1 size: 128880 MB
>>> node 2 cpus:
>>> node 2 size: 129024 MB
>>> node distances:
>>> node   0   1   2 
>>>   0:  10  32  60 
>>>   1:  32  10  50 
>>>   2:  255  255  10
>>>
>>> It is seen that number of pages that get promoted is really low and
>>> the reason for it happens to be that the NUMA hint fault latency turns
>>> out to be much higher than the hot threshold most of the times. Here
>>> are a few latency and threshold sample values captured from
>>> should_numa_migrate_memory() routine when the benchmark was run:
>>>
>>> latency	threshold (in ms)
>>> 20620	1125
>>> 56185	1125
>>> 98710	1250
>>> 148871	1375
>>> 182891	1625
>>> 369415	1875
>>> 630745	2000
>> 
>> The access latency of your workload is 20s to 630s, which appears too
>> long.  Can you try to increase the range of threshold to deal with that?
>> For example,
>> 
>> echo 100000 > /sys/kernel/debug/sched/numa_balancing/hot_threshold_ms
>
> That of course should help. But I was exploring alternatives where the
> notion of hotness can be de-linked from the absolute scanning time to

In fact, only relative time from scan to hint fault is recorded and
calculated, we have only limited bits.

> the extent possible. For large memory workloads where only parts of memory
> get accessed at once, the scanning time can lag from the actual access
> time significantly as the data above shows. Wondering if such cases can
> be addressed without having to be workload-specific.

Does it really matter to promote the quite cold pages (accessed every
more than 20s)?  And if so, how can we adjust the current algorithm to
cover that?  I think that may be possible via extending the threshold
range.  And I think that we can find some way to extending the range by
default if necessary.

--
Best Regards,
Huang, Ying
Bharata B Rao March 28, 2024, 6:29 a.m. UTC | #4
On 28-Mar-24 11:33 AM, Huang, Ying wrote:
> Bharata B Rao <bharata@amd.com> writes:
> 
>> On 28-Mar-24 11:05 AM, Huang, Ying wrote:
>>> Bharata B Rao <bharata@amd.com> writes:
>>>
>>>> In order to check how efficiently the existing NUMA balancing
>>>> based hot page promotion mechanism can detect hot regions and
>>>> promote pages for workloads with large memory footprints, I
>>>> wrote and tested a program that allocates huge amount of
>>>> memory but routinely touches only small parts of it.
>>>>
>>>> This microbenchmark provisions memory both on DRAM node and CXL node.
>>>> It then divides the entire allocated memory into chunks of smaller
>>>> size and randomly choses a chunk for generating memory accesses.
>>>> Each chunk is then accessed for a fixed number of iterations to
>>>> create the notion of hotness. Within each chunk, the individual
>>>> pages at 4K granularity are again accessed in random fashion.
>>>>
>>>> When a chunk is taken up for access in this manner, its pages
>>>> can either be residing on DRAM or CXL. In the latter case, the NUMA
>>>> balancing driven hot page promotion logic is expected to detect and
>>>> promote the hot pages that reside on CXL.
>>>>
>>>> The experiment was conducted on a 2P AMD Bergamo system that has
>>>> CXL as the 3rd node.
>>>>
>>>> $ numactl -H
>>>> available: 3 nodes (0-2)
>>>> node 0 cpus: 0-127,256-383
>>>> node 0 size: 128054 MB
>>>> node 1 cpus: 128-255,384-511
>>>> node 1 size: 128880 MB
>>>> node 2 cpus:
>>>> node 2 size: 129024 MB
>>>> node distances:
>>>> node   0   1   2 
>>>>   0:  10  32  60 
>>>>   1:  32  10  50 
>>>>   2:  255  255  10
>>>>
>>>> It is seen that number of pages that get promoted is really low and
>>>> the reason for it happens to be that the NUMA hint fault latency turns
>>>> out to be much higher than the hot threshold most of the times. Here
>>>> are a few latency and threshold sample values captured from
>>>> should_numa_migrate_memory() routine when the benchmark was run:
>>>>
>>>> latency	threshold (in ms)
>>>> 20620	1125
>>>> 56185	1125
>>>> 98710	1250
>>>> 148871	1375
>>>> 182891	1625
>>>> 369415	1875
>>>> 630745	2000
>>>
>>> The access latency of your workload is 20s to 630s, which appears too
>>> long.  Can you try to increase the range of threshold to deal with that?
>>> For example,
>>>
>>> echo 100000 > /sys/kernel/debug/sched/numa_balancing/hot_threshold_ms
>>
>> That of course should help. But I was exploring alternatives where the
>> notion of hotness can be de-linked from the absolute scanning time to
> 
> In fact, only relative time from scan to hint fault is recorded and
> calculated, we have only limited bits.
> 
>> the extent possible. For large memory workloads where only parts of memory
>> get accessed at once, the scanning time can lag from the actual access
>> time significantly as the data above shows. Wondering if such cases can
>> be addressed without having to be workload-specific.
> 
> Does it really matter to promote the quite cold pages (accessed every
> more than 20s)?  And if so, how can we adjust the current algorithm to
> cover that?  I think that may be possible via extending the threshold
> range.  And I think that we can find some way to extending the range by
> default if necessary.

I don't think the pages are cold but rather the existing mechanism fails
to categorize them as hot. This is because the pages were scanned way
before the accesses start happening. When repeated accesses are made to
a chunk of memory that has been scanned a while back, none of those
accesses get classified as hot because the scan time is way behind
the current access time. That's the reason we are seeing the value
of latency ranging from 20s to 630s as shown above.

Regards,
Bharata.
Huang, Ying March 29, 2024, 1:14 a.m. UTC | #5
Bharata B Rao <bharata@amd.com> writes:

> On 28-Mar-24 11:33 AM, Huang, Ying wrote:
>> Bharata B Rao <bharata@amd.com> writes:
>> 
>>> On 28-Mar-24 11:05 AM, Huang, Ying wrote:
>>>> Bharata B Rao <bharata@amd.com> writes:
>>>>
>>>>> In order to check how efficiently the existing NUMA balancing
>>>>> based hot page promotion mechanism can detect hot regions and
>>>>> promote pages for workloads with large memory footprints, I
>>>>> wrote and tested a program that allocates huge amount of
>>>>> memory but routinely touches only small parts of it.
>>>>>
>>>>> This microbenchmark provisions memory both on DRAM node and CXL node.
>>>>> It then divides the entire allocated memory into chunks of smaller
>>>>> size and randomly choses a chunk for generating memory accesses.
>>>>> Each chunk is then accessed for a fixed number of iterations to
>>>>> create the notion of hotness. Within each chunk, the individual
>>>>> pages at 4K granularity are again accessed in random fashion.
>>>>>
>>>>> When a chunk is taken up for access in this manner, its pages
>>>>> can either be residing on DRAM or CXL. In the latter case, the NUMA
>>>>> balancing driven hot page promotion logic is expected to detect and
>>>>> promote the hot pages that reside on CXL.
>>>>>
>>>>> The experiment was conducted on a 2P AMD Bergamo system that has
>>>>> CXL as the 3rd node.
>>>>>
>>>>> $ numactl -H
>>>>> available: 3 nodes (0-2)
>>>>> node 0 cpus: 0-127,256-383
>>>>> node 0 size: 128054 MB
>>>>> node 1 cpus: 128-255,384-511
>>>>> node 1 size: 128880 MB
>>>>> node 2 cpus:
>>>>> node 2 size: 129024 MB
>>>>> node distances:
>>>>> node   0   1   2 
>>>>>   0:  10  32  60 
>>>>>   1:  32  10  50 
>>>>>   2:  255  255  10
>>>>>
>>>>> It is seen that number of pages that get promoted is really low and
>>>>> the reason for it happens to be that the NUMA hint fault latency turns
>>>>> out to be much higher than the hot threshold most of the times. Here
>>>>> are a few latency and threshold sample values captured from
>>>>> should_numa_migrate_memory() routine when the benchmark was run:
>>>>>
>>>>> latency	threshold (in ms)
>>>>> 20620	1125
>>>>> 56185	1125
>>>>> 98710	1250
>>>>> 148871	1375
>>>>> 182891	1625
>>>>> 369415	1875
>>>>> 630745	2000
>>>>
>>>> The access latency of your workload is 20s to 630s, which appears too
>>>> long.  Can you try to increase the range of threshold to deal with that?
>>>> For example,
>>>>
>>>> echo 100000 > /sys/kernel/debug/sched/numa_balancing/hot_threshold_ms
>>>
>>> That of course should help. But I was exploring alternatives where the
>>> notion of hotness can be de-linked from the absolute scanning time to
>> 
>> In fact, only relative time from scan to hint fault is recorded and
>> calculated, we have only limited bits.
>> 
>>> the extent possible. For large memory workloads where only parts of memory
>>> get accessed at once, the scanning time can lag from the actual access
>>> time significantly as the data above shows. Wondering if such cases can
>>> be addressed without having to be workload-specific.
>> 
>> Does it really matter to promote the quite cold pages (accessed every
>> more than 20s)?  And if so, how can we adjust the current algorithm to
>> cover that?  I think that may be possible via extending the threshold
>> range.  And I think that we can find some way to extending the range by
>> default if necessary.
>
> I don't think the pages are cold but rather the existing mechanism fails
> to categorize them as hot. This is because the pages were scanned way
> before the accesses start happening. When repeated accesses are made to
> a chunk of memory that has been scanned a while back, none of those
> accesses get classified as hot because the scan time is way behind
> the current access time. That's the reason we are seeing the value
> of latency ranging from 20s to 630s as shown above.

If repeated accesses continue, the page will be identified as hot when
it is scanned next time even if we don't expand the threshold range.  If
the repeated accesses only last very short time, it makes little sense
to identify the pages as hot.  Right?

The bits to record scan time or hint page fault is limited, so it's
possible for it to overflow anyway.  We scan scale time stamp if
necessary (for example, from 1ms to 10ms).  But it's hard to scale fault
counter.  And nobody can guarantee the frequency of hint page fault must
be less 1/ms, if it's 10/ms, it can record even short interval.

--
Best Regards,
Huang, Ying
Bharata B Rao April 1, 2024, 12:20 p.m. UTC | #6
On 29-Mar-24 6:44 AM, Huang, Ying wrote:
> Bharata B Rao <bharata@amd.com> writes:
<snip>
>> I don't think the pages are cold but rather the existing mechanism fails
>> to categorize them as hot. This is because the pages were scanned way
>> before the accesses start happening. When repeated accesses are made to
>> a chunk of memory that has been scanned a while back, none of those
>> accesses get classified as hot because the scan time is way behind
>> the current access time. That's the reason we are seeing the value
>> of latency ranging from 20s to 630s as shown above.
> 
> If repeated accesses continue, the page will be identified as hot when
> it is scanned next time even if we don't expand the threshold range.  If
> the repeated accesses only last very short time, it makes little sense
> to identify the pages as hot.  Right?

The total allocated memory here is 192G and the chunk size is 1G. Each
time one such 1G chunk is taken up randomly for generating memory accesses.
Within that 1G, 262144 random accesses are performed and 262144 such
accesses are repeated for 512 times. I thought that should be enough
to classify that chunk of memory as hot. But as we see, often times
the scan time is lagging the access time by a large value.

Let me instrument the code further to learn more insights (if possible)
about the scanning/fault time behaviors here.

Leaving the fault count based threshold apart, do you think there is
value in updating the scan time for skipped pages/PTEs during every
scan so that the scan time remains current for all the pages?

> 
> The bits to record scan time or hint page fault is limited, so it's
> possible for it to overflow anyway.  We scan scale time stamp if
> necessary (for example, from 1ms to 10ms).  But it's hard to scale fault
> counter.  And nobody can guarantee the frequency of hint page fault must
> be less 1/ms, if it's 10/ms, it can record even short interval.

Yes, with the approach I have taken, the time factor is out of the
equation and the notion of hotness is purely a factor of the number of
faults (or accesses)

Regards,
Bharata.
Huang, Ying April 2, 2024, 2:03 a.m. UTC | #7
Bharata B Rao <bharata@amd.com> writes:

> On 29-Mar-24 6:44 AM, Huang, Ying wrote:
>> Bharata B Rao <bharata@amd.com> writes:
> <snip>
>>> I don't think the pages are cold but rather the existing mechanism fails
>>> to categorize them as hot. This is because the pages were scanned way
>>> before the accesses start happening. When repeated accesses are made to
>>> a chunk of memory that has been scanned a while back, none of those
>>> accesses get classified as hot because the scan time is way behind
>>> the current access time. That's the reason we are seeing the value
>>> of latency ranging from 20s to 630s as shown above.
>> 
>> If repeated accesses continue, the page will be identified as hot when
>> it is scanned next time even if we don't expand the threshold range.  If
>> the repeated accesses only last very short time, it makes little sense
>> to identify the pages as hot.  Right?
>
> The total allocated memory here is 192G and the chunk size is 1G. Each
> time one such 1G chunk is taken up randomly for generating memory accesses.
> Within that 1G, 262144 random accesses are performed and 262144 such
> accesses are repeated for 512 times. I thought that should be enough
> to classify that chunk of memory as hot.

IIUC, some pages are accessed in very short time (maybe within 1ms).
This isn't repeated access in a long period.  I think that pages
accessed repeatedly in a long period are good candidates for promoting.
But pages accessed frequently in only very short time aren't.

> But as we see, often times
> the scan time is lagging the access time by a large value.
>
> Let me instrument the code further to learn more insights (if possible)
> about the scanning/fault time behaviors here.
>
> Leaving the fault count based threshold apart, do you think there is
> value in updating the scan time for skipped pages/PTEs during every
> scan so that the scan time remains current for all the pages?

No, I don't think so.  That makes hint page fault latency more
inaccurate.

>> 
>> The bits to record scan time or hint page fault is limited, so it's
>> possible for it to overflow anyway.  We scan scale time stamp if
>> necessary (for example, from 1ms to 10ms).  But it's hard to scale fault
>> counter.  And nobody can guarantee the frequency of hint page fault must
>> be less 1/ms, if it's 10/ms, it can record even short interval.
>
> Yes, with the approach I have taken, the time factor is out of the
> equation and the notion of hotness is purely a factor of the number of
> faults (or accesses)

Sorry, I don't get your idea here.  I think that the fault count may be
worse than time in quite some cases.

--
Best Regards,
Huang, Ying
Bharata B Rao April 2, 2024, 9:26 a.m. UTC | #8
On 02-Apr-24 7:33 AM, Huang, Ying wrote:
> Bharata B Rao <bharata@amd.com> writes:
> 
>> On 29-Mar-24 6:44 AM, Huang, Ying wrote:
>>> Bharata B Rao <bharata@amd.com> writes:
>> <snip>
>>>> I don't think the pages are cold but rather the existing mechanism fails
>>>> to categorize them as hot. This is because the pages were scanned way
>>>> before the accesses start happening. When repeated accesses are made to
>>>> a chunk of memory that has been scanned a while back, none of those
>>>> accesses get classified as hot because the scan time is way behind
>>>> the current access time. That's the reason we are seeing the value
>>>> of latency ranging from 20s to 630s as shown above.
>>>
>>> If repeated accesses continue, the page will be identified as hot when
>>> it is scanned next time even if we don't expand the threshold range.  If
>>> the repeated accesses only last very short time, it makes little sense
>>> to identify the pages as hot.  Right?
>>
>> The total allocated memory here is 192G and the chunk size is 1G. Each
>> time one such 1G chunk is taken up randomly for generating memory accesses.
>> Within that 1G, 262144 random accesses are performed and 262144 such
>> accesses are repeated for 512 times. I thought that should be enough
>> to classify that chunk of memory as hot.
> 
> IIUC, some pages are accessed in very short time (maybe within 1ms).
> This isn't repeated access in a long period.  I think that pages
> accessed repeatedly in a long period are good candidates for promoting.
> But pages accessed frequently in only very short time aren't.

Here are the numbers for the 192nd chunk:

Each iteration of 262144 random accesses takes around ~10ms
512 such iterations are taking ~5s
numa_scan_seq is 16 when this chunk is accessed.
And no page promotions were done from this chunk. All the
time should_numa_migrate_memory() found the NUMA hint fault
latency to be higher than threshold.

Are these time periods considered too short for the pages
to be detected as hot and promoted?

> 
>> But as we see, often times
>> the scan time is lagging the access time by a large value.
>>
>> Let me instrument the code further to learn more insights (if possible)
>> about the scanning/fault time behaviors here.
>>
>> Leaving the fault count based threshold apart, do you think there is
>> value in updating the scan time for skipped pages/PTEs during every
>> scan so that the scan time remains current for all the pages?
> 
> No, I don't think so.  That makes hint page fault latency more
> inaccurate.

For the case that I have shown, depending on a old value of scan
time doesn't work well when pages get accessed after a long time
since scanning. At least with the scheme I show in patch 2/2,
probability of detecting pages as hot increases.

Regards,
Bharata.
Huang, Ying April 3, 2024, 8:40 a.m. UTC | #9
Bharata B Rao <bharata@amd.com> writes:

> On 02-Apr-24 7:33 AM, Huang, Ying wrote:
>> Bharata B Rao <bharata@amd.com> writes:
>> 
>>> On 29-Mar-24 6:44 AM, Huang, Ying wrote:
>>>> Bharata B Rao <bharata@amd.com> writes:
>>> <snip>
>>>>> I don't think the pages are cold but rather the existing mechanism fails
>>>>> to categorize them as hot. This is because the pages were scanned way
>>>>> before the accesses start happening. When repeated accesses are made to
>>>>> a chunk of memory that has been scanned a while back, none of those
>>>>> accesses get classified as hot because the scan time is way behind
>>>>> the current access time. That's the reason we are seeing the value
>>>>> of latency ranging from 20s to 630s as shown above.
>>>>
>>>> If repeated accesses continue, the page will be identified as hot when
>>>> it is scanned next time even if we don't expand the threshold range.  If
>>>> the repeated accesses only last very short time, it makes little sense
>>>> to identify the pages as hot.  Right?
>>>
>>> The total allocated memory here is 192G and the chunk size is 1G. Each
>>> time one such 1G chunk is taken up randomly for generating memory accesses.
>>> Within that 1G, 262144 random accesses are performed and 262144 such
>>> accesses are repeated for 512 times. I thought that should be enough
>>> to classify that chunk of memory as hot.
>> 
>> IIUC, some pages are accessed in very short time (maybe within 1ms).
>> This isn't repeated access in a long period.  I think that pages
>> accessed repeatedly in a long period are good candidates for promoting.
>> But pages accessed frequently in only very short time aren't.
>
> Here are the numbers for the 192nd chunk:
>
> Each iteration of 262144 random accesses takes around ~10ms
> 512 such iterations are taking ~5s
> numa_scan_seq is 16 when this chunk is accessed.
> And no page promotions were done from this chunk. All the
> time should_numa_migrate_memory() found the NUMA hint fault
> latency to be higher than threshold.
>
> Are these time periods considered too short for the pages
> to be detected as hot and promoted?

Yes.  I think so.  This is burst accessing, not repeated accessing.
IIUC, NUMA balancing based promotion only works for repeated accessing
for long time, for example, >100s.

>> 
>>> But as we see, often times
>>> the scan time is lagging the access time by a large value.
>>>
>>> Let me instrument the code further to learn more insights (if possible)
>>> about the scanning/fault time behaviors here.
>>>
>>> Leaving the fault count based threshold apart, do you think there is
>>> value in updating the scan time for skipped pages/PTEs during every
>>> scan so that the scan time remains current for all the pages?
>> 
>> No, I don't think so.  That makes hint page fault latency more
>> inaccurate.
>
> For the case that I have shown, depending on a old value of scan
> time doesn't work well when pages get accessed after a long time
> since scanning. At least with the scheme I show in patch 2/2,
> probability of detecting pages as hot increases.

Yes.  This may help your cases, but it will hurt other cases with
incorrect hint page fault latency.

To resolve your issue, we can increase the max value of the hot
threshold automatically.  We can work on that if you can find a real
workload.

--
Best Regards,
Huang, Ying
Bharata B Rao April 12, 2024, 4 a.m. UTC | #10
On 03-Apr-24 2:10 PM, Huang, Ying wrote:
> Bharata B Rao <bharata@amd.com> writes:
> 
>> On 02-Apr-24 7:33 AM, Huang, Ying wrote:
>>> Bharata B Rao <bharata@amd.com> writes:
>>>
>>>> On 29-Mar-24 6:44 AM, Huang, Ying wrote:
>>>>> Bharata B Rao <bharata@amd.com> writes:
>>>> <snip>
>>>>>> I don't think the pages are cold but rather the existing mechanism fails
>>>>>> to categorize them as hot. This is because the pages were scanned way
>>>>>> before the accesses start happening. When repeated accesses are made to
>>>>>> a chunk of memory that has been scanned a while back, none of those
>>>>>> accesses get classified as hot because the scan time is way behind
>>>>>> the current access time. That's the reason we are seeing the value
>>>>>> of latency ranging from 20s to 630s as shown above.
>>>>>
>>>>> If repeated accesses continue, the page will be identified as hot when
>>>>> it is scanned next time even if we don't expand the threshold range.  If
>>>>> the repeated accesses only last very short time, it makes little sense
>>>>> to identify the pages as hot.  Right?
>>>>
>>>> The total allocated memory here is 192G and the chunk size is 1G. Each
>>>> time one such 1G chunk is taken up randomly for generating memory accesses.
>>>> Within that 1G, 262144 random accesses are performed and 262144 such
>>>> accesses are repeated for 512 times. I thought that should be enough
>>>> to classify that chunk of memory as hot.
>>>
>>> IIUC, some pages are accessed in very short time (maybe within 1ms).
>>> This isn't repeated access in a long period.  I think that pages
>>> accessed repeatedly in a long period are good candidates for promoting.
>>> But pages accessed frequently in only very short time aren't.
>>
>> Here are the numbers for the 192nd chunk:
>>
>> Each iteration of 262144 random accesses takes around ~10ms
>> 512 such iterations are taking ~5s
>> numa_scan_seq is 16 when this chunk is accessed.
>> And no page promotions were done from this chunk. All the
>> time should_numa_migrate_memory() found the NUMA hint fault
>> latency to be higher than threshold.
>>
>> Are these time periods considered too short for the pages
>> to be detected as hot and promoted?
> 
> Yes.  I think so.  This is burst accessing, not repeated accessing.
> IIUC, NUMA balancing based promotion only works for repeated accessing
> for long time, for example, >100s.

Hmm... When a page is accessed 512 times over a period of 5s and it is
still not detected as hot. This is understandable if fresh scanning couldn't
be done as the accesses were bursty and hence they couldn't be captured via
NUMA hint faults. But here the access captured via hint fault is being rejected
as not hot because the scanning was done a while back. But I do see the challenge
here since we depend on scanning time to obtain the frequency-of-access metric.

BTW, for the above same scenario with numa_balancing_mode=1, the remote
accesses get detected and migration to source node is tried. It is a different
matter that eventually pages can't be migrated in this specific scenario as
the src node is already full.

Regards,
Bharata.
Huang, Ying April 12, 2024, 7:28 a.m. UTC | #11
Bharata B Rao <bharata@amd.com> writes:

> On 03-Apr-24 2:10 PM, Huang, Ying wrote:
>> Bharata B Rao <bharata@amd.com> writes:
>> 
>>> On 02-Apr-24 7:33 AM, Huang, Ying wrote:
>>>> Bharata B Rao <bharata@amd.com> writes:
>>>>
>>>>> On 29-Mar-24 6:44 AM, Huang, Ying wrote:
>>>>>> Bharata B Rao <bharata@amd.com> writes:
>>>>> <snip>
>>>>>>> I don't think the pages are cold but rather the existing mechanism fails
>>>>>>> to categorize them as hot. This is because the pages were scanned way
>>>>>>> before the accesses start happening. When repeated accesses are made to
>>>>>>> a chunk of memory that has been scanned a while back, none of those
>>>>>>> accesses get classified as hot because the scan time is way behind
>>>>>>> the current access time. That's the reason we are seeing the value
>>>>>>> of latency ranging from 20s to 630s as shown above.
>>>>>>
>>>>>> If repeated accesses continue, the page will be identified as hot when
>>>>>> it is scanned next time even if we don't expand the threshold range.  If
>>>>>> the repeated accesses only last very short time, it makes little sense
>>>>>> to identify the pages as hot.  Right?
>>>>>
>>>>> The total allocated memory here is 192G and the chunk size is 1G. Each
>>>>> time one such 1G chunk is taken up randomly for generating memory accesses.
>>>>> Within that 1G, 262144 random accesses are performed and 262144 such
>>>>> accesses are repeated for 512 times. I thought that should be enough
>>>>> to classify that chunk of memory as hot.
>>>>
>>>> IIUC, some pages are accessed in very short time (maybe within 1ms).
>>>> This isn't repeated access in a long period.  I think that pages
>>>> accessed repeatedly in a long period are good candidates for promoting.
>>>> But pages accessed frequently in only very short time aren't.
>>>
>>> Here are the numbers for the 192nd chunk:
>>>
>>> Each iteration of 262144 random accesses takes around ~10ms
>>> 512 such iterations are taking ~5s
>>> numa_scan_seq is 16 when this chunk is accessed.
>>> And no page promotions were done from this chunk. All the
>>> time should_numa_migrate_memory() found the NUMA hint fault
>>> latency to be higher than threshold.
>>>
>>> Are these time periods considered too short for the pages
>>> to be detected as hot and promoted?
>> 
>> Yes.  I think so.  This is burst accessing, not repeated accessing.
>> IIUC, NUMA balancing based promotion only works for repeated accessing
>> for long time, for example, >100s.
>
> Hmm... When a page is accessed 512 times over a period of 5s and it is
> still not detected as hot. This is understandable if fresh scanning couldn't
> be done as the accesses were bursty and hence they couldn't be captured via
> NUMA hint faults. But here the access captured via hint fault is being rejected
> as not hot because the scanning was done a while back. But I do see the challenge
> here since we depend on scanning time to obtain the frequency-of-access metric.

Consider some pages that will be accessed once every 1 hour, should we
consider it hot or not?  Will your proposed method deal with that
correctly?

> BTW, for the above same scenario with numa_balancing_mode=1, the remote
> accesses get detected and migration to source node is tried. It is a different
> matter that eventually pages can't be migrated in this specific scenario as
> the src node is already full.

--
Best Regards,
Huang, Ying
Bharata B Rao April 12, 2024, 8:16 a.m. UTC | #12
On 12-Apr-24 12:58 PM, Huang, Ying wrote:
> Bharata B Rao <bharata@amd.com> writes:
> 
>> On 03-Apr-24 2:10 PM, Huang, Ying wrote:
>>>> Here are the numbers for the 192nd chunk:
>>>>
>>>> Each iteration of 262144 random accesses takes around ~10ms
>>>> 512 such iterations are taking ~5s
>>>> numa_scan_seq is 16 when this chunk is accessed.
>>>> And no page promotions were done from this chunk. All the
>>>> time should_numa_migrate_memory() found the NUMA hint fault
>>>> latency to be higher than threshold.
>>>>
>>>> Are these time periods considered too short for the pages
>>>> to be detected as hot and promoted?
>>>
>>> Yes.  I think so.  This is burst accessing, not repeated accessing.
>>> IIUC, NUMA balancing based promotion only works for repeated accessing
>>> for long time, for example, >100s.
>>
>> Hmm... When a page is accessed 512 times over a period of 5s and it is
>> still not detected as hot. This is understandable if fresh scanning couldn't
>> be done as the accesses were bursty and hence they couldn't be captured via
>> NUMA hint faults. But here the access captured via hint fault is being rejected
>> as not hot because the scanning was done a while back. But I do see the challenge
>> here since we depend on scanning time to obtain the frequency-of-access metric.
> 
> Consider some pages that will be accessed once every 1 hour, should we
> consider it hot or not?  Will your proposed method deal with that
> correctly?

The proposed method removes the absolute time as a factor for the decision and instead
relies on the number of hint faults that have occurred since that page was scanned last.
As long as there are enough hint faults happening in that 1 hour (which means a lot many
other accesses have been captured in that 1 hour), that page shouldn't be considered as
hot. You did mention earlier about hint fault rate varying a lot and one thing I haven't
tried yet is to vary the fault threshold based on current or historical fault rate.

Regards,
Bharata.
Huang, Ying April 12, 2024, 8:48 a.m. UTC | #13
Bharata B Rao <bharata@amd.com> writes:

> On 12-Apr-24 12:58 PM, Huang, Ying wrote:
>> Bharata B Rao <bharata@amd.com> writes:
>> 
>>> On 03-Apr-24 2:10 PM, Huang, Ying wrote:
>>>>> Here are the numbers for the 192nd chunk:
>>>>>
>>>>> Each iteration of 262144 random accesses takes around ~10ms
>>>>> 512 such iterations are taking ~5s
>>>>> numa_scan_seq is 16 when this chunk is accessed.
>>>>> And no page promotions were done from this chunk. All the
>>>>> time should_numa_migrate_memory() found the NUMA hint fault
>>>>> latency to be higher than threshold.
>>>>>
>>>>> Are these time periods considered too short for the pages
>>>>> to be detected as hot and promoted?
>>>>
>>>> Yes.  I think so.  This is burst accessing, not repeated accessing.
>>>> IIUC, NUMA balancing based promotion only works for repeated accessing
>>>> for long time, for example, >100s.
>>>
>>> Hmm... When a page is accessed 512 times over a period of 5s and it is
>>> still not detected as hot. This is understandable if fresh scanning couldn't
>>> be done as the accesses were bursty and hence they couldn't be captured via
>>> NUMA hint faults. But here the access captured via hint fault is being rejected
>>> as not hot because the scanning was done a while back. But I do see the challenge
>>> here since we depend on scanning time to obtain the frequency-of-access metric.
>> 
>> Consider some pages that will be accessed once every 1 hour, should we
>> consider it hot or not?  Will your proposed method deal with that
>> correctly?
>
> The proposed method removes the absolute time as a factor for the decision and instead
> relies on the number of hint faults that have occurred since that page was scanned last.
> As long as there are enough hint faults happening in that 1 hour (which means a lot many
> other accesses have been captured in that 1 hour), that page shouldn't be considered as
> hot. You did mention earlier about hint fault rate varying a lot and one thing I haven't
> tried yet is to vary the fault threshold based on current or historical fault rate.

In your original example, if a lot many other accesses between NUMA
balancing page table scanning and 512 page accesses, you cannot identify
the page as hot too, right?

If the NUMA balancing page table scanning period is much longer than 5s,
it's high possible that we cannot distinguish between 1 and 512 page
accesses within 5s with your method and the original method.

Better discuss the behavior with a more detail example, for example,
when the page is scanned, how many pages are accessed, how long between
accesses, etc.

--
Best Regards,
Huang, Ying