From f92079c565cac253a5f36792a94c7250f2594447 Mon Sep 17 00:00:00 2001 From: Anton Beloglazov Date: Tue, 2 Oct 2012 16:11:09 +1000 Subject: [PATCH] Moved calculate_migration_time from the local manager to common --- neat/common.py | 16 ++++++++++++++++ neat/globals/manager.py | 24 ++++++++++++------------ neat/locals/manager.py | 19 +------------------ tests/locals/test_manager.py | 14 -------------- tests/test_common.py | 14 ++++++++++++++ 5 files changed, 43 insertions(+), 44 deletions(-) diff --git a/neat/common.py b/neat/common.py index 76b1261..74f493b 100644 --- a/neat/common.py +++ b/neat/common.py @@ -21,6 +21,7 @@ from neat.contracts_extra import * import os import time import json +import numpy from neat.config import * from neat.db_utils import * @@ -221,4 +222,19 @@ def parse_parameters(params): """ return dict((str(k), v) for k, v in json.loads(params).items()) + +@contract +def calculate_migration_time(vms, bandwidth): + """ Calculate the mean migration time from VM RAM usage data. + + :param vms: A map of VM UUIDs to the corresponding maximum RAM in MB. + :type vms: dict(str: int) + + :param bandwidth: The network bandwidth in MB/s. + :type bandwidth: float,>0 + + :return: The mean VM migration time in seconds. + :rtype: float + """ + return float(numpy.mean(vms.values()) / bandwidth) diff --git a/neat/globals/manager.py b/neat/globals/manager.py index 7abe777..00ac573 100644 --- a/neat/globals/manager.py +++ b/neat/globals/manager.py @@ -267,8 +267,8 @@ def execute_underload(config, state, host): :param state: A state dictionary. :type state: dict(str: *) - :param state: A host name. - :type state: str + :param host: A host name. + :type host: str :return: The updated state dictionary. :rtype: dict(str: *) @@ -284,8 +284,8 @@ def execute_underload(config, state, host): for host, vms in hosts_to_vms.items(): host_cpu_mhz = sum(vms_last_cpu[x] for x in vms) if host_cpu_mhz > 0: - host_cpu_usage[host] = host_cpu_mhz - host_ram_usage[host] = host_used_ram(state['nova'], host) + hosts_cpu_usage[host] = host_cpu_mhz + hosts_ram_usage[host] = host_used_ram(state['nova'], host) else: # Exclude inactive hosts del hosts_cpu_total[host] @@ -302,8 +302,8 @@ def execute_underload(config, state, host): vms_ram = vms_ram_limit(state['nova'], vms_to_migrate) time_step = int(config.get('data_collector_interval')) - migration_time = calculate_migration_time( - vm_ram, + migration_time = common.calculate_migration_time( + vms_ram, float(config.get('network_migration_bandwidth'))) if 'vm_placement' not in state: @@ -397,7 +397,7 @@ def vms_by_host(nova, host): :return: A list of VM UUIDs from the specified host. :rtype: list(str) """ - return [vm.id for vm in nova.servers.list() + return [str(vm.id) for vm in nova.servers.list() if vm_hostname(vm) == host] @@ -470,8 +470,8 @@ def execute_overload(config, state, vm_uuids): for host, vms in hosts_to_vms.items(): host_cpu_mhz = sum(vms_last_cpu[x] for x in vms) if host_cpu_mhz > 0: - host_cpu_usage[host] = host_cpu_mhz - host_ram_usage[host] = host_used_ram(state['nova'], host) + hosts_cpu_usage[host] = host_cpu_mhz + hosts_ram_usage[host] = host_used_ram(state['nova'], host) else: inactive_hosts_cpu[host] = hosts_cpu_total[host] inactive_hosts_ram[host] = hosts_ram_total[host] @@ -483,8 +483,8 @@ def execute_overload(config, state, vm_uuids): vms_ram = vms_ram_limit(state['nova'], vms_to_migrate) time_step = int(config.get('data_collector_interval')) - migration_time = calculate_migration_time( - vm_ram, + migration_time = common.calculate_migration_time( + vms_ram, float(config.get('network_migration_bandwidth'))) if 'vm_placement' not in state: @@ -503,7 +503,7 @@ def execute_overload(config, state, vm_uuids): placement, vm_placement_state = vm_placement( hosts_cpu_usage, hosts_cpu_total, hosts_ram_usage, hosts_ram_total, - host_cpu_usage, host_ram_usage, + inactive_hosts_cpu, inactive_hosts_ram, vms_cpu, vms_ram, vm_placement_state) state['vm_placement_state'] = vm_placement_state diff --git a/neat/locals/manager.py b/neat/locals/manager.py index a80eca4..a5d210c 100644 --- a/neat/locals/manager.py +++ b/neat/locals/manager.py @@ -103,7 +103,6 @@ local manager performs the following steps: from contracts import contract from neat.contracts_extra import * -import numpy import itertools import neat.common as common @@ -214,7 +213,7 @@ def execute(config, state): host_cpu_utilization = vm_mhz_to_percentage( vm_cpu_mhz, physical_cpu_mhz_total) time_step = int(config['data_collector_interval']) - migration_time = calculate_migration_time( + migration_time = common.calculate_migration_time( vm_ram, float(config['network_migration_bandwidth'])) if 'underload_detection' not in state: @@ -373,19 +372,3 @@ def vm_mhz_to_percentage(vms, physical_cpu_mhz): """ data = itertools.izip_longest(*vms.values(), fillvalue=0) return [float(sum(x)) / physical_cpu_mhz for x in data] - - -@contract -def calculate_migration_time(vms, bandwidth): - """ Calculate the mean migration time from VM RAM usage data. - - :param vms: A map of VM UUIDs to the corresponding maximum RAM in MB. - :type vms: dict(str: int) - - :param bandwidth: The network bandwidth in MB/s. - :type bandwidth: float,>0 - - :return: The mean VM migration time in seconds. - :rtype: float - """ - return float(numpy.mean(vms.values()) / bandwidth) diff --git a/tests/locals/test_manager.py b/tests/locals/test_manager.py index b352267..6c3c63e 100644 --- a/tests/locals/test_manager.py +++ b/tests/locals/test_manager.py @@ -155,17 +155,3 @@ class LocalManager(TestCase): 'c': [100, 100, 700]}, 3000), [0.1, 0.2, 0.4]) - - @qc(10) - def calculate_migration_time( - data=dict_( - keys=str_(of='abc123-', min_length=36, max_length=36), - values=int_(min=1, max=1000), - min_length=1, max_length=10 - ), - bandwidth=float_(min=1., max=100.) - ): - ram = data.values() - migration_time = float(sum(ram)) / len(ram) / bandwidth - assert manager.calculate_migration_time(data, bandwidth) == \ - migration_time diff --git a/tests/test_common.py b/tests/test_common.py index 48aa59e..718486f 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -145,3 +145,17 @@ class Common(TestCase): params = '{"param1": 0.56, "param2": "abc"}' self.assertEqual(common.parse_parameters(params), {'param1': 0.56, 'param2': 'abc'}) + + @qc(10) + def calculate_migration_time( + data=dict_( + keys=str_(of='abc123-', min_length=36, max_length=36), + values=int_(min=1, max=1000), + min_length=1, max_length=10 + ), + bandwidth=float_(min=1., max=100.) + ): + ram = data.values() + migration_time = float(sum(ram)) / len(ram) / bandwidth + assert common.calculate_migration_time(data, bandwidth) == \ + migration_time