mm, swap: Sort swap entries before free
Subsystems (2)
PATCH 1/1 mm, swap: Sort swap entries before free
From: Huang Ying <[email protected]>
To reduce the lock contention of swap_info_struct->lock when freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, the swap_info_struct->lock needs to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.
To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by swapcache_free_entries() could be reduced after the patch.
Test the patch via measuring the run time of swap_cache_free_entries() during the exit phase of the applications use much swap space. The results shows that the average run time of swap_cache_free_entries() reduced about 20% after applying the patch.
v3:
- Add some comments in code per Rik's suggestion.
v2:
- Avoid sort swap entries if there is only one swap device.
--- mm/swapfile.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
mm/swapfile.c +12 -0
@@ -37,6 +37,7 @@#include <linux/swapfile.h> #include <linux/export.h> #include <linux/swap_slots.h> +#include <linux/sort.h> #include <asm/pgtable.h> #include <asm/tlbflush.h>
@@ -1065,6 +1066,13 @@ void swapcache_free(swp_entry_t entry) } } +static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (long )(swp_type (*e1) -swp_type (*e2)); +} +void swapcache_free_entries (swp_entry_t *entries,int n) {struct swap_info_struct *p, *prev;
@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev =NULL ; p =NULL ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +if (nr_swapfiles >1 ) +sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL );for (i =0 ; i < n; ++i) { p =swap_info_get_cont (entries[i], prev);if (p)
Signature · 7 lines
2.11.0 -- 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 · Ying
To solve the issue, the per-CPU buffer is sorted according to the swap
device before freeing the swap entries.A A Test shows that the time spent byswapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()
during the exit phase of the applications use much swap space.A A The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.
Quoted 6 lines
Signed-off-by Huang Ying <[email protected]>Acked-by Tim Chen <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>
--
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>
Andrew Morton
maintainerQuoted 11 lines · Ying
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.
Quoted 8 lines
To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.
"20%" is useful info, but it is much better to present the absolute numbers, please. If it's "20% of one nanosecond" then the patch isn't very interesting. If it's "20% of 35 seconds" then we know we have more work to do.
If there is indeed still a significant problem here then perhaps it would be better to move the percpu swp_entry_t buffer into the per-device structure swap_info_struct, so it becomes "per cpu, per device". That way we should be able to reduce contention further.
Or maybe we do something else - it all depends upon the significance of this problem, which is why a full description of your measurements is useful.
--
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 22 lines · Andrew Morton
Quoted 11 lines · Ying
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.Quoted 8 lines
To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.
Quoted 4 lines
"20%" is useful info, but it is much better to present the absolute numbers, please. If it's "20% of one nanosecond" then the patch isn't very interesting. If it's "20% of 35 seconds" then we know we have more work to do.
The average run time of swap_cache_free_entries() is reduced from about ~137us to ~111us. The total samples of swap_cache_free_entries() is about 200000, run on 16 CPUs, so the wall time is about 1.7s. I will revise the tests to get the total run time reduction.
Quoted 8 lines
If there is indeed still a significant problem here then perhaps it would be better to move the percpu
swp_entry_tbuffer into the per-device structureswap_info_struct, so it becomes "per cpu, per device". That way we should be able to reduce contention further.Or maybe we do something else - it all depends upon the significance of this problem, which is why a full description of your measurements is useful.
Yes. I will provide more and better measurement firstly.
Best Regards,
Huang, Ying
--
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 22 lines · Andrew Morton
Quoted 11 lines · Ying
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.Quoted 8 lines
To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.
Quoted 4 lines
"20%" is useful info, but it is much better to present the absolute numbers, please. If it's "20% of one nanosecond" then the patch isn't very interesting. If it's "20% of 35 seconds" then we know we have more work to do.
I added memory freeing timing capability to vm-scalability test suite. The result shows the memory freeing time reduced from 2.64s to 2.31s (about -12.5%).
Best Regards,
Huang, Ying
Quoted 8 lines
If there is indeed still a significant problem here then perhaps it would be better to move the percpu
swp_entry_tbuffer into the per-device structureswap_info_struct, so it becomes "per cpu, per device". That way we should be able to reduce contention further.Or maybe we do something else - it all depends upon the significance of this problem, which is why a full description of your measurements is useful.
--
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 24 lines · Ying
Quoted 22 lines · Andrew Morton
Quoted 19 lines · Ying
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.
Quoted 8 lines
Quoted 4 lines
"20%" is useful info, but it is much better to present the absolute numbers, please. If it's "20% of one nanosecond" then the patch isn't very interesting. If it's "20% of 35 seconds" then we know we have more work to do.
I added memory freeing timing capability to vm-scalability test suite. The result shows the memory freeing time reduced from 2.64s to 2.31s (about -12.5%).
The memory space to free is 96G (including swap). The machine has 144 CPU, 32G RAM, and 96G swap. The process number is 16.
Best Regards,
Huang, Ying
Quoted 11 lines
Best Regards,
Huang, YingQuoted 8 lines
If there is indeed still a significant problem here then perhaps it would be better to move the percpu
swp_entry_tbuffer into the per-device structureswap_info_struct, so it becomes "per cpu, per device". That way we should be able to reduce contention further.Or maybe we do something else - it all depends upon the significance of this problem, which is why a full description of your measurements is useful.
--
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>
Hi Huang,
Quoted 67 lines · Ying
From: Huang Ying <[email protected]>
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.Signed-off-by Huang Ying <[email protected]>Acked-by Tim Chen <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>v3:
- Add some comments in code per Rik's suggestion.
v2:
- Avoid sort swap entries if there is only one swap device.
--- mm/swapfile.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/swapfile.c +8
-0@@ -37,6 +37,7 @@#include <linux/swapfile.h> #include <linux/export.h> #include <linux/swap_slots.h> +#include <linux/sort.h> #include <asm/pgtable.h> #include <asm/tlbflush.h> @@ -1065,6 +1066,13 @@ void swapcache_free(swp_entry_t entry) } } +static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (long )(swp_type (*e1) -swp_type (*e2)); +} +void swapcache_free_entries (swp_entry_t *entries,int n) {struct swap_info_struct *p, *prev;
mm/swapfile.c void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 7 lines
mm/swapfile.c +4
-0@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev =NULL ; p =NULL ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +if (nr_swapfiles >1 ) +sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL );
Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well, nr_swapfiles is never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Thanks.
--
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 84 lines · Minchan Kim
Hi Huang,
Quoted 67 lines · Ying
From: Huang Ying <[email protected]>
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.Signed-off-by Huang Ying <[email protected]>Acked-by Tim Chen <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>v3:
- Add some comments in code per Rik's suggestion.
v2:
- Avoid sort swap entries if there is only one swap device.
--- mm/swapfile.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/swapfile.c +8
-0@@ -37,6 +37,7 @@#include <linux/swapfile.h> #include <linux/export.h> #include <linux/swap_slots.h> +#include <linux/sort.h> #include <asm/pgtable.h> #include <asm/tlbflush.h> @@ -1065,6 +1066,13 @@ void swapcache_free(swp_entry_t entry) } } +static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (long )(swp_type (*e1) -swp_type (*e2)); +} +void swapcache_free_entries (swp_entry_t *entries,int n) {struct swap_info_struct *p, *prev;mm/swapfile.c void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 7 lines
mm/swapfile.c +4
-0@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev =NULL ; p =NULL ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +if (nr_swapfiles >1 ) +sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL );Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
Quoted 6 lines
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Best Regards,
Huang, Ying
--
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>
Minchan Kim
NAKQuoted 90 lines · Ying
Quoted 88 lines · Minchan Kim
Hi Huang,
Quoted 67 lines · Ying
From: Huang Ying <[email protected]>
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.Signed-off-by Huang Ying <[email protected]>Acked-by Tim Chen <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>v3:
- Add some comments in code per Rik's suggestion.
v2:
- Avoid sort swap entries if there is only one swap device.
--- mm/swapfile.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/swapfile.c +8
-0@@ -37,6 +37,7 @@#include <linux/swapfile.h> #include <linux/export.h> #include <linux/swap_slots.h> +#include <linux/sort.h> #include <asm/pgtable.h> #include <asm/tlbflush.h> @@ -1065,6 +1066,13 @@ void swapcache_free(swp_entry_t entry) } } +static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (long )(swp_type (*e1) -swp_type (*e2)); +} +void swapcache_free_entries (swp_entry_t *entries,int n) {struct swap_info_struct *p, *prev;mm/swapfile.c void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 7 lines
mm/swapfile.c +4
-0@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev =NULL ; p =NULL ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +if (nr_swapfiles >1 ) +sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL );Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.
Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Huh?
1. swapon /dev/XXX1
2. swapon /dev/XXX2
3. swapoff /dev/XXX2
4. use only one swap
5. then, always pointless sort.
Do not add such bogus code.
Nacked.
--
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 97 lines · Minchan Kim
Quoted 90 lines · Ying
Quoted 88 lines · Minchan Kim
Hi Huang,
Quoted 67 lines · Ying
From: Huang Ying <[email protected]>
To reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.Test the patch via measuring the run time of
swap_cache_free_entries()during the exit phase of the applications use much swap space. The results shows that the average run time ofswap_cache_free_entries()reduced about 20% after applying the patch.Signed-off-by Huang Ying <[email protected]>Acked-by Tim Chen <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>v3:
- Add some comments in code per Rik's suggestion.
v2:
- Avoid sort swap entries if there is only one swap device.
--- mm/swapfile.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)mm/swapfile.c +8
-0@@ -37,6 +37,7 @@#include <linux/swapfile.h> #include <linux/export.h> #include <linux/swap_slots.h> +#include <linux/sort.h> #include <asm/pgtable.h> #include <asm/tlbflush.h> @@ -1065,6 +1066,13 @@ void swapcache_free(swp_entry_t entry) } } +static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (long )(swp_type (*e1) -swp_type (*e2)); +} +void swapcache_free_entries (swp_entry_t *entries,int n) {struct swap_info_struct *p, *prev;mm/swapfile.c void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 7 lines
mm/swapfile.c +4
-0@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev =NULL ; p =NULL ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +if (nr_swapfiles >1 ) +sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL );Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
Best Regards,
Huang, Ying
Quoted 3 lines
Do not add such bogus code.
Nacked.
--
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 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
mm/swapfile.c void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){mm/swapfile.c +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev =NULL ; p =NULL ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +if (nr_swapfiles >1 ) +sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL );Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.
Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (nr_swapfiles > 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,
void swapcache_free_entries(swp_entry_t *entries, int n) {
struct swap_info_struct *p, *prev;
int i;
+ swp_entry_t entry;
+ unsigned int prev_swp_type;
if (n <= 0)
return;
+ prev_swp_type = swp_type(entries[0]);
+ for (i = n - 1; i > 0; i--) {
+ if (swp_type(entries[i]) != prev_swp_type)
+ break;
+ }
+
+ /* Sort swap entries by swap device, so each lock is only taken once. */
+ if (i)
+ sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);
prev = NULL;
p = NULL;
for (i = 0; i < n; ++i) {
- p = swap_info_get_cont(entries[i], prev);
+ entry = entries[i];
+ p = swap_info_get_cont(entry, prev);
if (p)
- swap_entry_free(p, entries[i]);
+ swap_entry_free(p, entry);
prev = p;
}
if (p)
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
I will send out the formal patch soon.
Best Regards,
Huang, Ying
--
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 49 lines · Ying
Quoted 47 lines · Ying
Quoted 44 lines · Minchan Kim
Quoted 27 lines · Ying
Quoted 24 lines · Minchan Kim
Hi Huang,
Quoted 10 lines · Ying
From: Huang Ying <[email protected]>
A void
swapcache_free_entries(swp_entry_t *entries, int n)A {
A structswap_info_struct*p, *prev;(quoted diff) +0
-0@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n)A
A prev = NULL;
A p = NULL;mm/swapfile.c void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 4 lines
mm/swapfile.c +4
-0+ +/* Sort swap entries by swap device, so each lock is only taken once. */ +if (nr_swapfiles >1 ) +sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL );Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
Quoted 8 lines
Quoted 6 lines
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes.A A That should be better.A A I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.
Quoted 5 lines
Quoted 3 lines
Yes.A A In this situation we will do unnecessary sorting.A A What I don't know is whether the unnecessary sorting will hurt performance in real life.A A I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test).A A
It is possible that nr_swapfiles > 1 when we have only 1 swapfile due to swapoff. A The nr_swapfiles never decrement on swapoff. We will need to use another counter in alloc_swap_info and swapoff to track the true number of swapfiles in use to have a fast path that avoid the search and sort for the 1 swap case.
Tim
--
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 53 lines · Tim Chen
Quoted 52 lines · Ying
Quoted 50 lines · Ying
Quoted 37 lines · Minchan Kim
Quoted 27 lines · Ying
Quoted 24 lines · Minchan Kim
Hi Huang,
Quoted 10 lines · Ying
From: Huang Ying <[email protected]>
A void
swapcache_free_entries(swp_entry_t *entries, int n)A {
A structswap_info_struct*p, *prev;(quoted diff) +0
-0@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n)A
A prev = NULL;
A p = NULL;Review
Quoted 4 lines
(quoted diff) +4
-0+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
Quoted 8 lines
Quoted 6 lines
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes.A A That should be better.A A I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Yes.A A In this situation we will do unnecessary sorting.A A What I don't know is whether the unnecessary sorting will hurt performance in real life.A A I can do some measurement.
Quoted 8 lines
Quoted 2 lines
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test).A AIt is possible that
nr_swapfiles> 1 when we have only 1 swapfile due to swapoff. A Thenr_swapfilesnever decrement on swapoff. We will need to use another counter inalloc_swap_infoand swapoff to track the true number of swapfiles in use to have a fast path that avoid the search and sort for the 1 swap case.
Yes. That is a possible optimization. But it doesn't cover another use cases raised by Minchan (two swap device with different priority). So in general, we still need to check whether there are entries from multiple swap devices in the array. Given the cost of the checking code is really low, I think maybe we can just always use the checking code. Do you think so?
Best Regards,
Huang, Ying
--
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 69 lines · Ying
Quoted 60 lines · Tim Chen
Quoted 58 lines · Ying
Quoted 55 lines · Ying
Quoted 41 lines · Minchan Kim
Quoted 30 lines · Ying
Quoted 26 lines · Minchan Kim
Hi Huang,
Quoted 11 lines · Ying
From: Huang Ying <[email protected]>
A void
swapcache_free_entries(swp_entry_t *entries, int n)A {
A structswap_info_struct*p, *prev;(quoted diff) +0
-0@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n)A
A prev = NULL;
A p = NULL;Review
Quoted 4 lines
(quoted diff) +4
-0+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
Quoted 8 lines
Quoted 6 lines
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes.A A That should be better.A A I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Yes.A A In this situation we will do unnecessary sorting.A A What I don't know is whether the unnecessary sorting will hurt performance in real life.A A I can do some measurement.
Quoted 7 lines
Quoted 2 lines
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test).A AIt is possible that
nr_swapfiles> 1 when we have only 1 swapfile due to swapoff. A Thenr_swapfilesnever decrement on swapoff. We will need to use another counter inalloc_swap_infoand swapoff to track the true number of swapfiles in use to have a fast path that avoid the search and sort for the 1 swap case.
Quoted 6 lines
Yes.A A That is a possible optimization.A A But it doesn't cover another use cases raised by Minchan (two swap device with different priority).A A So in general, we still need to check whether there are entries from multiple swap devices in the array.A A Given the cost of the checking code is really low, I think maybe we can just always use the checking code. Do you think so?
The single swap case is very common. It will be better if we can bypass the extra logic and cost for multiple swap. A Yes, we still need the proper check to see if sort is necessary as you proposed for the multiple swap case.
Tim
--
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 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;
Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }
That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)
Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
mm/swapfile.c +27 -17
@@ -1073,30 +1073,40 @@ static int swp_entry_cmp(const void *ent1, const void *ent2)return (long )(swp_type (*e1) -swp_type (*e2)); } -void swapcache_free_entries (swp_entry_t *entries,int n) +void swapcache_free_entries (swp_entry_t *entries,int nr) { -struct swap_info_struct *p, *prev;int i; +struct swap_info_struct *cur, *prev =NULL ; +bool sorted = false; -if (n <=0 ) +if (nr <=0 )return ; - prev =NULL ; - p =NULL ; - -/* Sort swap entries by swap device, so each lock is only taken once. */ -if (nr_swapfiles >1 ) -sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL ); -for (i =0 ; i < n; ++i) { - p =swap_info_get_cont (entries[i], prev); -if (p) -swap_entry_free (p, entries[i]); -else +for (i =0 ; i < nr; i++) { + cur =swap_info_get_cont (entries[i], prev); +if (!cur)break ; - prev = p; +if (cur != prev && !sorted && prev) { +spin_unlock (&cur->lock); +/* +* Sort swap entries by swap device, +* so each lock is only taken once. +*/ +sort (entries + i, nr - i, +sizeof (swp_entry_t), + swp_entry_cmp,NULL ); + sorted = true; + prev =NULL ; + i--; +continue ; + } + +swap_entry_free (cur, entries[i]); + prev = cur; } -if (p) -spin_unlock (&p->lock); + +if (cur) +spin_unlock (&cur->lock); }/*
--
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 101 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
Quoted 6 lines
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Yes. What I concerned is that one swap device may be locked twice instead of once during the freeing. I will give it some test.
Best Regards,
Huang, Ying
Quoted 52 lines
mm/swapfile.c +23
-15@@ -1073,30 +1073,40 @@ static int swp_entry_cmp(const void *ent1, const void *ent2)return (long )(swp_type (*e1) -swp_type (*e2)); } -void swapcache_free_entries (swp_entry_t *entries,int n) +void swapcache_free_entries (swp_entry_t *entries,int nr) { -struct swap_info_struct *p, *prev;int i; +struct swap_info_struct *cur, *prev =NULL ; +bool sorted = false; -if (n <=0 ) +if (nr <=0 )return ; - prev =NULL ; - p =NULL ; - -/* Sort swap entries by swap device, so each lock is only taken once. */ -if (nr_swapfiles >1 ) -sort (entries, n,sizeof (entries[0 ]), swp_entry_cmp,NULL ); -for (i =0 ; i < n; ++i) { - p =swap_info_get_cont (entries[i], prev); -if (p) -swap_entry_free (p, entries[i]); -else +for (i =0 ; i < nr; i++) { + cur =swap_info_get_cont (entries[i], prev); +if (!cur)break ; - prev = p; +if (cur != prev && !sorted && prev) { +spin_unlock (&cur->lock); +/* +* Sort swap entries by swap device, +* so each lock is only taken once. +*/ +sort (entries + i, nr - i, +sizeof (swp_entry_t), + swp_entry_cmp,NULL ); + sorted = true; + prev =NULL ; + i--; +continue ; + } + +swap_entry_free (cur, entries[i]);
mm/swapfile.c static int swp_entry_cmp(const void *ent1, const void *ent2) · line 1112
Quoted 10 lines
mm/swapfile.c +4
-2+ prev = cur; } -if (p) -spin_unlock (&p->lock); + +if (cur) +spin_unlock (&cur->lock); }/*
--
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>
Huang, Ying
Acked-by Tim ChenQuoted 101 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
Quoted 7 lines
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
Best Regards,
Huang, Ying
Quoted 37 lines
From
7bd903c42749c448ef6acbbdee8dcbc1c5b498b9Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Thu, 23 Feb 2017 13:05:20 +0800
Subject: [PATCH -v5] mm, swap: Sort swap entries before freeA
---
Amm/swapfile.c| 43 ++++++++++++++++++++++++++++++++++++++----- A 1 file changed, 38 insertions(+), 5 deletions(-)mm/swapfile.c +0
-0@@ -37,6 +37,7 @@A #include <
linux/swapfile.h>
A #include <linux/export.h>
A #include <linux/swap_slots.h>
+#include <linux/sort.h>
A
A #include <asm/pgtable.h>
A #include <asm/tlbflush.h>mm/swapfile.c +0
-0@@ -1065,20 +1066,52 @@ void swapcache_free(swp_entry_t entry) A } A }A
mm/swapfile.c +7
-0+static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (int )(swp_type (*e1) -swp_type (*e2)); +} +A void
swapcache_free_entries(swp_entry_t *entries, int n)A {
A structswap_info_struct*p, *prev;
mm/swapfile.c
Quoted 4 lines
mm/swapfile.c +3
-1-int i; +int i, m; +swp_entry_t entry; +unsigned int prev_swp_type;
I think it will be clearer to name prev_swp_type as first_swp_type as this is the swp type of the first entry.
Quoted 17 lines
A
A if (n <= 0)
A return;
A
A prev = NULL;
A p = NULL;mm/swapfile.c +7
-4-for (i =0 ; i < n; ++i) { - p =swap_info_get_cont (entries[i], prev); -if (p) -swap_entry_free (p, entries[i]); + m =0 ; + prev_swp_type =swp_type (entries[0 ]); +for (i =0 ; i < n; i++) { + entry = entries[i]; +if (likely (swp_type (entry) == prev_swp_type)) { + p =swap_info_get_cont (entry, prev); +if (likely (p))
mm/swapfile.c
Quoted 10 lines
mm/swapfile.c +10
-0+swap_entry_free (p, entry); + prev = p; + }else if (!m) + m = i; + } +if (p) +spin_unlock (&p->lock); +if (likely (!m)) +return ; +
We could still have prev_swp_type at the first entry after sorting. and we can avoid an unlock/relock for this case if we do this:
if (likely(!m)) {
if (p)
spin_unlock(&p->lock);
return;
}mm/swapfile.c
Quoted 3 lines
mm/swapfile.c +3
-0+/* Sort swap entries by swap device, so each lock is only taken once. */ +sort (entries + m, n - m,sizeof (entries[0 ]), swp_entry_cmp,NULL ); + prev =NULL ;
Can eliminate prev=NULL if we adopt the above change.
mm/swapfile.c
Quoted 4 lines
mm/swapfile.c +4
-0+for (i = m; i < n; i++) { + entry = entries[i]; +if (swp_type (entry) == prev_swp_type) +continue ;
The if/continue statement seems incorrect. When swp_type(entry) == prev_swp_type we also need to free entry. A The if/continue statement should be deleted.
Say we have 3 entries with swp_type
1,2,1
We will get prev_swp_type as 1 and free the first entry and sort the remaining two. A The last entry with swp_type 1 will not be freed.
Quoted 6 lines
mm/swapfile.c +3
-0+ p =swap_info_get_cont (entry, prev); +if (likely (p)) +swap_entry_free (p, entry);A prev = p;
A }
A if (p)
Thanks.
Tim
--
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 35 lines · Tim Chen
Quoted 35 lines
From
7bd903c42749c448ef6acbbdee8dcbc1c5b498b9Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Thu, 23 Feb 2017 13:05:20 +0800
Subject: [PATCH -v5] mm, swap: Sort swap entries before freeA
---
Amm/swapfile.c| 43 ++++++++++++++++++++++++++++++++++++++----- A 1 file changed, 38 insertions(+), 5 deletions(-)mm/swapfile.c +0
-0@@ -37,6 +37,7 @@A #include <
linux/swapfile.h>
A #include <linux/export.h>
A #include <linux/swap_slots.h>
+#include <linux/sort.h>
A
A #include <asm/pgtable.h>
A #include <asm/tlbflush.h>mm/swapfile.c +0
-0@@ -1065,20 +1066,52 @@ void swapcache_free(swp_entry_t entry) A } A }A
mm/swapfile.c +7
-0+static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (int )(swp_type (*e1) -swp_type (*e2)); +} +A void
swapcache_free_entries(swp_entry_t *entries, int n)A {
Quoted 8 lines
mm/swapfile.c
Quoted 5 lines
A struct
swap_info_struct*p, *prev;mm/swapfile.c +3
-1-int i; +int i, m; +swp_entry_t entry; +unsigned int prev_swp_type;I think it will be clearer to name
prev_swp_typeasfirst_swp_typeas this is the swp type of the first entry.
Yes. That is better! Will do that.
Quoted 51 lines
Quoted 17 lines
A
A if (n <= 0)
A return;
A
A prev = NULL;
A p = NULL;mm/swapfile.c +7
-4-for (i =0 ; i < n; ++i) { - p =swap_info_get_cont (entries[i], prev); -if (p) -swap_entry_free (p, entries[i]); + m =0 ; + prev_swp_type =swp_type (entries[0 ]); +for (i =0 ; i < n; i++) { + entry = entries[i]; +if (likely (swp_type (entry) == prev_swp_type)) { + p =swap_info_get_cont (entry, prev); +if (likely (p))mm/swapfile.c
Quoted 10 lines
mm/swapfile.c +10
-0+swap_entry_free (p, entry); + prev = p; + }else if (!m) + m = i; + } +if (p) +spin_unlock (&p->lock); +if (likely (!m)) +return ; +We could still have
prev_swp_typeat the first entry after sorting. and we can avoid an unlock/relock for this case if we do this:if (likely(!m)) { if (p) spin_unlock(&p->lock); return; }mm/swapfile.c
Quoted 3 lines
mm/swapfile.c +3
-0+/* Sort swap entries by swap device, so each lock is only taken once. */ +sort (entries + m, n - m,sizeof (entries[0 ]), swp_entry_cmp,NULL ); + prev =NULL ;Can eliminate prev=NULL if we adopt the above change.
mm/swapfile.c
Quoted 4 lines
mm/swapfile.c +4
-0+for (i = m; i < n; i++) { + entry = entries[i]; +if (swp_type (entry) == prev_swp_type) +continue ;The if/continue statement seems incorrect. When
swp_type(entry)==prev_swp_typewe also need to free entry. A The if/continue statement should be deleted.
Quoted 6 lines
Say we have 3 entries with
swp_type
1,2,1We will get
prev_swp_typeas 1 and free the first entry and sort the remaining two. A The last entry withswp_type1 will not be freed.
The first loop in the function will scan all elements of the array, so the first and third entry will be freed in the first loop. Then the the second and the third entry will be sorted. But all entries with the same swap type (device) of the first entry needn't to be freed again. The key point is that we will scan all elements of the array in the first loop, record the first entry that has different swap type (device).
Best Regards,
Huang, Ying
Quoted 10 lines
Quoted 6 lines
mm/swapfile.c +3
-0+ p =swap_info_get_cont (entry, prev); +if (likely (p)) +swap_entry_free (p, entry);A prev = p;
A }
A if (p)Thanks.
Tim
--
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 106 lines · Ying
Quoted 36 lines · Tim Chen
Quoted 36 lines
From
7bd903c42749c448ef6acbbdee8dcbc1c5b498b9Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Thu, 23 Feb 2017 13:05:20 +0800
Subject: [PATCH -v5] mm, swap: Sort swap entries before freeA
---
Amm/swapfile.c| 43 ++++++++++++++++++++++++++++++++++++++----- A 1 file changed, 38 insertions(+), 5 deletions(-)mm/swapfile.c +0
-0@@ -37,6 +37,7 @@A #include <
linux/swapfile.h>
A #include <linux/export.h>
A #include <linux/swap_slots.h>
+#include <linux/sort.h>
A
A #include <asm/pgtable.h>
A #include <asm/tlbflush.h>mm/swapfile.c +0
-0@@ -1065,20 +1066,52 @@ void swapcache_free(swp_entry_t entry) A } A }A
mm/swapfile.c +7
-0+static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (int )(swp_type (*e1) -swp_type (*e2)); +} +A void
swapcache_free_entries(swp_entry_t *entries, int n)Quoted 8 lines
mm/swapfile.c
Quoted 6 lines
A {
A structswap_info_struct*p, *prev;mm/swapfile.c +3
-1-int i; +int i, m; +swp_entry_t entry; +unsigned int prev_swp_type;I think it will be clearer to name
prev_swp_typeasfirst_swp_typeas this is the swp type of the first entry.Yes.A A That is better!A A Will do that.
Quoted 52 lines
Quoted 19 lines
A
A if (n <= 0)
A return;
A
A prev = NULL;
A p = NULL;mm/swapfile.c +7
-4-for (i =0 ; i < n; ++i) { - p =swap_info_get_cont (entries[i], prev); -if (p) -swap_entry_free (p, entries[i]); + m =0 ; + prev_swp_type =swp_type (entries[0 ]); +for (i =0 ; i < n; i++) { + entry = entries[i]; +if (likely (swp_type (entry) == prev_swp_type)) { + p =swap_info_get_cont (entry, prev); +if (likely (p))mm/swapfile.c
Quoted 10 lines
mm/swapfile.c +10
-0+swap_entry_free (p, entry); + prev = p; + }else if (!m) + m = i; + } +if (p) +spin_unlock (&p->lock); +if (likely (!m)) +return ; +We could still have
prev_swp_typeat the first entry after sorting. and we can avoid an unlock/relock for this case if we do this:if (likely(!m)) { if (p) spin_unlock(&p->lock); return; }mm/swapfile.c
Quoted 4 lines
mm/swapfile.c +3
-0+/* Sort swap entries by swap device, so each lock is only taken once. */ +sort (entries + m, n - m,sizeof (entries[0 ]), swp_entry_cmp,NULL ); + prev =NULL ;Can eliminate prev=NULL if we adopt the above change.
mm/swapfile.c
Quoted 5 lines
mm/swapfile.c +4
-0+for (i = m; i < n; i++) { + entry = entries[i]; +if (swp_type (entry) == prev_swp_type) +continue ;The if/continue statement seems incorrect. When
swp_type(entry)==prev_swp_typewe also need to free entry. A The if/continue statement should be deleted.Quoted 6 lines
Say we have 3 entries with
swp_type
1,2,1We will get
prev_swp_typeas 1 and free the first entry and sort the remaining two. A The last entry withswp_type1 will not be freed.
Quoted 7 lines
The first loop in the function will scan all elements of the array, so the first and third entry will be freed in the first loop.A A Then the the second and the third entry will be sorted.A A But all entries with the same swap type (device) of the first entry needn't to be freed again. The key point is that we will scan all elements of the array in the first loop, record the first entry that has different swap type (device).
I was under the wrong impression that the code break from the first loop when it finds a different swp type. A Yes, we should skip the free in the second loop if the first loop scan the whole list.
Thanks.
Tim
Quoted 15 lines
Best Regards,
Huang, YingQuoted 11 lines
Quoted 8 lines
mm/swapfile.c +3
-0+ p =swap_info_get_cont (entry, prev); +if (likely (p)) +swap_entry_free (p, entry);A prev = p;
A }
A if (p)Thanks.
Tim
--
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 111 lines · Ying
Quoted 101 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
Quoted 7 lines
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Quoted 3 lines
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
How worse with approach I suggested compared to as-is? Unless it's too bad, let's not add more complicated thing to just enhance the minor usecase in such even *slow* path.
It adds code size/maintainance overead.
With your suggestion, it might enhance a bit with speicific benchmark but not sure it's really worth for real practice.
Quoted 132 lines
Best Regards,
Huang, YingFrom
7bd903c42749c448ef6acbbdee8dcbc1c5b498b9Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Thu, 23 Feb 2017 13:05:20 +0800
Subject: [PATCH -v5] mm, swap: Sort swap entries before freeTo reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.With the patch, the memory (some swapped out) free time reduced 13.6% (from 2.59s to 2.28s) in the vm-scalability swap-w-rand test case with 16 processes. The test is done on a Xeon E5 v3 system. The swap device used is a RAM simulated PMEM (persistent memory) device. To test swapping, the test case creates 16 processes, which allocate and write to the anonymous pages until the RAM and part of the swap device is used up, finally the memory (some swapped out) is freed before exit.
Signed-off-by Huang Ying <[email protected]>Acked-by Tim Chen <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>v5:
- Use a smarter way to determine whether sort is necessary.
v4:
- Avoid unnecessary sort if all entries are from one swap device.
v3:
- Add some comments in code per Rik's suggestion.
v2:
- Avoid sort swap entries if there is only one swap device.
--- mm/swapfile.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-)mm/swapfile.c +38
-5@@ -37,6 +37,7 @@#include <linux/swapfile.h> #include <linux/export.h> #include <linux/swap_slots.h> +#include <linux/sort.h> #include <asm/pgtable.h> #include <asm/tlbflush.h> @@ -1065,20 +1066,52 @@ void swapcache_free(swp_entry_t entry) } } +static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (int )(swp_type (*e1) -swp_type (*e2)); +} +void swapcache_free_entries (swp_entry_t *entries,int n) {struct swap_info_struct *p, *prev; -int i; +int i, m; +swp_entry_t entry; +unsigned int prev_swp_type;if (n <=0 )return ; prev =NULL ; p =NULL ; -for (i =0 ; i < n; ++i) { - p =swap_info_get_cont (entries[i], prev); -if (p) -swap_entry_free (p, entries[i]); + m =0 ; + prev_swp_type =swp_type (entries[0 ]); +for (i =0 ; i < n; i++) { + entry = entries[i]; +if (likely (swp_type (entry) == prev_swp_type)) { + p =swap_info_get_cont (entry, prev); +if (likely (p)) +swap_entry_free (p, entry); + prev = p; + }else if (!m) + m = i; + } +if (p) +spin_unlock (&p->lock); +if (likely (!m)) +return ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +sort (entries + m, n - m,sizeof (entries[0 ]), swp_entry_cmp,NULL ); + prev =NULL ; +for (i = m; i < n; i++) { + entry = entries[i]; +if (swp_type (entry) == prev_swp_type) +continue ; + p =swap_info_get_cont (entry, prev); +if (likely (p)) +swap_entry_free (p, entry); prev = p; }if (p)Signature · 1 line
2.11.0
--
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 112 lines · Minchan Kim
Quoted 111 lines · Ying
Quoted 108 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Quoted 5 lines
Quoted 3 lines
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
How worse with approach I suggested compared to as-is?
The performance difference between your version and my version is small for my testing.
Quoted 5 lines
Unless it's too bad, let's not add more complicated thing to just enhance the minor usecase in such even *slow* path.
It adds code size/maintainance overead.
With your suggestion, it might enhance a bit with speicific benchmark but not sure it's really worth for real practice.
I don't think the code complexity has much difference between our latest versions. As for complexity, I think my original version which just uses nr_swapfiles to avoid sort() for single swap device is simple and good enough for this task. Maybe we can just improve the correctness of swap device counting as Tim suggested.
Best Regards,
Huang, Ying
Quoted 124 lines
Quoted 124 lines
Best Regards,
Huang, YingFrom
7bd903c42749c448ef6acbbdee8dcbc1c5b498b9Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Thu, 23 Feb 2017 13:05:20 +0800
Subject: [PATCH -v5] mm, swap: Sort swap entries before freeTo reduce the lock contention of
swap_info_struct->lockwhen freeing swap entry. The freed swap entries will be collected in a per-CPU buffer firstly, and be really freed later in batch. During the batch freeing, if the consecutive swap entries in the per-CPU buffer belongs to same swap device, theswap_info_struct->lockneeds to be acquired/released only once, so that the lock contention could be reduced greatly. But if there are multiple swap devices, it is possible that the lock may be unnecessarily released/acquired because the swap entries belong to the same swap device are non-consecutive in the per-CPU buffer.To solve the issue, the per-CPU buffer is sorted according to the swap device before freeing the swap entries. Test shows that the time spent by
swapcache_free_entries()could be reduced after the patch.With the patch, the memory (some swapped out) free time reduced 13.6% (from 2.59s to 2.28s) in the vm-scalability swap-w-rand test case with 16 processes. The test is done on a Xeon E5 v3 system. The swap device used is a RAM simulated PMEM (persistent memory) device. To test swapping, the test case creates 16 processes, which allocate and write to the anonymous pages until the RAM and part of the swap device is used up, finally the memory (some swapped out) is freed before exit.
Signed-off-by Huang Ying <[email protected]>Acked-by Tim Chen <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>v5:
- Use a smarter way to determine whether sort is necessary.
v4:
- Avoid unnecessary sort if all entries are from one swap device.
v3:
- Add some comments in code per Rik's suggestion.
v2:
- Avoid sort swap entries if there is only one swap device.
--- mm/swapfile.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-)mm/swapfile.c +36
-5@@ -37,6 +37,7 @@#include <linux/swapfile.h> #include <linux/export.h> #include <linux/swap_slots.h> +#include <linux/sort.h> #include <asm/pgtable.h> #include <asm/tlbflush.h> @@ -1065,20 +1066,52 @@ void swapcache_free(swp_entry_t entry) } } +static int swp_entry_cmp (const void *ent1,const void *ent2) +{ +const swp_entry_t *e1 = ent1, *e2 = ent2; + +return (int )(swp_type (*e1) -swp_type (*e2)); +} +void swapcache_free_entries (swp_entry_t *entries,int n) {struct swap_info_struct *p, *prev; -int i; +int i, m; +swp_entry_t entry; +unsigned int prev_swp_type;if (n <=0 )return ; prev =NULL ; p =NULL ; -for (i =0 ; i < n; ++i) { - p =swap_info_get_cont (entries[i], prev); -if (p) -swap_entry_free (p, entries[i]); + m =0 ; + prev_swp_type =swp_type (entries[0 ]); +for (i =0 ; i < n; i++) { + entry = entries[i]; +if (likely (swp_type (entry) == prev_swp_type)) { + p =swap_info_get_cont (entry, prev); +if (likely (p)) +swap_entry_free (p, entry); + prev = p; + }else if (!m) + m = i; + } +if (p) +spin_unlock (&p->lock); +if (likely (!m)) +return ; + +/* Sort swap entries by swap device, so each lock is only taken once. */ +sort (entries + m, n - m,sizeof (entries[0 ]), swp_entry_cmp,NULL ); + prev =NULL ; +for (i = m; i < n; i++) { + entry = entries[i]; +if (swp_type (entry) == prev_swp_type) +continue ; + p =swap_info_get_cont (entry, prev);
Quoted 8 lines
Quoted 8 lines
mm/swapfile.c +2
-0+if (likely (p)) +swap_entry_free (p, entry); prev = p; }if (p)Signature · 1 line
2.11.0
--
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 114 lines · Ying
Quoted 112 lines · Minchan Kim
Quoted 111 lines · Ying
Quoted 108 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
How worse with approach I suggested compared to as-is?
The performance difference between your version and my version is small for my testing.
If so, why should we add code to optimize further?
Quoted 9 lines
Quoted 6 lines
Unless it's too bad, let's not add more complicated thing to just enhance the minor usecase in such even *slow* path.
It adds code size/maintainance overead.
With your suggestion, it might enhance a bit with speicific benchmark but not sure it's really worth for real practice.I don't think the code complexity has much difference between our latest versions. As for complexity, I think my original version which just
What I suggested is to avoid pointless overhead for *major* usecase and the code you are adding now is to optimize further for *minor* usecase. And now I dobut the code you are adding is really worth unless it makes a meaningful output.
If it doesn't, it adds just overhead(code size, maintainance, power and performance). You might argue it's really *small* so it would be okay but think about that you would be not only one in the community so kernel bloats day by day with code to handle corner cases.
Quoted 2 lines
uses
nr_swapfilesto avoidsort()for single swap device is simple and good enough for this task. Maybe we can just improve the correctness of
But it hurts *major* usecase.
Quoted 1 line
swap device counting as Tim suggested.
I don't know what Tim suggested. Anyway, my point is that minor usecase doesn't hurt major usecase and justify the benefit if you want to put more. So I'm okay with either solution to meet it.
--
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 148 lines · Minchan Kim
Quoted 114 lines · Ying
Quoted 112 lines · Minchan Kim
Quoted 111 lines · Ying
Quoted 108 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
How worse with approach I suggested compared to as-is?
The performance difference between your version and my version is small for my testing.
If so, why should we add code to optimize further?
Quoted 9 lines
Quoted 6 lines
Unless it's too bad, let's not add more complicated thing to just enhance the minor usecase in such even *slow* path.
It adds code size/maintainance overead.
With your suggestion, it might enhance a bit with speicific benchmark but not sure it's really worth for real practice.I don't think the code complexity has much difference between our latest versions. As for complexity, I think my original version which just
What I suggested is to avoid pointless overhead for *major* usecase and the code you are adding now is to optimize further for *minor* usecase. And now I dobut the code you are adding is really worth unless it makes a meaningful output.
If it doesn't, it adds justoverhead(code size, maintainance, power and performance). You might argue it's really *small* so it would be okay but think about that you would be not only one in the community so kernel bloats day by day with code to handle corner cases.Quoted 2 lines
uses
nr_swapfilesto avoidsort()for single swap device is simple and good enough for this task. Maybe we can just improve the correctness of
Quoted 8 lines
But it hurts *major* usecase.
Quoted 1 line
swap device counting as Tim suggested.
I don't know what Tim suggested. Anyway, my point is that minor usecase doesn't hurt major usecase and justify the benefit if you want to put more. So I'm okay with either solution to meet it.
Tim suggested to add a mechanism to correctly track how many swap devices are in use in swapon/swapoff. So we only sort if the number of the swap device > 1. This will not cover multiple swap devices with different priorities, but will cover the major usecases. The code should be simpler.
Best Regards,
Huang, Ying
--
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 159 lines · Ying
Quoted 148 lines · Minchan Kim
Quoted 114 lines · Ying
Quoted 112 lines · Minchan Kim
Quoted 111 lines · Ying
Quoted 108 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Review void swapcache_free_entries(swp_entry_t *entries, int n) · line 1089
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +4
-0struct swap_info_struct *p, *prev;@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n) prev = NULL; p = NULL; + + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (nr_swapfiles > 1) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
How worse with approach I suggested compared to as-is?
The performance difference between your version and my version is small for my testing.
If so, why should we add code to optimize further?
Quoted 9 lines
Quoted 6 lines
Unless it's too bad, let's not add more complicated thing to just enhance the minor usecase in such even *slow* path.
It adds code size/maintainance overead.
With your suggestion, it might enhance a bit with speicific benchmark but not sure it's really worth for real practice.I don't think the code complexity has much difference between our latest versions. As for complexity, I think my original version which just
What I suggested is to avoid pointless overhead for *major* usecase and the code you are adding now is to optimize further for *minor* usecase. And now I dobut the code you are adding is really worth unless it makes a meaningful output.
If it doesn't, it adds justoverhead(code size, maintainance, power and performance). You might argue it's really *small* so it would be okay but think about that you would be not only one in the community so kernel bloats day by day with code to handle corner cases.Quoted 2 lines
uses
nr_swapfilesto avoidsort()for single swap device is simple and good enough for this task. Maybe we can just improve the correctness ofQuoted 8 lines
But it hurts *major* usecase.
Quoted 1 line
swap device counting as Tim suggested.
I don't know what Tim suggested. Anyway, my point is that minor usecase doesn't hurt major usecase and justify the benefit if you want to put more. So I'm okay with either solution to meet it.
Quoted 5 lines
Tim suggested to add a mechanism to correctly track how many swap devices are in use in swapon/swapoff. So we only sort if the number of the swap device > 1. This will not cover multiple swap devices with different priorities, but will cover the major usecases. The code should be simpler.
As you know, it doesn't solve multiple swaps by priority. Even, there are cases full with entries same swap device although multiple swap devices are used.
So, I think runtime sorting by judging need to be sored is still better.
--
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 160 lines · Minchan Kim
Quoted 159 lines · Ying
Quoted 156 lines · Minchan Kim
Quoted 114 lines · Ying
Quoted 112 lines · Minchan Kim
Quoted 111 lines · Ying
Quoted 108 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
Quoted 13 lines · Ying
From: Huang Ying <[email protected]>
void swapcache_free_entries(swp_entry_t *entries, int n)
{
struct swap_info_struct *p, *prev;
@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n)prev = NULL;
p = NULL;
+
+ /* Sort swap entries by swap device, so each lock is only taken once. */
+ if (nr_swapfiles > 1)
+ sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage would be zero unless other swap used up. In case of that, this sorting is pointless.
As well,
nr_swapfilesis never decreased so if we enable multiple swaps and then disable until a swap is remained, this sorting is pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and, then we can sort it.
Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
How worse with approach I suggested compared to as-is?
The performance difference between your version and my version is small for my testing.
If so, why should we add code to optimize further?
Quoted 9 lines
Quoted 6 lines
Unless it's too bad, let's not add more complicated thing to just enhance the minor usecase in such even *slow* path.
It adds code size/maintainance overead.
With your suggestion, it might enhance a bit with speicific benchmark but not sure it's really worth for real practice.I don't think the code complexity has much difference between our latest versions. As for complexity, I think my original version which just
What I suggested is to avoid pointless overhead for *major* usecase and the code you are adding now is to optimize further for *minor* usecase. And now I dobut the code you are adding is really worth unless it makes a meaningful output.
If it doesn't, it adds justoverhead(code size, maintainance, power and performance). You might argue it's really *small* so it would be okay but think about that you would be not only one in the community so kernel bloats day by day with code to handle corner cases.Quoted 2 lines
uses
nr_swapfilesto avoidsort()for single swap device is simple and good enough for this task. Maybe we can just improve the correctness ofBut it hurts *major* usecase.
Quoted 1 line
swap device counting as Tim suggested.
I don't know what Tim suggested. Anyway, my point is that minor usecase doesn't hurt major usecase and justify the benefit if you want to put more. So I'm okay with either solution to meet it.
Quoted 7 lines
Quoted 5 lines
Tim suggested to add a mechanism to correctly track how many swap devices are in use in swapon/swapoff. So we only sort if the number of the swap device > 1. This will not cover multiple swap devices with different priorities, but will cover the major usecases. The code should be simpler.
As you know, it doesn't solve multiple swaps by priority.
I don't think this is *major* usecase.
Quoted 2 lines
Even, there are cases full with entries same swap device although multiple swap devices are used.
Why, if you have multiple swap device, every time you will allocate from different swap device. Although there are swap alloc slots cache, the possibility of full alignment is low.
Even if there are cases all entries come from one swap device, the sorting is fast in fact because the array is short and the elements are sorted (same swap type) already. So it is not necessary to worry about that too much.
Best Regards,
Huang, Ying
Quoted 2 lines
So, I think runtime sorting by judging need to be sored is still better.
--
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 175 lines · Ying
Quoted 160 lines · Minchan Kim
Quoted 159 lines · Ying
Quoted 156 lines · Minchan Kim
Quoted 114 lines · Ying
Quoted 112 lines · Minchan Kim
Quoted 111 lines · Ying
Quoted 108 lines · Minchan Kim
Quoted 64 lines · Ying
Quoted 46 lines · Ying
Quoted 36 lines · Minchan Kim
Quoted 29 lines · Ying
Quoted 27 lines · Minchan Kim
Hi Huang,
On Fri, Apr 07, 2017 at 02:49:01PM +0800, Huang, Ying wrote:
From: Huang Ying <[email protected]>void swapcache_free_entries(swp_entry_t *entries, int n)
{
struct swap_info_struct *p, *prev;
@@ -1075,6 +1083,10 @@ void swapcache_free_entries(swp_entry_t *entries, int n)prev = NULL;
p = NULL;
+
+ /* Sort swap entries by swap device, so each lock is only taken once. */
+ if (nr_swapfiles > 1)
+ sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);Let's think on other cases.
There are two swaps and they are configured by priority so a swap's usage
would be zero unless other swap used up. In case of that, this sorting
is pointless.As well, nr_swapfiles is never decreased so if we enable multiple
swaps and then disable until a swap is remained, this sorting is
pointelss, too.Quoted 5 lines
Quoted 2 lines
How about lazy sorting approach? IOW, if we found prev != p and,
then we can sort it.Yes. That should be better. I just don't know whether the added complexity is necessary, given the array is short and sort is fast.
Quoted 7 lines
Huh?
1. swapon
/dev/XXX1
2. swapon/dev/XXX2
3. swapoff/dev/XXX2
4. use only one swap
5. then, always pointless sort.Quoted 3 lines
Yes. In this situation we will do unnecessary sorting. What I don't know is whether the unnecessary sorting will hurt performance in real life. I can do some measurement.
I tested the patch with 1 swap device and 1 process to eat memory (remove the "if (
nr_swapfiles> 1)" for test). I think this is the worse case because there is no lock contention. The memory freeing time increased from 1.94s to 2.12s (increase ~9.2%). So there is some overhead for some cases. I change the algorithm to something like below,void
swapcache_free_entries(swp_entry_t *entries, int n){(quoted diff) +1
-0struct swap_info_struct *p, *prev; int i; + swp_entry_t entry;Review
Quoted 10 lines
(quoted diff) +6
-0+ unsigned int prev_swp_type; if (n <= 0) return; + prev_swp_type = swp_type(entries[0]); + for (i = n - 1; i > 0; i--) { + if (swp_type(entries[i]) != prev_swp_type) + break; + }That's really what I want to avoid. For many swap usecases, it adds unnecessary overhead.
Quoted 17 lines
(quoted diff) +7
-2+ + /* Sort swap entries by swap device, so each lock is only taken once. */ + if (i) + sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); prev = NULL; p = NULL; for (i = 0; i < n; ++i) { - p = swap_info_get_cont(entries[i], prev); + entry = entries[i]; + p = swap_info_get_cont(entry, prev); if (p) - swap_entry_free(p, entries[i]); + swap_entry_free(p, entry); prev = p; } if (p)Quoted 2 lines
With this patch, the memory freeing time increased from 1.94s to 1.97s. I think this is good enough. Do you think so?
What I mean is as follows(I didn't test it at all):
With this, sort entries if we found multiple entries in current entries. It adds some condition checks for non-multiple swap usecase but it would be more cheaper than the sorting. And it adds a [un]lock overhead for multiple swap usecase but it should be a compromise for single-swap usecase which is more popular.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
How about the following solution? It can avoid [un]lock overhead and double lock issue for multiple swap user case and has good performance for one swap user case too.
How worse with approach I suggested compared to as-is?
The performance difference between your version and my version is small for my testing.
If so, why should we add code to optimize further?
Quoted 9 lines
Quoted 6 lines
Unless it's too bad, let's not add more complicated thing to just enhance the minor usecase in such even *slow* path.
It adds code size/maintainance overead.
With your suggestion, it might enhance a bit with speicific benchmark but not sure it's really worth for real practice.I don't think the code complexity has much difference between our latest versions. As for complexity, I think my original version which just
What I suggested is to avoid pointless overhead for *major* usecase and the code you are adding now is to optimize further for *minor* usecase. And now I dobut the code you are adding is really worth unless it makes a meaningful output.
If it doesn't, it adds justoverhead(code size, maintainance, power and performance). You might argue it's really *small* so it would be okay but think about that you would be not only one in the community so kernel bloats day by day with code to handle corner cases.Quoted 2 lines
uses
nr_swapfilesto avoidsort()for single swap device is simple and good enough for this task. Maybe we can just improve the correctness ofBut it hurts *major* usecase.
Quoted 1 line
swap device counting as Tim suggested.
I don't know what Tim suggested. Anyway, my point is that minor usecase doesn't hurt major usecase and justify the benefit if you want to put more. So I'm okay with either solution to meet it.
Quoted 7 lines
Quoted 5 lines
Tim suggested to add a mechanism to correctly track how many swap devices are in use in swapon/swapoff. So we only sort if the number of the swap device > 1. This will not cover multiple swap devices with different priorities, but will cover the major usecases. The code should be simpler.
As you know, it doesn't solve multiple swaps by priority.
I don't think this is *major* usecase.
Quoted 2 lines
Even, there are cases full with entries same swap device although multiple swap devices are used.
Quoted 8 lines
Why, if you have multiple swap device, every time you will allocate from different swap device. Although there are swap alloc slots cache, the possibility of full alignment is low.
Even if there are cases all entries come from one swap device, the sorting is fast in fact because the array is short and the elements are sorted (same swap type) already. So it is not necessary to worry about that too much.
In fact, during the test, I found the overhead of sort() is comparable with the performance difference of adding likely()/unlikely() to the "if" in the function.
Best Regards,
Huang, Ying
Quoted 5 lines
Best Regards,
Huang, YingQuoted 2 lines
So, I think runtime sorting by judging need to be sored is still better.
--
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 3 lines · Ying
In fact, during the test, I found the overhead of
sort()is comparable with the performance difference of adding likely()/unlikely() to the "if" in the function.
Huang,
This discussion is started from your optimization code:
if (nr_swapfiles > 1)
sort();
I don't have such fast machine so cannot test it. However, you added such optimization code in there so I guess it's *worth* to review so with spending my time, I pointed out what you are missing and suggested a idea to find a compromise.
Now you are saying sort is so fast so no worth to add more logics to avoid the overhead?
Then, please just drop that if condition part and instead, sort it unconditionally.
--
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>
Hi, Minchan,
Quoted 9 lines · Minchan Kim
Quoted 3 lines · Ying
In fact, during the test, I found the overhead of
sort()is comparable with the performance difference of adding likely()/unlikely() to the "if" in the function.Huang,
This discussion is started from your optimization code:
Quoted 7 lines
if (
nr_swapfiles> 1)
sort();I don't have such fast machine so cannot test it. However, you added such optimization code in there so I guess it's *worth* to review so with spending my time, I pointed out what you are missing and suggested a idea to find a compromise.
Sorry for wasting your time and Thanks a lot for your review and suggestion!
When I started talking this with you, I found there is some measurable overhead of sort(). But later when I done more tests, I found the measurable overhead is at the same level of likely()/unlikely() compiler notation. So you help me to find that, Thanks again!
Quoted 4 lines
Now you are saying sort is so fast so no worth to add more logics to avoid the overhead?
Then, please just drop that if condition part and instead, sort it unconditionally.
Now, because we found the overhead of sort() is low, I suggest to put minimal effort to avoid it. Like the original implementation,
if (nr_swapfiles > 1)
sort();
Or, we can make nr_swapfiles more correct as Tim suggested (tracking the number of the swap devices during swap on/off).
Best Regards,
Huang, Ying
--
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>
Hi Huang,
Quoted 34 lines · Ying
Hi, Minchan,
Quoted 9 lines · Minchan Kim
Quoted 3 lines · Ying
In fact, during the test, I found the overhead of
sort()is comparable with the performance difference of adding likely()/unlikely() to the "if" in the function.Huang,
This discussion is started from your optimization code:
Quoted 7 lines
if (
nr_swapfiles> 1)
sort();I don't have such fast machine so cannot test it. However, you added such optimization code in there so I guess it's *worth* to review so with spending my time, I pointed out what you are missing and suggested a idea to find a compromise.
Sorry for wasting your time and Thanks a lot for your review and suggestion!
When I started talking this with you, I found there is some measurable overhead of
sort(). But later when I done more tests, I found the measurable overhead is at the same level of likely()/unlikely() compiler notation. So you help me to find that, Thanks again!Quoted 4 lines
Now you are saying sort is so fast so no worth to add more logics to avoid the overhead?
Then, please just drop that if condition part and instead, sort it unconditionally.
Quoted 5 lines
Now, because we found the overhead of
sort()is low, I suggest to put minimal effort to avoid it. Like the original implementation,if (
nr_swapfiles> 1)
sort();
It might confuse someone in future and would make him/her send a patch to fix like we discussed. If the logic is not clear and doesn't have measureable overhead, just leave it which is more simple/clear.
Quoted 3 lines
Or, we can make
nr_swapfilesmore correct as Tim suggested (tracking the number of the swap devices during swap on/off).
It might be better option but it's still hard to justify the patch because you said it's hard to measure. Such optimiztion patch should be from numbers.
--
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 40 lines · Minchan Kim
Hi Huang,
Quoted 37 lines · Ying
Hi, Minchan,
Quoted 9 lines · Minchan Kim
Quoted 3 lines · Ying
In fact, during the test, I found the overhead of
sort()is comparable with the performance difference of adding likely()/unlikely() to the "if" in the function.Huang,
This discussion is started from your optimization code:
Quoted 7 lines
if (
nr_swapfiles> 1)
sort();I don't have such fast machine so cannot test it. However, you added such optimization code in there so I guess it's *worth* to review so with spending my time, I pointed out what you are missing and suggested a idea to find a compromise.
Sorry for wasting your time and Thanks a lot for your review and suggestion!
When I started talking this with you, I found there is some measurable overhead of
sort(). But later when I done more tests, I found the measurable overhead is at the same level of likely()/unlikely() compiler notation. So you help me to find that, Thanks again!Quoted 4 lines
Now you are saying sort is so fast so no worth to add more logics to avoid the overhead?
Then, please just drop that if condition part and instead, sort it unconditionally.Now, because we found the overhead of
sort()is low, I suggest to put minimal effort to avoid it. Like the original implementation,
Quoted 6 lines
Quoted 2 lines
if (
nr_swapfiles> 1)
sort();It might confuse someone in future and would make him/her send a patch to fix like we discussed. If the logic is not clear and doesn't have measureable overhead, just leave it which is more simple/clear.
Because the added code is minimal and cheap, I tend to keep it and add some comments to avoid confusion. For example,
/*
* Although nr_swapfiles isn't absolute correct, but the overhead of sort()
* is so low that it isn't necessary to optimize further.
*/
Quoted 7 lines
Quoted 3 lines
Or, we can make
nr_swapfilesmore correct as Tim suggested (tracking the number of the swap devices during swap on/off).It might be better option but it's still hard to justify the patch because you said it's hard to measure. Such optimiztion patch should be from numbers.
OK.
Best Regards,
Huang, Ying
--
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>