Blame SOURCES/kvm-tools-kvm_stat-simplify-initializers.patch

4a2fec
From d0ced131bf9b63e3197d5f997e6fe796df69ccec Mon Sep 17 00:00:00 2001
4a2fec
From: David Hildenbrand <david@redhat.com>
4a2fec
Date: Tue, 17 Oct 2017 19:15:51 +0200
4a2fec
Subject: [PATCH 46/69] tools/kvm_stat: simplify initializers
4a2fec
4a2fec
RH-Author: David Hildenbrand <david@redhat.com>
4a2fec
Message-id: <20171017191605.2378-26-david@redhat.com>
4a2fec
Patchwork-id: 77333
4a2fec
O-Subject: [RHEL-7.5 qemu-kvm-rhev PATCH 25/39] tools/kvm_stat: simplify initializers
4a2fec
Bugzilla: 1497137
4a2fec
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
4a2fec
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
RH-Acked-by: Thomas Huth <thuth@redhat.com>
4a2fec
4a2fec
Upstream-status: linux.git c469117df05955901d2950b6130770e526b1dbf4
4a2fec
4a2fec
commit c469117df05955901d2950b6130770e526b1dbf4
4a2fec
Author: Stefan Raspl <raspl@linux.vnet.ibm.com>
4a2fec
Date:   Wed Jun 7 21:08:32 2017 +0200
4a2fec
4a2fec
    tools/kvm_stat: simplify initializers
4a2fec
4a2fec
    Simplify a couple of initialization routines:
4a2fec
    * TracepointProvider and DebugfsProvider: Pass pid into __init__() instead
4a2fec
      of switching to the requested value in an extra call after initializing
4a2fec
      to the default first.
4a2fec
    * Pass a single options object into Stats.__init__(), delaying options
4a2fec
      evaluation accordingly, instead of evaluating options first and passing
4a2fec
      several parts of the options object to Stats.__init__() individually.
4a2fec
    * Eliminate Stats.update_provider_pid(), since this 2-line function is now
4a2fec
      used in a single place only.
4a2fec
    * Remove extra call to update_drilldown() in Tui.__init__() by getting the
4a2fec
      value of options.fields right initially when parsing options.
4a2fec
    * Simplify get_providers() logic.
4a2fec
    * Avoid duplicate fields initialization by handling it once in the
4a2fec
      providers' __init__() methods.
4a2fec
4a2fec
    Signed-off-by: Stefan Raspl <raspl@linux.vnet.ibm.com>
4a2fec
    Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
4a2fec
Signed-off-by: David Hildenbrand <david@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 scripts/kvm/kvm_stat | 74 +++++++++++++++++++++++++---------------------------
4a2fec
 1 file changed, 36 insertions(+), 38 deletions(-)
4a2fec
4a2fec
diff --git a/scripts/kvm/kvm_stat b/scripts/kvm/kvm_stat
4a2fec
index e38791d..b8522d2 100755
4a2fec
--- a/scripts/kvm/kvm_stat
4a2fec
+++ b/scripts/kvm/kvm_stat
4a2fec
@@ -295,6 +295,13 @@ class ArchS390(Arch):
4a2fec
 ARCH = Arch.get_arch()
4a2fec
 
4a2fec
 
4a2fec
+def is_field_wanted(fields_filter, field):
4a2fec
+    """Indicate whether field is valid according to fields_filter."""
4a2fec
+    if not fields_filter:
4a2fec
+        return True
4a2fec
+    return re.match(fields_filter, field) is not None
4a2fec
+
4a2fec
+
4a2fec
 def walkdir(path):
4a2fec
     """Returns os.walk() data for specified directory.
4a2fec
 
4a2fec
@@ -581,11 +588,11 @@ class TracepointProvider(object):
4a2fec
     Manages the events/groups from which it acquires its data.
4a2fec
 
4a2fec
     """
4a2fec
-    def __init__(self):
4a2fec
+    def __init__(self, pid, fields_filter):
4a2fec
         self.group_leaders = []
4a2fec
         self.filters = get_filters()
4a2fec
-        self._fields = self.get_available_fields()
4a2fec
-        self._pid = 0
4a2fec
+        self.update_fields(fields_filter)
4a2fec
+        self.pid = pid
4a2fec
 
4a2fec
     def get_available_fields(self):
4a2fec
         """Returns a list of available event's of format 'event name(filter
4a2fec
@@ -613,6 +620,11 @@ class TracepointProvider(object):
4a2fec
         fields += extra
4a2fec
         return fields
4a2fec
 
4a2fec
+    def update_fields(self, fields_filter):
4a2fec
+        """Refresh fields, applying fields_filter"""
4a2fec
+        self._fields = [field for field in self.get_available_fields()
4a2fec
+                        if is_field_wanted(fields_filter, field)]
4a2fec
+
4a2fec
     def setup_traces(self):
4a2fec
         """Creates all event and group objects needed to be able to retrieve
4a2fec
         data."""
4a2fec
@@ -723,13 +735,12 @@ class TracepointProvider(object):
4a2fec
 class DebugfsProvider(object):
4a2fec
     """Provides data from the files that KVM creates in the kvm debugfs
4a2fec
     folder."""
4a2fec
-    def __init__(self):
4a2fec
-        self._fields = self.get_available_fields()
4a2fec
+    def __init__(self, pid, fields_filter):
4a2fec
+        self.update_fields(fields_filter)
4a2fec
         self._baseline = {}
4a2fec
-        self._pid = 0
4a2fec
         self.do_read = True
4a2fec
         self.paths = []
4a2fec
-        self.reset()
4a2fec
+        self.pid = pid
4a2fec
 
4a2fec
     def get_available_fields(self):
4a2fec
         """"Returns a list of available fields.
4a2fec
@@ -739,6 +750,11 @@ class DebugfsProvider(object):
4a2fec
         """
4a2fec
         return walkdir(PATH_DEBUGFS_KVM)[2]
4a2fec
 
4a2fec
+    def update_fields(self, fields_filter):
4a2fec
+        """Refresh fields, applying fields_filter"""
4a2fec
+        self._fields = [field for field in self.get_available_fields()
4a2fec
+                        if is_field_wanted(fields_filter, field)]
4a2fec
+
4a2fec
     @property
4a2fec
     def fields(self):
4a2fec
         return self._fields
4a2fec
@@ -754,9 +770,8 @@ class DebugfsProvider(object):
4a2fec
 
4a2fec
     @pid.setter
4a2fec
     def pid(self, pid):
4a2fec
+        self._pid = pid
4a2fec
         if pid != 0:
4a2fec
-            self._pid = pid
4a2fec
-
4a2fec
             vms = walkdir(PATH_DEBUGFS_KVM)[1]
4a2fec
             if len(vms) == 0:
4a2fec
                 self.do_read = False
4a2fec
@@ -818,33 +833,19 @@ class Stats(object):
4a2fec
     provider data.
4a2fec
 
4a2fec
     """
4a2fec
-    def __init__(self, providers, pid, fields=None):
4a2fec
-        self.providers = providers
4a2fec
-        self._pid_filter = pid
4a2fec
-        self._fields_filter = fields
4a2fec
+    def __init__(self, options):
4a2fec
+        self.providers = get_providers(options)
4a2fec
+        self._pid_filter = options.pid
4a2fec
+        self._fields_filter = options.fields
4a2fec
         self.values = {}
4a2fec
-        self.update_provider_pid()
4a2fec
-        self.update_provider_filters()
4a2fec
 
4a2fec
     def update_provider_filters(self):
4a2fec
         """Propagates fields filters to providers."""
4a2fec
-        def wanted(key):
4a2fec
-            if not self._fields_filter:
4a2fec
-                return True
4a2fec
-            return re.match(self._fields_filter, key) is not None
4a2fec
-
4a2fec
         # As we reset the counters when updating the fields we can
4a2fec
         # also clear the cache of old values.
4a2fec
         self.values = {}
4a2fec
         for provider in self.providers:
4a2fec
-            provider_fields = [key for key in provider.get_available_fields()
4a2fec
-                               if wanted(key)]
4a2fec
-            provider.fields = provider_fields
4a2fec
-
4a2fec
-    def update_provider_pid(self):
4a2fec
-        """Propagates pid filters to providers."""
4a2fec
-        for provider in self.providers:
4a2fec
-            provider.pid = self._pid_filter
4a2fec
+            provider.update_fields(self._fields_filter)
4a2fec
 
4a2fec
     def reset(self):
4a2fec
         self.values = {}
4a2fec
@@ -870,7 +871,8 @@ class Stats(object):
4a2fec
         if pid != self._pid_filter:
4a2fec
             self._pid_filter = pid
4a2fec
             self.values = {}
4a2fec
-            self.update_provider_pid()
4a2fec
+            for provider in self.providers:
4a2fec
+                provider.pid = self._pid_filter
4a2fec
 
4a2fec
     def get(self):
4a2fec
         """Returns a dict with field -> (value, delta to last value) of all
4a2fec
@@ -896,7 +898,6 @@ class Tui(object):
4a2fec
     def __init__(self, stats):
4a2fec
         self.stats = stats
4a2fec
         self.screen = None
4a2fec
-        self.update_drilldown()
4a2fec
 
4a2fec
     def __enter__(self):
4a2fec
         """Initialises curses for later use.  Based on curses.wrapper
4a2fec
@@ -1270,7 +1271,7 @@ Press any other key to refresh statistics immediately.
4a2fec
                          )
4a2fec
     optparser.add_option('-f', '--fields',
4a2fec
                          action='store',
4a2fec
-                         default=None,
4a2fec
+                         default=DEFAULT_REGEX,
4a2fec
                          dest='fields',
4a2fec
                          help='fields to display (regex)',
4a2fec
                          )
4a2fec
@@ -1297,12 +1298,10 @@ def get_providers(options):
4a2fec
     """Returns a list of data providers depending on the passed options."""
4a2fec
     providers = []
4a2fec
 
4a2fec
-    if options.tracepoints:
4a2fec
-        providers.append(TracepointProvider())
4a2fec
     if options.debugfs:
4a2fec
-        providers.append(DebugfsProvider())
4a2fec
-    if len(providers) == 0:
4a2fec
-        providers.append(TracepointProvider())
4a2fec
+        providers.append(DebugfsProvider(options.pid, options.fields))
4a2fec
+    if options.tracepoints or not providers:
4a2fec
+        providers.append(TracepointProvider(options.pid, options.fields))
4a2fec
 
4a2fec
     return providers
4a2fec
 
4a2fec
@@ -1347,8 +1346,7 @@ def main():
4a2fec
         sys.stderr.write('Did you use a (unsupported) tid instead of a pid?\n')
4a2fec
         sys.exit('Specified pid does not exist.')
4a2fec
 
4a2fec
-    providers = get_providers(options)
4a2fec
-    stats = Stats(providers, options.pid, fields=options.fields)
4a2fec
+    stats = Stats(options)
4a2fec
 
4a2fec
     if options.log:
4a2fec
         log(stats)
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec