KSM: Replace jhash2 with xxhash
PATCH 1/1 KSM: Replace jhash2 with xxhash
xxhash much faster then jhash,
ex. for x86_64 host:PAGE_SIZE: 4096, loop count: 1048576
jhash2: 0xacbc7a5b time: 1907 ms, th: 2251.9 MiB/s
xxhash32: 0x570da981 time: 739 ms, th: 5809.4 MiB/s
xxhash64: 0xa1fa032ab85bbb62 time: 371 ms, th: 11556.6 MiB/s
xxhash64 on x86_32 work with ~ same speed as jhash2. xxhash32 on x86_32 work with ~ same speed as for x86_64
So replace jhash with xxhash,
and use fastest version for current target ARCH.
--- mm/Kconfig | 1 + mm/ksm.c | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-)
mm/Kconfig +1 -0
@@ -305,6 +305,7 @@ config MMU_NOTIFIER config KSM bool "Enable KSM for page merging" depends on MMU + select XXHASH help Enable Kernel Samepage Merging: KSM periodically scans those areas of an application's address space that an app has advised may be
mm/ksm.c +18 -7
@@ -25,7 +25,8 @@#include <linux/pagemap.h> #include <linux/rmap.h> #include <linux/spinlock.h> -#include <linux/jhash.h> +#include <linux/xxhash.h> +#include <linux/bitops.h> /* BITS_PER_LONG */ #include <linux/delay.h> #include <linux/kthread.h> #include <linux/wait.h>
@@ -51,6 +52,12 @@#define DO_NUMA (x) do { } while (0)#endif +#if BITS_PER_LONG ==64 +typedef u64 xxhash ; +#else +typedef u32 xxhash ; +#endif +/* * A few notes about the KSM scanning process, * to make it easier to understand the data structures below:
@@ -186,7 +193,7 @@ struct rmap_item { };struct mm_struct *mm;unsigned long address;/* + low bits used for flags below */ -unsigned int oldchecksum;/* when unstable */ +xxhash oldchecksum;/* when unstable */ union {struct rb_node node;/* when node of unstable tree */ struct {/* when listed from stable tree */
@@ -255,7 +262,7 @@ static unsigned int ksm_thread_pages_to_scan = 100;static unsigned int ksm_thread_sleep_millisecs =20 ;/* Checksum of an empty (zeroed) page */ -static unsigned int zero_checksum__read_mostly ; +static xxhash zero_checksum__read_mostly ;/* Whether to merge empty (zeroed) pages with actual zero pages */ static bool ksm_use_zero_pages__read_mostly ;
@@ -982,11 +989,15 @@ static int unmerge_and_remove_all_rmap_items(void) }#endif /* CONFIG_SYSFS */ -static u32 calc_checksum (struct page *page) +static xxhash calc_checksum (struct page *page) { -u32 checksum; +xxhash checksum;void *addr =kmap_atomic (page); - checksum =jhash2 (addr,PAGE_SIZE /4 ,17 ); +#if BITS_PER_LONG ==64 + checksum =xxh64 (addr,PAGE_SIZE ,0 ); +#else + checksum =xxh32 (addr,PAGE_SIZE ,0 ); +#endif kunmap_atomic (addr);return checksum; }
@@ -1994,7 +2005,7 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)struct page *tree_page =NULL ;struct stable_node *stable_node;struct page *kpage; -unsigned int checksum; +xxhash checksum;int err;bool max_page_sharing_bypass = false;
Signature · 7 lines
2.14.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Timofey Titovets
xxhash much faster then jhash,
ex. forx86_64host:PAGE_SIZE: 4096, loop count: 1048576
jhash2:0xacbc7a5btime: 1907 ms, th: 2251.9 MiB/s
xxhash32:0x570da981time: 739 ms, th: 5809.4 MiB/s
xxhash64:0xa1fa032ab85bbb62time: 371 ms, th: 11556.6 MiB/sxxhash64 on
x86_32work with ~ same speed as jhash2. xxhash32 onx86_32work with ~ same speed as forx86_64
Which CPU is that?
Quoted 3 lines
So replace jhash with xxhash,
and use fastest version for current target ARCH.
Can you do some macro-benchmarking too? Something that uses KSM and show how the performance changes.
You could manually increase the scan rate to make it easier to see.
mm/ksm.c line 59
Quoted 9 lines
mm/ksm.c +5
-0@@ -51,6 +52,12 @@#define DO_NUMA (x) do { } while (0)#endif +#if BITS_PER_LONG ==64 +typedef u64 xxhash ; +#else +typedef u32 xxhash ; +#endif
This should be in xxhash.h ?
xxhash_t would seem to be a better name.
mm/ksm.c static int unmerge_and_remove_all_rmap_items(void) · line 1000
Quoted 9 lines
mm/ksm.c +6
-2-u32 checksum; +xxhash checksum;void *addr =kmap_atomic (page); - checksum =jhash2 (addr,PAGE_SIZE /4 ,17 ); +#if BITS_PER_LONG ==64 + checksum =xxh64 (addr,PAGE_SIZE ,0 ); +#else + checksum =xxh32 (addr,PAGE_SIZE ,0 ); +#endif
This should also be generic in xxhash.h
-Andi
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
2017-09-21 18:36 GMT+03:00 Andi Kleen <[email protected]>:
Quoted 9 lines
Quoted 7 lines · Timofey Titovets
xxhash much faster then jhash,
ex. forx86_64host:PAGE_SIZE: 4096, loop count: 1048576
jhash2:0xacbc7a5btime: 1907 ms, th: 2251.9 MiB/s
xxhash32:0x570da981time: 739 ms, th: 5809.4 MiB/s
xxhash64:0xa1fa032ab85bbb62time: 371 ms, th: 11556.6 MiB/s
Quoted 4 lines
Quoted 2 lines
xxhash64 on
x86_32work with ~ same speed as jhash2. xxhash32 onx86_32work with ~ same speed as forx86_64Which CPU is that?
Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
---
I've access to some VM (Not KVM) with:
Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHzPAGE_SIZE: 4096, loop count: 1048576
jhash2: 0x15433d14 time: 3661 ms, th: 1173.144082 MiB/s
xxhash32: 0x3df3de36 time: 1163 ms, th: 3691.581922 MiB/s
xxhash64: 0x5d9e67755d3c9a6a time: 715 ms, th: 6006.628034 MiB/s
As additional info, xxhash work with ~ same as jhash2 speed at 32 byte input data.
For input smaller than 32 byte, jhash2 win, for input bigger, xxhash win.
Quoted 8 lines
Quoted 2 lines
So replace jhash with xxhash,
and use fastest version for current target ARCH.Can you do some macro-benchmarking too? Something that uses KSM and show how the performance changes.
You could manually increase the scan rate to make it easier to see.
Try use that patch with my patch to allow process all VMA on system [1]. I switch sleep_millisecs 20 -> 1
(I use htop to see CPU load of ksmd)
CPU: Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz
For jhash2: ~18%
For xxhash64: ~11%
(i didn't have x86_32 test machine, so by extrapolating values, so i expect for xxhash32: (18+11)/2 = ~14.5%)
KSM Statistic:
full_scans:481
max_page_sharing:256
merge_across_nodes:1
mode:[always] normal
pages_shared:39
pages_sharing:135
pages_to_scan:100
pages_unshared:4514
pages_volatile:310
run:1
sleep_millisecs:1
stable_node_chains:0
stable_node_chains_prune_millisecs:2000
stable_node_dups:0
use_zero_pages:0
Quoted 11 lines
mm/ksm.c line 59
Quoted 9 lines
mm/ksm.c +5
-0@@ -51,6 +52,12 @@#define DO_NUMA (x) do { } while (0)#endif +#if BITS_PER_LONG ==64 +typedef u64 xxhash ; +#else +typedef u32 xxhash ; +#endif This should be in
xxhash.h?
This is a "hack", for compile time chose appropriate hash function. xxhash ported from upstream code,
upstream version don't do that (IMHO), as this useless in most cases. That only can be useful for memory only hashes.
Because for persistent data it's obvious to always use one hash type 32/64.
Quoted 13 lines
xxhash_twould seem to be a better name.mm/ksm.c static int unmerge_and_remove_all_rmap_items(void) · line 1000
Quoted 9 lines
mm/ksm.c +6
-2-u32 checksum; +xxhash checksum;void *addr =kmap_atomic (page); - checksum =jhash2 (addr,PAGE_SIZE /4 ,17 ); +#if BITS_PER_LONG ==64 + checksum =xxh64 (addr,PAGE_SIZE ,0 ); +#else + checksum =xxh32 (addr,PAGE_SIZE ,0 ); +#endif This should also be generic in
xxhash.h
This *can* be generic in xxhash.h, when that solution will be used somewhere in the kernel code, not in the KSM only, not?
Because for now i didn't find other places with "big enough" input data, to replace jhash2 with xxhash.
Quoted 3 lines
-Andi
Thanks.
Links:
1. https://marc.info/?l=linux-mm&m=150539825420373&w=2
Signature · 8 lines
Have a nice day, Timofey. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 11 lines
Quoted 1 line
Which CPU is that?
Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
---
I've access to some VM (Not KVM) with:
Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHzPAGE_SIZE: 4096, loop count: 1048576
jhash2:0x15433d14time: 3661 ms, th: 1173.144082 MiB/s
xxhash32:0x3df3de36time: 1163 ms, th: 3691.581922 MiB/s
xxhash64:0x5d9e67755d3c9a6atime: 715 ms, th: 6006.628034 MiB/s
Quoted 3 lines
As additional info, xxhash work with ~ same as jhash2 speed at 32 byte input data.
For input smaller than 32 byte, jhash2 win, for input bigger, xxhash win.
Please put that information into the changelog when you repost.
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
So replace jhash with xxhash,
and use fastest version for current target ARCH.Can you do some macro-benchmarking too? Something that uses KSM and show how the performance changes.
You could manually increase the scan rate to make it easier to see.
Quoted 8 lines
Try use that patch with my patch to allow process all VMA on system [1]. I switch
sleep_millisecs20 -> 1(I use htop to see CPU load of ksmd)
CPU: Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz
For jhash2: ~18%
For xxhash64: ~11%
Ok that's a great result. Is a speedup also visible with the default sleep_millisecs value?
Quoted 12 lines
Quoted 11 lines
mm/ksm.c line 59
Quoted 9 lines
mm/ksm.c +5
-0@@ -51,6 +52,12 @@#define DO_NUMA (x) do { } while (0)#endif +#if BITS_PER_LONG ==64 +typedef u64 xxhash ; +#else +typedef u32 xxhash ; +#endif This should be in
xxhash.h?
Quoted 5 lines
This is a "hack", for compile time chose appropriate hash function. xxhash ported from upstream code,
upstream version don't do that (IMHO), as this useless in most cases. That only can be useful for memory only hashes.
Because for persistent data it's obvious to always use one hash type 32/64.
I don't think it's a hack. It makes sense. Just should be done centrally in Linux, not in a specific user.
Quoted 15 lines
Quoted 14 lines
xxhash_twould seem to be a better name.mm/ksm.c static int unmerge_and_remove_all_rmap_items(void) · line 1000
Quoted 9 lines
mm/ksm.c +6
-2-u32 checksum; +xxhash checksum;void *addr =kmap_atomic (page); - checksum =jhash2 (addr,PAGE_SIZE /4 ,17 ); +#if BITS_PER_LONG ==64 + checksum =xxh64 (addr,PAGE_SIZE ,0 ); +#else + checksum =xxh32 (addr,PAGE_SIZE ,0 ); +#endif This should also be generic in
xxhash.h
Quoted 2 lines
This *can* be generic in
xxhash.h, when that solution will be used somewhere in the kernel code, not in the KSM only, not?
Yes.
Quoted 3 lines
Because for now i didn't find other places with "big enough" input data, to replace jhash2 with xxhash.
Right, but we may get them.
-Andi
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
2017-09-21 23:05 GMT+03:00 Andi Kleen <[email protected]>:
Quoted 31 lines
Quoted 11 lines
Quoted 1 line
Which CPU is that?
Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
---
I've access to some VM (Not KVM) with:
Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHzPAGE_SIZE: 4096, loop count: 1048576
jhash2:0x15433d14time: 3661 ms, th: 1173.144082 MiB/s
xxhash32:0x3df3de36time: 1163 ms, th: 3691.581922 MiB/s
xxhash64:0x5d9e67755d3c9a6atime: 715 ms, th: 6006.628034 MiB/sQuoted 3 lines
As additional info, xxhash work with ~ same as jhash2 speed at 32 byte input data.
For input smaller than 32 byte, jhash2 win, for input bigger, xxhash win.Please put that information into the changelog when you repost.
Quoted 14 lines
Quoted 10 lines
Quoted 4 lines
So replace jhash with xxhash,
and use fastest version for current target ARCH.Can you do some macro-benchmarking too? Something that uses KSM and show how the performance changes.
You could manually increase the scan rate to make it easier to see.
Try use that patch with my patch to allow process all VMA on system [1]. I switch
sleep_millisecs20 -> 1
Quoted 8 lines
Quoted 5 lines
(I use htop to see CPU load of ksmd)
CPU: Intel(R) Xeon(R) CPU E5-2420 0 @ 1.90GHz
For jhash2: ~18%
For xxhash64: ~11%Ok that's a great result. Is a speedup also visible with the default
sleep_millisecsvalue?
With defaults:
jhash2: ~4.7%
xxhash64: ~3.3%
3.3/4.7 ~= 0.7 -> Profit: ~30%
11/18 ~= 0.6 -> Profit: ~40%
(if i calculate correctly of course)
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
mm/ksm.c line 59
Quoted 9 lines
mm/ksm.c +5
-0@@ -51,6 +52,12 @@#define DO_NUMA (x) do { } while (0)#endif +#if BITS_PER_LONG ==64 +typedef u64 xxhash ; +#else +typedef u32 xxhash ; +#endif This should be in
xxhash.h?
Quoted 8 lines
Quoted 5 lines
This is a "hack", for compile time chose appropriate hash function. xxhash ported from upstream code,
upstream version don't do that (IMHO), as this useless in most cases. That only can be useful for memory only hashes.
Because for persistent data it's obvious to always use one hash type 32/64.I don't think it's a hack. It makes sense. Just should be done centrally in Linux, not in a specific user.
So, i must add separate patch for xxhash.h?
If yes, may be you can suggest which list must be in copy? (i can't find any info about maintainers of ./lib/ in MAINTAINERS)
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
xxhash_twould seem to be a better name.mm/ksm.c static int unmerge_and_remove_all_rmap_items(void) · line 1000
Quoted 9 lines
mm/ksm.c +6
-2-u32 checksum; +xxhash checksum;void *addr =kmap_atomic (page); - checksum =jhash2 (addr,PAGE_SIZE /4 ,17 ); +#if BITS_PER_LONG ==64 + checksum =xxh64 (addr,PAGE_SIZE ,0 ); +#else + checksum =xxh32 (addr,PAGE_SIZE ,0 ); +#endif This should also be generic in
xxhash.h
Quoted 4 lines
Quoted 2 lines
This *can* be generic in
xxhash.h, when that solution will be used somewhere in the kernel code, not in the KSM only, not?Yes.
If we decide to patch xxhash.h,
may be that will be better to wrap above if-else by something like:
/*
* Only for in memory use
*/xxhash_t xxhash(const void *input, size_t length, uint64_t seed);
Quoted 8 lines
Quoted 3 lines
Because for now i didn't find other places with "big enough" input data, to replace jhash2 with xxhash.
Right, but we may get them.
-Andi
Thanks.
Signature · 8 lines
Have a nice day, Timofey. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 7 lines · Timofey Titovets
With defaults:
jhash2: ~4.7%
xxhash64: ~3.3%3.3/4.7 ~= 0.7 -> Profit: ~30%
11/18 ~= 0.6 -> Profit: ~40%
(if i calculate correctly of course)
Sounds good.
Please add all performance information to the changelog.
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 5 lines
Quoted 5 lines
mm/ksm.c +0
-0@@ -51,6 +52,12 @@#define DO_NUMA (x) do { } while (0)#endif Quoted 7 lines
mm/ksm.c line 59
Quoted 5 lines
mm/ksm.c +5
-0+#if BITS_PER_LONG ==64 +typedef u64 xxhash ; +#else +typedef u32 xxhash ; +#endif This should be in
xxhash.h?This is a "hack", for compile time chose appropriate hash function. xxhash ported from upstream code,
upstream version don't do that (IMHO), as this useless in most cases. That only can be useful for memory only hashes.
Because for persistent data it's obvious to always use one hash type 32/64.
Quoted 4 lines
Quoted 2 lines
I don't think it's a hack. It makes sense. Just should be done centrally in Linux, not in a specific user.
So, i must add separate patch for
xxhash.h?
Yes.
Quoted 2 lines
If yes, may be you can suggest which list must be in copy? (i can't find any info about maintainers of
./lib/inMAINTAINERS)
Just copy linux-kernel. It would be all merged together.
Quoted 6 lines
If we decide to patch
xxhash.h,
may be that will be better to wrap above if-else by something like:
/*
* Only for in memory use
*/xxhash_txxhash(const void *input, size_t length, uint64_t seed);
Yes that's fine.
-Andi
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Can you please CC the kvm list for these patches. There were other patches floating around that tried to use the crypto API with CRC and Claudio was working on some refactoring that made the has function arch specific.
Quoted 96 lines · Timofey Titovets
xxhash much faster then jhash,
ex. forx86_64host:PAGE_SIZE: 4096, loop count: 1048576
jhash2:0xacbc7a5btime: 1907 ms, th: 2251.9 MiB/s
xxhash32:0x570da981time: 739 ms, th: 5809.4 MiB/s
xxhash64:0xa1fa032ab85bbb62time: 371 ms, th: 11556.6 MiB/sxxhash64 on
x86_32work with ~ same speed as jhash2. xxhash32 onx86_32work with ~ same speed as forx86_64So replace jhash with xxhash,
and use fastest version for current target ARCH.Signed-off-by Timofey Titovets <[email protected]>--- mm/Kconfig | 1 + mm/ksm.c | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-)mm/Kconfig +1
-0@@ -305,6 +305,7 @@ config MMU_NOTIFIER config KSM bool "Enable KSM for page merging" depends on MMU + select XXHASH help Enable Kernel Samepage Merging: KSM periodically scans those areas of an application's address space that an app has advised may bemm/ksm.c +17
-6@@ -25,7 +25,8 @@#include <linux/pagemap.h> #include <linux/rmap.h> #include <linux/spinlock.h> -#include <linux/jhash.h> +#include <linux/xxhash.h> +#include <linux/bitops.h> /* BITS_PER_LONG */ #include <linux/delay.h> #include <linux/kthread.h> #include <linux/wait.h> @@ -51,6 +52,12 @@#define DO_NUMA (x) do { } while (0)#endif +#if BITS_PER_LONG ==64 +typedef u64 xxhash ; +#else +typedef u32 xxhash ; +#endif +/* * A few notes about the KSM scanning process, * to make it easier to understand the data structures below: @@ -186,7 +193,7 @@ struct rmap_item { };struct mm_struct *mm;unsigned long address;/* + low bits used for flags below */ -unsigned int oldchecksum;/* when unstable */ +xxhash oldchecksum;/* when unstable */ union {struct rb_node node;/* when node of unstable tree */ struct {/* when listed from stable tree */ @@ -255,7 +262,7 @@ static unsigned int ksm_thread_pages_to_scan = 100;static unsigned int ksm_thread_sleep_millisecs =20 ;/* Checksum of an empty (zeroed) page */ -static unsigned int zero_checksum__read_mostly ; +static xxhash zero_checksum__read_mostly ;/* Whether to merge empty (zeroed) pages with actual zero pages */ static bool ksm_use_zero_pages__read_mostly ;@@ -982,11 +989,15 @@ static int unmerge_and_remove_all_rmap_items(void) }#endif /* CONFIG_SYSFS */ -static u32 calc_checksum (struct page *page) +static xxhash calc_checksum (struct page *page) { -u32 checksum; +xxhash checksum;void *addr =kmap_atomic (page); - checksum =jhash2 (addr,PAGE_SIZE /4 ,17 ); +#if BITS_PER_LONG ==64 + checksum =xxh64 (addr,PAGE_SIZE ,0 ); +#else + checksum =xxh32 (addr,PAGE_SIZE ,0 ); +#endif kunmap_atomic (addr);return checksum; }
mm/ksm.c static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) · line 2011
Quoted 8 lines
mm/ksm.c +1
-1@@ -1994,7 +2005,7 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)struct page *tree_page =NULL ;struct stable_node *stable_node;struct page *kpage; -unsigned int checksum; +xxhash checksum;int err;bool max_page_sharing_bypass = false;
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>