X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=tools%2Fhugepages.py;h=4c91e7d25a78618b5022245650d5d8796aac2f64;hb=2cfae5e4569bf595e238a4ccb56a6ef5544a3265;hp=02e4f29cb216c2ec9d7e95d98fd81f3ea5d4b966;hpb=a9e754b1050b17464d9e77a942068956d872894e;p=vswitchperf.git diff --git a/tools/hugepages.py b/tools/hugepages.py index 02e4f29c..4c91e7d2 100644 --- a/tools/hugepages.py +++ b/tools/hugepages.py @@ -1,4 +1,4 @@ -# Copyright 2015-2016 Intel Corporation. +# Copyright 2015-2017 Intel Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ from tools import tasks from conf import settings _LOGGER = logging.getLogger(__name__) - +_ALLOCATED_HUGEPAGES = False # # hugepage management # @@ -37,23 +37,21 @@ def get_hugepage_size(): """ hugepage_size_re = re.compile(r'^Hugepagesize:\s+(?P\d+)\s+kB', re.IGNORECASE) - with open('/proc/meminfo', 'r') as fh: - data = fh.readlines() + with open('/proc/meminfo', 'r') as result_file: + data = result_file.readlines() 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') - return 0 + _LOGGER.error('Could not parse for hugepage size') + return 0 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) @@ -65,12 +63,31 @@ def allocate_hugepages(): except subprocess.CalledProcessError: _LOGGER.error('Unable to allocate hugepages.') return False + # pylint: disable=global-statement + global _ALLOCATED_HUGEPAGES + _ALLOCATED_HUGEPAGES = True return True 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 + """ + # pylint: disable=global-statement + global _ALLOCATED_HUGEPAGES + if _ALLOCATED_HUGEPAGES: + nr_hugepages = 'vm.nr_hugepages= 0' + try: + tasks.run_task(['sudo', 'sysctl', nr_hugepages], + _LOGGER, 'Trying to de-allocate hugepages..', True) + except subprocess.CalledProcessError: + _LOGGER.error('Unable to de-allocate hugepages.') + return False + _ALLOCATED_HUGEPAGES = False + return True + def get_free_hugepages(socket=None): """Get the free hugepage totals on the system. @@ -86,22 +103,21 @@ def get_free_hugepages(socket=None): meminfo_path = '/sys/devices/system/node/node{}/meminfo'.format( socket) else: - _LOGGER.info('No hugepage info found for socket {}'.format(socket)) + _LOGGER.info('No hugepage info found for socket %s', socket) return 0 else: meminfo_path = '/proc/meminfo' - with open(meminfo_path, 'r') as fh: - data = fh.readlines() + with open(meminfo_path, 'r') as result_file: + data = result_file.readlines() for line in data: match = hugepage_free_re.search(line) if match: _LOGGER.info('Hugepages free: %s %s', match.group('free_hp'), 'on socket {}'.format(socket) if socket else '') return int(match.group('free_hp')) - else: - _LOGGER.info('Could not parse for hugepage size') - return 0 + _LOGGER.info('Could not parse for hugepage size') + return 0 def is_hugepage_available(): @@ -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.')