~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/compute/stats.py

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman, Adam Gandelman, Chuck Short
  • Date: 2012-08-27 15:37:18 UTC
  • mfrom: (1.1.60)
  • Revision ID: package-import@ubuntu.com-20120827153718-lj8er44eqqz1gsrj
Tags: 2012.2~rc1~20120827.15815-0ubuntu1
[ Adam Gandelman ]
* New upstream release.

[ Chuck Short ]
* debian/patches/0001-Update-tools-hacking-for-pep8-1.2-and-
  beyond.patch: Dropped we dont run pep8 tests anymore.
* debian/control: Drop pep8 build depends
* debian/*.upstart.in: Make sure we transition correctly from runlevel
  1 to 2. (LP: #820694)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2012 OpenStack, LLC.
 
2
# All Rights Reserved.
 
3
#
 
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
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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
 
14
#    under the License.
 
15
 
 
16
from nova.compute import task_states
 
17
from nova.compute import vm_states
 
18
from nova import db
 
19
from nova.openstack.common import log as logging
 
20
 
 
21
LOG = logging.getLogger(__name__)
 
22
 
 
23
 
 
24
class Stats(dict):
 
25
    """Handler for updates to compute node workload stats."""
 
26
 
 
27
    def add_stats_for_instance(self, instance):
 
28
        self._increment("num_task_%s" % instance['task_state'])
 
29
 
 
30
        self._increment("num_vm_%s" % instance['vm_state'])
 
31
        self._increment("num_instances")
 
32
 
 
33
        os_type = instance['os_type']
 
34
        self._increment("num_os_type_%s" % os_type)
 
35
 
 
36
        proj_id = instance['project_id']
 
37
        self._increment("num_proj_%s" % proj_id)
 
38
 
 
39
        x = self.get("num_vcpus_used", 0)
 
40
        self["num_vcpus_used"] = x + instance["vcpus"]
 
41
 
 
42
    def calculate_io_workload(self):
 
43
        """Calculate an I/O based load by counting I/O heavy operations"""
 
44
 
 
45
        def _get(state, state_type):
 
46
            key = "num_%s_%s" % (state_type, state)
 
47
            return self.get(key, 0)
 
48
 
 
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")
 
55
 
 
56
        return (num_builds + num_rebuilds + num_resizes + num_migrations +
 
57
                num_snapshots + num_backups)
 
58
 
 
59
    def calculate_workload(self):
 
60
        """Calculate current load of the compute host based on
 
61
        task states.
 
62
        """
 
63
        current_workload = 0
 
64
        for k in self:
 
65
            if k.startswith("num_task") and not k.endswith("None"):
 
66
                current_workload += self[k]
 
67
        return current_workload
 
68
 
 
69
    @property
 
70
    def num_instances(self):
 
71
        return self.get("num_instances", 0)
 
72
 
 
73
    def num_instances_for_project(self, project_id):
 
74
        key = "num_proj_%s" % project_id
 
75
        return self.get(key, 0)
 
76
 
 
77
    def num_os_type(self, os_type):
 
78
        key = "num_os_type_%s" % os_type
 
79
        return self.get(key, 0)
 
80
 
 
81
    @property
 
82
    def num_vcpus_used(self):
 
83
        return self.get("num_vcpus_used", 0)
 
84
 
 
85
    def update_stats_for_instance(self, old_instance, instance):
 
86
        """Update stats after an instance is changed."""
 
87
 
 
88
        old_vm_state = old_instance['vm_state']
 
89
        new_vm_state = instance['vm_state']
 
90
 
 
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)
 
94
 
 
95
        if new_vm_state == vm_states.DELETED:
 
96
            self._decrement("num_instances")
 
97
 
 
98
            self._decrement("num_os_type_%s" % old_instance['os_type'])
 
99
 
 
100
            self._decrement("num_proj_%s" % old_instance["project_id"])
 
101
 
 
102
            x = self.get("num_vcpus_used", 0)
 
103
            self["num_vcpus_used"] = x - old_instance['vcpus']
 
104
 
 
105
        old_task_state = old_instance['task_state']
 
106
        new_task_state = instance['task_state']
 
107
 
 
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)
 
111
 
 
112
    def _decrement(self, key):
 
113
        x = self.get(key, 0)
 
114
        self[key] = x - 1
 
115
 
 
116
    def _increment(self, key):
 
117
        x = self.get(key, 0)
 
118
        self[key] = x + 1