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

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