

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 調校 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)

## 調校硬體中斷和接收佇列 - 影響 CPU 和網路
<a name="tune-hardware-interrupts"></a>

本節設定系統化、SMP IRQs、接收封包轉向 (RPS) 和接收流程轉向 (RFS) 的 CPU 核心用量。如需根據您使用之執行個體類型的一組建議設定，[附錄：中斷/RPS 調校的建議參數](#recommended-parameters)請參閱 。

1. 將系統化程序釘選為遠離代理程式 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` 將 取代為為核心和作業系統預留的核心 - 通常為第一和第二個，以及超執行緒核心對。這不應與上述選取的核心重疊。（例如，超執行緒 96-CPU 執行個體為「0，1，48，49」)。
+ `rps_core_mask` 是十六進位位元遮罩，指定哪些 CPUs 應處理傳入封包，每個數字代表 4 個 CPUs。它也必須從右側開始每 8 個字元以逗號分隔。建議允許所有 CPUs，並讓快取處理平衡。
  + 若要查看每個執行個體類型的建議參數清單，請參閱 [附錄：中斷/RPS 調校的建議參數](#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>

 設定 CPU C-state 以防止閒置，這可能會導致聯絡人開始時遺失封包。需要重新啟動執行個體。

```
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`。

## 重新啟動
<a name="reboot"></a>

成功套用所有調校後，請重新啟動執行個體，讓調校生效。

```
sudo reboot
```

## 附錄：中斷/RPS 調校的建議參數
<a name="recommended-parameters"></a>

 本節決定用於調校區段調整硬體中斷和接收佇列的建議參數值 - 影響 CPU 和網路。


| 系列 | 執行個體類型 | \$1\$1interrupt\$1core\$1list\$1 | \$1\$1rps\$1core\$1mask\$1 | 
| --- | --- | --- | --- | 
|  c7i  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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_tw/ground-station/latest/gs-agent-ug/ec2-instance-performance-tuning.html)  | 