-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize.sh
More file actions
executable file
·360 lines (304 loc) · 12.1 KB
/
Copy pathoptimize.sh
File metadata and controls
executable file
·360 lines (304 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
set -euo pipefail
# =============================================================================
# Fedora 44 Performance Optimization - Ryzen 9 5950X + 128GB RAM
# 2x NVMe MSI M460 1TB + 8x SATA SSD (btrfs 10 devices)
# Host GPU: RX 7700 XT | Passthrough: RX 9070 XT
# =============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
[[ $EUID -eq 0 ]] || error "Ejecutar como root: sudo ./optimize.sh"
# =============================================================================
# KERNEL PARAMETERS
# =============================================================================
optimize_kernel_params() {
info "Optimizando parametros del kernel..."
local current_args
current_args=$(cat /proc/cmdline)
local new_args=""
# Desactivar mitigaciones de CPU (Spectre, Meltdown, etc.) — max rendimiento
if ! echo "$current_args" | grep -q "mitigations=off"; then
new_args+=" mitigations=off"
fi
# Desactivar watchdog (reduce interrupciones innecesarias)
if ! echo "$current_args" | grep -q "nowatchdog"; then
new_args+=" nowatchdog nmi_watchdog=0"
fi
# Tickless full para todos los cores excepto el 0
if ! echo "$current_args" | grep -q "nohz_full"; then
new_args+=" nohz_full=1-31"
fi
# Desactivar audit (overhead en cada syscall)
if ! echo "$current_args" | grep -q "audit=0"; then
new_args+=" audit=0"
fi
# Desactivar split lock detection (penaliza rendimiento en VMs)
if ! echo "$current_args" | grep -q "split_lock_detect=off"; then
new_args+=" split_lock_detect=off"
fi
# Preempt=full para menor latencia
if ! echo "$current_args" | grep -q "preempt=full"; then
new_args+=" preempt=full"
fi
if [ -n "$new_args" ]; then
grubby --update-kernel=ALL --args="$new_args"
info "Kernel params añadidos:$new_args"
else
info "Kernel params ya optimizados"
fi
}
# =============================================================================
# SYSCTL — VM, Network, Kernel
# =============================================================================
optimize_sysctl() {
info "Aplicando sysctl optimizations..."
cat > /etc/sysctl.d/99-performance.conf <<'EOF'
# --- VM / Memory ---
# Swappiness bajo (128GB RAM, swap solo para emergencias)
vm.swappiness = 1
# Dirty pages: flush mas agresivo para evitar stalls en escritura
vm.dirty_ratio = 10
vm.dirty_background_ratio = 3
vm.dirty_expire_centisecs = 500
vm.dirty_writeback_centisecs = 100
# VFS cache pressure: mantener dentries/inodes en cache
vm.vfs_cache_pressure = 50
# Mas memoria para page cache
vm.page-cluster = 0
# Zone reclaim off (NUMA single node, no tiene sentido)
vm.zone_reclaim_mode = 0
# --- Kernel ---
# Desactivar NMI watchdog via sysctl (belt and suspenders con kernel param)
kernel.nmi_watchdog = 0
# Scheduler autogroup (agrupa procesos por TTY)
kernel.sched_autogroup_enabled = 1
# --- Network ---
# TCP BBR congestion control
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
# Buffers de red mas grandes
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 1048576 16777216
net.ipv4.tcp_wmem = 4096 1048576 16777216
net.core.netdev_max_backlog = 16384
net.core.somaxconn = 8192
# TCP fastopen
net.ipv4.tcp_fastopen = 3
# Reuse sockets
net.ipv4.tcp_tw_reuse = 1
# --- FS ---
# Mas file handles
fs.file-max = 2097152
fs.inotify.max_user_watches = 524288
EOF
sysctl -p /etc/sysctl.d/99-performance.conf
info "sysctl aplicado"
}
# =============================================================================
# I/O SCHEDULER + READ-AHEAD
# =============================================================================
optimize_io() {
info "Optimizando I/O..."
# NVMe: none es optimo (hardware queue)
# SATA SSDs: mq-deadline mejor para SSDs con menos queues
cat > /etc/udev/rules.d/60-io-scheduler.rules <<'EOF'
# NVMe: no scheduler (hardware handles it)
ACTION=="add|change", KERNEL=="nvme[0-9]*", ATTR{queue/scheduler}="none"
# SATA SSD: mq-deadline
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="mq-deadline"
EOF
# Read-ahead optimizado: 256KB para NVMe, 1MB para SATA
cat > /etc/udev/rules.d/61-read-ahead.rules <<'EOF'
ACTION=="add|change", KERNEL=="nvme[0-9]*", ATTR{queue/read_ahead_kb}="256"
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/read_ahead_kb}="1024"
EOF
# Aplicar ahora sin esperar reboot
for dev in /sys/block/nvme*/queue; do
echo "none" > "$dev/scheduler" 2>/dev/null || true
echo 256 > "$dev/read_ahead_kb" 2>/dev/null || true
done
for dev in /sys/block/sd[a-h]/queue; do
echo "mq-deadline" > "$dev/scheduler" 2>/dev/null || true
echo 1024 > "$dev/read_ahead_kb" 2>/dev/null || true
done
info "I/O schedulers y read-ahead configurados"
}
# =============================================================================
# CPU GOVERNOR + ENERGY
# =============================================================================
optimize_cpu() {
info "Optimizando CPU..."
# Crear perfil tuned custom que aplica todo de forma persistente
local TUNED_DIR="/etc/tuned/profiles/max-performance"
mkdir -p "$TUNED_DIR"
cat > "$TUNED_DIR/tuned.conf" <<'EOF'
[main]
summary=Max performance for Ryzen 9 5950X workstation
include=throughput-performance
[cpu]
governor=performance
energy_perf_bias=performance
energy_performance_preference=performance
min_perf_pct=100
[sysctl]
kernel.nmi_watchdog=0
EOF
# C-states profundos desactivados via tmpfiles (persistente)
local cstate_conf="/etc/tmpfiles.d/cpu-cstates.conf"
echo "# Desactivar C-states profundos" > "$cstate_conf"
for i in $(seq 0 31); do
echo "w /sys/devices/system/cpu/cpu${i}/cpuidle/state2/disable - - - - 1" >> "$cstate_conf"
done
# Aplicar ahora
for state in /sys/devices/system/cpu/cpu*/cpuidle/state[2-9]/disable; do
echo 1 > "$state" 2>/dev/null || true
done
systemctl restart tuned
tuned-adm profile max-performance
info "CPU en modo max performance"
}
# =============================================================================
# TRANSPARENT HUGEPAGES
# =============================================================================
optimize_hugepages() {
info "Configurando Transparent HugePages..."
# madvise: solo aplicaciones que lo pidan (mejor que always para workloads mixtos)
echo madvise > /sys/kernel/mm/transparent_hugepage/enabled
echo madvise > /sys/kernel/mm/transparent_hugepage/defrag
# Persistir
cat > /etc/tmpfiles.d/thp.conf <<'EOF'
w /sys/kernel/mm/transparent_hugepage/enabled - - - - madvise
w /sys/kernel/mm/transparent_hugepage/defrag - - - - madvise
EOF
info "THP en modo madvise"
}
# =============================================================================
# EXPLICIT HUGEPAGES (HugeTLB) — backing store para la VM win11
# =============================================================================
optimize_hugetlb() {
# La VM usa <memoryBacking><hugepages/></memoryBacking>; sin reserva
# QEMU falla con "unable to map backing store for guest RAM".
local vm_mem_gib=64
local hp_size_kb=2048
local nr_pages=$(( vm_mem_gib * 1024 * 1024 / hp_size_kb ))
info "Reservando ${nr_pages} hugepages de 2MB (${vm_mem_gib} GiB) para la VM..."
cat > /etc/sysctl.d/99-hugepages.conf <<EOF
# ${vm_mem_gib} GiB reservados para VM win11 (${nr_pages} paginas x 2MB)
# Tamaño debe coincidir con <memory> del domain.xml de la VM
vm.nr_hugepages = ${nr_pages}
EOF
sysctl -p /etc/sysctl.d/99-hugepages.conf
local got
got=$(cat /proc/sys/vm/nr_hugepages)
if [[ "$got" -lt "$nr_pages" ]]; then
warn "Solo se asignaron ${got}/${nr_pages} hugepages (fragmentacion). Persistido; tras reboot deberia completar."
else
info "Hugepages asignadas: ${got}"
fi
}
# =============================================================================
# BTRFS OPTIMIZATIONS
# =============================================================================
optimize_btrfs() {
info "Optimizando btrfs..."
# Remount con noatime (elimina writes de access time)
local fstab="/etc/fstab"
if grep -q "relatime" "$fstab"; then
sed -i 's/relatime/noatime/g' "$fstab"
mount -o remount,noatime /
mount -o remount,noatime /home
info "Cambiado relatime -> noatime en fstab"
else
info "fstab ya tiene noatime o custom mount options"
fi
# Desactivar autodefrag para SSDs (contraproducente)
# commit=120 para reducir frecuencia de journal flush
# Estas se aplican en el proximo mount o reboot via fstab
info "btrfs optimizado (noatime)"
}
# =============================================================================
# DISABLE UNNECESSARY SERVICES
# =============================================================================
optimize_services() {
info "Desactivando servicios innecesarios..."
local services=(
ModemManager # No hay modem
cups # No hay impresora
avahi-daemon # mDNS discovery innecesario
abrtd # Auto bug reporting
abrt-journal-core # ABRT journal
abrt-oops # ABRT kernel oops
fwupd # Firmware updates (hacer manual)
switcheroo-control # GPU switching (no aplicable, 2 GPUs dedicadas)
power-profiles-daemon # Conflicto con tuned
thermald # Intel only
)
for svc in "${services[@]}"; do
if systemctl is-enabled "$svc" &>/dev/null 2>&1; then
systemctl disable --now "$svc" 2>/dev/null && info " Desactivado: $svc" || true
fi
done
}
# =============================================================================
# LIMITS — file descriptors, memlock
# =============================================================================
optimize_limits() {
info "Configurando limits..."
cat > /etc/security/limits.d/99-performance.conf <<EOF
* soft nofile 1048576
* hard nofile 1048576
* soft memlock unlimited
* hard memlock unlimited
* soft nproc unlimited
* hard nproc unlimited
EOF
info "Limits configurados"
}
# =============================================================================
# ZRAM OPTIMIZATION
# =============================================================================
optimize_zram() {
info "Optimizando zram..."
# Con 128GB RAM, zram de 8GB es suficiente pero usar zstd en vez de lzo
if [ -f /etc/systemd/zram-generator.conf ]; then
sed -i 's/compression-algorithm.*/compression-algorithm = zstd/' /etc/systemd/zram-generator.conf 2>/dev/null || true
info "zram compresion: zstd"
fi
}
# =============================================================================
# MAIN
# =============================================================================
info "Optimizando Fedora 44 para maximo rendimiento..."
echo ""
optimize_kernel_params
optimize_sysctl
optimize_io
optimize_cpu
optimize_hugepages
optimize_hugetlb
optimize_btrfs
optimize_services
optimize_limits
optimize_zram
echo ""
echo -e "${GREEN}=========================================${NC}"
echo -e "${GREEN} OPTIMIZACION COMPLETADA${NC}"
echo -e "${GREEN}=========================================${NC}"
echo ""
echo "Cambios aplicados:"
echo " - Kernel: mitigations=off, nowatchdog, nohz_full, audit=0, split_lock_detect=off"
echo " - CPU: governor performance, C-states profundos desactivados"
echo " - Memory: swappiness=1, dirty pages optimizados, THP=madvise, HugeTLB 64GiB"
echo " - I/O: NVMe=none scheduler, SATA=mq-deadline, read-ahead optimizado"
echo " - Network: TCP BBR, buffers ampliados, fastopen"
echo " - Btrfs: noatime"
echo " - Servicios innecesarios desactivados"
echo " - Limits: nofile/memlock/nproc ampliados"
echo ""
echo -e "${YELLOW}REQUIERE REBOOT para aplicar parametros del kernel${NC}"