~openstack-charmers-archive/charms/trusty/neutron-gateway/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/core/hugepage.py

  • Committer: James Page
  • Date: 2015-10-22 13:23:58 UTC
  • Revision ID: james.page@ubuntu.com-20151022132358-qin1nvlnqn4aezaz
Tags: 15.10
15.10 Charm release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright 2014-2015 Canonical Limited.
 
4
#
 
5
# This file is part of charm-helpers.
 
6
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
 
 
19
import yaml
 
20
from charmhelpers.core import fstab
 
21
from charmhelpers.core import sysctl
 
22
from charmhelpers.core.host import (
 
23
    add_group,
 
24
    add_user_to_group,
 
25
    fstab_mount,
 
26
    mkdir,
 
27
)
 
28
from charmhelpers.core.strutils import bytes_from_string
 
29
from subprocess import check_output
 
30
 
 
31
 
 
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.
 
36
 
 
37
    Args:
 
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
 
45
    """
 
46
    group_info = add_group(group)
 
47
    gid = group_info.gr_gid
 
48
    add_user_to_group(user, group)
 
49
    sysctl_settings = {
 
50
        'vm.nr_hugepages': nr_hugepages,
 
51
        'vm.max_map_count': max_map_count,
 
52
        'vm.hugetlb_shm_group': gid,
 
53
    }
 
54
    if set_shmmax:
 
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)
 
63
    if fstab_entry:
 
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)
 
68
    if mount:
 
69
        fstab_mount(mnt_point)