From c4eacb669e0d5e8edae79a280d3598811b249680 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 11 Jul 2011 21:22:50 -0400 Subject: [PATCH] Minor cleanups in stats building Remove unused records, remove some unused and duplicate code. --- src/virtManager/connection.py | 50 ++++++++--------------- src/virtManager/details.py | 4 +- src/virtManager/domain.py | 77 +++++++++++------------------------ src/virtManager/host.py | 18 ++++---- src/virtManager/manager.py | 34 ++++++++++++---- 5 files changed, 80 insertions(+), 103 deletions(-) diff --git a/src/virtManager/connection.py b/src/virtManager/connection.py index 220f013ed..104e4c799 100644 --- a/src/virtManager/connection.py +++ b/src/virtManager/connection.py @@ -1548,35 +1548,27 @@ class vmmConnection(vmmGObject): rxRate += vm.network_rx_rate() txRate += vm.network_tx_rate() - pcentCpuTime = 0 + pcentHostCpu = 0 + pcentMem = mem * 100.0 / self.host_memory_size() + if len(self.record) > 0: prevTimestamp = self.record[0]["timestamp"] host_cpus = self.host_active_processor_count() - pcentCpuTime = ((cpuTime) * 100.0 / + pcentHostCpu = ((cpuTime) * 100.0 / ((now - prevTimestamp) * 1000.0 * 1000.0 * 1000.0 * host_cpus)) - # Due to timing diffs between getting wall time & getting - # the domain's time, its possible to go a tiny bit over - # 100% utilization. This freaks out users of the data, so - # we hard limit it. - if pcentCpuTime > 100.0: - pcentCpuTime = 100.0 - # Enforce >= 0 just in case - if pcentCpuTime < 0.0: - pcentCpuTime = 0.0 - pcentMem = mem * 100.0 / self.host_memory_size() - if pcentMem > 100.0: - pcentMem = 100.0 + pcentHostCpu = max(0.0, min(100.0, pcentHostCpu)) + pcentMem = max(0.0, min(100.0, pcentMem)) newStats = { "timestamp": now, "memory": mem, "memoryPercent": pcentMem, "cpuTime": cpuTime, - "cpuTimePercent": pcentCpuTime, + "cpuHostPercent": pcentHostCpu, "diskRdRate" : rdRate, "diskWrRate" : wrRate, "netRxRate" : rxRate, @@ -1589,32 +1581,26 @@ class vmmConnection(vmmGObject): ######################## # Stats getter methods # ######################## - def cpu_time_vector(self): + def _vector_helper(self, record_name): vector = [] stats = self.record for i in range(self.config.get_stats_history_length() + 1): if i < len(stats): - vector.append(stats[i]["cpuTimePercent"] / 100.0) + vector.append(stats[i][record_name] / 100.0) else: vector.append(0) return vector - def cpu_time_vector_limit(self, limit): - cpudata = self.cpu_time_vector() + def host_cpu_time_vector(self): + return self._vector_helper("cpuHostPercent") + def stats_memory_vector(self): + return self._vector_helper("memoryPercent") + + def host_cpu_time_vector_limit(self, limit): + cpudata = self.host_cpu_time_vector() if len(cpudata) > limit: cpudata = cpudata[0:limit] return cpudata - - def stats_memory_vector(self): - vector = [] - stats = self.record - for i in range(self.config.get_stats_history_length() + 1): - if i < len(stats): - vector.append(stats[i]["memoryPercent"] / 100.0) - else: - vector.append(0) - return vector - def disk_io_vector_limit(self, dummy): #No point to accumulate unnormalized I/O for a conenction return [0.0] @@ -1631,8 +1617,8 @@ class vmmConnection(vmmGObject): return self._get_record_helper("memory") def stats_memory_percentage(self): return self._get_record_helper("memoryPercent") - def cpu_time_percentage(self): - return self._get_record_helper("cpuTimePercent") + def host_cpu_time_percentage(self): + return self._get_record_helper("cpuHostPercent") def network_rx_rate(self): return self._get_record_helper("netRxRate") diff --git a/src/virtManager/details.py b/src/virtManager/details.py index 0f6ecfb1a..89f8e51a9 100644 --- a/src/virtManager/details.py +++ b/src/virtManager/details.py @@ -2178,7 +2178,7 @@ class vmmDetails(vmmGObjectUI): dsk_txt = _("Disabled") net_txt = _("Disabled") - cpu_txt = "%d %%" % self.vm.cpu_time_percentage() + cpu_txt = "%d %%" % self.vm.host_cpu_time_percentage() vm_memory = self.vm.stats_memory() host_memory = self.vm.get_connection().host_memory_size() @@ -2199,7 +2199,7 @@ class vmmDetails(vmmGObjectUI): self.window.get_widget("overview-disk-usage-text").set_markup(dsk_txt) self.cpu_usage_graph.set_property("data_array", - self.vm.cpu_time_vector()) + self.vm.host_cpu_time_vector()) self.memory_usage_graph.set_property("data_array", self.vm.stats_memory_vector()) self.disk_io_graph.set_property("data_array", diff --git a/src/virtManager/domain.py b/src/virtManager/domain.py index f33354327..f0e66b92a 100644 --- a/src/virtManager/domain.py +++ b/src/virtManager/domain.py @@ -1100,47 +1100,39 @@ class vmmDomain(vmmLibvirtObject): def _sample_mem_stats(self, info): currmem = info[2] maxmem = info[1] + pcentCurrMem = info[2] * 100.0 / self.connection.host_memory_size() pcentMaxMem = info[1] * 100.0 / self.connection.host_memory_size() - - if pcentCurrMem > 100: - pcentCurrMem = 100.0 - if pcentMaxMem > 100: - pcentMaxMem = 100.0 + pcentCurrMem = max(0.0, min(pcentCurrMem, 100.0)) + pcentMaxMem = max(0.0, min(pcentMaxMem, 100.0)) return pcentCurrMem, pcentMaxMem, currmem, maxmem def _sample_cpu_stats(self, info, now): prevCpuTime = 0 prevTimestamp = 0 + cpuTime = 0 + cpuTimeAbs = 0 + pcentHostCpu = 0 + if len(self.record) > 0: prevTimestamp = self.record[0]["timestamp"] prevCpuTime = self.record[0]["cpuTimeAbs"] - cpuTime = 0 - cpuTimeAbs = 0 - pcentCpuTime = 0 if not (info[0] in [libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED]): cpuTime = info[4] - prevCpuTime cpuTimeAbs = info[4] + hostcpus = self.connection.host_active_processor_count() - pcentCpuTime = ( - (cpuTime) * 100.0 / + pcentHostCpu = (((cpuTime) * 100.0) / (((now - prevTimestamp) * 1000.0 * 1000.0 * 1000.0) * - self.connection.host_active_processor_count())) + hostcpus)) - # Due to timing diffs between getting wall time & getting - # the domain's time, its possible to go a tiny bit over - # 100% utilization. This freaks out users of the data, so - # we hard limit it. - if pcentCpuTime > 100.0: - pcentCpuTime = 100.0 - # Enforce >= 0 just in case - if pcentCpuTime < 0.0: - pcentCpuTime = 0.0 - return cpuTime, cpuTimeAbs, pcentCpuTime + pcentHostCpu = max(0.0, min(100.0, pcentHostCpu)) + + return cpuTime, cpuTimeAbs, pcentHostCpu def _get_cur_rate(self, what): if len(self.record) > 1: @@ -1186,11 +1178,11 @@ class vmmDomain(vmmLibvirtObject): def in_out_vector_limit(self, data, limit): l = len(data) / 2 - end = [l, limit][l > limit] + end = min(l, limit) if l > limit: data = data[0:end] + data[l:l + end] - d = map(lambda x, y: (x + y) / 2, data[0:end], data[end:end * 2]) - return d + + return map(lambda x, y: (x + y) / 2, data[0:end], data[end:end * 2]) def toggle_sample_network_traffic(self, ignore1=None, ignore2=None, ignore3=None, ignore4=None): @@ -1229,8 +1221,8 @@ class vmmDomain(vmmLibvirtObject): return self._get_record_helper("maxMemPercent") def cpu_time(self): return self._get_record_helper("cpuTime") - def cpu_time_percentage(self): - return self._get_record_helper("cpuTimePercent") + def host_cpu_time_percentage(self): + return self._get_record_helper("cpuHostPercent") def network_rx_rate(self): return self._get_record_helper("netRxRate") def network_tx_rate(self): @@ -1256,10 +1248,8 @@ class vmmDomain(vmmLibvirtObject): def disk_io_rate(self): return self.disk_read_rate() + self.disk_write_rate() - def cpu_time_vector(self): - return self._vector_helper("cpuTimePercent") - def cpu_time_moving_avg_vector(self): - return self._vector_helper("cpuTimeMovingAvgPercent") + def host_cpu_time_vector(self): + return self._vector_helper("cpuHostPercent") def stats_memory_vector(self): return self._vector_helper("currMemPercent") def network_traffic_vector(self): @@ -1267,8 +1257,8 @@ class vmmDomain(vmmLibvirtObject): def disk_io_vector(self): return self._in_out_vector_helper("diskRdRate", "diskWrRate") - def cpu_time_vector_limit(self, limit): - cpudata = self.cpu_time_vector() + def host_cpu_time_vector_limit(self, limit): + cpudata = self.host_cpu_time_vector() if len(cpudata) > limit: cpudata = cpudata[0:limit] return cpudata @@ -1492,7 +1482,7 @@ class vmmDomain(vmmLibvirtObject): self.is_management_domain()): info[1] = self.connection.host_memory_size() - cpuTime, cpuTimeAbs, pcentCpuTime = self._sample_cpu_stats(info, now) + cpuTime, cpuTimeAbs, pcentHostCpu = self._sample_cpu_stats(info, now) (pcentCurrMem, pcentMaxMem, curmem, maxmem) = self._sample_mem_stats(info) rdBytes, wrBytes = self._sample_disk_io() @@ -1502,7 +1492,7 @@ class vmmDomain(vmmLibvirtObject): "timestamp": now, "cpuTime": cpuTime, "cpuTimeAbs": cpuTimeAbs, - "cpuTimePercent": pcentCpuTime, + "cpuHostPercent": pcentHostCpu, "curmem": curmem, "maxmem": maxmem, "currMemPercent": pcentCurrMem, @@ -1513,25 +1503,6 @@ class vmmDomain(vmmLibvirtObject): "netTxKB": txBytes / 1024, } - nSamples = 5 - if nSamples > len(self.record): - nSamples = len(self.record) - - if nSamples == 0: - avg = ["cpuTimeAbs"] - percent = 0 - else: - startCpuTime = self.record[nSamples - 1]["cpuTimeAbs"] - startTimestamp = self.record[nSamples - 1]["timestamp"] - - avg = ((newStats["cpuTimeAbs"] - startCpuTime) / nSamples) - percent = ((newStats["cpuTimeAbs"] - startCpuTime) * 100.0 / - (((now - startTimestamp) * 1000.0 * 1000.0 * 1000.0) * - self.connection.host_active_processor_count())) - - newStats["cpuTimeMovingAvg"] = avg - newStats["cpuTimeMovingAvgPercent"] = percent - for r in ["diskRd", "diskWr", "netRx", "netTx"]: newStats[r + "Rate"] = self._get_cur_rate(r + "KB") self._set_max_rate(newStats, r + "Rate") diff --git a/src/virtManager/host.py b/src/virtManager/host.py index 56bd5fa83..6bd4edd0b 100644 --- a/src/virtManager/host.py +++ b/src/virtManager/host.py @@ -361,17 +361,21 @@ class vmmHost(vmmGObjectUI): self.window.get_widget("config-autoconnect").set_active(auto) def refresh_resources(self, ignore=None): - self.window.get_widget("performance-cpu").set_text("%d %%" % self.conn.cpu_time_percentage()) vm_memory = self.conn.pretty_stats_memory() host_memory = self.conn.pretty_host_memory_size() - self.window.get_widget("performance-memory").set_text(_("%(currentmem)s of %(maxmem)s") % {'currentmem': vm_memory, 'maxmem': host_memory}) - - cpu_vector = self.conn.cpu_time_vector() - cpu_vector.reverse() - self.cpu_usage_graph.set_property("data_array", cpu_vector) - + cpu_vector = self.conn.host_cpu_time_vector() memory_vector = self.conn.stats_memory_vector() + + cpu_vector.reverse() memory_vector.reverse() + + self.window.get_widget("performance-cpu").set_text("%d %%" % + self.conn.host_cpu_time_percentage()) + self.window.get_widget("performance-memory").set_text( + _("%(currentmem)s of %(maxmem)s") % + {'currentmem': vm_memory, 'maxmem': host_memory}) + + self.cpu_usage_graph.set_property("data_array", cpu_vector) self.memory_usage_graph.set_property("data_array", memory_vector) def conn_state_changed(self, ignore1=None): diff --git a/src/virtManager/manager.py b/src/virtManager/manager.py index b1837ee3f..1043d5c05 100644 --- a/src/virtManager/manager.py +++ b/src/virtManager/manager.py @@ -1025,13 +1025,23 @@ class vmmManager(vmmGObjectUI): model.get_value(iter2, ROW_NAME)) def vmlist_cpu_usage_sorter(self, model, iter1, iter2): - return cmp(model.get_value(iter1, ROW_HANDLE).cpu_time_percentage(), model.get_value(iter2, ROW_HANDLE).cpu_time_percentage()) + obj1 = model.get_value(iter1, ROW_HANDLE) + obj2 = model.get_value(iter2, ROW_HANDLE) + + return cmp(obj1.host_cpu_time_percentage(), + obj2.host_cpu_time_percentage()) def vmlist_disk_io_sorter(self, model, iter1, iter2): - return cmp(model.get_value(iter1, ROW_HANDLE).disk_io_rate(), model.get_value(iter2, ROW_HANDLE).disk_io_rate()) + obj1 = model.get_value(iter1, ROW_HANDLE) + obj2 = model.get_value(iter2, ROW_HANDLE) + + return cmp(obj1.disk_io_rate(), obj2.disk_io_rate()) def vmlist_network_usage_sorter(self, model, iter1, iter2): - return cmp(model.get_value(iter1, ROW_HANDLE).network_traffic_rate(), model.get_value(iter2, ROW_HANDLE).network_traffic_rate()) + obj1 = model.get_value(iter1, ROW_HANDLE) + obj2 = model.get_value(iter2, ROW_HANDLE) + + return cmp(obj1.network_traffic_rate(), obj2.network_traffic_rate()) def enable_polling(self, ignore1, ignore2, conf_entry, userdata): if userdata == self.config.STATS_DISK: @@ -1081,21 +1091,27 @@ class vmmManager(vmmGObjectUI): set_stats[stats_id](visible) def cpu_usage_img(self, column_ignore, cell, model, _iter, data): - if model.get_value(_iter, ROW_HANDLE) is None: + obj = model.get_value(_iter, ROW_HANDLE) + if obj is None: return - data = model.get_value(_iter, ROW_HANDLE).cpu_time_vector_limit(40) + + data = obj.host_cpu_time_vector_limit(40) cell.set_property('data_array', data) def disk_io_img(self, column_ignore, cell, model, _iter, data): - if model.get_value(_iter, ROW_HANDLE) is None: + obj = model.get_value(_iter, ROW_HANDLE) + if obj is None: return - data = model.get_value(_iter, ROW_HANDLE).disk_io_vector_limit(40) + + data = obj.disk_io_vector_limit(40) cell.set_property('data_array', data) def network_traffic_img(self, column_ignore, cell, model, _iter, data): - if model.get_value(_iter, ROW_HANDLE) is None: + obj = model.get_value(_iter, ROW_HANDLE) + if obj is None: return - data = model.get_value(_iter, ROW_HANDLE).network_traffic_vector_limit(40) + + data = obj.network_traffic_vector_limit(40) cell.set_property('data_array', data) vmmGObjectUI.type_register(vmmManager)