16
16
from nova.compute import task_states
17
17
from nova.compute import vm_states
19
from nova.openstack.common import log as logging
21
LOG = logging.getLogger(__name__)
25
21
"""Handler for updates to compute node workload stats."""
27
def add_stats_for_instance(self, instance):
28
self._increment("num_task_%s" % instance['task_state'])
30
self._increment("num_vm_%s" % instance['vm_state'])
31
self._increment("num_instances")
33
os_type = instance['os_type']
34
self._increment("num_os_type_%s" % os_type)
36
proj_id = instance['project_id']
37
self._increment("num_proj_%s" % proj_id)
39
x = self.get("num_vcpus_used", 0)
40
self["num_vcpus_used"] = x + instance["vcpus"]
42
def calculate_io_workload(self):
24
super(Stats, self).__init__()
26
# Track instance states for compute node workload calculations:
30
super(Stats, self).clear()
35
def io_workload(self):
43
36
"""Calculate an I/O based load by counting I/O heavy operations"""
45
38
def _get(state, state_type):
82
75
def num_vcpus_used(self):
83
76
return self.get("num_vcpus_used", 0)
85
def update_stats_for_instance(self, old_instance, instance):
78
def update_stats_for_instance(self, instance):
86
79
"""Update stats after an instance is changed."""
88
old_vm_state = old_instance['vm_state']
89
new_vm_state = instance['vm_state']
91
if old_vm_state != new_vm_state:
92
self._decrement("num_vm_%s" % old_vm_state)
93
self._increment("num_vm_%s" % new_vm_state)
95
if new_vm_state == vm_states.DELETED:
81
uuid = instance['uuid']
83
# First, remove stats from the previous instance
85
if uuid in self.states:
86
old_state = self.states[uuid]
88
self._decrement("num_vm_%s" % old_state['vm_state'])
89
self._decrement("num_task_%s" % old_state['task_state'])
90
self._decrement("num_os_type_%s" % old_state['os_type'])
91
self._decrement("num_proj_%s" % old_state['project_id'])
92
x = self.get("num_vcpus_used", 0)
93
self["num_vcpus_used"] = x - old_state['vcpus']
96
self._increment("num_instances")
98
# Now update stats from the new instance state:
99
(vm_state, task_state, os_type, project_id, vcpus) = \
100
self._extract_state_from_instance(instance)
102
if vm_state == vm_states.DELETED:
96
103
self._decrement("num_instances")
98
self._decrement("num_os_type_%s" % old_instance['os_type'])
100
self._decrement("num_proj_%s" % old_instance["project_id"])
104
self.states.pop(uuid)
107
self._increment("num_vm_%s" % vm_state)
108
self._increment("num_task_%s" % task_state)
109
self._increment("num_os_type_%s" % os_type)
110
self._increment("num_proj_%s" % project_id)
102
111
x = self.get("num_vcpus_used", 0)
103
self["num_vcpus_used"] = x - old_instance['vcpus']
105
old_task_state = old_instance['task_state']
106
new_task_state = instance['task_state']
108
if old_task_state != new_task_state:
109
self._decrement("num_task_%s" % old_task_state)
110
self._increment("num_task_%s" % new_task_state)
112
self["num_vcpus_used"] = x + vcpus
114
# save updated I/O workload in stats:
115
self["io_workload"] = self.io_workload
112
117
def _decrement(self, key):
113
118
x = self.get(key, 0)
116
121
def _increment(self, key):
117
122
x = self.get(key, 0)
118
123
self[key] = x + 1
125
def _extract_state_from_instance(self, instance):
126
"""Save the useful bits of instance state for tracking purposes"""
128
uuid = instance['uuid']
129
vm_state = instance['vm_state']
130
task_state = instance['task_state']
131
os_type = instance['os_type']
132
project_id = instance['project_id']
133
vcpus = instance['vcpus']
135
self.states[uuid] = dict(vm_state=vm_state, task_state=task_state,
136
os_type=os_type, project_id=project_id,
139
return (vm_state, task_state, os_type, project_id, vcpus)