1
# -*- coding: utf-8 -*-
3
# Copyright 2014-2015 Canonical Limited.
5
# This file is part of charm-helpers.
7
# charm-helpers is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License version 3 as
9
# published by the Free Software Foundation.
11
# charm-helpers is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Lesser General Public License for more details.
16
# You should have received a copy of the GNU Lesser General Public License
17
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
20
from charmhelpers.core import fstab
21
from charmhelpers.core import sysctl
22
from charmhelpers.core.host import (
28
from charmhelpers.core.strutils import bytes_from_string
29
from subprocess import check_output
32
def hugepage_support(user, group='hugetlb', nr_hugepages=256,
33
max_map_count=65536, mnt_point='/run/hugepages/kvm',
34
pagesize='2MB', mount=True, set_shmmax=False):
35
"""Enable hugepages on system.
38
user (str) -- Username to allow access to hugepages to
39
group (str) -- Group name to own hugepages
40
nr_hugepages (int) -- Number of pages to reserve
41
max_map_count (int) -- Number of Virtual Memory Areas a process can own
42
mnt_point (str) -- Directory to mount hugepages on
43
pagesize (str) -- Size of hugepages
44
mount (bool) -- Whether to Mount hugepages
46
group_info = add_group(group)
47
gid = group_info.gr_gid
48
add_user_to_group(user, group)
49
if max_map_count < 2 * nr_hugepages:
50
max_map_count = 2 * nr_hugepages
52
'vm.nr_hugepages': nr_hugepages,
53
'vm.max_map_count': max_map_count,
54
'vm.hugetlb_shm_group': gid,
57
shmmax_current = int(check_output(['sysctl', '-n', 'kernel.shmmax']))
58
shmmax_minsize = bytes_from_string(pagesize) * nr_hugepages
59
if shmmax_minsize > shmmax_current:
60
sysctl_settings['kernel.shmmax'] = shmmax_minsize
61
sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf')
62
mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False)
63
lfstab = fstab.Fstab()
64
fstab_entry = lfstab.get_entry_by_attr('mountpoint', mnt_point)
66
lfstab.remove_entry(fstab_entry)
67
entry = lfstab.Entry('nodev', mnt_point, 'hugetlbfs',
68
'mode=1770,gid={},pagesize={}'.format(gid, pagesize), 0, 0)
69
lfstab.add_entry(entry)
71
fstab_mount(mnt_point)