1
# Copyright (c) 2012 OpenStack, LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
from nova.compute import task_states
17
from nova.compute import vm_states
19
from nova.openstack.common import log as logging
21
LOG = logging.getLogger(__name__)
25
"""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):
43
"""Calculate an I/O based load by counting I/O heavy operations"""
45
def _get(state, state_type):
46
key = "num_%s_%s" % (state_type, state)
47
return self.get(key, 0)
49
num_builds = _get(vm_states.BUILDING, "vm")
50
num_migrations = _get(task_states.RESIZE_MIGRATING, "task")
51
num_rebuilds = _get(task_states.REBUILDING, "task")
52
num_resizes = _get(task_states.RESIZE_PREP, "task")
53
num_snapshots = _get(task_states.IMAGE_SNAPSHOT, "task")
54
num_backups = _get(task_states.IMAGE_BACKUP, "task")
56
return (num_builds + num_rebuilds + num_resizes + num_migrations +
57
num_snapshots + num_backups)
59
def calculate_workload(self):
60
"""Calculate current load of the compute host based on
65
if k.startswith("num_task") and not k.endswith("None"):
66
current_workload += self[k]
67
return current_workload
70
def num_instances(self):
71
return self.get("num_instances", 0)
73
def num_instances_for_project(self, project_id):
74
key = "num_proj_%s" % project_id
75
return self.get(key, 0)
77
def num_os_type(self, os_type):
78
key = "num_os_type_%s" % os_type
79
return self.get(key, 0)
82
def num_vcpus_used(self):
83
return self.get("num_vcpus_used", 0)
85
def update_stats_for_instance(self, old_instance, instance):
86
"""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:
96
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"])
102
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
def _decrement(self, key):
116
def _increment(self, key):