Skip to content

Commit

Permalink
kvm: change memslot sorting rule from size to GFN
Browse files Browse the repository at this point in the history
it will allow to use binary search for GFN -> memslot
lookups, reducing lookup cost with large slots amount.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Igor Mammedov authored and bonzini committed Dec 4, 2014
1 parent d4ae84a commit 0e60b07
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions virt/kvm/kvm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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--;
Expand Down

0 comments on commit 0e60b07

Please sign in to comment.