Updated best_fit_decreasing to handle histories of VM CPU usage; added a last_n_vm_cpu parameter to specify how many last CPU usage values to average

This commit is contained in:
Anton Beloglazov 2012-10-30 11:25:41 +11:00
parent 59c96cbc48
commit 1357fc939d
2 changed files with 68 additions and 32 deletions

View File

@ -43,6 +43,7 @@ def best_fit_decreasing_factory(time_step, migration_time, params):
inactive_hosts_cpu, inactive_hosts_ram, \
vms_cpu, vms_ram, state=None: \
(best_fit_decreasing(
params['last_n_vm_cpu'],
get_available_resources(
params['cpu_threshold'],
hosts_cpu_usage,
@ -79,11 +80,14 @@ def get_available_resources(threshold, usage, total):
@contract
def best_fit_decreasing(hosts_cpu, hosts_ram,
def best_fit_decreasing(last_n_vm_cpu, hosts_cpu, hosts_ram,
inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram):
""" The Best Fit Decreasing (BFD) heuristic for placing VMs on hosts.
:param last_n_vm_cpu: The last n VM CPU usage values to average.
:type last_n_vm_cpu: int
:param hosts_cpu: A map of host names and their available CPU in MHz.
:type hosts_cpu: dict(str: int)
@ -97,7 +101,7 @@ def best_fit_decreasing(hosts_cpu, hosts_ram,
:type inactive_hosts_ram: dict(str: int)
:param vms_cpu: A map of VM UUID and their CPU utilization in MHz.
:type vms_cpu: dict(str: int)
:type vms_cpu: dict(str: list(int))
:param vms_ram: A map of VM UUID and their RAM usage in MB.
:type vms_ram: dict(str: int)
@ -105,8 +109,13 @@ def best_fit_decreasing(hosts_cpu, hosts_ram,
:return: A map of VM UUIDs to host names, or {} if cannot be solved.
:rtype: dict(str: str)
"""
vms = sorted(((v, vms_ram[k], k)
for k, v in vms_cpu.items()), reverse=True)
vms_tmp = []
for vm, cpu in vms_cpu.items():
last_n_cpu = cpu[-last_n_vm_cpu:]
vms_tmp.append((sum(last_n_cpu) / len(last_n_cpu),
vms_ram[vm],
vm))
vms = sorted(vms_tmp, reverse=True)
hosts = sorted(((v, hosts_ram[k], k)
for k, v in hosts_cpu.items()))
inactive_hosts = sorted(((v, inactive_hosts_ram[k], k)

View File

@ -26,7 +26,8 @@ class BinPacking(TestCase):
def test_best_fit_decreasing_factory(self):
alg = packing.best_fit_decreasing_factory(300, 20.,
{'cpu_threshold': 0.8,
'ram_threshold': 0.9})
'ram_threshold': 0.9,
'last_n_vm_cpu': 1})
hosts_cpu_usage = {
'host1': 200,
@ -53,9 +54,9 @@ class BinPacking(TestCase):
'host5': 1024,
'host6': 2048}
vms_cpu = {
'vm1': 1000,
'vm2': 1000,
'vm3': 1000}
'vm1': [100, 1000],
'vm2': [100, 1000],
'vm3': [100, 1000]}
vms_ram = {
'vm1': 2048,
'vm2': 4096,
@ -87,16 +88,16 @@ class BinPacking(TestCase):
inactive_hosts_cpu = {}
inactive_hosts_ram = {}
vms_cpu = {
'vm1': 2000,
'vm2': 1000,
'vm3': 3000}
'vm1': [100, 2000],
'vm2': [100, 1000],
'vm3': [100, 3000]}
vms_ram = {
'vm1': 512,
'vm2': 512,
'vm3': 512}
assert packing.best_fit_decreasing(
hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
1, hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram) == {
'vm1': 'host3',
'vm2': 'host2',
@ -113,16 +114,16 @@ class BinPacking(TestCase):
inactive_hosts_cpu = {}
inactive_hosts_ram = {}
vms_cpu = {
'vm1': 1000,
'vm2': 1000,
'vm3': 1000}
'vm1': [100, 1000],
'vm2': [100, 1000],
'vm3': [100, 1000]}
vms_ram = {
'vm1': 1536,
'vm2': 512,
'vm3': 1536}
assert packing.best_fit_decreasing(
hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
1, hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram) == {
'vm1': 'host1',
'vm2': 'host2',
@ -139,16 +140,16 @@ class BinPacking(TestCase):
inactive_hosts_cpu = {}
inactive_hosts_ram = {}
vms_cpu = {
'vm1': 1000,
'vm2': 1000,
'vm3': 1000}
'vm1': [100, 1000],
'vm2': [100, 1000],
'vm3': [100, 1000]}
vms_ram = {
'vm1': 1536,
'vm2': 1536,
'vm3': 1536}
assert packing.best_fit_decreasing(
hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
1, hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram) == {
'vm1': 'host1',
'vm2': 'host1',
@ -165,16 +166,16 @@ class BinPacking(TestCase):
inactive_hosts_cpu = {}
inactive_hosts_ram = {}
vms_cpu = {
'vm1': 1000,
'vm2': 1000,
'vm3': 1000}
'vm1': [100, 1000],
'vm2': [100, 1000],
'vm3': [100, 1000]}
vms_ram = {
'vm1': 3072,
'vm2': 1536,
'vm3': 1536}
assert packing.best_fit_decreasing(
hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
1, hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram) == {}
hosts_cpu = {
@ -194,16 +195,16 @@ class BinPacking(TestCase):
'host5': 1024,
'host6': 2048}
vms_cpu = {
'vm1': 1000,
'vm2': 1000,
'vm3': 1000}
'vm1': [100, 1000],
'vm2': [100, 1000],
'vm3': [100, 1000]}
vms_ram = {
'vm1': 2048,
'vm2': 4096,
'vm3': 2048}
assert packing.best_fit_decreasing(
hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
1, hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram) == {
'vm1': 'host6',
'vm2': 'host1',
@ -226,14 +227,40 @@ class BinPacking(TestCase):
'host5': 1024,
'host6': 2048}
vms_cpu = {
'vm1': 1000,
'vm2': 1000,
'vm3': 1000}
'vm1': [100, 1000],
'vm2': [100, 1000],
'vm3': [100, 1000]}
vms_ram = {
'vm1': 2048,
'vm2': 5120,
'vm3': 2048}
assert packing.best_fit_decreasing(
hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
1, hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram) == {}
hosts_cpu = {
'host1': 3000,
'host2': 1000,
'host3': 2000}
hosts_ram = {
'host1': 4096,
'host2': 1024,
'host3': 2048}
inactive_hosts_cpu = {}
inactive_hosts_ram = {}
vms_cpu = {
'vm1': [1000, 1000],
'vm2': [0, 2000],
'vm3': [500, 1500]}
vms_ram = {
'vm1': 1536,
'vm2': 1536,
'vm3': 1536}
assert packing.best_fit_decreasing(
2, hosts_cpu, hosts_ram, inactive_hosts_cpu, inactive_hosts_ram,
vms_cpu, vms_ram) == {
'vm1': 'host1',
'vm2': 'host1',
'vm3': 'host3'}