vom: Add support for redirect contracts in gbp
[vpp.git] / extras / vpp_config / vpplib / VppHugePageUtil.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """VPP Huge Page Utilities"""
15
16 import re
17
18 from vpplib.VPPUtil import VPPUtil
19
20 # VPP Huge page File
21 DEFAULT_VPP_HUGE_PAGE_CONFIG_FILENAME = "/etc/vpp/80-vpp.conf"
22 VPP_HUGEPAGE_CONFIG = """
23 vm.nr_hugepages={nr_hugepages}
24 vm.max_map_count={max_map_count}
25 vm.hugetlb_shm_group=0
26 kernel.shmmax={shmmax}
27 """
28
29
30 class VppHugePageUtil(object):
31     """
32     Huge Page Utilities
33     """
34     def hugepages_dryrun_apply(self):
35         """
36         Apply the huge page configuration
37
38         """
39
40         node = self._node
41         hugepages = node['hugepages']
42
43         vpp_hugepage_config = VPP_HUGEPAGE_CONFIG.format(
44             nr_hugepages=hugepages['total'],
45             max_map_count=hugepages['max_map_count'],
46             shmmax=hugepages['shmax'])
47
48         rootdir = node['rootdir']
49         filename = rootdir + node['hugepages']['hugepage_config_file']
50
51         cmd = 'echo "{0}" | sudo tee {1}'.\
52             format(vpp_hugepage_config, filename)
53         (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
54         if ret != 0:
55             raise RuntimeError('{} failed on node {} {} {}'.
56                                format(cmd, node['host'],
57                                       stdout, stderr))
58
59     def get_actual_huge_pages(self):
60         """
61         Get the current huge page configuration
62
63         :returns the hugepage total, hugepage free, hugepage size,
64         total memory, and total memory free
65         :rtype: tuple
66         """
67
68         # Get the memory information using /proc/meminfo
69         cmd = 'sudo cat /proc/meminfo'
70         (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
71         if ret != 0:
72             raise RuntimeError(
73                 '{} failed on node {} {} {}'.format(
74                     cmd, self._node['host'],
75                     stdout, stderr))
76
77         total = re.findall(r'HugePages_Total:\s+\w+', stdout)
78         free = re.findall(r'HugePages_Free:\s+\w+', stdout)
79         size = re.findall(r'Hugepagesize:\s+\w+\s+\w+', stdout)
80         memtotal = re.findall(r'MemTotal:\s+\w+\s+\w+', stdout)
81         memfree = re.findall(r'MemFree:\s+\w+\s+\w+', stdout)
82
83         total = total[0].split(':')[1].lstrip()
84         free = free[0].split(':')[1].lstrip()
85         size = size[0].split(':')[1].lstrip()
86         memtotal = memtotal[0].split(':')[1].lstrip()
87         memfree = memfree[0].split(':')[1].lstrip()
88         return total, free, size, memtotal, memfree
89
90     def show_huge_pages(self):
91         """
92         Print the current huge page configuration
93
94         """
95
96         node = self._node
97         hugepages = node['hugepages']
98         print "  {:30}: {}".format("Total System Memory",
99                                    hugepages['memtotal'])
100         print "  {:30}: {}".format("Total Free Memory",
101                                    hugepages['memfree'])
102         print "  {:30}: {}".format("Actual Huge Page Total",
103                                    hugepages['actual_total'])
104         print "  {:30}: {}".format("Configured Huge Page Total",
105                                    hugepages['total'])
106         print "  {:30}: {}".format("Huge Pages Free", hugepages['free'])
107         print "  {:30}: {}".format("Huge Page Size", hugepages['size'])
108
109     def get_huge_page_config(self):
110         """
111         Returns the huge page config.
112
113         :returns: The map max count and shmmax
114         """
115
116         total = self._node['hugepages']['total']
117         max_map_count = int(total) * 2 + 1024
118         shmmax = int(total) * 2 * 1024 * 1024
119         return max_map_count, shmmax
120
121     def __init__(self, node):
122         self._node = node