def inspect_memory_bandwidth(self, instance, duration=None): domain = self._get_domain_not_shut_off_or_raise(instance) try: stats = self.connection.domainListGetStats( [domain], libvirt.VIR_DOMAIN_STATS_PERF) perf = stats[0][1] return virt_inspector.MemoryBandwidthStats(total=perf["perf.mbmt"], local=perf["perf.mbml"]) except (KeyError, AttributeError) as e: # NOTE(sileht): KeyError if for libvirt >=2.0.0,<2.3.0, the perf # subsystem ws existing but not these attributes # https://github.com/libvirt/libvirt/commit/bae660869de0612bee2a740083fb494c27e3f80c msg = _('Perf is not supported by current version of libvirt, and ' 'failed to inspect memory bandwidth of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg) # domainListGetStats might launch an exception if the method or # mbmt/mbml perf event is not supported by the underlying hypervisor # being used by libvirt. except libvirt.libvirtError as e: msg = _('Failed to inspect memory bandwidth of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg)
def inspect_perf_events(self, instance, duration=None): domain = self._get_domain_not_shut_off_or_raise(instance) try: stats = self.connection.domainListGetStats( [domain], libvirt.VIR_DOMAIN_STATS_PERF) perf = stats[0][1] return virt_inspector.PerfEventsStats( cpu_cycles=perf["perf.cpu_cycles"], instructions=perf["perf.instructions"], cache_references=perf["perf.cache_references"], cache_misses=perf["perf.cache_misses"]) # NOTE(sileht): KeyError if for libvirt >=2.0.0,<2.3.0, the perf # subsystem ws existing but not these attributes # https://github.com/libvirt/libvirt/commit/bae660869de0612bee2a740083fb494c27e3f80c except (AttributeError, KeyError) as e: msg = _LE('Perf is not supported by current version of libvirt, ' 'and failed to inspect perf events of ' '%(instance_uuid)s, can not get info from libvirt: ' '%(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg) # domainListGetStats might launch an exception if the method or # mbmt/mbml perf event is not supported by the underlying hypervisor # being used by libvirt. except libvirt.libvirtError as e: msg = _LE('Failed to inspect perf events of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg)
def inspect_memory_usage(self, instance, duration=None): instance_name = util.instance_name(instance) domain = self._get_domain_not_shut_off_or_raise(instance) try: memory_stats = domain.memoryStats() if (memory_stats and memory_stats.get('available') and memory_stats.get('unused')): memory_used = (memory_stats.get('available') - memory_stats.get('unused')) # Stat provided from libvirt is in KB, converting it to MB. memory_used = memory_used / units.Ki return virt_inspector.MemoryUsageStats(usage=memory_used) else: msg = _('Failed to inspect memory usage of instance ' '<name=%(name)s, id=%(id)s>, ' 'can not get info from libvirt.') % { 'name': instance_name, 'id': instance.id } raise virt_inspector.NoDataException(msg) # memoryStats might launch an exception if the method is not supported # by the underlying hypervisor being used by libvirt. except libvirt.libvirtError as e: msg = _('Failed to inspect memory usage of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg)
def inspect_cpu_l3_cache(self, instance): domain = self._lookup_by_uuid(instance) try: stats = self.connection.domainListGetStats( [domain], libvirt.VIR_DOMAIN_STATS_PERF) perf = stats[0][1] usage = perf["perf.cmt"] return virt_inspector.CPUL3CacheUsageStats(l3_cache_usage=usage) except (KeyError, AttributeError) as e: # NOTE(sileht): KeyError if for libvirt >=2.0.0,<2.3.0, the perf # subsystem ws existing but not these attributes # https://github.com/libvirt/libvirt/commit/bae660869de0612bee2a740083fb494c27e3f80c msg = _('Perf is not supported by current version of libvirt, and ' 'failed to inspect l3 cache usage of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg) # domainListGetStats might launch an exception if the method or # cmt perf event is not supported by the underlying hypervisor # being used by libvirt. except libvirt.libvirtError as e: msg = _('Failed to inspect l3 cache usage of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg)
def inspect_cpu_l3_cache(self, instance): domain = self._lookup_by_uuid(instance) try: stats = self.connection.domainListGetStats( [domain], libvirt.VIR_DOMAIN_STATS_PERF) perf = stats[0][1] usage = perf["perf.cmt"] return virt_inspector.CPUL3CacheUsageStats(l3_cache_usage=usage) except AttributeError as e: msg = _('Perf is not supported by current version of libvirt, and ' 'failed to inspect l3 cache usage of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg) # domainListGetStats might launch an exception if the method or # cmt perf event is not supported by the underlying hypervisor # being used by libvirt. except libvirt.libvirtError as e: msg = _('Failed to inspect l3 cache usage of %(instance_uuid)s, ' 'can not get info from libvirt: %(error)s') % { 'instance_uuid': instance.id, 'error': e } raise virt_inspector.NoDataException(msg)
def test_get_samples_with_empty_stats(self): self._mock_inspect_instance(virt_inspector.NoDataException()) mgr = manager.AgentManager(0, self.CONF) pollster = instance_stats.MemoryBandwidthTotalPollster(self.CONF) def all_samples(): return list(pollster.get_samples(mgr, {}, [self.instance]))
def test_get_samples_with_empty_stats(self): self._mock_inspect_instance(virt_inspector.NoDataException()) mgr = manager.AgentManager(0, self.CONF) pollster = instance_stats.PerfCPUCyclesPollster(self.CONF) def all_samples(): return list(pollster.get_samples(mgr, {}, [self.instance])) self.assertRaises(plugin_base.PollsterPermanentError, all_samples)
def inner(in_self, instance, *args, **kwargs): try: return method(in_self, instance, *args, **kwargs) except libvirt.libvirtError as e: # NOTE(sileht): At this point libvirt connection error # have been reraise as tenacity.RetryError() msg = _LE('Failed to inspect instance %(instance_uuid)s stats, ' 'can not get info from libvirt: %(error)s') % { "instance_uuid": instance.id, "error": e} raise virt_inspector.NoDataException(msg)
def test_get_samples(self): next_value = iter(( virt_inspector.MemoryResidentStats(resident=1.0), virt_inspector.MemoryResidentStats(resident=2.0), virt_inspector.NoDataException(), virt_inspector.InstanceShutOffException(), )) def inspect_memory_resident(instance, duration): value = next(next_value) if isinstance(value, virt_inspector.MemoryResidentStats): return value else: raise value self.inspector.inspect_memory_resident = mock.Mock( side_effect=inspect_memory_resident) mgr = manager.AgentManager() pollster = memory.MemoryResidentPollster() @mock.patch('ceilometer.compute.pollsters.memory.LOG') def _verify_resident_memory_metering(expected_count, expected_resident_memory_mb, mylog): samples = list(pollster.get_samples(mgr, {}, [self.instance])) self.assertEqual(expected_count, len(samples)) if expected_count > 0: self.assertEqual(set(['memory.resident']), set([s.name for s in samples])) self.assertEqual(expected_resident_memory_mb, samples[0].volume) else: self.assertEqual(1, mylog.warning.call_count) self.assertEqual(0, mylog.exception.call_count) _verify_resident_memory_metering(1, 1.0) _verify_resident_memory_metering(1, 2.0) _verify_resident_memory_metering(0, 0) _verify_resident_memory_metering(0, 0)
def get_samples(self, manager, cache, resources): self._inspection_duration = self._record_poll_time() self.inspector.purge_inspection_cache() for instance in resources: LOG.debug('Checking CPU util for instance %s', instance.id) try: cpu_info = self.inspector.inspect_cpu_util( instance, self._inspection_duration) if not cpu_info: raise virt_inspector.NoDataException() LOG.debug("CPU UTIL: %(instance)s %(util)d", { 'instance': instance, 'util': cpu_info.util }) yield util.make_sample_from_instance( instance, name='cpu_util', type=sample.TYPE_GAUGE, unit='%', volume=cpu_info.util, ) except virt_inspector.NoDataException: LOG.debug( 'Inspector has not enough data for the %(pollster)s ' 'pollster.', {'pollster': self.__class__.__name__}) except virt_inspector.InstanceNotFoundException as err: # Instance was deleted while getting samples. Ignore it. LOG.debug('Exception while getting samples %s', err) except ceilometer.NotImplementedError: # Selected inspector does not implement this pollster. LOG.debug('Obtaining CPU Util is not implemented for %s', self.inspector.__class__.__name__) except Exception as err: LOG.exception(_('Could not get CPU Util for %(id)s: %(e)s'), { 'id': instance.id, 'e': err })
def inspect_memory_usage(instance, duration): raise virt_inspector.NoDataException()
def inspect_memory_bandwidth(instance, duration): raise virt_inspector.NoDataException()
def inspect_perf_events(instance, duration): raise virt_inspector.NoDataException()