NFVBENCH-184: Add a feature (+ options) allowing to change ownership of shared log...
[nfvbench.git] / nfvbench / utils.py
index 06f643c..6da14ed 100644 (file)
@@ -13,6 +13,7 @@
 #    under the License.
 
 import glob
+from math import gcd
 from math import isnan
 import os
 import re
@@ -50,7 +51,7 @@ def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
 
 
 def save_json_result(result, json_file, std_json_path, service_chain, service_chain_count,
-                     flow_count, frame_sizes):
+                     flow_count, frame_sizes, user_id=None, group_id=None):
     """Save results in json format file."""
     filepaths = []
     if json_file:
@@ -70,6 +71,11 @@ def save_json_result(result, json_file, std_json_path, service_chain, service_ch
                           sort_keys=True,
                           separators=(',', ': '),
                           default=lambda obj: obj.to_json())
+                # possibly change file ownership
+                if group_id is None:
+                    group_id = user_id
+                if user_id is not None:
+                    os.chown(file_path, user_id, group_id)
 
 
 def dict_to_json_dict(record):
@@ -154,7 +160,6 @@ def get_intel_pci(nic_slot=None, nic_ports=None):
     return pcis
 
 
-
 def parse_flow_count(flow_count):
     flow_count = str(flow_count)
     input_fc = flow_count
@@ -203,3 +208,46 @@ class RunLock(object):
             os.unlink(self._path)
         except (OSError, IOError):
             pass
+
+
+def get_divisors(n):
+    for i in range(1, int(n / 2) + 1):
+        if n % i == 0:
+            yield i
+    yield n
+
+
+def lcm(a, b):
+    """
+    Calculate the maximum possible value for both IP and ports,
+    eventually for maximum possible flow.
+    """
+    if a != 0 and b != 0:
+        lcm_value = a * b // gcd(a, b)
+        return lcm_value
+    raise TypeError(" IP size or port range can't be zero !")
+
+
+def find_tuples_equal_to_lcm_value(a, b, lcm_value):
+    """
+    Find numbers from two list matching a LCM value.
+    """
+    for x in a:
+        for y in b:
+            if lcm(x, y) == lcm_value:
+                yield (x, y)
+
+
+def find_max_size(max_size, tuples, flow):
+    if tuples:
+        if max_size > tuples[-1][0]:
+            max_size = tuples[-1][0]
+            return int(max_size)
+        if max_size > tuples[-1][1]:
+            max_size = tuples[-1][1]
+            return int(max_size)
+
+    for i in range(max_size, 1, -1):
+        if flow % i == 0:
+            return int(i)
+    return 1