kvm: memslots lookup optimization
cover kvm: memslots lookup optimization
Series speed-ups GFN to memslot lookup time by:
* introducing LRU cache, which improves looukup time for same slot workload (typically boot time of Windows and Linux guest)
* switching to binary search for GFN to memslot lookup, improving lookup time with large amount of memory slots
Igor Mammedov (5):
kvm: update_memslots: drop not needed check for the same number of
pages
kvm: update_memslots: drop not needed check for the same slot
kvm: search_memslots: add simple LRU memslot caching
kvm: change memslot sorting rule from size to GFN
kvm: optimize GFN to memslot lookup with large slots amount
include/linux/kvm_host.h | 28 +++++++++++++++++++++++----- virt/kvm/kvm_main.c | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.3.1
PATCH 1/5 kvm: update_memslots: drop not needed check for the same number of pages
if number of pages haven't changed sorting algorithm will do nothing, so there is no need to do extra check to avoid entering sorting logic.
--- virt/kvm/kvm_main.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-)
virt/kvm/kvm_main.c +13 -15
@@ -679,21 +679,19 @@ static void update_memslots(struct kvm_memslots *slots,struct kvm_memory_slot *mslots = slots->memslots;WARN_ON (mslots[i].id != id); -if (new->npages != mslots[i].npages) { -if (new->npages < mslots[i].npages) { -while (i <KVM_MEM_SLOTS_NUM -1 && - new->npages < mslots[i +1 ].npages) { - mslots[i] = mslots[i +1 ]; - slots->id_to_index[mslots[i].id] = i; - i++; - } - }else { -while (i >0 && - new->npages > mslots[i -1 ].npages) { - mslots[i] = mslots[i -1 ]; - slots->id_to_index[mslots[i].id] = i; - i--; - } +if (new->npages < mslots[i].npages) { +while (i <KVM_MEM_SLOTS_NUM -1 && + new->npages < mslots[i +1 ].npages) { + mslots[i] = mslots[i +1 ]; + slots->id_to_index[mslots[i].id] = i; + i++; + } + }else { +while (i >0 && + new->npages > mslots[i -1 ].npages) { + mslots[i] = mslots[i -1 ]; + slots->id_to_index[mslots[i].id] = i; + i--; } }
Signature · 1 line
1.8.3.1
PATCH 2/5 kvm: update_memslots: drop not needed check for the same slot
In reply to Igor MammedovUP/DOWN shift loops will shift array in needed
direction and stop at place where new slot should
be placed regardless of old slot size.
--- virt/kvm/kvm_main.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
virt/kvm/kvm_main.c +11 -14
@@ -679,20 +679,17 @@ static void update_memslots(struct kvm_memslots *slots,struct kvm_memory_slot *mslots = slots->memslots;WARN_ON (mslots[i].id != id); -if (new->npages < mslots[i].npages) { -while (i <KVM_MEM_SLOTS_NUM -1 && - new->npages < mslots[i +1 ].npages) { - mslots[i] = mslots[i +1 ]; - slots->id_to_index[mslots[i].id] = i; - i++; - } - }else { -while (i >0 && - new->npages > mslots[i -1 ].npages) { - mslots[i] = mslots[i -1 ]; - slots->id_to_index[mslots[i].id] = i; - i--; - } +while (i <KVM_MEM_SLOTS_NUM -1 && + new->npages < mslots[i +1 ].npages) { + mslots[i] = mslots[i +1 ]; + slots->id_to_index[mslots[i].id] = i; + i++; + } +while (i >0 && + new->npages > mslots[i -1 ].npages) { + mslots[i] = mslots[i -1 ]; + slots->id_to_index[mslots[i].id] = i; + i--; } mslots[i] = *new;
Signature · 1 line
1.8.3.1
PATCH 3/5 kvm: search_memslots: add simple LRU memslot caching
In reply to Igor MammedovIn typical guest boot workload only 2-3 memslots are used extensively, and at that it's mostly the same memslot lookup operation.
Adding LRU cache improves average lookup time from 46 to 28 cycles (~40%) for this workload.
--- include/linux/kvm_host.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
include/linux/kvm_host.h +10 -2
@@ -353,6 +353,7 @@ struct kvm_memslots {struct kvm_memory_slot memslots[KVM_MEM_SLOTS_NUM ];/* The mapping table from slot id to the index in memslots[]. */ short id_to_index[KVM_MEM_SLOTS_NUM ]; +atomic_t lru_slot; };struct kvm {
@@ -790,12 +791,19 @@ static inline void kvm_guest_exit(void)static inline struct kvm_memory_slot *search_memslots (struct kvm_memslots *slots,gfn_t gfn) { -struct kvm_memory_slot *memslot; +int slot =atomic_read (&slots->lru_slot); +struct kvm_memory_slot *memslot = &slots->memslots[slot]; + +if (gfn >= memslot->base_gfn && + gfn < memslot->base_gfn + memslot->npages) +return memslot;kvm_for_each_memslot (memslot, slots)if (gfn >= memslot->base_gfn && - gfn < memslot->base_gfn + memslot->npages) + gfn < memslot->base_gfn + memslot->npages) { +atomic_set (&slots->lru_slot, memslot - slots->memslots);return memslot; + }return NULL ; }
Signature · 1 line
1.8.3.1
PATCH 4/5 kvm: change memslot sorting rule from size to GFN
In reply to Igor Mammedovit will allow to use binary search for GFN -> memslot lookups, reducing lookup cost with large slots amount.
--- virt/kvm/kvm_main.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
virt/kvm/kvm_main.c +11 -6
@@ -666,10 +666,10 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) }/* -* Insert memslot and re-sort memslots based on their size, -* so the larger slots will get better fit. Sorting algorithm -* takes advantage of having initially sorted array and -* known changed memslot position. +* Insert memslot and re-sort memslots based on their GFN, +* so binary search could be used to lookup GFN. +* Sorting algorithm takes advantage of having initially +* sorted array and known changed memslot position. */ static void update_memslots (struct kvm_memslots *slots,struct kvm_memory_slot *new)
@@ -679,14 +679,19 @@ static void update_memslots(struct kvm_memslots *slots,struct kvm_memory_slot *mslots = slots->memslots;WARN_ON (mslots[i].id != id); +if (!new->npages) + new->base_gfn =0 ; +while (i <KVM_MEM_SLOTS_NUM -1 && - new->npages < mslots[i +1 ].npages) { + new->base_gfn <= mslots[i +1 ].base_gfn) { +if (!mslots[i +1 ].npages) +break ; mslots[i] = mslots[i +1 ]; slots->id_to_index[mslots[i].id] = i; i++; }while (i >0 && - new->npages > mslots[i -1 ].npages) { + new->base_gfn > mslots[i -1 ].base_gfn) { mslots[i] = mslots[i -1 ]; slots->id_to_index[mslots[i].id] = i; i--;
Signature · 1 line
1.8.3.1
PATCH 5/5 kvm: optimize GFN to memslot lookup with large slots amount
In reply to Igor MammedovCurrent linear search doesn't scale well when large amount of memslots is used and looked up slot is not in the beginning memslots array.
Taking in account that memslots don't overlap, it's possible to switch sorting order of memslots array from 'npages' to 'base_gfn' and use binary search for memslot lookup by GFN.
As result of switching to binary search lookup times are reduced with large amount of memslots.
Following is a table of search_memslot() cycles during WS2008R2 guest boot.
boot, boot + ~10 min
mostly same of using it,
slot lookup randomized lookup
max average average
cycles cycles cycles13 slots : 1450 28 30
13 slots : 1400 30 40
binary search
117 slots : 13000 30 460
117 slots : 2000 35 180
binary search
--- include/linux/kvm_host.h | 34 ++++++++++++++++++++++------------ virt/kvm/kvm_main.c | 8 +++++++- 2 files changed, 29 insertions(+), 13 deletions(-)
include/linux/kvm_host.h +22 -12
@@ -354,6 +354,7 @@ struct kvm_memslots {/* The mapping table from slot id to the index in memslots[]. */ short id_to_index[KVM_MEM_SLOTS_NUM ];atomic_t lru_slot; +int used_slots; };struct kvm {
@@ -791,19 +792,28 @@ static inline void kvm_guest_exit(void)static inline struct kvm_memory_slot *search_memslots (struct kvm_memslots *slots,gfn_t gfn) { +int start =0 , end = slots->used_slots;int slot =atomic_read (&slots->lru_slot); -struct kvm_memory_slot *memslot = &slots->memslots[slot]; - -if (gfn >= memslot->base_gfn && - gfn < memslot->base_gfn + memslot->npages) -return memslot; - -kvm_for_each_memslot (memslot, slots) -if (gfn >= memslot->base_gfn && - gfn < memslot->base_gfn + memslot->npages) { -atomic_set (&slots->lru_slot, memslot - slots->memslots); -return memslot; - } +struct kvm_memory_slot *memslots = slots->memslots; + +if (gfn >= memslots[slot].base_gfn && + gfn < memslots[slot].base_gfn + memslots[slot].npages) +return &memslots[slot]; + +while (start < end) { + slot = start + (end - start) /2 ; + +if (gfn >= memslots[slot].base_gfn) + end = slot; +else + start = slot +1 ; + } + +if (gfn >= memslots[start].base_gfn && + gfn < memslots[start].base_gfn + memslots[start].npages) { +atomic_set (&slots->lru_slot, start); +return &memslots[start]; + }return NULL ; }
virt/kvm/kvm_main.c +7 -1
@@ -679,8 +679,14 @@ static void update_memslots(struct kvm_memslots *slots,struct kvm_memory_slot *mslots = slots->memslots;WARN_ON (mslots[i].id != id); -if (!new->npages) +if (!new->npages) { new->base_gfn =0 ; +if (mslots[i].npages) + slots->used_slots--; + }else { +if (!mslots[i].npages) + slots->used_slots++; + }while (i <KVM_MEM_SLOTS_NUM -1 && new->base_gfn <= mslots[i +1 ].base_gfn) {
Signature · 1 line
1.8.3.1
Paolo Bonzini
maintainerQuoted 14 lines · Igor Mammedov
Series speed-ups GFN to memslot lookup time by:
* introducing LRU cache, which improves looukup time for same slot workload (typically boot time of Windows and Linux guest)
* switching to binary search for GFN to memslot lookup, improving lookup time with large amount of memory slotsIgor Mammedov (5):
kvm:update_memslots: drop not needed check for the same number of
pages
kvm:update_memslots: drop not needed check for the same slot
kvm:search_memslots: add simple LRU memslot caching
kvm: change memslot sorting rule from size to GFN
kvm: optimize GFN to memslot lookup with large slots amount
Quoted 4 lines
include/linux/kvm_host.h | 28 +++++++++++++++++++++++----- virt/kvm/kvm_main.c | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 25 deletions(-)
Applied patches 1-3 for now, I'm not in the mood for proving that the binary search is correct. :)
Paolo
Quoted 17 lines · On Mon, 01 Dec 2014 18:38:34 +0100 Paolo Bonzini <[email protected]> wrote:
Quoted 14 lines · Igor Mammedov
Series speed-ups GFN to memslot lookup time by:
* introducing LRU cache, which improves looukup time for same slot workload (typically boot time of Windows and Linux guest)
* switching to binary search for GFN to memslot lookup, improving lookup time with large amount of memory slotsIgor Mammedov (5):
kvm:update_memslots: drop not needed check for the same number of
pages
kvm:update_memslots: drop not needed check for the same slot
kvm:search_memslots: add simple LRU memslot caching
kvm: change memslot sorting rule from size to GFN
kvm: optimize GFN to memslot lookup with large slots amount
Quoted 7 lines
Quoted 4 lines
include/linux/kvm_host.h | 28 +++++++++++++++++++++++----- virt/kvm/kvm_main.c | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 25 deletions(-)Applied patches 1-3 for now, I'm not in the mood for proving that the binary search is correct. :)
Following write up could help with improving a proving mood :)
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binarySearch
Quoted 2 lines
Paolo
Paolo Bonzini
maintainerQuoted 15 lines · Igor Mammedov
Quoted 15 lines · Igor Mammedov
Quoted 14 lines
Series speed-ups GFN to memslot lookup time by:
* introducing LRU cache, which improves looukup time for same slot workload (typically boot time of Windows and Linux guest)
* switching to binary search for GFN to memslot lookup, improving lookup time with large amount of memory slotsIgor Mammedov (5):
kvm:update_memslots: drop not needed check for the same number of
pages
kvm:update_memslots: drop not needed check for the same slot
kvm:search_memslots: add simple LRU memslot caching
kvm: change memslot sorting rule from size to GFN
kvm: optimize GFN to memslot lookup with large slots amount
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
include/linux/kvm_host.h | 28 +++++++++++++++++++++++----- virt/kvm/kvm_main.c | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 25 deletions(-)Applied patches 1-3 for now, I'm not in the mood for proving that the binary search is correct. :)
Looks good, thanks. Gleb, any objections?
Paolo
Re: [PATCH 5/5] kvm: optimize GFN to memslot lookup with large slots amount
In reply to Igor Mammedov2014-12-01 17:29+0000, Igor Mammedov:
Quoted 25 lines
Current linear search doesn't scale well when large amount of memslots is used and looked up slot is not in the beginning memslots array.
Taking in account that memslots don't overlap, it's possible to switch sorting order of memslots array from 'npages' to 'base_gfn' and use binary search for memslot lookup by GFN.As result of switching to binary search lookup times are reduced with large amount of memslots.
Following is a table of
search_memslot()cycles during WS2008R2 guest boot.boot, boot + ~10 min mostly same of using it, slot lookup randomized lookup max average average cycles cycles cycles13 slots : 1450 28 30
13 slots : 1400 30 40
binary search
Quoted 7 lines
117 slots : 13000 30 460
117 slots : 2000 35 180
binary searchSigned-off-by Igor Mammedov <[email protected]>---
Fast ... it looks that we don't even want to transfort the list-in-array into a tree-in-array to have multiplication instead of division.
(Actually, all patches.)
Quoted 34 lines
include/linux/kvm_host.h | 34 ++++++++++++++++++++++------------ virt/kvm/kvm_main.c | 8 +++++++- 2 files changed, 29 insertions(+), 13 deletions(-)include/linux/kvm_host.h +2
-12@@ -354,6 +354,7 @@ struct kvm_memslots {/* The mapping table from slot id to the index in memslots[]. */ short id_to_index[KVM_MEM_SLOTS_NUM ];atomic_t lru_slot; +int used_slots; };struct kvm {@@ -791,19 +792,28 @@ static inline void kvm_guest_exit(void)static inline struct kvm_memory_slot *search_memslots (struct kvm_memslots *slots,gfn_t gfn) { +int start =0 , end = slots->used_slots;int slot =atomic_read (&slots->lru_slot); -struct kvm_memory_slot *memslot = &slots->memslots[slot]; - -if (gfn >= memslot->base_gfn && - gfn < memslot->base_gfn + memslot->npages) -return memslot; - -kvm_for_each_memslot (memslot, slots) -if (gfn >= memslot->base_gfn && - gfn < memslot->base_gfn + memslot->npages) { -atomic_set (&slots->lru_slot, memslot - slots->memslots); -return memslot; - }
include/linux/kvm_host.h static inline void kvm_guest_exit(void) · line 806
Quoted 10 lines
include/linux/kvm_host.h +10
-0+struct kvm_memory_slot *memslots = slots->memslots; + +if (gfn >= memslots[slot].base_gfn && + gfn < memslots[slot].base_gfn + memslots[slot].npages) +return &memslots[slot]; + +while (start < end) { + slot = start + (end - start) /2 ; + +if (gfn >= memslots[slot].base_gfn)
(Even thought division is costly, I think that checking here if 'slot'
is the one we want wouldn't help very much.)
Quoted 40 lines
include/linux/kvm_host.h +10
-0+ end = slot; + else + start = slot +1 ; + } + +if (gfn >= memslots[start].base_gfn && + gfn < memslots[start].base_gfn + memslots[start].npages) { +atomic_set (&slots->lru_slot, start); +return &memslots[start]; + }return NULL ; }virt/kvm/kvm_main.c +7
-1@@ -679,8 +679,14 @@ static void update_memslots(struct kvm_memslots *slots,struct kvm_memory_slot *mslots = slots->memslots;WARN_ON (mslots[i].id != id); -if (!new->npages) +if (!new->npages) { new->base_gfn =0 ; +if (mslots[i].npages) + slots->used_slots--; + }else { +if (!mslots[i].npages) + slots->used_slots++; + }while (i <KVM_MEM_SLOTS_NUM -1 && new->base_gfn <= mslots[i +1 ].base_gfn) {Signature · 6 lines
1.8.3.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Paolo Bonzini
maintainerRe: [PATCH 5/5] kvm: optimize GFN to memslot lookup with large slots amount
Quoted 7 lines · Radim Krčmář
Quoted 4 lines
include/linux/kvm_host.h static inline void kvm_guest_exit(void) · line 806
Quoted 4 lines
include/linux/kvm_host.h +4
-0+while (start < end) { + slot = start + (end - start) /2 ; + +if (gfn >= memslots[slot].base_gfn)(Even thought division is costly, I think that checking here if 'slot'
is the one we want wouldn't help very much.)
Division by an unsigned is just a right shift. Division by signed integer is a right shift + conditional move. We can change / 2 to explicit >> 1, or change start and end to unsigned, or both.
Paolo
Re: [PATCH 5/5] kvm: optimize GFN to memslot lookup with large slots amount
2014-12-02 19:45+0100, Paolo Bonzini:
Quoted 9 lines · Radim Krčmář
Quoted 7 lines
Quoted 4 lines
include/linux/kvm_host.h static inline void kvm_guest_exit(void) · line 806
Quoted 4 lines
include/linux/kvm_host.h +4
-0+while (start < end) { + slot = start + (end - start) /2 ; + +if (gfn >= memslots[slot].base_gfn)(Even thought division is costly, I think that checking here if 'slot'
is the one we want wouldn't help very much.)
Quoted 3 lines
Division by an unsigned is just a right shift. Division by signed integer is a right shift + conditional move. We can change / 2 to explicit >> 1, or change start and end to unsigned, or both.
My bad, no respectable optimizer would miss that.