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
25 from tools import tasks
26 from conf import settings
28 _LOGGER = logging.getLogger(__name__)
34 def get_hugepage_size():
35 """Return the size of the configured hugepages
37 hugepage_size_re = re.compile(r'^Hugepagesize:\s+(?P<size_hp>\d+)\s+kB',
39 with open('/proc/meminfo', 'r') as fh:
42 match = hugepage_size_re.search(line)
44 _LOGGER.info('Hugepages size: %s', match.group('size_hp'))
45 return int(match.group('size_hp'))
47 _LOGGER.error('Could not parse for hugepage size')
52 def allocate_hugepages():
53 """Allocate hugepages on the fly
55 hp_size = get_hugepage_size()
58 nr_hp = int(math.ceil(settings.getValue('HUGEPAGE_RAM_ALLOCATION')/hp_size))
59 _LOGGER.info('Will allocate %s hugepages.', nr_hp)
61 nr_hugepages = 'vm.nr_hugepages=' + str(nr_hp)
63 tasks.run_task(['sudo', 'sysctl', nr_hugepages],
64 _LOGGER, 'Trying to allocate hugepages..', True)
65 except subprocess.CalledProcessError:
66 _LOGGER.error('Unable to allocate hugepages.')
71 _LOGGER.error('Division by 0 will be supported in next release')
75 def is_hugepage_available():
76 """Check if hugepages are available on the system.
78 hugepage_re = re.compile(r'^HugePages_Free:\s+(?P<num_hp>\d+)$')
81 with open('/proc/meminfo') as mem_file:
82 mem_info = mem_file.readlines()
84 # first check if module is loaded
86 result = hugepage_re.match(line)
90 num_huge = result.group('num_hp')
92 _LOGGER.info('No free hugepages.')
93 if not allocate_hugepages():
96 _LOGGER.info('Found \'%s\' free hugepage(s).', num_huge)
102 def is_hugepage_mounted():
103 """Check if hugepages are mounted.
105 output = subprocess.check_output(['mount'], shell=True)
106 my_encoding = locale.getdefaultlocale()[1]
107 for line in output.decode(my_encoding).split('\n'):
108 if 'hugetlbfs' in line:
114 def mount_hugepages():
115 """Ensure hugepages are mounted.
117 if not is_hugepage_available():
120 if is_hugepage_mounted():
123 if not os.path.exists(settings.getValue('HUGEPAGE_DIR')):
124 tasks.run_task(['sudo', 'mkdir', settings.getValue('HUGEPAGE_DIR')], _LOGGER,
125 'Creating directory ' + settings.getValue('HUGEPAGE_DIR'), True)
127 tasks.run_task(['sudo', 'mount', '-t', 'hugetlbfs', 'nodev',
128 settings.getValue('HUGEPAGE_DIR')],
129 _LOGGER, 'Mounting hugepages...', True)
130 except subprocess.CalledProcessError:
131 _LOGGER.error('Unable to mount hugepages.')
134 def umount_hugepages():
135 """Ensure hugepages are unmounted.
137 if not is_hugepage_mounted():
141 tasks.run_task(['sudo', 'umount', settings.getValue('HUGEPAGE_DIR')],
142 _LOGGER, 'Unmounting hugepages...', True)
143 except subprocess.CalledProcessError:
144 _LOGGER.error('Unable to umount hugepages.')