summarylogtreecommitdiffstats
path: root/throttle
blob: 9f1656a6b954e1e50333a1b7fd943a5da0ac21fe (plain)
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
#!/bin/bash
# throttle - user discrection power management utility

throttle_ht() {
  for i in /sys/devices/system/cpu/cpu*/online; do \
    [[ "$(printf "${i}" | tr -cd [:digit:])" -ge "${CORES}" ]] && \
      printf "${HYPERTHREADS}" > "${i}" ; done &
  [[ "${HYPERTHREADS}" == "1" ]] && wait
}

check_ht() {
  printf "\nHyperthreads\n/sys/devices/system/cpu/cpu*/online\n"
  for i in /sys/devices/system/cpu/cpu*/online; do \
    [[ $(printf "${i}" | tr -cd [:digit:]) -ge "$CORES" ]] && \
      printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/devices/system/cpu/||g; s|/.*||g; N;s|\n|\t|'
}

throttle_gov() {
  for i in /sys/bus/cpu/drivers/processor/cpu*/cpufreq/scaling_governor; do \
    [[ "$(printf ${i} | tr -cd [:digit:])" -ge "${CORES}" && \
       "$(cat /sys/devices/system/cpu/cpu$(printf ${i} | tr -cd [:digit:])/online)" == 0 ]] && \
      continue ; printf "${GOVERNOR}" > "${i}" & done &
}

check_gov() {
  printf "\nCPU Governor\n/sys/bus/cpu/drivers/processor/cpu*/cpufreq/scaling_governor\n"
  for i in /sys/bus/cpu/drivers/processor/cpu*/cpufreq/scaling_governor; do \
    printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/bus/cpu/drivers/processor/||g; s|/.*||g; N;s|\n|\t|'
}

throttle_turbo() {
  [[ -f /sys/devices/system/cpu/intel_pstate/no_turbo ]] && \
    printf "${XPSTURBO}" > /sys/devices/system/cpu/intel_pstate/no_turbo &
}

check_turbo() {
  [[ -f /sys/devices/system/cpu/intel_pstate/no_turbo ]] && \
    echo -e "\nDisable Intel P-State Turbo\n/sys/devices/system/cpu/intel_pstate/\n"\
            "no_turbo\t$(cat /sys/devices/system/cpu/intel_pstate/no_turbo)"
}

throttle_backlight() {
  [[ -f /sys/class/backlight/acpi_video0 ]] && \
    for i in /sys/class/backlight/acpi_video*/brightness; do \
      printf "${BACKLIGHT}" > "${i}" & done &
}

check_backlight() {
  [[ -f /sys/class/backlight/acpi_video0 ]] && \
    printf "\nBacklight\n/sys/class/backlight/\n" && \
      for i in /sys/class/backlight/acpi_video*/brightness; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/class/backlight/||g; s|/.*||g; N;s|\n|\t|'
}

throttle_nvpm() {
  printf "nvidia-settings not found; only nvidia currently supported" || \
    DISPLAY=":0.0" nvidia-settings -a [gpu:0]/GPUPowerMizerMode="${NVPM}" > /dev/null &
}

check_nvpm() {
  [[ -n "$(command -v nvidia-settings)" ]] && \
    printf "\nNvidia PowerMizer\n " && \
      DISPLAY=":0.0" nvidia-settings -q [gpu:0]/GPUPowerMizerMode | grep "Attribute" | sed 's|.*\[||g;s|\]):||g;s| |\t|g;s|\.$||g'
}

throttle_help() {
  [[ ! "${1}" == "help" ]] && \
    printf "Invalid input: ${@}\n"
  echo -e "\nRuntime power management:\n"\
          "${0} {{cut,full}} - system-wide runtime powersaving\n"\
          "${0} {check} - inspect runtime powersaving\n"\
          "\nExtras:\n" \
          "${0} {gov,cpu} {cut,full} - CPU Governor\n"\
          "${0} turbo {on,off} - Intel P-State Turbo\n"\
          "${0} ht {on,off} - Hyperthreads\n"\
          "${0} backlight {0,1,2} - Backlight brightness (laptops only?)\n" \
          "${0} gpu {cut,full} - GPU runtime powersaving (only Nvidia ATM)\n"\
          "\nOptions also take {{powersave,start},{performace,stop},status} and are stackable:\n"\
          "\n${0} powersave gov full turbo on gpu full ht on check"
  exit
}

[[ -z "$@" ]] && throttle_help

#Find the number of physical cores (for hyperthreading control)
CORES="$(grep "^core id" /proc/cpuinfo | sort -u | wc -l)"

while (( "$#" )); do
  case "$1" in
    ht*)
      case "${1}" in \
        ht-on) HYPERTHREADS="1" ;; \
        ht-off) HYPERTHREADS="0" ;; esac
      [[ -z "${HYPERTHREADS}" ]] && \
        case "${2}" in \
          off|powersave|start) HYPERTHREADS="0"; SHIFT="2" ;; \
          on|performance|stop) HYPERTHREADS="1";  SHIFT="2" ;; \
          check|status) check_ht; SHIFT="2" ;; esac
      [[ -n "${HYPERTHREADS}" ]] && throttle_ht
      [[ -n "${SHIFT}" ]] && shift "${SHIFT}" || shift
      unset HYPERTHREADS SHIFT
    ;;
    gov*|cpu)
      case "${1}" in \
        gov-full) GOVERNOR="performance" ;; \
        gov-cut) GOVERNOR="powersave" ;; esac
      [[ -z "${GOVERNOR}" ]] && \
        case "${2}" in \
          cut|powersave|start) GOVERNOR="powersave"; SHIFT="2" ;; \
          full|performance|stop) GOVERNOR="performance"; SHIFT="2" ;; \
          check|status) check_gov; SHIFT="2" ;; esac
      [[ -n "${GOVERNOR}" ]] && throttle_gov
      [[ -n "${SHIFT}" ]] && shift "${SHIFT}" || shift
      unset GOVERNOR SHIFT
    ;;
    turbo*)
      case "${1}" in \
        turbo-on) XPSTURBO="0" ;; \
        turbo-off) XPSTURBO="1" ;; esac
      [[ -z "${XPSTURBO}" ]] && \
        case "${2}" in \
          off|powersave|start) XPSTURBO="1"; SHIFT="2" ;; \
          on|performance|stop) XPSTURBO="0"; SHIFT="2" ;; \
          check|status) check_turbo; SHIFT="2" ;; esac
      [[ -n "${XPSTURBO}" ]] && throttle_turbo
      [[ -n "${SHIFT}" ]] && shift "${SHIFT}" || shift
      unset XPSTURBO SHIFT
    ;;
    backlight)
      case "${2}" in \
        off|0|powersave|start) BACKLIGHT="0"; SHIFT="2" ;; \
        on|1|performance|stop) BACKLIGHT="1"; SHIFT="2" ;; \
        2) BACKLIGHT="2"; SHIFT="2" ;; \
        check|status) check_backlight; SHIFT="2" ;; esac
      [[ -n "${BACKLIGHT}" ]] && throttle_backlight
      [[ -n "${SHIFT}" ]] && shift "${SHIFT}" || shift
      unset BACKLIGHT SHIFT
    ;;
    gpu*)
      case "${1}" in \
        gpu-full) NVPM="1" ;; \
        gpu-cut) NVPM="0" ;; esac
      [[ -z "${NVPM}" ]] && \
        case "${2}" in \
          full|performance|stop) NVPM="1"; SHIFT="2" ;; \
          cut|powersave|start) NVPM="0"; SHIFT="2" ;; \
          check|status) check_nvpm; SHIFT="2" ;; esac
      [[ -n "${NVPM}" ]] && [[ -z "$(command -v nvidia-settings)" ]] && \
            throttle_nvpm
      [[ -n "${SHIFT}" ]] && shift "${SHIFT}" || shift
      unset NVPM SHIFT
    ;;
    full|performance|true|1|start|cut|powersave|false|0|stop)
      case "${1}" in
        full|performance|false|0|stop)
          . /etc/throttle.d/performance
        ;;
        cut|powersave|true|1|start)
          . /etc/throttle.d/powersave
        ;;
      esac

      # Hyperthreads
      throttle_ht

      # CPU Governor
      throttle_gov

      # Disable Intel P-State Turbo
      throttle_turbo

      # ENERGY_PERF_BIAS
      [[ -n "$(command -v x86_energy_perf_policy)" ]] && \
        x86_energy_perf_policy "${ENERGYPERF}" &

      # Backlight
      throttle_backlight

      # Virtual Memory (Swap)
      [[ "$(sysctl -n vm.laptop_mode)" == "${LAPTOP}" ]] || \
        printf "${LAPTOP}" > /proc/sys/vm/laptop_mode &
      [[ "$(sysctl -n vm.dirty_writeback_centisecs)" == "${DIRTYWBC}" ]] || \
        printf "${DIRTYWBC}" > /proc/sys/vm/dirty_writeback_centisecs &
      [[ "$(sysctl -n vm.dirty_expire_centisecs)" == "${DIRTYWBC}" ]] || \
        printf "${DIRTYWBC}" > /proc/sys/vm/dirty_expire_centisecs &

      # NMI watchdog
      [[ -f /proc/sys/kernel/watchdog ]] && \
          printf "${NMIDOG}" > /proc/sys/kernel/watchdog &

      # SATA link power management
      for i in /sys/class/scsi_host/host*/link_power_management_policy; do [[ -f "${i}" ]] && \
        printf "${POLICY}" > "${i}" & done &

      # Hard drives
      [[ -n "$(command -v hdparm)" ]] && \
        hdparm -qB "${APM}" -qM "${AAM}" /dev/[hs]d[a-z] 2> /dev/null &

      # Block device autosuspend
      for i in /sys/block/*/device/power/autosuspend_delay_ms; do \
        printf -- "${DISKAUTOSUSPEND}" > "${i}" & done &

      # Runtime power management for devices (with exceptions for devices by VID:PID)
      for i in /sys/{class/*,bus/*/devices}/*/power/control; do \
        [[ "${CONTROL}" == "auto" ]] && \
          [[ -n "$(printf ${i} | grep msr[${CORES}-$((${CORES} * 2 - 1))])" || \
             -n "$(printf ${i} | grep machinecheck[${CORES}-$((${CORES} * 2 - 1))])" || \
             -n "$(ls $(printf ${i} | sed 's|power.*||g')/*/ | grep 046D\:C52B)" || \
             -n "$(ls $(printf ${i} | sed 's|power.*||g')/*/ | grep 054C\:0268)" || \
             -f "$(printf ${i} | sed 's|/power.*|/idVendor|g')" && \
             "$(cat $(printf ${i} | sed 's|/power.*|/idVendor|g'))" == "0424" && \
             "$(cat $(printf ${i} | sed 's|/power.*|/idProduct|g'))" == "a700" ]] && \
            continue ; printf "${CONTROL}" > "${i}" & done &

      # USB Autosuspend (with exceptions for devices by VID:PID)
      for i in /sys/bus/usb/devices/*/power/autosuspend{,_delay_ms}; do \
        [[ "${AUTOSUSPEND}" == "1" ]] && \
          [[ -n "$(ls $(printf ${i} | sed 's|power.*||g')/*/ | grep 046D\:C52B)" || \
             -n "$(ls $(printf ${i} | sed 's|power.*||g')/*/ | grep 054C\:0268)" || \
             -f "$(printf ${i} | sed 's|/power.*|/idVendor|g')" && \
             "$(cat $(printf ${i} | sed 's|/power.*|/idVendor|g'))" == "0424" && \
             "$(cat $(printf ${i} | sed 's|/power.*|/idProduct|g'))" == "a700" ]] && \
            continue ; printf -- "${AUTOSUSPEND}" > "${i}" & done &

      # Powersaving for modules
      for i in /sys/module/*/parameters/power_save; do \
        printf "${POWERSAVE}" > "${i}" & done &
      for i in /sys/module/*/parameters/power_save_controller; do \
        printf "${CONTROLLER}" > "${i}" & done &

      # Network powersaving
      for i in /sys/class/net/wl*; do \
        [[ -n "$(command -v iw)" ]] && \
          iw dev "$(printf ${i} | sed 's/^.*wl/wl/')" set power_save "${WLPOWERSAVE}" 2> /dev/null & done &
      for i in /sys/class/net/e*; do \
        [[ -n "$(command -v ethtool)" ]] && \
          ethtool -s "$(printf ${i} | sed 's/^.*e/e/')" wol "${WOLA}" 2> /dev/null & done &
      for i in /sys/class/net/*/device/power/wakeup; do \
        printf "${WOLB}" > "${i}" & done &

      # LEDs
      for i in /sys/class/leds/*/brightness; do \
        printf "${LEDBRIGHT}" > "${i}" & done &

      # Nvidia PowerMizer
      throttle_nvpm

      shift
      unset GOVERNOR XPSTURBO HYPERTHREADS ENERGYPERF BACKLIGHT LAPTOP DIRTYWBC NMIDOG \
            POLICY APM AAM CONTROL AUTOSUSPEND POWERSAVE CONTROLLER \
            WLPOWERSAVE WOLA WOLB LEDBRIGHT NVPM
    ;;
    check|status)
      check_ht

      check_gov

      check_turbo

      [[ -n "$(command -v x86_energy_perf_policy)" ]] && \
        printf "\nEnergy Perf Bias\n" && \
        x86_energy_perf_policy -r #SUID

      check_backlight

      echo -e "\nVirtual Memory (Swap)\n/proc/sys/vm/\n"\
              "laptop_mode\t\t\t$(cat /proc/sys/vm/laptop_mode)\n"\
              "dirty_writeback_centisecs\t$(cat /proc/sys/vm/dirty_writeback_centisecs)\n"\
              "dirty_expire_centisecs\t\t$(cat /proc/sys/vm/dirty_expire_centisecs)"

      [[ -f /proc/sys/kernel/watchdog ]] && \
        echo -e "\nNMI watchdog\n"\
                "/proc/sys/kernel/watchdog\t$(cat /proc/sys/kernel/watchdog)"

      printf "\nSATA link power management\n/sys/class/scsi_host/host*/link_power_management_policy\n"
      for i in /sys/class/scsi_host/host*/link_power_management_policy; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/class/scsi_host/||g; s|/.*||g; N;s|\n|\t|'

      [[ -n "$(command -v hdparm)" ]] && \
        printf "\nHard drives\n/dev/[hs]d[a-z]\n" && \
        hdparm -B -M /dev/[hs]d[a-z] 2> /dev/null | sed 'N;s|\n||g; s|/dev/||g; s|\t| |g;' #SUID

      printf "\nBlock device autosuspend\n/sys/block/*/device/power/autosuspend_delay_ms\n"
      for i in /sys/block/*/device/power/autosuspend_delay_ms; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/block/||g; s|/device/.*||g; s|/.*||g; N;s|\n|\t|'

      printf "\nRuntime power management\n/sys/{class/*,bus/*/devices}/*/power/control\n"
      for i in /sys/{class/*,bus/*/devices}/*/power/control; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/class/||g; s|/sys/bus/||g; s|/devices||g; s|/power/control||g; N;s|\n|\t|'

      printf "\nUSB Autosuspend\n/sys/bus/usb/devices/*/power/autosuspend{,_delay_ms}\n"
      for i in /sys/bus/usb/devices/*/power/autosuspend{,_delay_ms}; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/bus/usb/devices/||g; s|/.*||g; N;s|\n|\t|'

      printf "\nPower saving for modules\n/sys/module/*/parameters/power_save{,_controller}\n"
      for i in /sys/module/*/parameters/power_save{,_controller}; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/module/||g; s|/parameters/.*||g; s|/.*||g; N;s|\n|\t|'

      printf "\nNetwork device powersaving\n/sys/class/net/{wl*,e*,*/device/power/wakeup}\n";
      [[ -n "$(command -v iw)" ]] && \
        for i in /sys/class/net/wl*; do \
          printf " ${i}\n$(iw dev $(printf ${i} | sed 's/^.*wl/wl/') get power_save)"; done | sed 's|^\t|  |g;s|/sys/class/net/||g; s|/.*||g'
      [[ -n "$(command -v ethtool)" ]] && \
        for i in /sys/class/net/e*; do \
          printf " ${i}\n$(ethtool $(printf ${i} | sed 's/^.*e/e/') | grep Wake-on)"; done | sed 's|^\t|  |g;s|/sys/class/net/||g; s|/.*||g'
      for i in /sys/class/net/*/device/power/wakeup; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/class/net/||g; s|/device/power/wakeup||g; s|/.*||g; N;s|\n|\t|'

      printf "\nLEDs\n/sys/class/leds/*/brightness\n"
      for i in /sys/class/leds/*/brightness; do \
        printf " ${i}\n$(cat ${i})\n"; done | sed 's|/sys/class/leds/||g; s|/brightness||g; s|/.*||g; N;s|\n|\t|'

      check_nvpm
      shift
    ;;
    help|*)
      throttle_help
    ;;
  esac
  [[ -z "${1}" ]] || wait # Hey, let's stop! :) *stops* ... YAY! =D
done

unset CORES