~stephanpampel/landscape-client-charm/landscape-client-charm

« back to all changes in this revision

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

  • Committer: 🤖 Landscape Builder
  • Author(s): Chad Smith
  • Date: 2017-03-03 23:19:24 UTC
  • mfrom: (62.1.4 landscape-client)
  • Revision ID: _landscape_builder-20170303231924-c0380jqvms8gi7cj
Merge add-apt-repository-retries [f=] [r=landscape-builder,ericsnowcurrently] [a=Chad Smith]
Sync charmhelpers for add_source retries to avoid hook errors on network timeouts.  Because of charmhelpers sync fetch._run_apt_command moved to fetch.ubuntu._run_apt_command and fetch has new dependency on osplatform

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
# 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
 
8
#
 
9
#  http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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.
 
16
 
 
17
import yaml
 
18
from charmhelpers.core import fstab
 
19
from charmhelpers.core import sysctl
 
20
from charmhelpers.core.host import (
 
21
    add_group,
 
22
    add_user_to_group,
 
23
    fstab_mount,
 
24
    mkdir,
 
25
)
 
26
from charmhelpers.core.strutils import bytes_from_string
 
27
from subprocess import check_output
 
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, set_shmmax=False):
 
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
    if max_map_count < 2 * nr_hugepages:
 
48
        max_map_count = 2 * nr_hugepages
 
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)