From ac246d3d6aa9201ae93c112c29b07ebf5db39544 Mon Sep 17 00:00:00 2001 From: Marek Marczykowski Date: Fri, 9 Mar 2012 17:23:41 +0100 Subject: [PATCH] dom0/qvm-ls: improve performance Do not evaluate each field twice - some of them (eg. disk usage) are expensive. --- dom0/qvm-tools/qvm-ls | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/dom0/qvm-tools/qvm-ls b/dom0/qvm-tools/qvm-ls index 48d4217b..2d2c750b 100755 --- a/dom0/qvm-tools/qvm-ls +++ b/dom0/qvm-tools/qvm-ls @@ -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()