1 # Copyright 2015-2017 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__)
29 _ALLOCATED_HUGEPAGES = False
35 def get_hugepage_size():
36 """Return the size of the configured hugepages
38 hugepage_size_re = re.compile(r'^Hugepagesize:\s+(?P<size_hp>\d+)\s+kB',
40 with open('/proc/meminfo', 'r') as result_file:
41 data = result_file.readlines()
43 match = hugepage_size_re.search(line)
45 _LOGGER.info('Hugepages size: %s kb', match.group('size_hp'))
46 return int(match.group('size_hp'))
47 _LOGGER.error('Could not parse for hugepage size')
51 def allocate_hugepages():
52 """Allocate hugepages on the fly
54 hp_size = get_hugepage_size()
56 nr_hp = int(math.ceil(settings.getValue('HUGEPAGE_RAM_ALLOCATION')/hp_size))
57 _LOGGER.info('Will allocate %s hugepages.', nr_hp)
59 nr_hugepages = 'vm.nr_hugepages=' + str(nr_hp)
61 tasks.run_task(['sudo', 'sysctl', nr_hugepages],
62 _LOGGER, 'Trying to allocate hugepages..', True)
63 except subprocess.CalledProcessError:
64 _LOGGER.error('Unable to allocate hugepages.')
66 # pylint: disable=global-statement
67 global _ALLOCATED_HUGEPAGES
68 _ALLOCATED_HUGEPAGES = True
72 _LOGGER.error('Division by 0 will be supported in next release')
75 def deallocate_hugepages():
76 """De-allocate hugepages that were allocated on the fly
78 # pylint: disable=global-statement
79 global _ALLOCATED_HUGEPAGES
80 if _ALLOCATED_HUGEPAGES:
81 nr_hugepages = 'vm.nr_hugepages= 0'
83 tasks.run_task(['sudo', 'sysctl', nr_hugepages],
84 _LOGGER, 'Trying to de-allocate hugepages..', True)
85 except subprocess.CalledProcessError:
86 _LOGGER.error('Unable to de-allocate hugepages.')
88 _ALLOCATED_HUGEPAGES = False
92 def get_free_hugepages(socket=None):
93 """Get the free hugepage totals on the system.
95 :param socket: optional socket param to get free hugepages on a socket. To
97 :returns: hugepage amount as int
99 hugepage_free_re = re.compile(r'HugePages_Free:\s+(?P<free_hp>\d+)$')
102 '/sys/devices/system/node/node{}/meminfo'.format(socket)):
103 meminfo_path = '/sys/devices/system/node/node{}/meminfo'.format(
106 _LOGGER.info('No hugepage info found for socket %s', socket)
109 meminfo_path = '/proc/meminfo'
111 with open(meminfo_path, 'r') as result_file:
112 data = result_file.readlines()
114 match = hugepage_free_re.search(line)
116 _LOGGER.info('Hugepages free: %s %s', match.group('free_hp'),
117 'on socket {}'.format(socket) if socket else '')
118 return int(match.group('free_hp'))
119 _LOGGER.info('Could not parse for hugepage size')
123 def is_hugepage_available():
124 """Check if hugepages are configured/available on the system.
126 hugepage_size_re = re.compile(r'^Hugepagesize:\s+(?P<size_hp>\d+)\s+kB',
130 with open('/proc/meminfo') as mem_file:
131 mem_info = mem_file.readlines()
133 # see if the hugepage size is the recommended value
134 for line in mem_info:
135 match_size = hugepage_size_re.match(line)
137 if match_size.group('size_hp') != '1048576':
140 'Hugepages not configured for recommend 1GB size. ',
141 'Currently set at ', match_size.group('size_hp'))
142 num_huge = get_free_hugepages()
144 _LOGGER.info('No free hugepages.')
145 if not allocate_hugepages():
148 _LOGGER.info('Found \'%s\' free hugepage(s).', num_huge)
152 def is_hugepage_mounted():
153 """Check if hugepages are mounted.
155 output = subprocess.check_output(['mount'], shell=True)
156 my_encoding = locale.getdefaultlocale()[1]
157 for line in output.decode(my_encoding).split('\n'):
158 if 'hugetlbfs' in line:
164 def mount_hugepages():
165 """Ensure hugepages are mounted. Raises RuntimeError if no configured
166 hugepages are available.
168 if not is_hugepage_available():
169 raise RuntimeError('No Hugepages configured.')
171 if is_hugepage_mounted():
174 if not os.path.exists(settings.getValue('HUGEPAGE_DIR')):
175 tasks.run_task(['sudo', 'mkdir', settings.getValue('HUGEPAGE_DIR')], _LOGGER,
176 'Creating directory ' + settings.getValue('HUGEPAGE_DIR'), True)
178 tasks.run_task(['sudo', 'mount', '-t', 'hugetlbfs', 'nodev',
179 settings.getValue('HUGEPAGE_DIR')],
180 _LOGGER, 'Mounting hugepages...', True)
181 except subprocess.CalledProcessError:
182 _LOGGER.error('Unable to mount hugepages.')
185 def umount_hugepages():
186 """Ensure hugepages are unmounted.
188 if not is_hugepage_mounted():
192 tasks.run_task(['sudo', 'umount', settings.getValue('HUGEPAGE_DIR')],
193 _LOGGER, 'Unmounting hugepages...', True)
194 except subprocess.CalledProcessError:
195 _LOGGER.error('Unable to umount hugepages.')
197 if not deallocate_hugepages():
198 _LOGGER.error('Unable to deallocate previously allocated hugepages.')