Added storing host CPU MHz locally and in the DB

This commit is contained in:
Anton Beloglazov 2012-10-22 16:25:20 +11:00
parent 02ff71133a
commit acf603f50b
2 changed files with 40 additions and 6 deletions

View File

@ -173,6 +173,8 @@ def init_state(config):
'previous_overload': -1,
'vir_connection': vir_connection,
'hostname': hostname,
'host_cpu_mhz_history':
deque([], int(config['data_collector_data_length'])),
'host_cpu_overload_threshold':
float(config['host_cpu_overload_threshold']) * \
host_cpu_usable_by_vms,
@ -262,11 +264,22 @@ def execute(config, state):
current_time,
vms_current.keys(),
added_vm_data)
host_cpu_time_total,
host_cpu_time_busy,
host_cpu_mhz = get_host_cpu_mhz(state['physical_cpu_mhz'],
state['previous_host_cpu_time_total'],
state['previous_host_cpu_time_busy'])
if state['previous_time'] > 0:
append_data_locally(path, cpu_mhz, data_length)
append_data_remotely(state['db'], cpu_mhz)
state['host_cpu_mhz_history'].append(host_cpu_mhz)
append_data_remotely(state['db'],
cpu_mhz,
state['hostname'],
host_cpu_mhz)
if log.isEnabledFor(logging.DEBUG):
log.debug('Collected new data: %s', str(cpu_mhz))
log.debug('Collected host CPU MHz: %s', str(host_cpu_mhz))
state['previous_overload'] = log_host_overload(
state['db'],
@ -278,6 +291,8 @@ def execute(config, state):
state['previous_time'] = current_time
state['previous_cpu_time'] = cpu_time
state['previous_host_cpu_time_total'] = host_cpu_time_total
state['previous_host_cpu_time_busy'] = host_cpu_time_busy
return state
@ -451,16 +466,23 @@ def append_data_locally(path, data, data_length):
@contract
def append_data_remotely(db, data):
""" Submit a CPU MHz values to the central database.
def append_data_remotely(db, data, hostname, host_cpu_mhz):
""" Submit CPU MHz values to the central database.
:param db: The database object.
:type db: Database
:param data: A map of VM UUIDs onto the corresponing CPU MHz values.
:type data: dict(str : int)
:param hostname: The host name.
:type hostname: str
:param host_cpu_mhz: An average host CPU utilization in MHz.
:type host_cpu_mhz: int
"""
db.insert_vm_cpu_mhz(data)
db.insert_host_cpu_mhz(hostname, host_cpu_mhz)
@contract

View File

@ -62,7 +62,8 @@ class Collector(TestCase):
and_return(physical_cpus).once()
config = {'sql_connection': 'db',
'host_cpu_overload_threshold': '0.95',
'host_cpu_usable_by_vms': '0.75'}
'host_cpu_usable_by_vms': '0.75',
'data_collector_data_length': '5'}
hostname = 'host1'
mhz = 13540
@ -86,6 +87,7 @@ class Collector(TestCase):
assert state['previous_overload'] == -1
assert state['vir_connection'] == vir_connection
assert state['hostname'] == hostname
assert deque_maxlen(state['host_cpu_mhz_history']) == 5
self.assertAlmostEqual(state['host_cpu_overload_threshold'],
0.7125, 3)
assert state['physical_cpus'] == physical_cpus
@ -330,7 +332,9 @@ class Collector(TestCase):
list_(of=int_(min=1, max=3000),
min_length=0, max_length=10)),
min_length=0, max_length=5
)
),
hostname=str_(of='abc123', min_length=5, max_length=10),
cpu_mhz=int_(min=0, max=3000)
):
db = db_utils.init_db('sqlite:///:memory:')
initial_data = []
@ -348,11 +352,15 @@ class Collector(TestCase):
if initial_data:
db.vm_resource_usage.insert().execute(initial_data)
collector.append_data_remotely(db, data_to_submit)
db.update_host(hostname, 1, 1, 1)
collector.append_data_remotely(db, data_to_submit, hostname, cpu_mhz)
for uuid, data in final_data.items():
assert db.select_cpu_mhz_for_vm(uuid, 11) == data
assert db.select_cpu_mhz_for_host(hostname, 1) == [cpu_mhz]
@qc
def get_cpu_mhz(
cpus=int_(min=1, max=8),
@ -525,3 +533,7 @@ class Collector(TestCase):
expect(db).insert_host_overload.never()
assert not collector.log_host_overload(db, 0.9, 'host', 0, 3000,
[1000, 1000, 600])
def deque_maxlen(coll):
return int(re.sub("\)$", "", re.sub(".*=", "", coll.__repr__())))