Fix racey exception when reading disk/net stats.

We need to fully populate the stats record before adding to record list,
otherwise a UI update callback might read an incomplete record.
This commit is contained in:
Cole Robinson 2009-07-22 13:37:53 -04:00
parent 1ecf0a8180
commit 7a09cbcad7

View File

@ -381,9 +381,9 @@ class vmmDomain(gobject.GObject):
ret = 0.0 ret = 0.0
return max(ret, 0,0) # avoid negative values at poweroff return max(ret, 0,0) # avoid negative values at poweroff
def _set_max_rate(self, what): def _set_max_rate(self, record, what):
if self.record[0][what] > self.maxRecord[what]: if record[what] > self.maxRecord[what]:
self.maxRecord[what] = self.record[0][what] self.maxRecord[what] = record[what]
def tick(self, now): def tick(self, now):
if self.connection.get_state() != self.connection.STATE_ACTIVE: if self.connection.get_state() != self.connection.STATE_ACTIVE:
@ -426,25 +426,30 @@ class vmmDomain(gobject.GObject):
"netTxKB": txBytes / 1024, "netTxKB": txBytes / 1024,
} }
self.record.insert(0, newStats)
nSamples = 5 nSamples = 5
if nSamples > len(self.record): if nSamples > len(self.record):
nSamples = len(self.record) nSamples = len(self.record)
if nSamples == 0:
avg = ["cpuTimeAbs"]
percent = 0
else:
startCpuTime = self.record[nSamples-1]["cpuTimeAbs"] startCpuTime = self.record[nSamples-1]["cpuTimeAbs"]
startTimestamp = self.record[nSamples-1]["timestamp"] startTimestamp = self.record[nSamples-1]["timestamp"]
if startTimestamp == now: avg = ((newStats["cpuTimeAbs"] - startCpuTime) / nSamples)
self.record[0]["cpuTimeMovingAvg"] = self.record[0]["cpuTimeAbs"] percent = ((newStats["cpuTimeAbs"] - startCpuTime) * 100.0 /
self.record[0]["cpuTimeMovingAvgPercent"] = 0 (((now - startTimestamp) * 1000.0 * 1000.0 * 1000.0) *
else: self.connection.host_active_processor_count()))
self.record[0]["cpuTimeMovingAvg"] = (self.record[0]["cpuTimeAbs"]-startCpuTime) / nSamples
self.record[0]["cpuTimeMovingAvgPercent"] = (self.record[0]["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" ]: for r in [ "diskRd", "diskWr", "netRx", "netTx" ]:
self.record[0][r + "Rate"] = self._get_cur_rate(r + "KB") newStats[r + "Rate"] = self._get_cur_rate(r + "KB")
self._set_max_rate(r + "Rate") self._set_max_rate(newStats, r + "Rate")
self.record.insert(0, newStats)
self._update_status(info[0]) self._update_status(info[0])
gobject.idle_add(util.idle_emit, self, "resources-sampled") gobject.idle_add(util.idle_emit, self, "resources-sampled")