sh 修改OSX上的TCP设置以获得更好的吞吐量和更低的延迟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 修改OSX上的TCP设置以获得更好的吞吐量和更低的延迟相关的知识,希望对你有一定的参考价值。

#!/bin/sh
#
# Copyright 2009-2016 RackTop Systems Inc. and/or its affiliates.
# http://www.racktopsystems.com
#
# The contents of this file are subject to the terms of the RackTop Commercial
# License ("RTCL"). You may not use, redistribute, or modify this file in source
# or binary forms except in compliance with the RTCL. You can obtain a copy at
# http://racktopsystems.com/legal/rtcl.txt.
#
# This script is meant to be used only on OSX systems, and must be executed with
# sudo, to obtain superuser provs., or su'd to root first.
#
# DO NOT run this script over and over, because same, or slightly different
# values will be added to /etc/sysctl.conf file.
# This should be run once, and the created, or updated file will contain proper
# settings. There is no checking that these keys don't already exist in
# /etc/sysctl.conf. Please, do make sure that there are no duplicate keys in the
# config file. Otherwise keep latest value and delete earlier entry.
# This script appends entries, so NEW entries will be last.
#
# If you have any questions at all, please contact support for racktop systems,
# or contact Sam @ szaydel@racktopsystems.com.
#
DEBUG=0

COUNT=5 # More samples reduces sample error, don't do less than 5.
LINK_TPUT=125000000 # Assumes a 1GbE link from desktop/laptop.
MAX_BUF=5242880 # Maximim buffer size of 10Mb
MIN_BUF=1048576 # Minimum buffer size is 1Mb
SYSCTL_CONF=/etc/sysctl.conf
SYSCTL_CMD=/usr/sbin/sysctl

[[ "${DEBUG}" -gt 0 ]] && set -o xtrace

function backup_sysctl_conf {
  [[ "${DEBUG}" -gt 0 ]] && set -o xtrace
  if [[ -f "${SYSCTL_CONF}" ]]; then
    cp -p "${SYSCTL_CONF}" "${SYSCTL_CONF}.rt-backup"
    return $?
  fi
  return 0
}

function calc_sendrcv_buf {
  [[ "${DEBUG}" -gt 0 ]] && set -o xtrace
  latency=`ping -c "${COUNT}" -W 1 localhost \
    | awk 'BEGIN  { found = 0; }
    $1 == "round-trip" { found = 1; times = $4; }
      END { if (found == 1) {
          split(times, parts, "/");
          avg_lat = parts[2];
        } else {
          avg_lat = 0.01; # Default to this value.
        }
      printf("%.4f\n", avg_lat); # Averaged latency value from samples.
  }'`

  # This is the bandwidth-delay product.
  # More info: https://en.wikipedia.org/wiki/Bandwidth-delay_product
  bdp=`/usr/bin/python -c "print "'"%d\n"'" % (${LINK_TPUT} * ${latency})"`

  # Determine correct buffer size for the send and receive buffers.
  # They are likely undersized for maximum throughput capability of system.
  if [[ "${bdp}" -lt "${MIN_BUF}" || "${bdp}" -eq "${MIN_BUF}" ]]; then
    newbufsz="${MIN_BUF}"
  elif [[ "${bdp}" -gt "${MAX_BUF}" || "${bdp}" -eq "${MAX_BUF}" ]]; then
    newbufsz="${MAX_BUF}"
  else
    if [[ "${bdp}" -lt $(( MIN_BUF * 2 )) ]]; then
      newbufsz=$(( MIN_BUF * 2 ))
    elif [[ "${bdp}" -gt $(( MIN_BUF * 2 )) &&
          "${bdp}" -lt $(( MIN_BUF * 4 )) ]]; then
      newbufsz=$(( MIN_BUF * 4 ))
    elif [[ "${bdp}" -gt $(( MIN_BUF * 4 )) &&
          "${bdp}" -lt $(( MIN_BUF * 8 )) ]]; then
      newbufsz=$(( MIN_BUF * 8 ))
    fi
  fi

  echo "${newbufsz}"
}

backup_sysctl_conf || { printf "Error: failed to backup ${SYSCTL_CONF}" ;
  exit 1 ; }

bufsz=`calc_sendrcv_buf`
echo "# Start modifications recommended by RackTop Systems" >> "${SYSCTL_CONF}"
echo kern.ipc.maxsockbuf=16777216 >> "${SYSCTL_CONF}"
echo net.inet.tcp.sendspace="${bufsz}" >> "${SYSCTL_CONF}"
echo net.inet.tcp.recvspace="${bufsz}" >> "${SYSCTL_CONF}"
echo net.inet.tcp.delayed_ack=2 >> "${SYSCTL_CONF}"
echo net.inet.raw.maxdgram=16384 >> "${SYSCTL_CONF}"
echo net.inet.raw.recvspace=16384 >> "${SYSCTL_CONF}"
echo net.inet.tcp.slowstart_flightsize=20 >> "${SYSCTL_CONF}"
echo net.inet.tcp.local_slowstart_flightsize=20 >> "${SYSCTL_CONF}"
echo net.inet.tcp.autorcvbufmax=2097152 >> "${SYSCTL_CONF}"
echo net.inet.tcp.autosndbufmax=2097152 >> "${SYSCTL_CONF}"

while read -r tunable; do
  if [[ "${DEBUG}" -gt 0 ]]; then
    echo "${SYSCTL_CMD}" -w "${tunable}"
  else
    "${SYSCTL_CMD}" -w "${tunable}"
  fi
done < "${SYSCTL_CONF}"
#!/bin/sh
#
# Copyright 2009-2018 RackTop Systems Inc. and/or its affiliates.
# http://www.racktopsystems.com
#
# The contents of this file are subject to the terms of the RackTop Commercial
# License ("RTCL"). You may not use, redistribute, or modify this file in source
# or binary forms except in compliance with the RTCL. You can obtain a copy at
# http://racktopsystems.com/legal/rtcl.txt.
#
# This script is meant to be used only on OSX systems, and must be executed with
# sudo, to obtain superuser provs., or su'd to root first.
#
# DO NOT run this script over and over, because same, or slightly different
# values will be added to /etc/sysctl.conf file.
# This should be run once, and the created, or updated file will contain proper
# settings. There is no checking that these keys don't already exist in
# /etc/sysctl.conf. Please, do make sure that there are no duplicate keys in the
# config file. Otherwise keep latest value and delete earlier entry.
# This script appends entries, so NEW entries will be last.
#
# If you have any questions at all, please contact support for racktop systems,
# or contact Sam @ szaydel@racktopsystems.com.
#
DEBUG=0

COUNT=5 # More samples reduces sample error, don't do less than 5.
# LINK_TPUT=125000000 # Assumes a 1GbE link from desktop/laptop.
LINK_TPUT=1250000000 # Assumes a 10GbE link from desktop/laptop.
MAX_BUF=5242880 # Maximim buffer size of 10Mb
MIN_BUF=1048576 # Minimum buffer size is 1Mb
SYSCTL_CONF=./etc/sysctl.conf
SYSCTL_CMD=/usr/sbin/sysctl

[[ "${DEBUG}" -gt 0 ]] && set -o xtrace

function backup_sysctl_conf {
  [[ "${DEBUG}" -gt 0 ]] && set -o xtrace
  if [[ -f "${SYSCTL_CONF}" ]]; then
    cp -p "${SYSCTL_CONF}" "${SYSCTL_CONF}.rt-backup"
    return $?
  fi
  return 0
}

function calc_sendrcv_buf {
  [[ "${DEBUG}" -gt 0 ]] && set -o xtrace
  latency=`ping -c "${COUNT}" -W 1 localhost \
    | awk 'BEGIN  { found = 0; }
    $1 == "round-trip" { found = 1; times = $4; }
      END { if (found == 1) {
          split(times, parts, "/");
          avg_lat = parts[2];
        } else {
          avg_lat = 0.01; # Default to this value.
        }
      printf("%.4f\n", avg_lat); # Averaged latency value from samples.
  }'`

  # This is the bandwidth-delay product.
  # More info: https://en.wikipedia.org/wiki/Bandwidth-delay_product
  bdp=`/usr/bin/python -c "print "'"%d\n"'" % (${LINK_TPUT} * ${latency})"`

  # Determine correct buffer size for the send and receive buffers.
  # They are likely undersized for maximum throughput capability of system.
  if [[ "${bdp}" -lt "${MIN_BUF}" || "${bdp}" -eq "${MIN_BUF}" ]]; then
    newbufsz="${MIN_BUF}"
  elif [[ "${bdp}" -gt "${MAX_BUF}" || "${bdp}" -eq "${MAX_BUF}" ]]; then
    newbufsz="${MAX_BUF}"
  else
    if [[ "${bdp}" -lt $(( MIN_BUF * 2 )) ]]; then
      newbufsz=$(( MIN_BUF * 2 ))
    elif [[ "${bdp}" -gt $(( MIN_BUF * 2 )) &&
          "${bdp}" -lt $(( MIN_BUF * 4 )) ]]; then
      newbufsz=$(( MIN_BUF * 4 ))
    elif [[ "${bdp}" -gt $(( MIN_BUF * 4 )) &&
          "${bdp}" -lt $(( MIN_BUF * 8 )) ]]; then
      newbufsz=$(( MIN_BUF * 8 ))
    fi
  fi

  echo "${newbufsz}"
}

backup_sysctl_conf || { printf "Error: failed to backup ${SYSCTL_CONF}" ;
  exit 1 ; }

bufsz=`calc_sendrcv_buf`
echo "# Start modifications recommended by RackTop Systems" >> "${SYSCTL_CONF}"
echo kern.ipc.maxsockbuf=16777216 >> "${SYSCTL_CONF}"
echo net.inet.tcp.sendspace="${bufsz}" >> "${SYSCTL_CONF}"
echo net.inet.tcp.recvspace="${bufsz}" >> "${SYSCTL_CONF}"
echo net.inet.tcp.delayed_ack=2 >> "${SYSCTL_CONF}"
echo net.inet.raw.maxdgram=16384 >> "${SYSCTL_CONF}"
echo net.inet.raw.recvspace=16384 >> "${SYSCTL_CONF}"
echo net.inet.tcp.slowstart_flightsize=20 >> "${SYSCTL_CONF}"
echo net.inet.tcp.local_slowstart_flightsize=20 >> "${SYSCTL_CONF}"
echo net.inet.tcp.autorcvbufmax=2097152 >> "${SYSCTL_CONF}"
echo net.inet.tcp.autosndbufmax=2097152 >> "${SYSCTL_CONF}"

loop=0
while read -r tunable; do
  if [ $loop -eq 0 ]; then # Skip first line with comments
    ((loop += 1))
    continue
  fi

  if [[ "${DEBUG}" -gt 0 ]]; then
    echo "${SYSCTL_CMD}" -w "${tunable}"
  else
    "${SYSCTL_CMD}" -w "${tunable}"
  fi
done < "${SYSCTL_CONF}"

以上是关于sh 修改OSX上的TCP设置以获得更好的吞吐量和更低的延迟的主要内容,如果未能解决你的问题,请参考以下文章

grpc 客户端 python:如何创建 grpc 客户端连接池以获得更好的吞吐量?

如何使用 C 对 TCP 和 UDP 进行基准测试?

多插座连接的优点

sh OSX上的hostname命令

我的 setInterval 函数减慢了我的代码速度——我怎样才能修改它以获得更好的性能?

git pull 上的 OSX 10.10 yosemite beta:git-sh-setup:没有这样的文件或目录