From de407853b18af6c75b78881120e3bddcb38f865c Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 14 Jun 2006 18:42:04 -0400 Subject: [PATCH] Hooked up instant state changes for pause/resume/shutdown operations, via domain lifecycle events --- src/virtManager/connection.py | 3 -- src/virtManager/console.py | 63 ++++++++++++++-------------- src/virtManager/details.py | 77 +++++++++++++++++++---------------- src/virtManager/domain.py | 24 ++++++++--- src/virtManager/engine.py | 13 ------ src/virtManager/manager.py | 19 ++++++--- 6 files changed, 108 insertions(+), 91 deletions(-) diff --git a/src/virtManager/connection.py b/src/virtManager/connection.py index 629c386e2..76a8225dc 100644 --- a/src/virtManager/connection.py +++ b/src/virtManager/connection.py @@ -11,8 +11,6 @@ class vmmConnection(gobject.GObject): [str, str]), "vm-removed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, [str, str]), - "vm-updated": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, - [str, str]), "disconnected": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, [str]) } @@ -102,7 +100,6 @@ class vmmConnection(gobject.GObject): self.hostinfo = self.vmm.getInfo() for uuid in self.vms.keys(): self.vms[uuid].tick(now) - self.emit("vm-updated", self.uri, uuid) return 1 diff --git a/src/virtManager/console.py b/src/virtManager/console.py index 48fb6b18c..aeb4fd08f 100644 --- a/src/virtManager/console.py +++ b/src/virtManager/console.py @@ -17,7 +17,6 @@ class vmmConsole(gobject.GObject): self.window = gtk.glade.XML(config.get_glade_file(), "vmm-console") self.config = config self.vm = vm - self.lastStatus = None topwin = self.window.get_widget("vmm-console") topwin.hide() @@ -38,6 +37,7 @@ class vmmConsole(gobject.GObject): self.window.get_widget("control-snapshot").set_icon_widget(gtk.Image()) self.window.get_widget("control-snapshot").get_icon_widget().set_from_file(config.get_icon_dir() + "/icon_snapshot.png") + self.ignorePause = False self.window.signal_autoconnect({ "on_vmm_console_delete_event": self.close, @@ -51,7 +51,8 @@ class vmmConsole(gobject.GObject): "on_control_details_clicked": self.control_vm_details, }) - self.refresh() + self.vm.connect("status-changed", self.update_widget_states) + self.update_widget_states(vm, vm.status()) def show(self): dialog = self.window.get_widget("vmm-console") @@ -66,13 +67,16 @@ class vmmConsole(gobject.GObject): return 0 def control_vm_shutdown(self, src): - info = self.vm.info() - if not(info[0] in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF ]): + status = self.vm.status() + if not(status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]): self.vm.shutdown() else: print "Shutdown requested, but machine is already shutting down / shutoff" def control_vm_pause(self, src): + if self.ignorePause: + return + status = self.vm.status() if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]: print "Pause/resume requested, but machine is shutdown / shutoff" @@ -88,7 +92,6 @@ class vmmConsole(gobject.GObject): else: print "Resume requested, but machine is already running" - def control_vm_terminal(self, src): self.emit("action-launch-terminal", self.vm.get_connection().get_uri(), self.vm.get_uuid()) @@ -98,32 +101,32 @@ class vmmConsole(gobject.GObject): def control_vm_details(self, src): self.emit("action-show-details", self.vm.get_connection().get_uri(), self.vm.get_uuid()) - def refresh(self): - print "Hell " + str(self) + " " + str(self.vm) - status = self.vm.status() - if self.lastStatus == status: - return - - if status == libvirt.VIR_DOMAIN_SHUTOFF: - self.window.get_widget("control-run").set_sensitive(True) - else: - self.window.get_widget("control-run").set_sensitive(False) - - if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF ]: - self.window.get_widget("control-pause").set_sensitive(False) - self.window.get_widget("control-shutdown").set_sensitive(False) - self.window.get_widget("control-terminal").set_sensitive(False) - self.window.get_widget("control-snapshot").set_sensitive(False) - else: - self.window.get_widget("control-pause").set_sensitive(True) - self.window.get_widget("control-shutdown").set_sensitive(True) - self.window.get_widget("control-terminal").set_sensitive(True) - self.window.get_widget("control-snapshot").set_sensitive(True) - if status == libvirt.VIR_DOMAIN_PAUSED: - self.window.get_widget("control-pause").set_active(True) + def update_widget_states(self, vm, status): + self.ignorePause = True + try: + print "Hell " + str(self) + " " + str(self.vm) + print "Update statu" + vm.run_status() + if status in [ libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]: + self.window.get_widget("control-run").set_sensitive(True) else: - self.window.get_widget("control-pause").set_active(False) + self.window.get_widget("control-run").set_sensitive(False) - self.lastStatus = status + if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF ,libvirt.VIR_DOMAIN_CRASHED ]: + self.window.get_widget("control-pause").set_sensitive(False) + self.window.get_widget("control-shutdown").set_sensitive(False) + self.window.get_widget("control-terminal").set_sensitive(False) + self.window.get_widget("control-snapshot").set_sensitive(False) + else: + self.window.get_widget("control-pause").set_sensitive(True) + self.window.get_widget("control-shutdown").set_sensitive(True) + self.window.get_widget("control-terminal").set_sensitive(True) + self.window.get_widget("control-snapshot").set_sensitive(True) + if status == libvirt.VIR_DOMAIN_PAUSED: + self.window.get_widget("control-pause").set_active(True) + else: + self.window.get_widget("control-pause").set_active(False) + except: + self.ignorePause = False + self.ignorePause = False gobject.type_register(vmmConsole) diff --git a/src/virtManager/details.py b/src/virtManager/details.py index 98b598a3d..033f16ea6 100644 --- a/src/virtManager/details.py +++ b/src/virtManager/details.py @@ -2,6 +2,7 @@ import gobject import gtk import gtk.glade +import libvirt import matplotlib matplotlib.use('GTK') @@ -26,7 +27,6 @@ class vmmDetails(gobject.GObject): self.window = gtk.glade.XML(config.get_glade_file(), "vmm-details") self.config = config self.vm = vm - self.lastStatus = None topwin = self.window.get_widget("vmm-details") topwin.hide() @@ -116,8 +116,12 @@ class vmmDetails(gobject.GObject): }) self.change_graph_ranges() - self.refresh() self.hw_selected() + self.vm.connect("status-changed", self.update_widget_states) + self.vm.connect("resources-sampled", self.refresh_resources) + + self.update_widget_states(vm, vm.status()) + self.refresh_resources(vm) def show(self): dialog = self.window.get_widget("vmm-details") @@ -142,16 +146,21 @@ class vmmDetails(gobject.GObject): return 0 def control_vm_shutdown(self, src): - if not(self.vm.run_status() in [ "shutdown", "shutoff" ]): + status = self.vm.status() + if not(status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]): self.vm.shutdown() else: print "Shutdown requested, but machine is already shutting down / shutoff" def control_vm_pause(self, src): - if self.vm.run_status() in [ "shutdown", "shutoff" ]: + if self.ignorePause: + return + + status = self.vm.status() + if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]: print "Pause/resume requested, but machine is shutdown / shutoff" else: - if self.vm.run_status() in [ "paused" ]: + if status in [ libvirt.VIR_DOMAIN_PAUSED ]: if not src.get_active(): self.vm.resume() else: @@ -162,7 +171,6 @@ class vmmDetails(gobject.GObject): else: print "Resume requested, but machine is already running" - def control_vm_terminal(self, src): self.emit("action-launch-terminal", self.vm.get_connection().get_uri(), self.vm.get_uuid()) @@ -188,39 +196,38 @@ class vmmDetails(gobject.GObject): self.network_traffic_graph.grid(True) self.network_traffic_line = None - def update_widget_states(self, status): - if self.lastStatus == status: - return - - if status == "shutoff": - self.window.get_widget("control-run").set_sensitive(True) - else: - self.window.get_widget("control-run").set_sensitive(False) - - if status in [ "shutoff", "shutdown" ]: - self.window.get_widget("control-pause").set_sensitive(False) - self.window.get_widget("control-shutdown").set_sensitive(False) - self.window.get_widget("control-terminal").set_sensitive(False) - self.window.get_widget("control-snapshot").set_sensitive(False) - else: - self.window.get_widget("control-pause").set_sensitive(True) - self.window.get_widget("control-shutdown").set_sensitive(True) - self.window.get_widget("control-terminal").set_sensitive(True) - self.window.get_widget("control-snapshot").set_sensitive(True) - if status == "paused": - self.window.get_widget("control-pause").set_active(True) + def update_widget_states(self, vm, status): + self.ignorePause = True + try: + print "Update statu" + vm.run_status() + if status in [ libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]: + self.window.get_widget("control-run").set_sensitive(True) else: - self.window.get_widget("control-pause").set_active(False) + self.window.get_widget("control-run").set_sensitive(False) - self.lastStatus = status + if status in [ libvirt.VIR_DOMAIN_SHUTDOWN, libvirt.VIR_DOMAIN_SHUTOFF, libvirt.VIR_DOMAIN_CRASHED ]: + self.window.get_widget("control-pause").set_sensitive(False) + self.window.get_widget("control-shutdown").set_sensitive(False) + self.window.get_widget("control-terminal").set_sensitive(False) + self.window.get_widget("control-snapshot").set_sensitive(False) + else: + self.window.get_widget("control-pause").set_sensitive(True) + self.window.get_widget("control-shutdown").set_sensitive(True) + self.window.get_widget("control-terminal").set_sensitive(True) + self.window.get_widget("control-snapshot").set_sensitive(True) + if status == libvirt.VIR_DOMAIN_PAUSED: + self.window.get_widget("control-pause").set_active(True) + else: + self.window.get_widget("control-pause").set_active(False) + except: + self.ignorePause = False + self.ignorePause = False - def refresh(self): - print "In details refresh" - status = self.vm.run_status() - self.update_widget_states(status) - - self.window.get_widget("overview-status-text").set_text(status) + self.window.get_widget("overview-status-text").set_text(self.vm.run_status()) self.window.get_widget("overview-status-icon").set_from_pixbuf(self.vm.run_status_icon()) + + def refresh_resources(self, vm): + print "In details refresh" self.window.get_widget("overview-cpu-usage-text").set_text("%d %%" % self.vm.cpu_time_percentage()) self.window.get_widget("overview-memory-usage-text").set_text("%d MB of %d MB" % (self.vm.current_memory()/1024, self.vm.get_connection().host_memory_size()/1024)) diff --git a/src/virtManager/domain.py b/src/virtManager/domain.py index 602ec8e5e..cd4d45c38 100644 --- a/src/virtManager/domain.py +++ b/src/virtManager/domain.py @@ -6,7 +6,10 @@ class vmmDomain(gobject.GObject): __gsignals__ = { "status-changed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, - [str]), + [int]), + "resources-sampled": (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + []), } def __init__(self, config, connection, vm, uuid): @@ -39,6 +42,7 @@ class vmmDomain(gobject.GObject): info = self.vm.info() status = info[0] status = self._normalize_status(status) + print "Update status check " + str(status) + " old " + str(self.lastStatus) if status != self.lastStatus: self.lastStatus = status self.emit("status-changed", status) @@ -57,21 +61,27 @@ class vmmDomain(gobject.GObject): prevTimestamp = self.record[0]["timestamp"] prevCpuTime = self.record[0]["cpuTimeAbs"] - pcentCpuTime = (info[4]-prevCpuTime) * 100 / ((now - prevTimestamp)*1000*1000*1000*self.connection.host_active_processor_count()) + 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] + pcentCpuTime = (cpuTime) * 100 / ((now - prevTimestamp)*1000*1000*1000*self.connection.host_active_processor_count()) pcentCurrMem = info[2] * 100 / self.connection.host_memory_size() pcentMaxMem = info[1] * 100 / self.connection.host_memory_size() newStats = { "timestamp": now, - "cpuTime": (info[4]-prevCpuTime), - "cpuTimeAbs": info[4], + "cpuTime": cpuTime, + "cpuTimeAbs": cpuTimeAbs, "cpuTimePercent": pcentCpuTime, "currMem": info[2], "currMemPercent": pcentCurrMem, "maxMem": info[1], "maxMemPercent": pcentMaxMem, } - + print "Update " + str(newStats) self.record.insert(0, newStats) nSamples = 5 @@ -90,6 +100,7 @@ class vmmDomain(gobject.GObject): self.record[0]["cpuTimeMovingAvgPercent"] = (self.record[0]["cpuTimeAbs"]-startCpuTime) * 100 / ((now-startTimestamp)*1000*1000*1000 * self.connection.host_active_processor_count()) self._update_status(info[0]) + self.emit("resources-sampled") def current_memory(self): @@ -179,14 +190,17 @@ class vmmDomain(gobject.GObject): return vector def shutdown(self): + print "Do shutdown" self.vm.shutdown() self._update_status() def suspend(self): + print "Do suspend" self.vm.suspend() self._update_status() def resume(self): + print "Do resume" self.vm.resume() self._update_status() diff --git a/src/virtManager/engine.py b/src/virtManager/engine.py index 2e2cd34d9..a29b84ff6 100644 --- a/src/virtManager/engine.py +++ b/src/virtManager/engine.py @@ -53,18 +53,6 @@ class vmmEngine: self.connections[hvuri]["windowConsole"][vmuuid].hide() del self.connections[hvuri]["windowConsole"][vmuuid] - def _do_vm_updated(self, connection, hvuri, vmuuid): - for uuid in self.connections[hvuri]["windowDetails"].keys(): - try: - self.connections[hvuri]["windowDetails"][uuid].refresh() - except: - pass - for uuid in self.connections[hvuri]["windowConsole"].keys(): - try: - self.connections[hvuri]["windowConsole"][uuid].refresh() - except: - pass - def reschedule_timer(self, ignore1,ignore2,ignore3,ignore4): self.schedule_timer() @@ -169,7 +157,6 @@ class vmmEngine: "windowConsole": {} } self.connections[key]["connection"].connect("disconnected", self._do_connection_disconnected) - self.connections[key]["connection"].connect("vm-updated", self._do_vm_updated) self.connections[key]["connection"].connect("vm-removed", self._do_vm_removed) self.connections[key]["connection"].tick() diff --git a/src/virtManager/manager.py b/src/virtManager/manager.py index 99ce28405..e3e72d81c 100644 --- a/src/virtManager/manager.py +++ b/src/virtManager/manager.py @@ -31,7 +31,6 @@ class vmmManager(gobject.GObject): self.connection.connect("vm-added", self.vm_added) self.connection.connect("vm-removed", self.vm_removed) - self.connection.connect("vm-updated", self.vm_updated) self.config.on_vmlist_status_visible_changed(self.toggle_status_visible_widget) self.config.on_vmlist_cpu_usage_visible_changed(self.toggle_cpu_usage_visible_widget) @@ -113,17 +112,19 @@ class vmmManager(gobject.GObject): def vm_added(self, connection, uri, vmuuid): vmlist = self.window.get_widget("vm-list") model = vmlist.get_model() - print "Added\n" + dup = 0 for row in range(model.iter_n_children(None)): vm = model.get_value(model.iter_nth_child(None, row), 0) if vm == vmuuid: dup = 1 - name = self.connection.get_vm(vmuuid).get_name() + vm = self.connection.get_vm(vmuuid) if dup != 1: - model.append([vmuuid, name]) + model.append([vmuuid, vm.get_name()]) + vm.connect("status-changed", self.vm_status_changed) + vm.connect("resources-sampled", self.vm_resources_sampled) def vm_removed(self, connection, uri, vmuuid): @@ -137,7 +138,15 @@ class vmmManager(gobject.GObject): model.remove(model.iter_nth_child(None, row)) break - def vm_updated(self, connection, uri, vmuuid): + def vm_status_changed(self, domain, status): + print "Status changed" + self.vm_updated(domain.get_uuid()) + + def vm_resources_sampled(self, domain): + print "Resource sampled" + self.vm_updated(domain.get_uuid()) + + def vm_updated(self, vmuuid): vmlist = self.window.get_widget("vm-list") model = vmlist.get_model()