~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

« back to all changes in this revision

Viewing changes to nova/compute/stats.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, Soren Hansen
  • Date: 2012-09-07 17:49:53 UTC
  • mfrom: (1.1.61)
  • Revision ID: package-import@ubuntu.com-20120907174953-oapuvix1jxm830he
Tags: 2012.2~rc1~20120907.15996-0ubuntu1
[ Chuck Short ]
* New upstream release.
* debian/nova-common.postinst: Drop nova_sudoers permission changing
  since we do it in the debian/rules. (LP: #995285)

[ Soren Hansen ]
* Update debian/watch to account for symbolically named tarballs and
  use newer URL.
* Fix Launchpad URLs in debian/watch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
from nova.compute import task_states
17
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
18
 
23
19
 
24
20
class Stats(dict):
25
21
    """Handler for updates to compute node workload stats."""
26
22
 
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):
 
23
    def __init__(self):
 
24
        super(Stats, self).__init__()
 
25
 
 
26
        # Track instance states for compute node workload calculations:
 
27
        self.states = {}
 
28
 
 
29
    def clear(self):
 
30
        super(Stats, self).clear()
 
31
 
 
32
        self.states.clear()
 
33
 
 
34
    @property
 
35
    def io_workload(self):
43
36
        """Calculate an I/O based load by counting I/O heavy operations"""
44
37
 
45
38
        def _get(state, state_type):
82
75
    def num_vcpus_used(self):
83
76
        return self.get("num_vcpus_used", 0)
84
77
 
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."""
87
80
 
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:
 
81
        uuid = instance['uuid']
 
82
 
 
83
        # First, remove stats from the previous instance
 
84
        # state:
 
85
        if uuid in self.states:
 
86
            old_state = self.states[uuid]
 
87
 
 
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']
 
94
        else:
 
95
            # new instance
 
96
            self._increment("num_instances")
 
97
 
 
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)
 
101
 
 
102
        if vm_state == vm_states.DELETED:
96
103
            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
 
 
 
104
            self.states.pop(uuid)
 
105
 
 
106
        else:
 
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']
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)
 
112
            self["num_vcpus_used"] = x + vcpus
 
113
 
 
114
        # save updated I/O workload in stats:
 
115
        self["io_workload"] = self.io_workload
111
116
 
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
 
124
 
 
125
    def _extract_state_from_instance(self, instance):
 
126
        """Save the useful bits of instance state for tracking purposes"""
 
127
 
 
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']
 
134
 
 
135
        self.states[uuid] = dict(vm_state=vm_state, task_state=task_state,
 
136
                                 os_type=os_type, project_id=project_id,
 
137
                                 vcpus=vcpus)
 
138
 
 
139
        return (vm_state, task_state, os_type, project_id, vcpus)