test-definitions/tree/master/auto-test/libhugetlbfs

#!/bin/sh

# shellcheck disable=SC1091
. ../../lib/sh-test-lib
#给变量赋初值
OUTPUT="$(pwd)/output"
RESULT_FILE="${OUTPUT}/result.txt"
RESULT_LOG="${OUTPUT}/result_log.txt"
TMP_LOG="${OUTPUT}/tmp_log.txt"
TEST_PASS_LOG="${OUTPUT}/test_pass_log.txt"
TEST_FAIL_LOG="${OUTPUT}/test_fail_log.txt"
TEST_SKIP_LOG="${OUTPUT}/test_skip_log.txt"
CWD=""

WORD_SIZE="64"
VERSION="2.20"
#定义一个函数说明这个脚本该如何使用
usage() {
    echo "Usage: $0 [-b <4|64>] [-s <true>] [-v <libhugetlbfs-version>]" 1>&2
    exit 1
}
#parse 位置参数
while getopts "b:s:v:" o; do
  case "$o" in
    b) WORD_SIZE="${OPTARG}" ;;
    s) SKIP_INSTALL="${OPTARG}" ;;
    v) VERSION="${OPTARG}" ;;
    *) usage ;;
  esac
done
#定义函数,基本是通过sed来替换插入到文件中
parse_output() {
    # Avoid results summary lines start with "*"
    sed -i -e 's/\//-/g' "${TMP_LOG}"
    # shellcheck disable=SC2063
    grep -v "*"  "${TMP_LOG}" | tee -a "${RESULT_LOG}"
    # Parse each type of results
    egrep "PASS" "${RESULT_LOG}" | tee -a "${TEST_PASS_LOG}"
    sed -i -e 's/ (inconclusive)//g' "${TEST_PASS_LOG}"
    sed -i -e 's/(//g' "${TEST_PASS_LOG}"
    sed -i -e 's/)://g' "${TEST_PASS_LOG}"
    sed -i -e 's/://g' "${TEST_PASS_LOG}"
    awk '{for (i=1; i<NF-1; i++) printf $i "-"; print $i " " $NF}' "${TEST_PASS_LOG}" 2>&1 | tee -a "${RESULT_FILE}"
    sed -i -e 's/PASS/pass/g' "${RESULT_FILE}"

    egrep "FAIL" "${RESULT_LOG}" | cut -d: -f 1-2 2>&1 | tee -a "${TEST_FAIL_LOG}"
    sed -i -e 's/ (inconclusive)//g' "${TEST_FAIL_LOG}"
    sed -i -e 's/(//g' "${TEST_FAIL_LOG}"
    sed -i -e 's/)//g' "${TEST_FAIL_LOG}"
    sed -i -e 's/://g' "${TEST_FAIL_LOG}"
    awk '{for (i=1; i<NF; i++) printf $i "-"; print $i " " "fail"}' "${TEST_FAIL_LOG}" 2>&1 | tee -a "${RESULT_FILE}"

    egrep "SKIP" "${RESULT_LOG}" | cut -d: -f 1-2 2>&1 | tee -a "${TEST_SKIP_LOG}"
    egrep "Bad configuration" "${RESULT_LOG}" | cut -d: -f 1-2 2>&1 | tee -a "${TEST_SKIP_LOG}"
    sed -i -e 's/ (inconclusive)//g' "${TEST_SKIP_LOG}"
    sed -i -e 's/(//g' "${TEST_SKIP_LOG}"
    sed -i -e 's/)//g' "${TEST_SKIP_LOG}"
    sed -i -e 's/://g' "${TEST_SKIP_LOG}"
    awk '{for (i=1; i<NF; i++) printf $i "-"; print $i " " "skip"}' "${TEST_SKIP_LOG}" 2>&1 | tee -a "${RESULT_FILE}"

    # Replace "=" with "_" in test case names
    sed -i -e 's/=/_/g' "${RESULT_FILE}"
    # Clean up
    rm -rf "${TMP_LOG}" "${RESULT_LOG}" "${TEST_PASS_LOG}" "${TEST_FAIL_LOG}" "${TEST_SKIP_LOG}"

}

libhugetlbfs_setup() {
    mount_point="/mnt/hugetlb/"
    # Allocate hugepages
    echo 200 > /proc/sys/vm/nr_hugepages
	#umount 掉挂载点
    umount "${mount_point}" > /dev/null 2>&1 || true
	#新建目录,重新mount到新的目录
    mkdir -p "${mount_point}"
    mount -t hugetlbfs "${mount_point}"
}

libhugetlbfs_cleanup() {
	#umount掉挂载点
    umount "${mount_point}" > /dev/null 2>&1 || true
    if [ -n "${CWD}" ]; then
       # shellcheck disable=SC2164
	   #进入当前目录,删除目录
       cd "${CWD}"
       rm -rf libhugetlbfs-"${VERSION}" > /dev/null 2>&1 || true
       rm -rf libhugetlbfs-"${VERSION}".tar.gz > /dev/null 2>&1 || true
    fi
}

libhugetlbfs_build_test() {
    # shellcheck disable=SC2140
    # Upstream tree
#    wget https://github.com/libhugetlbfs/libhugetlbfs/releases/download/"${VERSION}"/libhugetlbfs-"${VERSION}".tar.gz
    #TODO
    # Private tree with CentOS build fix
    # When patch is upstream remove private tree and enable upstream tree
	#下载tar包,解压tar包,并重新make
    wget http://github.com/nareshkamboju/libhugetlbfs/releases/download/"${VERSION}"/libhugetlbfs-"${VERSION}".tar.gz
    CWD=$(pwd)
    tar -xvf libhugetlbfs-"${VERSION}".tar.gz
    # shellcheck disable=SC2164
    cd libhugetlbfs-"${VERSION}"
    make BUILDTYPE=NATIVEONLY
}

libhugetlbfs_run_test() {
    # shellcheck disable=SC2164
    cd tests
    # Run tests
    # Redirect stdout (not stderr)
	#执行脚本开始测试
    ./run_tests.py -b "${WORD_SIZE}" | tee -a "${TMP_LOG}"
    parse_output
}
#不同的发行版安装不同的依赖包
install() {
    dist_name
    # shellcheck disable=SC2154
    case "${dist}" in
      debian|ubuntu)
        pkgs="binutils gcc make python sed tar wget"
        install_deps "${pkgs}" "${SKIP_INSTALL}"
        ;;
      fedora|centos)
        pkgs="binutils gcc glibc-static make python sed tar wget"
        install_deps "${pkgs}" "${SKIP_INSTALL}"
        ;;
    esac
}

# Test run.
#检查是否是root,不是root用户的话,则输出错误日志
! check_root && error_msg "This script must be run as root"
create_out_dir "${OUTPUT}"
# shellcheck disable=SC2164
cd "${OUTPUT}"

info_msg "About to run libhugetlbfs test..."
info_msg "Output directory: ${OUTPUT}"
#从这里看到查询kernel config 有两种方式,一种是/proc/config.gz
if [ -f /proc/config.gz ]
then
CONFIG_HUGETLBFS=$(zcat /proc/config.gz | grep "CONFIG_HUGETLBFS=")
CONFIG_HUGETLB_PAGE=$(zcat /proc/config.gz | grep "CONFIG_HUGETLB_PAGE=")
#另外一种是/boot/config-"$(uname -r)" 
elif [ -f /boot/config-"$(uname -r)" ]
then
KERNEL_CONFIG_FILE="/boot/config-$(uname -r)"
CONFIG_HUGETLBFS=$(grep "CONFIG_HUGETLBFS=" "${KERNEL_CONFIG_FILE}")
CONFIG_HUGETLB_PAGE=$(grep "CONFIG_HUGETLB_PAGE=" "${KERNEL_CONFIG_FILE}")
else
exit_on_skip "libhugetlb-pre-requirements" "Kernel config file not available"
fi

HUGETLBFS=$(grep hugetlbfs /proc/filesystems | awk '{print $2}')

[ "${CONFIG_HUGETLBFS}" = "CONFIG_HUGETLBFS=y" ] && [ "${CONFIG_HUGETLB_PAGE}" = "CONFIG_HUGETLB_PAGE=y" ] && [ "${HUGETLBFS}" = "hugetlbfs" ]
exit_on_skip "libhugetlb-pre-requirements" "Kernel config CONFIG_HUGETLBFS=y and CONFIG_HUGETLB_PAGE=y not enabled"

# Install packages
install

# Setup libhugetlbfs mount point
libhugetlbfs_setup
#找到run_test.py的目录
PRE_BUILD_PATH="$(find /usr/lib*/libhugetlbfs -type f -name run_tests.py)"

if [ -n "${PRE_BUILD_PATH}" ]
then
    echo "pre built libhugetlbfs found on rootfs"
    # shellcheck disable=SC2164
    cd /usr/lib*/libhugetlbfs
else
    # Build libhugetlbfs tests
    libhugetlbfs_build_test
fi

# Run libhugetlbfs tests
libhugetlbfs_run_test

# Unmount libhugetlbfs mount point
libhugetlbfs_cleanup

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值