dom0/qvm-ls: improve performance

Do not evaluate each field twice - some of them (eg. disk usage) are expensive.
This commit is contained in:
Marek Marczykowski 2012-03-09 17:23:41 +01:00
parent c7f3b1a685
commit ac246d3d6a

View File

@ -167,16 +167,28 @@ def main():
assert len(vms_to_display) == no_vms
# First calculate the maximum width of each field we want to display
total_width = 0;
# also collect data to display
for f in fields_to_display:
fields[f]["max_width"] = len(f)
for vm in vms_to_display:
l = len(str(eval(fields[f]["func"])))
data_to_display = []
for vm in vms_to_display:
data_row = {}
for f in fields_to_display:
data_row[f] = str(eval(fields[f]["func"]))
l = len(data_row[f])
if l > fields[f]["max_width"]:
fields[f]["max_width"] = l
data_to_display.append(data_row)
try:
vm.verify_files()
except QubesException as err:
print >> sys.stderr, "WARNING: VM '{0}' has corrupted files!".format(vm.name)
# XXX: For what?
total_width = 0;
for f in fields_to_display:
total_width += fields[f]["max_width"]
# Display the header
s = ""
for f in fields_to_display:
@ -195,16 +207,11 @@ def main():
print s
# ... and the actual data
for vm in vms_to_display:
for row in data_to_display:
s = ""
for f in fields_to_display:
fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
s += fmt.format(eval(fields[f]["func"]))
s += fmt.format(row[f])
print s
try:
vm.verify_files()
except QubesException as err:
print >> sys.stderr, "WARNING: VM '{0}' has corrupted files!".format(vm.name)
main()