blob: 48991090f04d5a2390aa86764aa234e27e3fa72b [file] [log] [blame]
John DeNisco68b0ee32017-09-27 16:35:23 -04001# 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
Paul Vinciguerra339bc6b2018-12-19 02:05:25 -080014from __future__ import print_function
15
John DeNisco68b0ee32017-09-27 16:35:23 -040016"""VPP Huge Page Utilities"""
17
18import re
19
20from vpplib.VPPUtil import VPPUtil
21
22# VPP Huge page File
23DEFAULT_VPP_HUGE_PAGE_CONFIG_FILENAME = "/etc/vpp/80-vpp.conf"
24VPP_HUGEPAGE_CONFIG = """
25vm.nr_hugepages={nr_hugepages}
26vm.max_map_count={max_map_count}
27vm.hugetlb_shm_group=0
28kernel.shmmax={shmmax}
29"""
30
31
32class VppHugePageUtil(object):
33 """
34 Huge Page Utilities
35 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020036
John DeNisco68b0ee32017-09-27 16:35:23 -040037 def hugepages_dryrun_apply(self):
38 """
39 Apply the huge page configuration
40
41 """
42
43 node = self._node
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020044 hugepages = node["hugepages"]
John DeNisco68b0ee32017-09-27 16:35:23 -040045
46 vpp_hugepage_config = VPP_HUGEPAGE_CONFIG.format(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020047 nr_hugepages=hugepages["total"],
48 max_map_count=hugepages["max_map_count"],
49 shmmax=hugepages["shmax"],
50 )
John DeNisco68b0ee32017-09-27 16:35:23 -040051
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052 rootdir = node["rootdir"]
53 filename = rootdir + node["hugepages"]["hugepage_config_file"]
John DeNisco68b0ee32017-09-27 16:35:23 -040054
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020055 cmd = 'echo "{0}" | sudo tee {1}'.format(vpp_hugepage_config, filename)
John DeNisco68b0ee32017-09-27 16:35:23 -040056 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
57 if ret != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020058 raise RuntimeError(
59 "{} failed on node {} {} {}".format(cmd, node["host"], stdout, stderr)
60 )
John DeNisco68b0ee32017-09-27 16:35:23 -040061
62 def get_actual_huge_pages(self):
63 """
64 Get the current huge page configuration
65
66 :returns the hugepage total, hugepage free, hugepage size,
67 total memory, and total memory free
68 :rtype: tuple
69 """
70
71 # Get the memory information using /proc/meminfo
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020072 cmd = "sudo cat /proc/meminfo"
John DeNisco68b0ee32017-09-27 16:35:23 -040073 (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
74 if ret != 0:
75 raise RuntimeError(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 "{} failed on node {} {} {}".format(
77 cmd, self._node["host"], stdout, stderr
78 )
79 )
John DeNisco68b0ee32017-09-27 16:35:23 -040080
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 total = re.findall(r"HugePages_Total:\s+\w+", stdout)
82 free = re.findall(r"HugePages_Free:\s+\w+", stdout)
83 size = re.findall(r"Hugepagesize:\s+\w+\s+\w+", stdout)
84 memtotal = re.findall(r"MemTotal:\s+\w+\s+\w+", stdout)
85 memfree = re.findall(r"MemFree:\s+\w+\s+\w+", stdout)
John DeNisco68b0ee32017-09-27 16:35:23 -040086
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020087 total = total[0].split(":")[1].lstrip()
88 free = free[0].split(":")[1].lstrip()
89 size = size[0].split(":")[1].lstrip()
90 memtotal = memtotal[0].split(":")[1].lstrip()
91 memfree = memfree[0].split(":")[1].lstrip()
John DeNisco68b0ee32017-09-27 16:35:23 -040092 return total, free, size, memtotal, memfree
93
94 def show_huge_pages(self):
95 """
96 Print the current huge page configuration
97
98 """
99
100 node = self._node
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200101 hugepages = node["hugepages"]
102 print(" {:30}: {}".format("Total System Memory", hugepages["memtotal"]))
103 print(" {:30}: {}".format("Total Free Memory", hugepages["memfree"]))
104 print(" {:30}: {}".format("Actual Huge Page Total", hugepages["actual_total"]))
105 print(" {:30}: {}".format("Configured Huge Page Total", hugepages["total"]))
106 print(" {:30}: {}".format("Huge Pages Free", hugepages["free"]))
107 print(" {:30}: {}".format("Huge Page Size", hugepages["size"]))
John DeNisco68b0ee32017-09-27 16:35:23 -0400108
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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200116 total = self._node["hugepages"]["total"]
John DeNisco68b0ee32017-09-27 16:35:23 -0400117 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