# tuned configuration
#==============================================================#
# File      :   tuned.conf (crit)
# Desc      :   Tune operating system for critical systems
# Time      :   {{ '%Y-%m-%d %H:%M' |strftime }}
# Host      :   {{ nodename }} @ {{ inventory_hostname }}
# Path      :   /etc/tuned/crit/tuned.conf
# Note      :   ANSIBLE MANAGED, CHANGE WITH CAUTIONS!
# License   :   Apache-2.0 @ https://pigsty.io/docs/about/license/
# Copyright :   2018-2026  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#

[main]
summary=Optimize for CRIT System
include=network-latency

[cpu]
force_latency=1
governor=performance
energy_perf_bias=performance
min_perf_pct=100

[vm]
# disable transparent hugepages
transparent_hugepages=never

[scheduler]
sched_migration_cost_ns=500000

[sysctl]
#-------------------------------------------------------------#
#                           KERNEL                            #
#-------------------------------------------------------------#
# disable numa balancing
kernel.numa_balancing=0

# do not schedule postgres process in group
kernel.sched_autogroup_enabled = 0

# shmall in pages
kernel.shmall = {{ (node_pages|int * 0.75)|int }}

# shmmax size in bytes
kernel.shmmax = {{ (node_mem_bytes|int * 0.75)|int }}

# total shmem segs
kernel.shmmni=8192

# total msg queue number, set to mem size in MB
kernel.msgmni=32768

# max length of message queue
kernel.msgmnb=65536

# max size of message
kernel.msgmax=65536

kernel.pid_max=131072

# SEMMSL SEMMNS SEMOPM SEMMNI (16MiB)
kernel.sem=4096 524288 2048 8192

#-------------------------------------------------------------#
#                           Memory                            #
#-------------------------------------------------------------#
# try not using swap
vm.swappiness=1

# disable when most mem are for file cache
vm.zone_reclaim_mode=0

{% if node_overcommit_ratio|int > 0 %}
# overcommit threshhold = {{ node_overcommit_ratio }}%
vm.overcommit_memory=2
vm.overcommit_ratio={{ node_overcommit_ratio }}
{% else %}
vm.overcommit_memory=0
vm.overcommit_ratio=100
{% endif %}

# Starts write at 1% for CRIT system (10% -> 1%)
vm.dirty_background_ratio=1

# Limit to 5% for CRIT system (default 40% -> 5%)
vm.dirty_ratio=5

# Use 1s for CRIT system (30s -> 1s)
vm.dirty_expire_centisecs = 100

# Use 1s for CRIT system (5s -> 1s)
vm.dirty_writeback_centisecs = 100

# deny access on 0x00000 - 0x10000
vm.mmap_min_addr=65536

#-------------------------------------------------------------#
#                        Filesystem                           #
#-------------------------------------------------------------#
# max open files: 382589 -> 16M
fs.file-max=16777216

# max concurrent unfinished async io, should be larger than 1M.  65536->1M
fs.aio-max-nr=1048576

#-------------------------------------------------------------#
#                          Network                            #
#-------------------------------------------------------------#
# max connection in listen queue (triggers re-trans if full)
net.core.somaxconn=65535
# for CRIT with 25G+ network, use 16384 max backlog
net.core.netdev_max_backlog=8192
# tcp receive/transmit buffer default = 256KiB
net.core.rmem_default=262144
net.core.wmem_default=262144
# receive/transmit buffer limit = 4MiB, for 25G+ network, use 16777216
net.core.rmem_max=4194304
net.core.wmem_max=4194304

net.ipv4.tcp_rmem="4096 87380 16777216"
net.ipv4.tcp_wmem="4096 16384 16777216"
net.ipv4.udp_mem="3145728 4194304 16777216"

# allow unprivileged icmp ping
net.ipv4.ping_group_range=0 65535

# features
net.ipv4.ip_forward=1
net.ipv4.ip_nonlocal_bind=1
net.ipv4.tcp_timestamps=1
net.ipv4.tcp_syncookies=1
net.ipv4.ip_local_port_range=10000 64999
# do not reuse timewait socket for CRIT template
net.ipv4.tcp_tw_reuse=1

# syn retry, use one more than OLTP
net.ipv4.tcp_synack_retries=2
net.ipv4.tcp_syn_retries=2

# keepalive: 60/20/3 for OLTP system
net.ipv4.tcp_keepalive_time=60
net.ipv4.tcp_keepalive_intvl=20
net.ipv4.tcp_keepalive_probes=3

# reduce fin timeout from 60s to 30s for CRIT system
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_max_tw_buckets=262144
# 128 -> 8192 to increase the listen queue size
net.ipv4.tcp_max_syn_backlog=8192

# 512K entries
net.netfilter.nf_conntrack_max=524288

# kubernetes only
#net.ipv4.neigh.default.gc_thresh1=80000
#net.ipv4.neigh.default.gc_thresh2=90000
#net.ipv4.neigh.default.gc_thresh3=100000
#net.bridge.bridge-nf-call-iptables=1
#net.bridge.bridge-nf-call-ip6tables=1
#net.bridge.bridge-nf-call-arptables=1


#-------------------------------------------------------------#
#                            Disk                             #
#-------------------------------------------------------------#
[disk-scheduler-vd]
type=disk
devices=vd[a-z]
elevator=none

[disk-scheduler-xvd]
type=disk
devices=xvd[a-z]
elevator=none

[disk-scheduler-nvme]
type=disk
devices=nvme*
elevator=none
readahead=128
nomerges=1
