Subversion Repositories configs

Rev

Rev 3 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 - 1
# vim: set syn=sh :
2
#
3
# This script contains functions which can be useful in ktune profile scripts.
4
#
5
 
6
#
7
# DISK tuning
8
#
9
 
10
DISKS_DEV="$(command ls -d1 /dev/[shv]d*[a-z] 2>/dev/null)"
33 - 11
DISKS_SYS="$(command ls -d1 /sys/block/{sd,cciss,dm-,vd,dasd,xvd}* 2>/dev/null)"
3 - 12
 
13
# SATA Aggressive Link Power Management
14
# usage: set_disk_alpm policy
15
set_disk_alpm() {
16
	policy=$1
17
 
18
        for host in /sys/class/scsi_host/*; do
19
                if [ -f $host/ahci_port_cmd ]; then
20
                        port_cmd=`cat $host/ahci_port_cmd`;
21
                        if [ $((0x$port_cmd & 0x240000)) = 0 -a -f $host/link_power_management_policy ]; then
22
                                echo $policy >$host/link_power_management_policy;
23
                        else
24
                                echo "max_performance" >$host/link_power_management_policy;
25
                        fi
26
                fi
27
        done
28
}
29
 
30
# usage: set_disk_apm level
31
set_disk_apm() {
32
	level=$1
33
	for disk in $DISKS_DEV; do
34
		hdparm -B $level $disk &>/dev/null
35
	done
36
}
37
 
38
# usage: set_disk_spindown level
39
set_disk_spindown() {
40
	level=$1
41
	for disk in $DISKS_DEV; do
42
		hdparm -S $level $disk &>/dev/null
43
	done
44
}
45
 
46
DISK_READAHEAD_SAVE="/var/run/tuned/disk_readahead.save"
47
 
48
# usage: multiply_disk_readahead by
49
multiply_disk_readahead() {
50
	by=$1
51
	rm -f "$DISK_READAHEAD_SAVE"
52
 
53
	# float multiplication not supported in bash
54
	# bc might not be installed, python is available for sure
55
 
56
	for disk in $DISKS_SYS; do
57
		control="${disk}/queue/read_ahead_kb"
58
		old=$(cat $control)
59
		new=$(echo "print int($old*$by)" | python)
60
		echo "echo $old > $control" >> "$DISK_READAHEAD_SAVE" 2>/dev/null
61
		(echo $new > $control) &>/dev/null
62
	done
63
}
64
 
65
restore_disk_readahead() {
66
	if [ -r "$DISK_READAHEAD_SAVE" ]; then
67
		/bin/sh "$DISK_READAHEAD_SAVE" &>/dev/null
68
		rm -f "$DISK_READAHEAD_SAVE"
69
	fi
70
}
71
 
72
# usage: remount_disk options partition1 partition2 ...
73
remount_partitions() {
74
	options=$1
75
	shift
76
 
77
	for partition in $@; do
78
		mount -o remount,$options $partition
79
	done
80
}
81
 
82
_devices_no_write_back_cache() {
83
	for device in $@; do
84
		grep -q "write back" /sys/block/"$device"/device/scsi_disk/*/cache_type 2>/dev/null && return 1
85
	done
86
	return 0
87
}
88
 
89
_disk_barriers_remount() {
90
	mount_options="$1"
91
 
92
	lsblk -lno TYPE,KNAME,MOUNTPOINT | \
93
	awk '
94
		{ type=$1; name=$2; mountpoint=$3; }
95
 
96
		(type == "disk") {
97
				device=name
98
				next
99
		}
100
 
101
		(mountpoint ~ /^\// && mountpoint != "/" && mountpoint != "/boot") {
102
			mountpoints[mountpoint] = mountpoints[mountpoint] " " device
103
		}
104
 
105
		END {
106
			for (mountpoint in mountpoints) {
107
				print mountpoint, mountpoints[mountpoint]
108
			}
109
		}
110
	' | \
111
	while read mountpoint devices; do
112
		if _devices_no_write_back_cache $devices; then
113
			mount -o remount,"$mount_options" "$mountpoint" >/dev/null 2>&1
114
		fi
115
	done
116
}
117
 
118
# remounts all non-root and non-boot partitions with nobarrier option
119
# SCSI drives with write back cache are skipped
120
disable_disk_barriers() {
121
	_disk_barriers_remount nobarrier
122
}
123
 
124
# see: disable_disk_barriers
125
enable_disk_barriers() {
126
	_disk_barriers_remount barrier
127
}
128
 
129
#
130
# CPU tuning
131
#
132
 
133
CPUSPEED_SAVE_FILE="/var/run/tuned/ktune-cpuspeed.save"
134
CPUSPEED_ORIG_GOV="/var/run/tuned/ktune-cpuspeed-governor-%s.save"
135
CPUSPEED_STARTED="/var/run/tuned/ktune-cpuspeed-started"
136
CPUSPEED_CFG="/etc/sysconfig/cpuspeed"
137
CPUSPEED_INIT="/etc/init.d/cpuspeed"
138
CPUS="$(ls -d1 /sys/devices/system/cpu/cpu* | sed 's;^.*/;;' |  grep "cpu[0-9]\+")"
139
 
140
# set CPU governor setting and store the old settings
141
# usage: set_cpu_governor governor
142
set_cpu_governor() {
143
	governor=$1
144
 
145
	# prefer governor setting using cpuspeed daemon
146
	if [ -e $CPUSPEED_INIT ]; then
147
		if [ ! -e $CPUSPEED_SAVE_FILE -a -e $CPUSPEED_CFG ]; then
148
			cp -p $CPUSPEED_CFG $CPUSPEED_SAVE_FILE
149
			sed -e 's/^GOVERNOR=.*/GOVERNOR='$governor'/g' $CPUSPEED_SAVE_FILE > $CPUSPEED_CFG
150
		fi
151
 
152
		service cpuspeed status &> /dev/null
153
		[ $? -eq 3 ] && touch $CPUSPEED_STARTED || rm -f $CPUSPEED_STARTED
154
 
155
		service cpuspeed restart &> /dev/null
156
 
157
	# direct change using /sys fs
158
	elif [ -e /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
159
		echo >&2
160
		echo "Suggestion: install 'cpuspeed' package to get best tuning results." >&2
161
		echo "Falling back to '$governor' scaling governor for all CPUs." >&2
162
		echo >&2
163
 
164
		for cpu in $CPUS; do
165
			gov_file=/sys/devices/system/cpu/$cpu/cpufreq/scaling_governor
166
			save_file=$(printf $CPUSPEED_ORIG_GOV $cpu)
167
			rm -f $save_file
168
			if [ -e $gov_file ]; then
169
				cat $gov_file > $save_file
170
				echo $governor > $gov_file
171
			fi
172
		done
173
	fi
174
}
175
 
176
# re-enable previous CPU governor settings
177
# usage: restore_cpu_governor
178
restore_cpu_governor() {
179
	if [ -e $CPUSPEED_INIT ]; then
180
		if [ -e $CPUSPEED_SAVE_FILE ]; then
181
			cp -fp $CPUSPEED_SAVE_FILE $CPUSPEED_CFG
182
			rm -f $CPUSPEED_SAVE_FILE
183
		fi
184
 
185
		if [ -e $CPUSPEED_STARTED ]; then
186
			rm -f $CPUSPEED_STARTED
187
			service cpuspeed stop &> /dev/null
188
		else
189
			service cpuspeed restart &> /dev/null
190
		fi
191
	elif [ -e /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]; then
192
		for cpu in $CPUS; do
193
			cpufreq_dir=/sys/devices/system/cpu/$cpu/cpufreq
194
			save_file=$(printf $CPUSPEED_ORIG_GOV $cpu)
195
 
196
			if [ -e $cpufreq_dir/scaling_governor ]; then
197
				if [ -e $save_file ]; then
198
					cat $save_file > $cpufreq_dir/scaling_governor
199
					rm -f $save_file
200
				else
201
					echo userspace > $cpufreq_dir/scaling_governor
202
					cat $cpufreq_dir/cpuinfo_max_freq > $cpufreq_dir/scaling_setspeed
203
				fi
204
			fi
205
		done
206
	fi
207
}
208
 
209
_cpu_multicore_powersave() {
210
	value=$1
211
	[ -e /sys/devices/system/cpu/sched_mc_power_savings ] && echo $value > /sys/devices/system/cpu/sched_mc_power_savings
212
}
213
 
214
# enable multi core power savings for low wakeup systems
215
enable_cpu_multicore_powersave() {
216
	_cpu_multicore_powersave 1
217
}
218
 
219
disable_cpu_multicore_powersave() {
220
	_cpu_multicore_powersave 0
221
}
222
 
223
#
224
# MEMORY tuning
225
#
226
 
227
THP_ENABLE="/sys/kernel/mm/redhat_transparent_hugepage/enabled"
228
THP_SAVE="/var/run/tuned/ktune-thp.save"
229
 
230
[ -e "$THP_ENABLE" ] || THP_ENABLE="/sys/kernel/mm/transparent_hugepage/enabled"
231
 
232
set_transparent_hugepages() {
233
	if [ -e $THP_ENABLE ]; then
234
		cut -f2 -d'[' $THP_ENABLE  | cut -f1 -d']' > $THP_SAVE
235
		(echo "$1" > $THP_ENABLE) &> /dev/null
236
	fi
237
}
238
 
239
restore_transparent_hugepages() {
240
	if [ -e $THP_SAVE ]; then
241
		(echo $(cat $THP_SAVE) > $THP_ENABLE) &> /dev/null
242
		rm -f $THP_SAVE
243
	fi
244
}
245
 
246
#
247
# WIFI tuning
248
#
249
 
250
# usage: _wifi_set_power_level level
251
_wifi_set_power_level() {
252
	# 0    auto, PM enabled
253
	# 1-5  least savings and lowest latency - most savings and highest latency
254
	# 6    disable power savings
255
	level=$1
256
 
257
	# apply the settings using iwpriv
258
	ifaces=$(cat /proc/net/wireless | grep -v '|' | sed 's@^ *\([^:]*\):.*@\1@')
259
	for iface in $ifaces; do
260
		iwpriv $iface set_power $level
261
	done
262
 
263
	# some adapters may relay on sysfs
264
	for i in /sys/bus/pci/devices/*/power_level; do
265
		(echo $level > $i) &> /dev/null
266
	done
267
}
268
 
269
enable_wifi_powersave() {
270
	_wifi_set_power_level 5
271
}
272
 
273
disable_wifi_powersave() {
274
	_wifi_set_power_level 0
275
}
276
 
277
#
278
# BLUETOOTH tuning
279
#
280
 
281
disable_bluetooth() {
282
	hciconfig hci0 down >/dev/null 2>&1
283
	lsmod | grep -q btusb && rmmod btusb
284
}
285
 
286
enable_bluetooth() {
287
	modprobe btusb
288
	hciconfig hci0 up >/dev/null 2>&1
289
}
290
 
291
#
292
# USB tuning
293
#
294
 
295
_usb_autosuspend() {
296
	value=$1
297
	for i in /sys/bus/usb/devices/*/power/autosuspend; do echo $value > $i; done &> /dev/null
298
}
299
 
300
enable_usb_autosuspend() {
301
	_usb_autosuspend 1
302
}
303
 
304
disable_usb_autosuspend() {
305
	_usb_autosuspend 0
306
}
307
 
308
#
309
# SOUND CARDS tuning
310
#
311
 
312
_snd_ac97_powersave() {
313
	value=$1
314
	[ -e /sys/module/snd_ac97_codec/parameters/power_save ] && echo $value > /sys/module/snd_ac97_codec/parameters/power_save
315
}
316
 
317
enable_snd_ac97_powersave() {
318
	_snd_ac97_powersave Y
319
}
320
 
321
disable_snd_ac97_powersave() {
322
	_snd_ac97_powersave N
323
}
324
 
325
#
326
# CD DRIVES tuning
327
#
328
 
329
_cd_polling() {
330
	[ "$1" == "1" ] && opts=--enable-polling || opts=
331
 
332
	cddrives=$(command ls -1 /dev/scd* 2>/dev/null)
333
	for i in $cddrives; do hal-disable-polling $opts --device $(readlink -f $i); done &>/dev/null
334
}
335
 
336
enable_cd_polling() {
337
	_cd_polling 1
338
}
339
 
340
disable_cd_polling() {
341
	_cd_polling 0
342
}
343
 
344
#
345
# SOFTWARE tuning
346
#
347
 
348
RSYSLOG_CFG="/etc/rsyslog.conf"
349
RSYSLOG_SAVE="/var/run/tuned/ktune-cpuspeed.save"
350
 
351
disable_logs_syncing() {
352
	cp -p $RSYSLOG_CFG $RSYSLOG_SAVE
353
	sed -i 's/ \/var\/log/-\/var\/log/' $RSYSLOG_CFG
354
}
355
 
356
restore_logs_syncing() {
357
	mv $RSYSLOG_SAVE $RSYSLOG_CFG
358
}
359
 
360
#
361
# HARDWARE SPECIFIC tuning
362
#
363
 
364
# Asus EEE with Intel Atom
365
_eee_fsb_control() {
366
	value=$1
367
	if [ -e /sys/devices/platform/eeepc/she ]; then
368
		echo $value > /sys/devices/platform/eeepc/she
369
	elif [ -e /sys/devices/platform/eeepc/cpufv ]; then
370
		echo $value > /sys/devices/platform/eeepc/cpufv
371
	fi
372
}
373
 
374
eee_set_reduced_fsb() {
375
	_eee_fsb_control 2
376
}
377
 
378
eee_set_normal_fsb() {
379
	_eee_fsb_control 1
380
}
381
 
382
#
383
# KTUNE ACTION PROCESSING
384
#
385
 
386
error_not_implemented() {
387
	echo "tuned: ktune script function '$1' is not implemented." >&2
388
}
389
 
390
# implicit actions, will be used if not provided by profile script:
391
#
392
# * start    must be implemented
393
# * stop     must be implemented
394
# * reload   runs start
395
# * restart  runs stop + start
396
# * status   returns 0
397
 
398
start() {
399
	error_not_implemented start
400
	return 16
401
}
402
 
403
stop() {
404
	error_not_implemented stop
405
	return 16
406
}
407
 
408
reload() {
409
	start
410
	return $?
411
}
412
 
413
restart() {
414
	stop && start
415
	return $?
416
}
417
 
418
status() {
419
	return 0
420
}
421
 
422
# main processing
423
 
424
process() {
425
	VAR_SUBSYS_KTUNE="/var/lock/subsys/ktune"
426
 
427
	case "$1" in
428
	start)
429
		[ -f "$VAR_SUBSYS_KTUNE" ] && exit 0
430
		start
431
		RETVAL=$?
432
		;;
433
	stop)
434
		[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0
435
		stop
436
		RETVAL=$?
437
		;;
438
	reload)
439
		[ -f "$VAR_SUBSYS_KTUNE" ] && reload
440
		RETVAL=$?
441
		;;
442
	restart|force-reload)
443
		[ -f "$VAR_SUBSYS_KTUNE" ] && restart
444
		RETVAL=$?
445
		;;
446
	condrestart|try-restart)
447
		[ -f "$VAR_SUBSYS_KTUNE" ] || exit 0
448
		restart
449
		RETVAL=$?
450
		;;
451
	status)
452
		status
453
		RETVAL=$?
454
		;;
455
		*)
456
		echo $"Usage: $0 {start|stop|restart|condrestart|status}"
457
		RETVAL=2
458
		;;
459
	esac
460
 
461
	exit $RETVAL
462
}