

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 调整您的 EC2 实例的性能
<a name="ec2-instance-performance-tuning"></a>

**注意**  
如果您使用 CloudFormation 模板配置 AWS 资源，则会自动应用这些调整。如果您使用 AMI 或手动创建了 EC2 实例，则必须应用这些性能优化才能实现最可靠的性能。

请记住，应用任何优化后重启您的实例。

**主题**
+ [调整硬件中断和接收队列-影响 CPU 和网络](#tune-hardware-interrupts)
+ [Tune Rx 中断合并-影响网络](#tune-rx-interrupts)
+ [调整 Rx 环形缓冲区-影响网络](#tune-rx-ring-buffer)
+ [调整 CPU C-State-影响 CPU](#tune-cpu-c-state)
+ [保留入口端口-影响网络](#reserve-ingress-ports)
+ [Reboot](#reboot)

## 调整硬件中断和接收队列-影响 CPU 和网络
<a name="tune-hardware-interrupts"></a>

本节配置 systemd、SMP IRQs、接收数据包导向 (RPS) 和接收流控制 (RFS) 的 CPU 内核使用情况。有关基于您正在使用的实例类型的一组推荐设置，请参阅 [附录： interrupt/RPS Tune 的推荐参数](#recommended-parameters)。

1. 将 systemd 进程固定在远离座席 CPU 内核的位置。

1. 将硬件中断请求路由到远离座席 CPU 内核的位置。

1. 配置 RPS 以防止单个网络接口卡的硬件队列成为网络流量的瓶颈。

1. 配置 RFS 以提高 CPU 缓存命中率，从而减少网络延迟。

RPM 提供的 `set_irq_affinity.sh` 脚本为您配置了以上所有功能。添加到 crontab，这样它就会应用于每次启动：

```
echo "@reboot sudo /opt/aws/groundstation/bin/set_irq_affinity.sh '${interrupt_core_list}' '${rps_core_mask}' >> /var/log/user-data.log 2>&1" >>/var/spool/cron/root
```
+ `interrupt_core_list`替换为为内核和操作系统保留的内核，通常是第一和第二个，以及超线程内核对。这不应与上面选择的内核重叠。（例如：“0,1,48,49” 表示超线程、96 个 CPU 的实例）。
+ `rps_core_mask`是一个十六进制位掩码，用于指定 CPUs 应处理传入的数据包，每个数字表示 4。 CPUs此外，还必须从右边开始每 8 个字符用逗号分隔。建议全部允许 CPUs ，让缓存来处理平衡。
  + 要查看每种实例类型的推荐参数列表，请参阅 [附录： interrupt/RPS Tune 的推荐参数](#recommended-parameters)。
+  96-CPU 实例的示例：

  ```
  echo "@reboot sudo /opt/aws/groundstation/bin/set_irq_affinity.sh '0,1,48,49' 'ffffffff,ffffffff,ffffffff' >> /var/log/user-data.log 2>&1" >>/var/spool/cron/root
  ```

## Tune Rx 中断合并-影响网络
<a name="tune-rx-interrupts"></a>

 中断合并有助于防止主机系统中出现过多的中断，并帮助提高网络吞吐量。使用此配置，可以收集数据包，并且每 128 微秒生成一个中断。添加到 crontab，这样它就会应用于每次启动：

```
echo "@reboot sudo ethtool -C ${interface} rx-usecs 128 tx-usecs 128 >>/var/log/user-data.log 2>&1" >>/var/spool/cron/root
```
+ 替换 `interface` 为配置为接收数据的网络接口（以太网适配器）。通常，这是`eth0`因为这是为 EC2 实例分配的默认网络接口。

## 调整 Rx 环形缓冲区-影响网络
<a name="tune-rx-ring-buffer"></a>

 增加 Rx 环形缓冲区的环形条目数，以防止在突发连接期间丢包或溢出。添加到 crontab，这样每次启动时都会正确设置它：

```
echo "@reboot sudo ethtool -G ${interface} rx 16384 >>/var/log/user-data.log 2>&1" >>/var/spool/cron/root
```
+ 替换 `interface` 为配置为接收数据的网络接口（以太网适配器）。通常，这是`eth0`因为这是为 EC2 实例分配的默认网络接口。
+ 如果设置`c6i`家庭实例，则需要修改命令以将环形缓冲区设置为`8192`，而不是`16384`。

## 调整 CPU C-State-影响 CPU
<a name="tune-cpu-c-state"></a>

 设置 C-State，避免在联络开始时 CPU 空闲导致数据包丢失。要求实例重启。

```
echo "GRUB_CMDLINE_LINUX_DEFAULT=\"console=tty0 console=ttyS0,115200n8 net.ifnames=0 biosdevname=0 nvme_core.io_timeout=4294967295 intel_idle.max_cstate=1 processor.max_cstate=1 max_cstate=1\"" >/etc/default/grub
echo "GRUB_TIMEOUT=0" >>/etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
```

## 保留入口端口-影响网络
<a name="reserve-ingress-ports"></a>

 预留您的 `AwsGroundStationAgentEndpoint` 入口地址端口范围内的所有端口，以防止与内核使用发生冲突。端口使用冲突将导致联络和数据传输失败。

```
echo "net.ipv4.ip_local_reserved_ports=${port_range_min}-${port_range_max}" >> /etc/sysctl.conf
```
+ 示例：`echo "net.ipv4.ip_local_reserved_ports=42000-43500" >> /etc/sysctl.conf`。

## Reboot
<a name="reboot"></a>

成功应用所有优化后，重启实例才能使优化生效。

```
sudo reboot
```

## 附录： interrupt/RPS Tune 的推荐参数
<a name="recommended-parameters"></a>

 本部分确定了在“优化硬件中断和接收队列：影响 CPU 和网络”优化部分中使用的推荐参数值。


| 系列 | 实例类型 | ${interrupt\_core\_list} | ${rps\_core\_mask} | 
| --- | --- | --- | --- | 
| c7i |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| c6i |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| c5 |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| c5n |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| m5 |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| r5 |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| r5n |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| g4dn |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| p4d |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 
| p3dn |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  |  [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 