1 # Copyright 2015-2016 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """Automation of hugepages management
24 from tools import tasks
25 from conf import settings
27 _LOGGER = logging.getLogger(__name__)
34 def is_hugepage_available():
35 """Check if hugepages are available on the system.
37 hugepage_re = re.compile(r'^HugePages_Free:\s+(?P<num_hp>\d+)$')
40 with open('/proc/meminfo') as mem_file:
41 mem_info = mem_file.readlines()
43 # first check if module is loaded
45 result = hugepage_re.match(line)
49 num_huge = result.group('num_hp')
51 _LOGGER.info('No free hugepages.')
53 _LOGGER.info('Found \'%s\' free hugepage(s).', num_huge)
59 def is_hugepage_mounted():
60 """Check if hugepages are mounted.
62 output = subprocess.check_output(['mount'], shell=True)
63 my_encoding = locale.getdefaultlocale()[1]
64 for line in output.decode(my_encoding).split('\n'):
65 if 'hugetlbfs' in line:
71 def mount_hugepages():
72 """Ensure hugepages are mounted.
74 if not is_hugepage_available():
77 if is_hugepage_mounted():
80 if not os.path.exists(settings.getValue('HUGEPAGE_DIR')):
81 tasks.run_task(['sudo', 'mkdir', settings.getValue('HUGEPAGE_DIR')], _LOGGER,
82 'Creating directory ' + settings.getValue('HUGEPAGE_DIR'), True)
84 tasks.run_task(['sudo', 'mount', '-t', 'hugetlbfs', 'nodev',
85 settings.getValue('HUGEPAGE_DIR')],
86 _LOGGER, 'Mounting hugepages...', True)
87 except subprocess.CalledProcessError:
88 _LOGGER.error('Unable to mount hugepages.')
91 def umount_hugepages():
92 """Ensure hugepages are unmounted.
94 if not is_hugepage_mounted():
98 tasks.run_task(['sudo', 'umount', settings.getValue('HUGEPAGE_DIR')],
99 _LOGGER, 'Unmounting hugepages...', True)
100 except subprocess.CalledProcessError:
101 _LOGGER.error('Unable to umount hugepages.')