diff options
author | Janosch Frank | 2016-01-11 16:17:52 +0100 |
---|---|---|
committer | Paolo Bonzini | 2016-01-26 15:58:13 +0100 |
commit | e75a36abb42e8ae30bb4ed6fee74699a510ea6e6 (patch) | |
tree | 0dae5e5598322babab0700dee18347e21cd9e293 | |
parent | scripts/kvm/kvm_stat: Encapsulate filters variable (diff) | |
download | qemu-e75a36abb42e8ae30bb4ed6fee74699a510ea6e6.tar.gz qemu-e75a36abb42e8ae30bb4ed6fee74699a510ea6e6.tar.xz qemu-e75a36abb42e8ae30bb4ed6fee74699a510ea6e6.zip |
scripts/kvm/kvm_stat: Cleanup of Stats class
Converted class definition to new style and renamed improper named
variables.
Introduced property for fields_filter.
Moved member variable declaration to init, so one can see all class
variables when reading the init method.
Completely clear the values dict, as we don't need to keep single values.
Signed-off-by: Janosch Frank <frankja@linux.vnet.ibm.com>
Message-Id: <1452525484-32309-23-git-send-email-frankja@linux.vnet.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rwxr-xr-x | scripts/kvm/kvm_stat | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/scripts/kvm/kvm_stat b/scripts/kvm/kvm_stat index 7837f40ea6..203873e913 100755 --- a/scripts/kvm/kvm_stat +++ b/scripts/kvm/kvm_stat @@ -472,31 +472,41 @@ class DebugfsProvider(object): return int(file(PATH_DEBUGFS_KVM + '/' + key).read()) return dict([(key, val(key)) for key in self._fields]) -class Stats: +class Stats(object): def __init__(self, providers, fields=None): self.providers = providers - self.fields_filter = fields - self._update() - def _update(self): + self._fields_filter = fields + self.values = {} + self.update_provider_filters() + + def update_provider_filters(self): def wanted(key): - if not self.fields_filter: + if not self._fields_filter: return True - return re.match(self.fields_filter, key) is not None - self.values = dict() - for d in self.providers: - provider_fields = [key for key in d.fields if wanted(key)] - for key in provider_fields: - self.values[key] = None - d.fields = provider_fields - def set_fields_filter(self, fields_filter): - self.fields_filter = fields_filter - self._update() + return re.match(self._fields_filter, key) is not None + + # As we reset the counters when updating the fields we can + # also clear the cache of old values. + self.values = {} + for provider in self.providers: + provider_fields = [key for key in provider.fields if wanted(key)] + provider.fields = provider_fields + + @property + def fields_filter(self): + return self._fields_filter + + @fields_filter.setter + def fields_filter(self, fields_filter): + self._fields_filter = fields_filter + self.update_provider_filters() + def get(self): - for d in self.providers: - new = d.read() - for key in d.fields: + for provider in self.providers: + new = provider.read() + for key in provider.fields: oldval = self.values.get(key, (0, 0)) - newval = new[key] + newval = new.get(key, 0) newdelta = None if oldval is not None: newdelta = newval - oldval[0] @@ -514,9 +524,9 @@ def tui(screen, stats): def update_drilldown(): if not fields_filter: if drilldown: - stats.set_fields_filter(None) + stats.fields_filter = None else: - stats.set_fields_filter(r'^[^\(]*$') + stats.fields_filter = r'^[^\(]*$' update_drilldown() def refresh(sleeptime): screen.erase() |