~cbjchen/charms/trusty/heat/heat-ha-rebased

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-08-19 13:53:16 UTC
  • Revision ID: liam.young@canonical.com-20150819135316-w62fjchlamcur3k0
[gnuoy,trivial] Charmhelper sync (+1'd by mojo)

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
 
 
29
 
 
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):
 
33
    """Enable hugepages on system.
 
34
 
 
35
    Args:
 
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
 
43
    """
 
44
    group_info = add_group(group)
 
45
    gid = group_info.gr_gid
 
46
    add_user_to_group(user, group)
 
47
    sysctl_settings = {
 
48
        'vm.nr_hugepages': nr_hugepages,
 
49
        'vm.max_map_count': max_map_count,
 
50
        'vm.hugetlb_shm_group': gid,
 
51
    }
 
52
    sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf')
 
53
    mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False)
 
54
    lfstab = fstab.Fstab()
 
55
    fstab_entry = lfstab.get_entry_by_attr('mountpoint', mnt_point)
 
56
    if fstab_entry:
 
57
        lfstab.remove_entry(fstab_entry)
 
58
    entry = lfstab.Entry('nodev', mnt_point, 'hugetlbfs',
 
59
                         'mode=1770,gid={},pagesize={}'.format(gid, pagesize), 0, 0)
 
60
    lfstab.add_entry(entry)
 
61
    if mount:
 
62
        fstab_mount(mnt_point)