
Reviewed-by: Daniel Barboza <dhbarboza82@gmail.com> Tested-by: Daniel Barboza <dhbarboza82@gmail.com> On 10/28/2015 02:46 PM, pvital@linux.vnet.ibm.com wrote:
From: Paulo Vital <pvital@linux.vnet.ibm.com>
Add support to capture the guest's memory utilization by using libvirt 'virDomainMemoryStats'. The return is the percentage of memory utilization in the guest's stats dictionary.
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- src/wok/plugins/kimchi/docs/API.md | 2 ++ src/wok/plugins/kimchi/model/vms.py | 15 +++++++++++++++ src/wok/plugins/kimchi/tests/test_mockmodel.py | 2 +- src/wok/plugins/kimchi/tests/test_model.py | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/wok/plugins/kimchi/docs/API.md b/src/wok/plugins/kimchi/docs/API.md index a9333b5..eb3905f 100644 --- a/src/wok/plugins/kimchi/docs/API.md +++ b/src/wok/plugins/kimchi/docs/API.md @@ -107,6 +107,8 @@ server. * stats: Virtual machine statistics: * cpu_utilization: A number between 0 and 100 which indicates the percentage of CPU utilization. + * mem_utilization: A number between 0 and 100 which indicates the + percentage of memory utilization. * net_throughput: Expresses total network throughput for reads and writes across all virtual interfaces (kb/s). * net_throughput_peak: The highest recent value of 'net_throughput'. diff --git a/src/wok/plugins/kimchi/model/vms.py b/src/wok/plugins/kimchi/model/vms.py index 60145d0..6e77bae 100644 --- a/src/wok/plugins/kimchi/model/vms.py +++ b/src/wok/plugins/kimchi/model/vms.py @@ -896,6 +896,7 @@ class VMModel(object): self.stats[vm_uuid].update({'timestamp': timestamp})
self._get_percentage_cpu_usage(vm_uuid, info, seconds) + self._get_percentage_mem_usage(vm_uuid, dom, seconds) self._get_network_io_rate(vm_uuid, dom, seconds) self._get_disk_io_rate(vm_uuid, dom, seconds) except Exception as e: @@ -914,6 +915,19 @@ class VMModel(object):
self.stats[vm_uuid].update({'cputime': info[4], 'cpu': percentage})
+ def _get_percentage_mem_usage(self, vm_uuid, dom, seconds): + # Get the guest's memory stats + memStats = dom.memoryStats() + if memStats: + memUsed = memStats.get('available') - memStats.get('unused') + else: + wok_log.debug('Failed to measure memory usage of the guest.') + + percentage = max(0.0, min(100.0, ((memUsed * 100.0) / + memStats.get('available')))) + + self.stats[vm_uuid].update({'mem_usage': percentage}) + def _get_network_io_rate(self, vm_uuid, dom, seconds): prevNetRxKB = self.stats[vm_uuid].get('netRxKB', 0) prevNetTxKB = self.stats[vm_uuid].get('netTxKB', 0) @@ -1000,6 +1014,7 @@ class VMModel(object): vm_stats = self.stats.get(dom.UUIDString(), {}) res = {} res['cpu_utilization'] = vm_stats.get('cpu', 0) + res['mem_utilization'] = vm_stats.get('mem_usage', 0) res['net_throughput'] = vm_stats.get('net_io', 0) res['net_throughput_peak'] = vm_stats.get('max_net_io', 100) res['io_throughput'] = vm_stats.get('disk_io', 0) diff --git a/src/wok/plugins/kimchi/tests/test_mockmodel.py b/src/wok/plugins/kimchi/tests/test_mockmodel.py index 54a1ac8..7c914b8 100644 --- a/src/wok/plugins/kimchi/tests/test_mockmodel.py +++ b/src/wok/plugins/kimchi/tests/test_mockmodel.py @@ -125,7 +125,7 @@ class MockModelTests(unittest.TestCase): 'screenshot', 'icon', 'graphics', 'users', 'groups', 'access', 'persistent'))
- stats_keys = set(('cpu_utilization', + stats_keys = set(('cpu_utilization', 'mem_utilization', 'net_throughput', 'net_throughput_peak', 'io_throughput', 'io_throughput_peak'))
diff --git a/src/wok/plugins/kimchi/tests/test_model.py b/src/wok/plugins/kimchi/tests/test_model.py index 4ae837c..fdbe3b4 100644 --- a/src/wok/plugins/kimchi/tests/test_model.py +++ b/src/wok/plugins/kimchi/tests/test_model.py @@ -95,7 +95,7 @@ class ModelTests(unittest.TestCase): 'screenshot', 'icon', 'graphics', 'users', 'groups', 'access', 'persistent'))
- stats_keys = set(('cpu_utilization', + stats_keys = set(('cpu_utilization', 'mem_utilization', 'net_throughput', 'net_throughput_peak', 'io_throughput', 'io_throughput_peak')) info = inst.vm_lookup('test')