Merge "rhel73_install: Provide installer script for RHEL 7.3"
[vswitchperf.git] / tools / hugepages.py
index 02e4f29..d233f04 100644 (file)
@@ -26,7 +26,7 @@ from tools import tasks
 from conf import settings
 
 _LOGGER = logging.getLogger(__name__)
-
+_allocated_hugepages = False
 #
 # hugepage management
 #
@@ -42,7 +42,7 @@ def get_hugepage_size():
         for line in data:
             match = hugepage_size_re.search(line)
             if match:
-                _LOGGER.info('Hugepages size: %s', match.group('size_hp'))
+                _LOGGER.info('Hugepages size: %s kb', match.group('size_hp'))
                 return int(match.group('size_hp'))
         else:
             _LOGGER.error('Could not parse for hugepage size')
@@ -53,23 +53,39 @@ def allocate_hugepages():
     """Allocate hugepages on the fly
     """
     hp_size = get_hugepage_size()
-
     if hp_size > 0:
-        nr_hp = int(math.ceil(settings.getValue('HUGEPAGE_RAM_ALLOCATION')/hp_size))
-        _LOGGER.info('Will allocate %s hugepages.', nr_hp)
+       nr_hp = int(math.ceil(settings.getValue('HUGEPAGE_RAM_ALLOCATION')/hp_size))
+       _LOGGER.info('Will allocate %s hugepages.', nr_hp)
+
+       nr_hugepages = 'vm.nr_hugepages=' + str(nr_hp)
+       try:
+           tasks.run_task(['sudo', 'sysctl', nr_hugepages],
+                          _LOGGER, 'Trying to allocate hugepages..', True)
+       except subprocess.CalledProcessError:
+           _LOGGER.error('Unable to allocate hugepages.')
+           return False
+       global _allocated_hugepages
+       _allocated_hugepages = True
+       return True
 
-        nr_hugepages = 'vm.nr_hugepages=' + str(nr_hp)
+    else:
+        _LOGGER.error('Division by 0 will be supported in next release')
+        return False
+
+def deallocate_hugepages():
+    """De-allocate hugepages that were allocated on the fly
+    """
+    global _allocated_hugepages
+    if _allocated_hugepages:
+        nr_hugepages = 'vm.nr_hugepages= 0'
         try:
             tasks.run_task(['sudo', 'sysctl', nr_hugepages],
-                           _LOGGER, 'Trying to allocate hugepages..', True)
+                           _LOGGER, 'Trying to de-allocate hugepages..', True)
         except subprocess.CalledProcessError:
-            _LOGGER.error('Unable to allocate hugepages.')
+            _LOGGER.error('Unable to de-allocate hugepages.')
             return False
-        return True
-
-    else:
-        _LOGGER.error('Division by 0 will be supported in next release')
-        return False
+        _allocated_hugepages = False
+    return True
 
 
 def get_free_hugepages(socket=None):
@@ -178,4 +194,5 @@ def umount_hugepages():
     except subprocess.CalledProcessError:
         _LOGGER.error('Unable to umount hugepages.')
 
-
+    if not deallocate_hugepages():
+        _LOGGER.error('Unable to deallocate previously allocated hugepages.')