John DeNisco | 68b0ee3 | 2017-09-27 16:35:23 -0400 | [diff] [blame] | 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 | |
Paul Vinciguerra | 339bc6b | 2018-12-19 02:05:25 -0800 | [diff] [blame] | 14 | from __future__ import print_function |
| 15 | |
John DeNisco | 68b0ee3 | 2017-09-27 16:35:23 -0400 | [diff] [blame] | 16 | """VPP Huge Page Utilities""" |
| 17 | |
| 18 | import re |
| 19 | |
| 20 | from vpplib.VPPUtil import VPPUtil |
| 21 | |
| 22 | # VPP Huge page File |
| 23 | DEFAULT_VPP_HUGE_PAGE_CONFIG_FILENAME = "/etc/vpp/80-vpp.conf" |
| 24 | VPP_HUGEPAGE_CONFIG = """ |
| 25 | vm.nr_hugepages={nr_hugepages} |
| 26 | vm.max_map_count={max_map_count} |
| 27 | vm.hugetlb_shm_group=0 |
| 28 | kernel.shmmax={shmmax} |
| 29 | """ |
| 30 | |
| 31 | |
| 32 | class VppHugePageUtil(object): |
| 33 | """ |
| 34 | Huge Page Utilities |
| 35 | """ |
| 36 | def hugepages_dryrun_apply(self): |
| 37 | """ |
| 38 | Apply the huge page configuration |
| 39 | |
| 40 | """ |
| 41 | |
| 42 | node = self._node |
| 43 | hugepages = node['hugepages'] |
| 44 | |
| 45 | vpp_hugepage_config = VPP_HUGEPAGE_CONFIG.format( |
| 46 | nr_hugepages=hugepages['total'], |
| 47 | max_map_count=hugepages['max_map_count'], |
| 48 | shmmax=hugepages['shmax']) |
| 49 | |
| 50 | rootdir = node['rootdir'] |
| 51 | filename = rootdir + node['hugepages']['hugepage_config_file'] |
| 52 | |
| 53 | cmd = 'echo "{0}" | sudo tee {1}'.\ |
| 54 | format(vpp_hugepage_config, filename) |
| 55 | (ret, stdout, stderr) = VPPUtil.exec_command(cmd) |
| 56 | if ret != 0: |
| 57 | raise RuntimeError('{} failed on node {} {} {}'. |
| 58 | format(cmd, node['host'], |
| 59 | stdout, stderr)) |
| 60 | |
| 61 | def get_actual_huge_pages(self): |
| 62 | """ |
| 63 | Get the current huge page configuration |
| 64 | |
| 65 | :returns the hugepage total, hugepage free, hugepage size, |
| 66 | total memory, and total memory free |
| 67 | :rtype: tuple |
| 68 | """ |
| 69 | |
| 70 | # Get the memory information using /proc/meminfo |
| 71 | cmd = 'sudo cat /proc/meminfo' |
| 72 | (ret, stdout, stderr) = VPPUtil.exec_command(cmd) |
| 73 | if ret != 0: |
| 74 | raise RuntimeError( |
| 75 | '{} failed on node {} {} {}'.format( |
| 76 | cmd, self._node['host'], |
| 77 | stdout, stderr)) |
| 78 | |
| 79 | total = re.findall(r'HugePages_Total:\s+\w+', stdout) |
| 80 | free = re.findall(r'HugePages_Free:\s+\w+', stdout) |
| 81 | size = re.findall(r'Hugepagesize:\s+\w+\s+\w+', stdout) |
| 82 | memtotal = re.findall(r'MemTotal:\s+\w+\s+\w+', stdout) |
| 83 | memfree = re.findall(r'MemFree:\s+\w+\s+\w+', stdout) |
| 84 | |
| 85 | total = total[0].split(':')[1].lstrip() |
| 86 | free = free[0].split(':')[1].lstrip() |
| 87 | size = size[0].split(':')[1].lstrip() |
| 88 | memtotal = memtotal[0].split(':')[1].lstrip() |
| 89 | memfree = memfree[0].split(':')[1].lstrip() |
| 90 | return total, free, size, memtotal, memfree |
| 91 | |
| 92 | def show_huge_pages(self): |
| 93 | """ |
| 94 | Print the current huge page configuration |
| 95 | |
| 96 | """ |
| 97 | |
| 98 | node = self._node |
| 99 | hugepages = node['hugepages'] |
Paul Vinciguerra | 339bc6b | 2018-12-19 02:05:25 -0800 | [diff] [blame] | 100 | print (" {:30}: {}".format("Total System Memory", |
| 101 | hugepages['memtotal'])) |
| 102 | print (" {:30}: {}".format("Total Free Memory", |
| 103 | hugepages['memfree'])) |
| 104 | print (" {:30}: {}".format("Actual Huge Page Total", |
| 105 | hugepages['actual_total'])) |
| 106 | print (" {:30}: {}".format("Configured Huge Page Total", |
| 107 | hugepages['total'])) |
| 108 | print (" {:30}: {}".format("Huge Pages Free", hugepages['free'])) |
| 109 | print (" {:30}: {}".format("Huge Page Size", hugepages['size'])) |
John DeNisco | 68b0ee3 | 2017-09-27 16:35:23 -0400 | [diff] [blame] | 110 | |
| 111 | def get_huge_page_config(self): |
| 112 | """ |
| 113 | Returns the huge page config. |
| 114 | |
| 115 | :returns: The map max count and shmmax |
| 116 | """ |
| 117 | |
| 118 | total = self._node['hugepages']['total'] |
| 119 | max_map_count = int(total) * 2 + 1024 |
| 120 | shmmax = int(total) * 2 * 1024 * 1024 |
| 121 | return max_map_count, shmmax |
| 122 | |
| 123 | def __init__(self, node): |
| 124 | self._node = node |