PAPI: Add support for format/unformat functions.
[vpp.git] / src / vpp-api / python / vpp_papi / vpp_stats.py
index ecd734b..76e1e54 100644 (file)
@@ -62,7 +62,8 @@ def make_string_vector(api, strings):
     if type(strings) is not list:
         strings = [strings]
     for s in strings:
-        vec = api.stat_segment_string_vector(vec, ffi.new("char []", s.encode()))
+        vec = api.stat_segment_string_vector(vec, ffi.new("char []",
+                                                          s.encode()))
     return vec
 
 
@@ -127,24 +128,45 @@ class VPPStats:
     def dump(self, counters):
         stats = {}
         rv = self.api.stat_segment_dump(counters)
+        # Raise exception and retry
+        if rv == ffi.NULL:
+            raise IOError()
         rv_len = self.api.stat_segment_vec_len(rv)
         for i in range(rv_len):
             n = ffi.string(rv[i].name).decode()
             e = stat_entry_to_python(self.api, rv[i])
-            stats[n] = e
+            if e is not None:
+                stats[n] = e
         return stats
 
     def get_counter(self, name):
-        dir = self.ls(name)
-        return self.dump(dir).values()[0]
+        retries = 0
+        while True:
+            try:
+                dir = self.ls(name)
+                return self.dump(dir).values()[0]
+            except Exception as e:
+                if retries > 10:
+                    return None
+                retries += 1
+                pass
 
     def disconnect(self):
         self.api.stat_segment_disconnect()
 
     def set_errors(self):
         '''Return all errors counters > 0'''
-        error_names = self.ls(['/err/'])
-        error_counters = self.dump(error_names)
+        retries = 0
+        while True:
+            try:
+                error_names = self.ls(['/err/'])
+                error_counters = self.dump(error_names)
+                break
+            except Exception as e:
+                if retries > 10:
+                    return None
+                retries += 1
+                pass
         return {k: error_counters[k]
                 for k in error_counters.keys() if error_counters[k]}