diff --git a/neat/db.py b/neat/db.py index 3493b77..31e08fc 100644 --- a/neat/db.py +++ b/neat/db.py @@ -119,7 +119,7 @@ class Database(object): self.vm_resource_usage.insert().execute(query) @contract - def update_host(self, hostname, cpu_mhz, ram): + def update_host(self, hostname, cpu_mhz, cpu_cores, ram): """ Insert new or update the corresponding host record. :param hostname: A host name. @@ -128,6 +128,9 @@ class Database(object): :param cpu_mhz: The total CPU frequency of the host in MHz. :type cpu_mhz: int,>0 + :param cpu_mhz: The number of physical CPU cores. + :type cpu_mhz: int,>0 + :param ram: The total amount of RAM of the host in MB. :type ram: int,>0 @@ -141,6 +144,7 @@ class Database(object): id = self.hosts.insert().execute( hostname=hostname, cpu_mhz=cpu_mhz, + cpu_cores=cpu_cores, ram=ram).inserted_primary_key[0] log.info('Created a new DB record for a host %s, id=%d', hostname, id) @@ -149,6 +153,7 @@ class Database(object): self.connection.execute(self.hosts.update(). where(self.hosts.c.id == row['id']). values(cpu_mhz=cpu_mhz, + cpu_cores=cpu_cores, ram=ram)) return row['id'] @@ -156,13 +161,15 @@ class Database(object): def select_host_characteristics(self): """ Select the characteristics of all the hosts. - :return: Two dicts of host names to their CPU MHz and RAM. - :rtype: tuple(dict(str: int), dict(str: int)) + :return: Three dicts of hostnames to CPU MHz, cores, and RAM. + :rtype: tuple(dict(str: int), dict(str: int), dict(str: int)) """ - hosts_cpu = {} + hosts_cpu_mhz = {} + hosts_cpu_cores = {} hosts_ram = {} for x in self.hosts.select().execute().fetchall(): hostname = str(x[1]) - hosts_cpu[hostname] = int(x[2]) - hosts_ram[hostname] = int(x[3]) - return hosts_cpu, hosts_ram + hosts_cpu_mhz[hostname] = int(x[2]) + hosts_cpu_cores[hostname] = int(x[3]) + hosts_ram[hostname] = int(x[4]) + return hosts_cpu_mhz, hosts_cpu_cores, hosts_ram diff --git a/neat/db_utils.py b/neat/db_utils.py index 84c8c39..6135313 100644 --- a/neat/db_utils.py +++ b/neat/db_utils.py @@ -42,6 +42,7 @@ def init_db(sql_connection): Column('id', Integer, primary_key=True), Column('hostname', String(255), nullable=False), Column('cpu_mhz', Integer, nullable=False), + Column('cpu_cores', Integer, nullable=False), Column('ram', Integer, nullable=False)) vms = Table('vms', metadata, diff --git a/neat/globals/manager.py b/neat/globals/manager.py index d5ee663..4fe0cd8 100644 --- a/neat/globals/manager.py +++ b/neat/globals/manager.py @@ -282,7 +282,7 @@ def execute_underload(config, state, host): :rtype: dict(str: *) """ underloaded_host = host - hosts_cpu_total, hosts_ram_total = state['db'].select_host_characteristics() + hosts_cpu_total, _, hosts_ram_total = state['db'].select_host_characteristics() hosts_to_vms = vms_by_hosts(state['nova'], state['compute_hosts']) vms_last_cpu = state['db'].select_last_cpu_mhz_for_vms() @@ -488,7 +488,7 @@ def execute_overload(config, state, vm_uuids): :return: The updated state dictionary. :rtype: dict(str: *) """ - hosts_cpu_total, hosts_ram_total = state['db'].select_host_characteristics() + hosts_cpu_total, _, hosts_ram_total = state['db'].select_host_characteristics() hosts_to_vms = vms_by_hosts(state['nova'], state['compute_hosts']) vms_last_cpu = state['db'].select_last_cpu_mhz_for_vms() diff --git a/neat/locals/collector.py b/neat/locals/collector.py index 402475b..5cb4cf4 100644 --- a/neat/locals/collector.py +++ b/neat/locals/collector.py @@ -160,7 +160,7 @@ def init_state(config): physical_cpus = common.physical_cpu_count(vir_connection) db = init_db(config['sql_connection']) - db.update_host(hostname, host_cpu_mhz, host_ram) + db.update_host(hostname, host_cpu_mhz, physical_cpus, host_ram) return {'previous_time': 0., 'previous_cpu_time': dict(), diff --git a/tests/locals/test_collector.py b/tests/locals/test_collector.py index a27806a..8ddc423 100644 --- a/tests/locals/test_collector.py +++ b/tests/locals/test_collector.py @@ -72,7 +72,7 @@ class Collector(TestCase): db = mock('db') expect(collector).init_db('db').and_return(db).once() - expect(db).update_host(hostname, mhz, ram).once() + expect(db).update_host(hostname, mhz, physical_cpus, ram).once() state = collector.init_state(config) assert state['previous_time'] == 0 diff --git a/tests/test_db.py b/tests/test_db.py index dcee95c..f94fa2b 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -110,29 +110,32 @@ class Db(TestCase): @qc(1) def update_host(): db = db_utils.init_db('sqlite:///:memory:') - db.update_host('host1', 3000, 4000) + db.update_host('host1', 3000, 4, 4000) hosts = db.hosts.select().execute().fetchall() assert len(hosts) == 1 host = hosts[0] assert host['hostname'] == 'host1' assert host['cpu_mhz'] == 3000 + assert host['cpu_cores'] == 4 assert host['ram'] == 4000 - db.update_host('host1', 3500, 8000) + db.update_host('host1', 3500, 8, 8000) hosts = db.hosts.select().execute().fetchall() assert len(hosts) == 1 host = hosts[0] assert host['hostname'] == 'host1' assert host['cpu_mhz'] == 3500 + assert host['cpu_cores'] == 8 assert host['ram'] == 8000 @qc(1) def select_host_characteristics(): db = db_utils.init_db('sqlite:///:memory:') - assert db.select_host_characteristics() == ({}, {}) + assert db.select_host_characteristics() == ({}, {}, {}) - db.update_host('host1', 3000, 4000) - db.update_host('host2', 3500, 8000) + db.update_host('host1', 3000, 4, 4000) + db.update_host('host2', 3500, 8, 8000) assert db.select_host_characteristics() == \ ({'host1': 3000, 'host2': 3500}, + {'host1': 4, 'host2': 8}, {'host1': 4000, 'host2': 8000}) diff --git a/tests/test_db_utils.py b/tests/test_db_utils.py index 4b7a91b..d50ac15 100644 --- a/tests/test_db_utils.py +++ b/tests/test_db_utils.py @@ -34,7 +34,7 @@ class DbUtils(TestCase): assert isinstance(db.vms, Table) assert isinstance(db.vm_resource_usage, Table) assert db.hosts.c.keys() == \ - ['id', 'hostname', 'cpu_mhz', 'ram'] + ['id', 'hostname', 'cpu_mhz', 'cpu_cores', 'ram'] assert db.vms.c.keys() == \ ['id', 'uuid'] assert db.vm_resource_usage.c.keys() == \