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 os.makedirs(settings.getValue('HUGEPAGE_DIR'))
83 tasks.run_task(['sudo', 'mount', '-t', 'hugetlbfs', 'nodev',
84 settings.getValue('HUGEPAGE_DIR')],
85 _LOGGER, 'Mounting hugepages...', True)
86 except subprocess.CalledProcessError:
87 _LOGGER.error('Unable to mount hugepages.')
90 def umount_hugepages():
91 """Ensure hugepages are unmounted.
93 if not is_hugepage_mounted():
97 tasks.run_task(['sudo', 'umount', settings.getValue('HUGEPAGE_DIR')],
98 _LOGGER, 'Unmounting hugepages...', True)
99 except subprocess.CalledProcessError:
100 _LOGGER.error('Unable to umount hugepages.')