forked from linrunner/TLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlp-functions.in
2420 lines (1941 loc) · 68.8 KB
/
tlp-functions.in
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# tlp - power management functions
#
# Copyright (c) 2015 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.
#
# Some concepts and descriptions were adapted from:
# - laptop-mode-tools
# - thinkwiki.org
# ----------------------------------------------------------------------------
# Constants
readonly TLPVER="0.8.900"
readonly CONFFILE=@TLP_CONF@
readonly RUNDIR=/var/run/tlp
readonly ETHTOOL=ethtool
readonly HDPARM=hdparm
readonly IWC=iwconfig
readonly IW=iw
readonly MODPRO=modprobe
readonly LOGGER=logger
readonly UDEVADM=udevadm
readonly LAPMODE=laptop_mode
readonly NMCLI=nmcli
readonly NMD=NetworkManager
readonly NMTOOL=nm-tool
readonly ENERGYPERF=x86_energy_perf_policy
readonly DBUSSEND=dbus-send
readonly SYSTEMD=systemd
readonly SYSTEMCTL=systemctl
readonly INITCTL=initctl
readonly TPACPIBAT=$LIBDIR/tpacpi-bat # LIBDIR is initialized by main program
readonly TPACPIDIR=/sys/devices/platform/thinkpad_acpi
readonly SMAPIDIR=/sys/devices/platform/smapi
readonly ACPIBATDIR=/sys/class/power_supply
readonly NETD=/sys/class/net
readonly BLUETOOTHD=/sys/class/bluetooth
readonly PCID=/sys/bus/pci/devices
readonly PCIDRV=/sys/bus/pci/drivers
readonly I915D=/sys/module/i915/parameters
readonly RADD=/sys/module/radeon
readonly DMID=/sys/class/dmi/id/
readonly CPU_BOOST_ALL_CTRL=/sys/devices/system/cpu/cpufreq/boost
readonly INTEL_PSTATED=/sys/devices/system/cpu/intel_pstate
readonly CPU_MIN_PERF_PCT=$INTEL_PSTATED/min_perf_pct
readonly CPU_MAX_PERF_PCT=$INTEL_PSTATED/max_perf_pct
readonly CPU_TURBO_PSTATE=$INTEL_PSTATED/no_turbo
readonly USBD=/sys/bus/usb/devices
readonly USB_TIMEOUT=2
readonly USB_TIMEOUT_MS=2000
readonly USB_WWAN_VENDORS="0bdb 05c6 1199"
readonly USB_DONE=usb_done
readonly DOCKGLOB="/sys/devices/platform/dock.?"
readonly MODULES="acpi_call coretemp msr thinkpad_acpi tp_smapi"
readonly STATEDIR="/var/lib/tlp"
readonly RFSTATEFILE=$STATEDIR/rfkill-saved
readonly BAYSTATEFILE=$STATEDIR/bay-saved
readonly DISK_NOP_WORDS="_ keep"
readonly DEFAULT_DISK_DEVICES="sda"
readonly DEFAULT_DISK_IO_SCHEDULER="cfq"
readonly DEFAULT_PM_DRIVER_BLACKLIST="radeon nouveau"
readonly DEFAULT_USB_DRIVER_BLACKLIST="usbhid"
# ----------------------------------------------------------------------------
# Control
nodebug=0
# ----------------------------------------------------------------------------
# Functions
# --- Tests
wordinlist () { # test if word in list
# $1: word, $2: whitespace-separated list of words
local word
if [ -n "${1-}" ]; then
for word in ${2-}; do
[ "${word}" != "${1}" ] || return 0 # exact match
done
fi
return 1 # no match
}
echo_debug () { # write debug msg if tag matches -- $1: tag; $2: msg;
[ "$nodebug" = "1" ] && return 0
if wordinlist "$1" "$TLP_DEBUG"; then
$LOGGER -p debug -t "tlp[$$,$PPID]" "$2"
fi
}
cmd_exists () { # test if command exists -- $1: command
command -v $1 > /dev/null 2>&1
}
check_sysfs () { # check if sysfile exists -- $1: routine; $2: sysfs path
if wordinlist "sysfs" "$TLP_DEBUG"; then
if [ ! -e $2 ]; then
$LOGGER -p debug -t "tlp[$$,$PPID]" "$1: $2 nonexistent"
fi
fi
}
test_root () { # test root privilege -- rc: 0=root, 1=not root
[ "$(id -u)" = "0" ]
}
check_root () { # show error message and exit when root privilege missing
if ! test_root; then
echo "Error: missing root privilege." 1>&2
exit 1
fi
}
check_tlp_enabled () { # check if TLP is enabled in config file
# rc: 0=disabled/1=enabled
if [ ! "$TLP_ENABLE" = "1" ]; then
echo "Error: TLP power save is disabled. Set TLP_ENABLE=1 in $CONFFILE." 1>&2
return 1
else
return 0
fi
}
check_laptop_mode_tools () { # check if lmt installed -- rc: 0=not installed, 1=installed
if cmd_exists $LAPMODE; then
echo 1>&2
echo "***Warning: laptop-mode-tools detected, this may cause conflicts with TLP." 1>&2
echo " Please uninstall laptop-mode-tools." 1>&2
echo 1>&2
echo_debug "pm" "check_laptop_mode_tools: yes"
return 1
else
return 0
fi
}
check_systemd () { # check if systemd is the active init system (PID 1) and systemctl is installed
# rc: 0=yes, 1=no
[ -d /run/systemd/system ] && cmd_exists $SYSTEMCTL
}
check_upstart () { # check if upstart is active init system (PID 1)
# rc: 0=yes, 1=no
cmd_exists $INITCTL && $INITCTL --version | grep -q upstart
}
check_openrc () { # check if openrc is the active init system (PID 1)
# rc: 0=yes, 1=no
[ -e /run/openrc/softlevel ]
}
# --- PATH
add_sbin2path () { # check if /sbin /usr/sbin in $PATH, otherwise add them
# retval: $PATH, $oldpath, $addpath
local sp
oldpath="$PATH"
addpath=""
for sp in /usr/sbin /sbin; do
if [ -d $sp ] && [ ! -h $sp ]; then
# dir exists and is not a symlink
case ":$PATH:" in
*":$sp:"*) # $sp already in $PATH
;;
*) # $sp not in $PATH, add it
addpath="$addpath:$sp"
;;
esac
fi
done
if [ -n "$addpath" ]; then
export PATH="${PATH}${addpath}"
fi
return 0
}
# --- Configuration
read_defaults () { # read config file
if [ -f $CONFFILE ]; then
. $CONFFILE
return 0
else
return 1
fi
}
# --- Kernel Modules
load_modules () { # load needed kernel modules
local mod
# verify module loading is allowed (else explicitly disabled)
# and possible (else implicitly disabled)
[ "${TLP_LOAD_MODULES:-y}" = "y" ] && [ -e /proc/modules ] || return 0
# load modules, ignore any errors
for mod in $MODULES; do
$MODPRO $mod > /dev/null 2>&1
done
return 0
}
# --- DMI
read_dmi () { # read DMI data -- $*: keywords; stdout: dmi strings
local ds key outr
outr=""
for key in $*; do
ds="$( cat ${DMID}/$key 2> /dev/null | \
egrep -iv 'not available|to be filled|DMI table is broken' )"
if [ -n "$outr" ]; then
[ -n "$ds" ] && outr="$outr $ds"
else
outr="$ds"
fi
done
echo $outr
return 0
}
# --- ThinkPad
is_thinkpad () { # check for ThinkPad hardware and save model string
# rc: 0=ThinkPad, 1=other hardware
# retval: $tpmodel
if [ -d $TPACPIDIR ]; then
# kernel module thinkpad_acpi is loaded
# get DMI product string
tpmodel="$( read_dmi product_version )"
# check DMI product string for occurence of "ThinkPad"
if echo $tpmodel | grep -q "ThinkPad"; then
# it's a real ThinkPad --> save model substring
tpmodel=${tpmodel#ThinkPad }
tpmodel=${tpmodel#Edge }
return 0
fi
fi
# no ThinkPad detected
tpmodel=""
return 1
}
# --- Power Source
get_sys_power_supply () { # get current power source
# rc: 0=ac, 1=battery, 2=unknown
local psrc
local rc=
for psrc in /sys/class/power_supply/*; do
# -f $psrc/type not necessary - cat 2>.. handles this
case "$(cat $psrc/type 2> /dev/null)" in
Mains)
# AC detected, check if online
if [ "$(cat $psrc/online 2> /dev/null)" = "1" ]; then
rc=0
break
fi
# else AC not online => keep $rc as-is
;;
Battery)
# set rc to battery, but don't stop looking for AC
rc=1
;;
*)
echo_debug "pm" "unknown power supply: ${psrc##*/}"
;;
esac
done
# set rc to unknown if we haven't seen any AC/battery power source so far
: ${rc:=2}
return $rc
}
get_power_state () { # get current power source -- rc: 0=ac, 1=battery
# similar to get_sys_power_supply(),
# but maps unknown power source to TLP_DEFAULT_MODE
local psrc
get_sys_power_supply
psrc=$?
if [ $psrc -eq 2 ]; then
# unknown power supply, apply default mode
case "$TLP_DEFAULT_MODE" in
ac|AC) psrc=0 ;;
bat|BAT) psrc=1 ;;
*) psrc=0 ;; # use AC if no default mode configured
esac
fi
return $psrc
}
echo_started_mode () { # print operation mode -- $1: 0=ac mode, 1=battery mode
if [ "$1" = "0" ]; then
echo "TLP started in AC mode."
else
echo "TLP started in battery mode."
fi
return 0
}
# --- Locking and Semaphores
set_run_flag () { # set flag -- $1: flag name
# rc: 0=success/1,2=failed
local rc
if mkdir -p $RUNDIR 2> /dev/null 1>&2 ; then
touch $RUNDIR/$1; rc=$?
echo_debug "lock" "set_run_flag.touch: $1; rc=$rc"
return $rc
else
# mkdir failed
echo_debug "lock" "set_run_flag.mkdir_failed"
return 2
fi
}
reset_run_flag () { # reset flag -- $1: flag name
if rm $RUNDIR/$1 2> /dev/null 1>&2 ; then
echo_debug "lock" "reset_run_flag($1).remove"
else
echo_debug "lock" "reset_run_flag($1).not_found"
fi
return 0
}
check_run_flag () { # check flag -- $1: flag name
# rc: 0=flag set/1=flag not set
local rc
[ -f $RUNDIR/$1 ]; rc=$?
echo_debug "lock" "check_run_flag($1): rc=$rc"
return $rc
}
check_and_set_semaphore () { # check and set semaphore -- $1: semaphore name
# rc: 0=success/1=failed
local rc
{ mkdir -p $RUNDIR && mkdir $RUNDIR/$1 ; } 2> /dev/null 1>&2
rc=$?
echo_debug "lock" "check_and_set_semaphore($1): rc=$rc"
return $rc
}
unset_semaphore () { # unset semaphore -- $1: semaphore name
local rc
rmdir $RUNDIR/$1 2> /dev/null 1>&2
rc=$?
echo_debug "lock" "unset_semaphore($1): rc=$rc"
return $rc
}
set_timed_lock () { # create timestamp n seconds in the future
# $1: lock id, $2: lock duration [s]
local rc
local lock=${1}_timed_lock_$(date +%s -d "+${2} seconds")
set_run_flag $lock; rc=$?
echo_debug "lock" "set_timed_lock($1, $2): $lock; rc=$rc"
return $rc
}
check_timed_lock () { # check if active timestamp exists
# $1: lock id; rc: 0=locked/1=not locked
local lockid=$1
local lockfile locktime
local time=$(date +%s)
for lockfile in $RUNDIR/${lockid}_timed_lock_*; do
if [ -f $lockfile ]; then
locktime=${lockfile#${RUNDIR}/${lockid}_timed_lock_}
if [ $time -lt $(( $locktime - 120 )) ]; then
# timestamp is more than 120 secs in the future,
# something weird has happened -> remove it
rm $lockfile
echo_debug "lock" "check_timed_lock($1).remove_invalid: ${lockfile#${RUNDIR}/}"
elif [ $time -lt $locktime ]; then
# timestamp in the future -> we're locked
echo_debug "lock" "check_timed_lock($1).locked: $time, $locktime"
return 0
else
# obsolete timestamp -> remove it
rm $lockfile
echo_debug "lock" "check_timed_lock($1).remove_obsolete: ${lockfile#${RUNDIR}/}"
fi
fi
done
echo_debug "lock" "check_timed_lock($1).not_locked: $time"
return 1
}
# --- Filesystem
set_laptopmode () { # set kernel laptop mode -- $1: 0=ac mode, 1=battery mode
check_sysfs "set_laptopmode" "/proc/sys/vm/laptop_mode"
local isec
if [ "$1" = "1" ]; then
isec=${DISK_IDLE_SECS_ON_BAT:-}
else
isec=${DISK_IDLE_SECS_ON_AC:-}
fi
# replace with empty string if non-numeric chars are contained
isec=$(echo "$isec" | egrep '^[0-9]+$')
if [ -z "$isec" ]; then
# do nothing if unconfigured or non numeric value
echo_debug "pm" "set_laptopmode($1).not_configured"
return 0
fi
echo_debug "pm" "set_laptopmode($1): $isec"
echo $isec > /proc/sys/vm/laptop_mode
return 0
}
set_dirty_parms () { # set filesystem buffer params
# $1: 0=ac mode, 1=battery mode
# concept from laptop-mode-tools
local age cage df
check_sysfs "set_dirty_parms" "/proc/sys/vm"
if [ "$1" = "1" ]; then
age=${MAX_LOST_WORK_SECS_ON_BAT:-0}
else
age=${MAX_LOST_WORK_SECS_ON_AC:-0}
fi
# calc age in centisecs, non numeric values result in "0"
cage=$(($age * 100))
if [ "$cage" = "0" ]; then
# do nothing if unconfigured or invalid age
echo_debug "pm" "set_dirty_parms($1).not_configured"
return 0
fi
echo_debug "pm" "set_dirty_parms($1): $cage"
for df in /proc/sys/vm/dirty_writeback_centisecs \
/proc/sys/vm/dirty_expire_centisecs \
/proc/sys/fs/xfs/age_buffer_centisecs \
/proc/sys/fs/xfs/xfssyncd_centisecs; do
[ -w $df ] && echo $cage > $df
done
[ -w /proc/sys/fs/xfs/xfsbufd_centisecs ] \
&& echo 3000 > /proc/sys/fs/xfs/xfsbufd_centisecs
return 0
}
# --- CPU
check_intel_pstate () { # detect intel_pstate driver -- retval: $intel_pstate
# Note: intel_pstate requires Linux 3.9 or higher
intel_pstate=0
[ -d $INTEL_PSTATED ] && intel_pstate=1
return 0
}
set_scaling_governor () { # set scaling governor -- $1: 0=ac mode, 1=battery mode
local gov cpu
if [ "$1" = "1" ]; then
gov=$CPU_SCALING_GOVERNOR_ON_BAT
else
gov=$CPU_SCALING_GOVERNOR_ON_AC
fi
if [ -n "$gov" ]; then
echo_debug "pm" "set_scaling_governor($1): $gov"
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
[ -f $cpu ] && echo $gov > $cpu 2> /dev/null
done
fi
return 0
}
set_scaling_min_max_freq () { # set scaling limits -- $1: 0=ac mode, 1=battery mode
local minfreq maxfreq cpu
if [ "$1" = "1" ]; then
minfreq=$CPU_SCALING_MIN_FREQ_ON_BAT
maxfreq=$CPU_SCALING_MAX_FREQ_ON_BAT
else
minfreq=$CPU_SCALING_MIN_FREQ_ON_AC
maxfreq=$CPU_SCALING_MAX_FREQ_ON_AC
fi
if [ -n "$minfreq" ]; then
echo_debug "pm" "set_scaling_min_max_freq($1).min: $minfreq"
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_min_freq; do
[ -f $cpu ] && echo $minfreq > $cpu 2> /dev/null
done
fi
if [ -n "$maxfreq" ]; then
echo_debug "pm" "set_scaling_min_max_freq($1).max: $maxfreq"
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq; do
[ -f $cpu ] && echo $maxfreq > $cpu 2> /dev/null
done
fi
return 0
}
set_cpu_perf_pct () { # set Intel P-state performance
# $1: 0=ac mode, 1=battery mode
local min max
check_intel_pstate
if [ "$intel_pstate" != "1" ]; then
echo_debug "pm" "set_cpu_perf_pct($1).no_intel_pstate"
return 0
fi
if [ "$1" = "1" ]; then
min="${CPU_MIN_PERF_ON_BAT:-}"
max="${CPU_MAX_PERF_ON_BAT:-}"
else
min="${CPU_MIN_PERF_ON_AC:-}"
max="${CPU_MAX_PERF_ON_AC:-}"
fi
if [ ! -f $CPU_MIN_PERF_PCT ]; then
echo_debug "pm" "set_cpu_perf_pct($1).min.not_supported"
elif [ -n "$min" ]; then
{ echo "$min" > $CPU_MIN_PERF_PCT; } 2> /dev/null
echo_debug "pm" "set_cpu_perf_pct($1).min: $min"
else
echo_debug "pm" "set_cpu_perf_pct($1).min.not_configured"
fi
if [ ! -f $CPU_MAX_PERF_PCT ]; then
echo_debug "pm" "set_cpu_perf_pct($1).max.not_supported"
elif [ -n "$max" ]; then
{ echo "$max" > $CPU_MAX_PERF_PCT; } 2> /dev/null
echo_debug "pm" "set_cpu_perf_pct($1).max: $max"
else
echo_debug "pm" "set_cpu_perf_pct($1).max.not_configured"
fi
return 0
}
set_cpu_boost_all () { # $1: 0=ac mode, 1=battery mode
# global cpu boost behavior control based on the current power mode
#
# Relevant config option(s): CPU_BOOST_ON_{AC,BAT} with values {'',0,1}
#
# Note:
# * needs commit #615b7300717b9ad5c23d1f391843484fe30f6c12
# (linux-2.6 tree), "Add support for disabling dynamic overclocking",
# => requires Linux 3.7 or later
local val ival
if [ "$1" = "1" ]; then
val="${CPU_BOOST_ON_BAT:-}"
else
val="${CPU_BOOST_ON_AC:-}"
fi
if [ -z "$val" ]; then
# do nothing if unconfigured
echo_debug "pm" "set_cpu_boost_all($1).not_configured"
return 0
fi
check_intel_pstate
if [ $intel_pstate -eq 1 ]; then
# use intel_pstate sysfiles
if [ -f $CPU_TURBO_PSTATE ]; then
ival=$(($val ^ 1))
echo "$ival" > $CPU_TURBO_PSTATE 2> /dev/null
echo_debug "pm" "set_cpu_boost_all($1).intel_pstate: $val"
else
echo_debug "pm" "set_cpu_boost_all($1).intel_pstate.cpu_not_supported"
fi
elif [ -f $CPU_BOOST_ALL_CTRL ]; then
# use acpi_cpufreq sysfiles
# simple test for attribute "w" doesn't work, so actually write
if ( echo "$val" > $CPU_BOOST_ALL_CTRL ) 2> /dev/null; then
echo_debug "pm" "set_cpu_boost_all($1).acpi_cpufreq: $val"
else
echo_debug "pm" "set_cpu_boost_all($1).acpi_cpufreq.cpu_not_supported"
fi
else
echo_debug "pm" "set_cpu_boost_all($1).not_available"
fi
return 0
}
set_sched_powersave () { # set multi-core/-thread powersave policy
# $1: 0=ac mode, 1=battery mode
local pwr pool sdev
local avail=0
if [ "$1" = "1" ]; then
pwr=${SCHED_POWERSAVE_ON_BAT:-}
else
pwr=${SCHED_POWERSAVE_ON_AC:-}
fi
if [ -z "$pwr" ]; then
# do nothing if unconfigured
echo_debug "pm" "set_sched_powersave($1).not_configured"
return 0
fi
for pool in mc smp smt; do
sdev="/sys/devices/system/cpu/sched_${pool}_power_savings"
if [ -f $sdev ]; then
echo_debug "pm" "set_sched_powersave($1): ${sdev##/*/} $pwr"
echo $pwr > "$sdev"
avail=1
fi
done
[ "$avail" = "1" ] || echo_debug "pm" "set_sched_powersave($1).not_available"
return 0
}
set_nmi_watchdog () { # enable/disable nmi watchdog
local nmiwd=${NMI_WATCHDOG:-}
if [ -z "$nmiwd" ]; then
# do nothing if unconfigured
echo_debug "pm" "set_nmi_watchdog.not_configured"
return 0
fi
if [ -f /proc/sys/kernel/nmi_watchdog ]; then
echo "$nmiwd" > /proc/sys/kernel/nmi_watchdog 2> /dev/null
if [ $? = 0 ]; then
echo_debug "pm" "set_nmi_watchdog: $nmiwd"
else
echo_debug "pm" "set_nmi_watchdog.disabled_by_kernel: $nmiwd"
fi
else
echo_debug "pm" "set_nmi_watchdog.not_available"
fi
return 0
}
set_phc_controls () { # set core voltages
local control
local ctrl_avail="0"
phc_controls=${PHC_CONTROLS:-}
if [ -z "$phc_controls" ]; then
# do nothing if unconfigured
echo_debug "pm" "set_phc_controls.not_configured"
return 0
fi
for control in /sys/devices/system/cpu/cpu*/cpufreq/phc_controls; do
if [ -f $control ]; then
echo_debug "pm" "set_phc_controls: $control $phc_controls"
echo $phc_controls > $control
ctrl_avail="1"
fi
done
[ "$ctrl_avail" = "0" ] && echo_debug "pm" "set_phc_controls.not_available"
return 0
}
set_energy_perf_policy () { # set performance versus energy savings policy
# $1: 0=ac mode, 1=battery mode
local perf rc
if [ "$1" = "1" ]; then
perf=${ENERGY_PERF_POLICY_ON_BAT:-}
else
perf=${ENERGY_PERF_POLICY_ON_AC:-}
fi
if [ -z "$perf" ]; then
echo_debug "pm" "set_energy_perf_policy($1).not_configured"
elif ! cmd_exists $ENERGYPERF; then
echo_debug "pm" "set_energy_perf_policy($1).not_available"
else
$ENERGYPERF $perf > /dev/null 2>&1
rc=$?
case $rc in
0) echo_debug "pm" "set_energy_perf_policy($1): $perf" ;;
1) echo_debug "pm" "set_energy_perf_policy($1): $perf -- unsupported cpu" ;;
2) echo_debug "pm" "set_energy_perf_policy($1): $perf -- kernel specific x86_energy_perf_policy missing" ;;
*) echo_debug "pm" "set_energy_perf_policy($1): $perf -- unknown rc=$rc " ;;
esac
return $rc
fi
return 0
}
# --- Storage Devices
check_disk_hdparm_cap () { # check if relevant disk device
# $1: dev; rc: 0=yes/1=no
if [ -z "$($HDPARM -I /dev/$1 2>&1 | \
egrep 'Invalid argument|Invalid exchange|missing sense data|No such device')" ]; then
return 0
else
return 1
fi
}
echo_disk_model () { # print disk model -- $1: dev
local model
model=$($HDPARM -I /dev/$1 2>&1 | grep 'Model Number' | \
cut -f2 -d: | sed -r 's/^ *//' )
echo "$model"
return 0
}
echo_disk_firmware () { # print firmware version --- $1: dev
local firmware
firmware=$($HDPARM -I /dev/$1 2>&1 | grep 'Firmware Revision' | \
cut -f2 -d: | sed -r 's/^ *//' )
echo "$firmware"
return 0
}
get_disk_state () { # get disk power state -- $1: dev; retval: $disk_state
disk_state=$($HDPARM -C /dev/$1 2>&1 | awk -F ':' '/drive state is/ { gsub(/ /,"",$2); print $2; }')
[ -z "$disk_state" ] && disk_state="(not available)"
return 0
}
spindown_disk () { # stop spindle motor -- $1: dev
$HDPARM -y /dev/$1 > /dev/null 2>&1
return 0
}
get_disk_apm_level () { # get disk apm level -- $1: dev; rc: apm
local apm
apm=$($HDPARM -I /dev/$1 2>&1 | grep 'Advanced power management level' | \
cut -f2 -d: | egrep "^ *[0-9]+ *$")
if [ -n "$apm" ]; then
return $apm
else
return 0
fi
}
get_disk_trim_capability () { # check for trim capability
# $1: dev; rc: 0=no, 1=yes, 254=no ssd device
local trim
if [ -n "$($HDPARM -I /dev/$1 2>&1 | grep 'Solid State Device')" ]; then
if [ -n "$($HDPARM -I /dev/$1 2>&1 | grep 'TRIM supported')" ]; then
trim=1
else
trim=0
fi
else
trim=255
fi
return $trim
}
get_disk_dev () { # translate disk id to device (sdX)
# $1: id or dev; retval: $disk_dev, $disk_id
if [ -h /dev/disk/by-id/$1 ]; then
# $1 is disk id
disk_id=$1
disk_dev=$(echo $disk_id | sed -r 's/-part[1-9][0-9]*$//')
disk_dev=$(readlink /dev/disk/by-id/$disk_dev)
disk_dev=${disk_dev##*/}
else
# $1 is disk dev
disk_dev=$1
disk_id=""
fi
# strip partition number
disk_dev=$(echo $disk_dev | sed -r 's/[1-9][0-9]*$//')
}
set_disk_apm_level () { # set disk apm level
# $1: 0=ac mode, 1=battery mode
local pwrmode="$1"
local dev log_message
# when undefined use default
: ${DISK_DEVICES:=${DEFAULT_DISK_DEVICES}}
# set @argv := apmlist (blanks removed - relying on a sane $IFS)
if [ "$pwrmode" = "1" ]; then
set -- $DISK_APM_LEVEL_ON_BAT
else
set -- $DISK_APM_LEVEL_ON_AC
fi
# exit if empty apmlist
[ $# -gt 0 ] || return 0
# pairwise iteration DISK_DEVICES[1,n], apmlist[1,m]; m > 0
# for j in [1,n]: disk_dev[j], apmlist[min(j,m)]
#
for dev in $DISK_DEVICES; do
: ${1:?BUG: broken DISK_APM_LEVEL list handling}
get_disk_dev $dev
log_message="set_disk_apm_level($pwrmode): $disk_dev [$disk_id] $1"
if [ ! -b /dev/$disk_dev ]; then
echo_debug "disk" "${log_message} -- missing"
elif ! check_disk_hdparm_cap $disk_dev; then
echo_debug "disk" "${log_message} -- not supported"
elif wordinlist "$1" "$DISK_NOP_WORDS"; then
echo_debug "disk" "${log_message} -- keep as is"
else
echo_debug "disk" "${log_message}"
$HDPARM -B $1 /dev/$disk_dev > /dev/null 2>&1
fi
# last entry in apmlist applies to all remaining disks
[ $# -lt 2 ] || shift
done
return 0
}
set_disk_spindown_timeout () { # set disk spindown timeout
# $1: 0=ac mode, 1=battery mode
local pwrmode="$1"
local dev log_message
# when undefined use default
: ${DISK_DEVICES:=${DEFAULT_DISK_DEVICES}}
# set @argv := timeoutlist
if [ "$pwrmode" = "1" ]; then
set -- $DISK_SPINDOWN_TIMEOUT_ON_BAT
else
set -- $DISK_SPINDOWN_TIMEOUT_ON_AC
fi
# exit if empty timeoutlist
[ $# -gt 0 ] || return 0
# pairwise iteration DISK_DEVICES[1,n], timeoutlist[1,m]; m > 0
# for j in [1,n]: disk_dev[j], timeoutlist[min(j,m)]
#
for dev in $DISK_DEVICES; do
: ${1:?BUG: broken DISK_SPINDOWN_TIMEOUT list handling}
get_disk_dev $dev
log_message="set_disk_spindown_timeout($pwrmode): $disk_dev [$disk_id] $1"
if [ ! -b /dev/$disk_dev ]; then
echo_debug "disk" "${log_message} -- missing"
elif ! check_disk_hdparm_cap $disk_dev; then
echo_debug "disk" "${log_message} -- not supported"
elif wordinlist "$1" "$DISK_NOP_WORDS"; then
echo_debug "disk" "${log_message} -- keep as is"
else
echo_debug "disk" "${log_message}"
$HDPARM -S $1 /dev/$disk_dev > /dev/null 2>&1
fi
# last entry in timeoutlist applies to all remaining disks
[ $# -lt 2 ] || shift
done
return 0
}
set_disk_io_sched () { # set disk io scheduler
local dev sched schedctrl log_message
# when undefined use default
: ${DISK_DEVICES:=${DEFAULT_DISK_DEVICES}}
# set @argv := schedlist
set -- $DISK_IOSCHED
# exit if empty timeoutlist
[ $# -gt 0 ] || return 0
# pairwise iteration DISK_DEVICES[1,n], schedlist[1,m]; m > 0
# for j in [1,min(n,m)] : disk_dev[j], schedlistj]
# for j in [min(n,m)+1,n] : disk_dev[j], %DEFAULT_DISK_IO_SCHEDULER
#
for dev in $DISK_DEVICES; do
get_disk_dev $dev
# get sched from argv, use default scheduler when list is too short
sched=${1:-${DEFAULT_DISK_IO_SCHEDULER}}
schedctrl="/sys/block/$disk_dev/queue/scheduler"
log_message="set_disk_io_sched: $disk_dev [$disk_id] $sched"
if [ ! -b /dev/$disk_dev ]; then
echo_debug "disk" "${log_message} -- missing"
elif [ ! -f $schedctrl ]; then
echo_debug "disk" "${log_message} -- not supported"
elif wordinlist "$sched" "$DISK_NOP_WORDS"; then
echo_debug "disk" "${log_message} -- keep as is"
else
echo_debug "disk" "${log_message}"
echo -n $sched > $schedctrl
fi
# using %DEFAULT_DISK_IO_SCHEDULER when argv is empty
[ $# -eq 0 ] || shift
done
return 0
}
# --- Device Power Management
set_sata_link_power () { # set sata link power management
# $1: 0=ac mode, 1=battery mode
local i
local pwr=""
local ctrl_avail="0"
if [ "$1" = "1" ]; then
pwr=${SATA_LINKPWR_ON_BAT:-}
else