EC2 + CentOS performance optimizations

1 minute read Published:

Disabling hyperthreading and upgrading the kernel on CentOS/RHEL 7.

Some basic things you can do to improve EC2 performance. This is following some performance tweaking experiments done for Vertica on AWS.

Upgrade kernel on CentOS or RHEL 7

You can put this in your Packer shell scripts somewhere. Run it as root:

yum -y install epel-release
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum -y --enablerepo=elrepo-kernel install kernel-ml
grub2-set-default 0

With this you can install the Linux kernel 4.14 (as of today) and newer, which brings some performance benefits.

Disable hyperthreading

Here’s an article from AWS on how to disable hyperthreading on EC2.

The reason why is this Localytics article which recommends disabling HT for Vertica.

The code is as follows:

#!/usr/bin/sh

for cpunum in $(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | cut -s -d, -f2- | tr ',' '\n' | sort -un)
do
    echo 0 > /sys/devices/system/cpu/cpu$cpunum/online
done

Now if you want this to happen at every boot, you can put it in cloud-init’s per-boot directory.

Here’s an excerpt from the EC2 userdata which creates the per-boot script disable_ht.sh and executes it:

echo '#!/usr/bin/env bash

for cpunum in $(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | cut -s -d, -f2- | tr ',' '\n' | sort -un)
do
    echo 0 > /sys/devices/system/cpu/cpu$cpunum/online
done
' > /var/lib/cloud/scripts/per-boot/disable_ht.sh

chmod u+x /var/lib/cloud/scripts/per-boot/disable_ht.sh
/var/lib/cloud/scripts/per-boot/disable_ht.sh