1
# -*- coding: utf-8 -*-
3
# Copyright 2014-2015 Canonical Limited.
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
18
from charmhelpers.core import fstab
19
from charmhelpers.core import sysctl
20
from charmhelpers.core.host import (
26
from charmhelpers.core.strutils import bytes_from_string
27
from subprocess import check_output
30
def hugepage_support(user, group='hugetlb', nr_hugepages=256,
31
max_map_count=65536, mnt_point='/run/hugepages/kvm',
32
pagesize='2MB', mount=True, set_shmmax=False):
33
"""Enable hugepages on system.
36
user (str) -- Username to allow access to hugepages to
37
group (str) -- Group name to own hugepages
38
nr_hugepages (int) -- Number of pages to reserve
39
max_map_count (int) -- Number of Virtual Memory Areas a process can own
40
mnt_point (str) -- Directory to mount hugepages on
41
pagesize (str) -- Size of hugepages
42
mount (bool) -- Whether to Mount hugepages
44
group_info = add_group(group)
45
gid = group_info.gr_gid
46
add_user_to_group(user, group)
47
if max_map_count < 2 * nr_hugepages:
48
max_map_count = 2 * nr_hugepages
50
'vm.nr_hugepages': nr_hugepages,
51
'vm.max_map_count': max_map_count,
52
'vm.hugetlb_shm_group': gid,
55
shmmax_current = int(check_output(['sysctl', '-n', 'kernel.shmmax']))
56
shmmax_minsize = bytes_from_string(pagesize) * nr_hugepages
57
if shmmax_minsize > shmmax_current:
58
sysctl_settings['kernel.shmmax'] = shmmax_minsize
59
sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf')
60
mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False)
61
lfstab = fstab.Fstab()
62
fstab_entry = lfstab.get_entry_by_attr('mountpoint', mnt_point)
64
lfstab.remove_entry(fstab_entry)
65
entry = lfstab.Entry('nodev', mnt_point, 'hugetlbfs',
66
'mode=1770,gid={},pagesize={}'.format(gid, pagesize), 0, 0)
67
lfstab.add_entry(entry)
69
fstab_mount(mnt_point)