def _update_data(self): # Don't allow to call this function so frequently (<= 1 sec) now = timeutils.utcnow() if self._data.get("timestamp") is not None: delta = now - self._data.get("timestamp") if delta.seconds <= 1: return self._data = {} self._data["timestamp"] = now # Extract node's CPU statistics. try: stats = self.driver.get_host_cpu_stats() self._data["cpu.user.time"] = stats["user"] self._data["cpu.kernel.time"] = stats["kernel"] self._data["cpu.idle.time"] = stats["idle"] self._data["cpu.iowait.time"] = stats["iowait"] self._data["cpu.frequency"] = stats["frequency"] except (NotImplementedError, TypeError, KeyError): LOG.exception( _LE("Not all properties needed are implemented " "in the compute driver")) raise exception.ResourceMonitorError( monitor=self.__class__.__name__) # The compute driver API returns the absolute values for CPU times. # We compute the utilization percentages for each specific CPU time # after calculating the delta between the current reading and the # previous reading. stats["total"] = (stats["user"] + stats["kernel"] + stats["idle"] + stats["iowait"]) cputime = float(stats["total"] - self._cpu_stats.get("total", 0)) # NOTE(jwcroppe): Convert all the `perc` values to their integer forms # since pre-conversion their values are within the range [0, 1] and the # objects.MonitorMetric.value field requires an integer. perc = (stats["user"] - self._cpu_stats.get("user", 0)) / cputime self._data["cpu.user.percent"] = int(perc * 100) perc = (stats["kernel"] - self._cpu_stats.get("kernel", 0)) / cputime self._data["cpu.kernel.percent"] = int(perc * 100) perc = (stats["idle"] - self._cpu_stats.get("idle", 0)) / cputime self._data["cpu.idle.percent"] = int(perc * 100) perc = (stats["iowait"] - self._cpu_stats.get("iowait", 0)) / cputime self._data["cpu.iowait.percent"] = int(perc * 100) # Compute the current system-wide CPU utilization as a percentage. used = stats["user"] + stats["kernel"] + stats["iowait"] prev_used = (self._cpu_stats.get("user", 0) + self._cpu_stats.get("kernel", 0) + self._cpu_stats.get("iowait", 0)) perc = (used - prev_used) / cputime self._data["cpu.percent"] = int(perc * 100) self._cpu_stats = stats.copy()
def _update_data(self, **kwargs): # Don't allow to call this function so frequently (<= 1 sec) now = timeutils.utcnow() if self._data.get("timestamp") is not None: delta = now - self._data.get("timestamp") if delta.seconds <= 1: return self._data = {} self._data["timestamp"] = now # Extract node's CPU statistics. try: stats = self.driver.get_host_cpu_stats() self._data["cpu.user.time"] = stats["user"] self._data["cpu.kernel.time"] = stats["kernel"] self._data["cpu.idle.time"] = stats["idle"] self._data["cpu.iowait.time"] = stats["iowait"] self._data["cpu.frequency"] = stats["frequency"] except (NotImplementedError, TypeError, KeyError) as ex: LOG.exception( _("Not all properties needed are implemented " "in the compute driver: %s"), ex) raise exception.ResourceMonitorError( monitor=self.__class__.__name__) # The compute driver API returns the absolute values for CPU times. # We compute the utilization percentages for each specific CPU time # after calculating the delta between the current reading and the # previous reading. stats["total"] = (stats["user"] + stats["kernel"] + stats["idle"] + stats["iowait"]) cputime = float(stats["total"] - self._cpu_stats.get("total", 0)) perc = (stats["user"] - self._cpu_stats.get("user", 0)) / cputime self._data["cpu.user.percent"] = perc perc = (stats["kernel"] - self._cpu_stats.get("kernel", 0)) / cputime self._data["cpu.kernel.percent"] = perc perc = (stats["idle"] - self._cpu_stats.get("idle", 0)) / cputime self._data["cpu.idle.percent"] = perc perc = (stats["iowait"] - self._cpu_stats.get("iowait", 0)) / cputime self._data["cpu.iowait.percent"] = perc # Compute the current system-wide CPU utilization as a percentage. used = stats["user"] + stats["kernel"] + stats["iowait"] prev_used = (self._cpu_stats.get("user", 0) + self._cpu_stats.get("kernel", 0) + self._cpu_stats.get("iowait", 0)) perc = (used - prev_used) / cputime self._data["cpu.percent"] = perc self._cpu_stats = stats.copy()