Merge "bugfix: Fix creation of vsperfenv in Ubuntu"
[vswitchperf.git] / tools / hugepages.py
1 # Copyright 2015-2016 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Automation of hugepages management
16 """
17
18 import os
19 import re
20 import subprocess
21 import logging
22 import locale
23
24 from tools import tasks
25 from conf import settings
26
27 _LOGGER = logging.getLogger(__name__)
28
29 #
30 # hugepage management
31 #
32
33
34 def is_hugepage_available():
35     """Check if hugepages are available on the system.
36     """
37     hugepage_re = re.compile(r'^HugePages_Free:\s+(?P<num_hp>\d+)$')
38
39     # read in meminfo
40     with open('/proc/meminfo') as mem_file:
41         mem_info = mem_file.readlines()
42
43     # first check if module is loaded
44     for line in mem_info:
45         result = hugepage_re.match(line)
46         if not result:
47             continue
48
49         num_huge = result.group('num_hp')
50         if not num_huge:
51             _LOGGER.info('No free hugepages.')
52         else:
53             _LOGGER.info('Found \'%s\' free hugepage(s).', num_huge)
54         return True
55
56     return False
57
58
59 def is_hugepage_mounted():
60     """Check if hugepages are mounted.
61     """
62     output = subprocess.check_output(['mount'], shell=True)
63     my_encoding = locale.getdefaultlocale()[1]
64     for line in output.decode(my_encoding).split('\n'):
65         if 'hugetlbfs' in line:
66             return True
67
68     return False
69
70
71 def mount_hugepages():
72     """Ensure hugepages are mounted.
73     """
74     if not is_hugepage_available():
75         return
76
77     if is_hugepage_mounted():
78         return
79
80     if not os.path.exists(settings.getValue('HUGEPAGE_DIR')):
81         tasks.run_task(['sudo', 'mkdir', settings.getValue('HUGEPAGE_DIR')], _LOGGER,
82                        'Creating directory ' + settings.getValue('HUGEPAGE_DIR'), True)
83     try:
84         tasks.run_task(['sudo', 'mount', '-t', 'hugetlbfs', 'nodev',
85                         settings.getValue('HUGEPAGE_DIR')],
86                        _LOGGER, 'Mounting hugepages...', True)
87     except subprocess.CalledProcessError:
88         _LOGGER.error('Unable to mount hugepages.')
89
90
91 def umount_hugepages():
92     """Ensure hugepages are unmounted.
93     """
94     if not is_hugepage_mounted():
95         return
96
97     try:
98         tasks.run_task(['sudo', 'umount', settings.getValue('HUGEPAGE_DIR')],
99                        _LOGGER, 'Unmounting hugepages...', True)
100     except subprocess.CalledProcessError:
101         _LOGGER.error('Unable to umount hugepages.')
102
103