| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> |
| 3 | ;;; |
| 4 | ;;; This file is part of GNU Guix. |
| 5 | ;;; |
| 6 | ;;; GNU Guix is free software; you can redistribute it and/or modify |
| 7 | ;;; it under the terms of the GNU General Public License as published by |
| 8 | ;;; the Free Software Foundation, either version 3 of the License, or |
| 9 | ;;; (at your option) any later version. |
| 10 | ;;; |
| 11 | ;;; GNU Guix is distributed in the hope that it will be useful, |
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | ;;; GNU General Public License for more details. |
| 15 | ;;; |
| 16 | ;;; You should have received a copy of the GNU General Public License |
| 17 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | (define-module (gnu services pm) |
| 20 | #:use-module (guix gexp) |
| 21 | #:use-module (guix packages) |
| 22 | #:use-module (guix records) |
| 23 | #:use-module (gnu packages admin) |
| 24 | #:use-module (gnu packages linux) |
| 25 | #:use-module (gnu services) |
| 26 | #:use-module (gnu services base) |
| 27 | #:use-module (gnu services configuration) |
| 28 | #:use-module (gnu services shepherd) |
| 29 | #:use-module (gnu system shadow) |
| 30 | #:export (tlp-service-type |
| 31 | tlp-configuration |
| 32 | |
| 33 | thermald-configuration |
| 34 | thermald-service-type)) |
| 35 | |
| 36 | (define (uglify-field-name field-name) |
| 37 | (let ((str (symbol->string field-name))) |
| 38 | (string-join (string-split |
| 39 | (string-upcase |
| 40 | (if (string-suffix? "?" str) |
| 41 | (substring str 0 (1- (string-length str))) |
| 42 | str)) |
| 43 | #\-) |
| 44 | "_"))) |
| 45 | |
| 46 | (define (serialize-field field-name val) |
| 47 | (format #t "~a=~a\n" (uglify-field-name field-name) val)) |
| 48 | |
| 49 | (define (serialize-boolean field-name val) |
| 50 | (serialize-field field-name (if val "1" "0"))) |
| 51 | (define-maybe boolean) |
| 52 | |
| 53 | (define (serialize-string field-name val) |
| 54 | (serialize-field field-name val)) |
| 55 | (define-maybe string) |
| 56 | |
| 57 | (define (space-separated-string-list? val) |
| 58 | (and (list? val) |
| 59 | (and-map (lambda (x) |
| 60 | (and (string? x) (not (string-index x #\space)))) |
| 61 | val))) |
| 62 | (define (serialize-space-separated-string-list field-name val) |
| 63 | (serialize-field field-name |
| 64 | (format #f "~s" |
| 65 | (string-join val " ")))) |
| 66 | (define-maybe space-separated-string-list) |
| 67 | |
| 68 | (define (non-negative-integer? val) |
| 69 | (and (exact-integer? val) (not (negative? val)))) |
| 70 | (define (serialize-non-negative-integer field-name val) |
| 71 | (serialize-field field-name val)) |
| 72 | (define-maybe non-negative-integer) |
| 73 | |
| 74 | (define (on-off-boolean? val) |
| 75 | (boolean? val)) |
| 76 | (define (serialize-on-off-boolean field-name val) |
| 77 | (serialize-field field-name (if val "on" "off"))) |
| 78 | (define-maybe on-off-boolean) |
| 79 | |
| 80 | (define (y-n-boolean? val) |
| 81 | (boolean? val)) |
| 82 | (define (serialize-y-n-boolean field-name val) |
| 83 | (serialize-field field-name (if val "Y" "N"))) |
| 84 | |
| 85 | (define-configuration tlp-configuration |
| 86 | (tlp |
| 87 | (package tlp) |
| 88 | "The TLP package.") |
| 89 | |
| 90 | (tlp-enable? |
| 91 | (boolean #t) |
| 92 | "Set to true if you wish to enable TLP.") |
| 93 | |
| 94 | (tlp-default-mode |
| 95 | (string "AC") |
| 96 | "Default mode when no power supply can be detected. Alternatives are |
| 97 | AC and BAT.") |
| 98 | |
| 99 | (disk-idle-secs-on-ac |
| 100 | (non-negative-integer 0) |
| 101 | "Number of seconds Linux kernel has to wait after the disk goes idle, |
| 102 | before syncing on AC.") |
| 103 | |
| 104 | (disk-idle-secs-on-bat |
| 105 | (non-negative-integer 2) |
| 106 | "Same as @code{disk-idle-ac} but on BAT mode.") |
| 107 | |
| 108 | (max-lost-work-secs-on-ac |
| 109 | (non-negative-integer 15) |
| 110 | "Dirty pages flushing periodicity, expressed in seconds.") |
| 111 | |
| 112 | (max-lost-work-secs-on-bat |
| 113 | (non-negative-integer 60) |
| 114 | "Same as @code{max-lost-work-secs-on-ac} but on BAT mode.") |
| 115 | |
| 116 | (cpu-scaling-governor-on-ac |
| 117 | (maybe-space-separated-string-list 'disabled) |
| 118 | "CPU frequency scaling governor on AC mode. With intel_pstate |
| 119 | driver, alternatives are powersave and performance. With acpi-cpufreq driver, |
| 120 | alternatives are ondemand, powersave, performance and conservative.") |
| 121 | |
| 122 | (cpu-scaling-governor-on-bat |
| 123 | (maybe-space-separated-string-list 'disabled) |
| 124 | "Same as @code{cpu-scaling-governor-on-ac} but on BAT mode.") |
| 125 | |
| 126 | (cpu-scaling-min-freq-on-ac |
| 127 | (maybe-non-negative-integer 'disabled) |
| 128 | "Set the min available frequency for the scaling governor on AC.") |
| 129 | |
| 130 | (cpu-scaling-max-freq-on-ac |
| 131 | (maybe-non-negative-integer 'disabled) |
| 132 | "Set the max available frequency for the scaling governor on AC.") |
| 133 | |
| 134 | (cpu-scaling-min-freq-on-bat |
| 135 | (maybe-non-negative-integer 'disabled) |
| 136 | "Set the min available frequency for the scaling governor on BAT.") |
| 137 | |
| 138 | (cpu-scaling-max-freq-on-bat |
| 139 | (maybe-non-negative-integer 'disabled) |
| 140 | "Set the max available frequency for the scaling governor on BAT.") |
| 141 | |
| 142 | (cpu-min-perf-on-ac |
| 143 | (maybe-non-negative-integer 'disabled) |
| 144 | "Limit the min P-state to control the power dissipation of the CPU, |
| 145 | in AC mode. Values are stated as a percentage of the available performance.") |
| 146 | |
| 147 | (cpu-max-perf-on-ac |
| 148 | (maybe-non-negative-integer 'disabled) |
| 149 | "Limit the max P-state to control the power dissipation of the CPU, |
| 150 | in AC mode. Values are stated as a percentage of the available performance.") |
| 151 | |
| 152 | (cpu-min-perf-on-bat |
| 153 | (maybe-non-negative-integer 'disabled) |
| 154 | "Same as @code{cpu-min-perf-on-ac} on BAT mode.") |
| 155 | |
| 156 | (cpu-max-perf-on-bat |
| 157 | (maybe-non-negative-integer 'disabled) |
| 158 | "Same as @code{cpu-max-perf-on-ac} on BAT mode.") |
| 159 | |
| 160 | (cpu-boost-on-ac? |
| 161 | (maybe-boolean 'disabled) |
| 162 | "Enable CPU turbo boost feature on AC mode.") |
| 163 | |
| 164 | (cpu-boost-on-bat? |
| 165 | (maybe-boolean 'disabled) |
| 166 | "Same as @code{cpu-boost-on-ac?} on BAT mode.") |
| 167 | |
| 168 | (sched-powersave-on-ac? |
| 169 | (boolean #f) |
| 170 | "Allow Linux kernel to minimize the number of CPU cores/hyper-threads |
| 171 | used under light load conditions.") |
| 172 | |
| 173 | (sched-powersave-on-bat? |
| 174 | (boolean #t) |
| 175 | "Same as @code{sched-powersave-on-ac?} but on BAT mode.") |
| 176 | |
| 177 | (nmi-watchdog? |
| 178 | (boolean #f) |
| 179 | "Enable Linux kernel NMI watchdog.") |
| 180 | |
| 181 | (phc-controls |
| 182 | (maybe-string 'disabled) |
| 183 | "For Linux kernels with PHC patch applied, change CPU voltages. |
| 184 | An example value would be @samp{\"F:V F:V F:V F:V\"}.") |
| 185 | |
| 186 | (energy-perf-policy-on-ac |
| 187 | (string "performance") |
| 188 | "Set CPU performance versus energy saving policy on AC. Alternatives are |
| 189 | performance, normal, powersave.") |
| 190 | |
| 191 | (energy-perf-policy-on-bat |
| 192 | (string "powersave") |
| 193 | "Same as @code{energy-perf-policy-ac} but on BAT mode.") |
| 194 | |
| 195 | (disks-devices |
| 196 | (space-separated-string-list '("sda")) |
| 197 | "Hard disk devices.") |
| 198 | |
| 199 | (disk-apm-level-on-ac |
| 200 | (space-separated-string-list '("254" "254")) |
| 201 | "Hard disk advanced power management level.") |
| 202 | |
| 203 | (disk-apm-level-on-bat |
| 204 | (space-separated-string-list '("128" "128")) |
| 205 | "Same as @code{disk-apm-bat} but on BAT mode.") |
| 206 | |
| 207 | (disk-spindown-timeout-on-ac |
| 208 | (maybe-space-separated-string-list 'disabled) |
| 209 | "Hard disk spin down timeout. One value has to be specified for |
| 210 | each declared hard disk.") |
| 211 | |
| 212 | (disk-spindown-timeout-on-bat |
| 213 | (maybe-space-separated-string-list 'disabled) |
| 214 | "Same as @code{disk-spindown-timeout-on-ac} but on BAT mode.") |
| 215 | |
| 216 | (disk-iosched |
| 217 | (maybe-space-separated-string-list 'disabled) |
| 218 | "Select IO scheduler for disk devices. One value has to be specified |
| 219 | for each declared hard disk. Example alternatives are cfq, deadline and noop.") |
| 220 | |
| 221 | (sata-linkpwr-on-ac |
| 222 | (string "max_performance") |
| 223 | "SATA aggressive link power management (ALPM) level. Alternatives are |
| 224 | min_power, medium_power, max_performance.") |
| 225 | |
| 226 | (sata-linkpwr-on-bat |
| 227 | (string "min_power") |
| 228 | "Same as @code{sata-linkpwr-ac} but on BAT mode.") |
| 229 | |
| 230 | (sata-linkpwr-blacklist |
| 231 | (maybe-string 'disabled) |
| 232 | "Exclude specified SATA host devices for link power management.") |
| 233 | |
| 234 | (ahci-runtime-pm-on-ac? |
| 235 | (maybe-on-off-boolean 'disabled) |
| 236 | "Enable Runtime Power Management for AHCI controller and disks |
| 237 | on AC mode.") |
| 238 | |
| 239 | (ahci-runtime-pm-on-bat? |
| 240 | (maybe-on-off-boolean 'disabled) |
| 241 | "Same as @code{ahci-runtime-pm-on-ac} on BAT mode.") |
| 242 | |
| 243 | (ahci-runtime-pm-timeout |
| 244 | (non-negative-integer 15) |
| 245 | "Seconds of inactivity before disk is suspended.") |
| 246 | |
| 247 | (pcie-aspm-on-ac |
| 248 | (string "performance") |
| 249 | "PCI Express Active State Power Management level. Alternatives are |
| 250 | default, performance, powersave.") |
| 251 | |
| 252 | (pcie-aspm-on-bat |
| 253 | (string "powersave") |
| 254 | "Same as @code{pcie-aspm-ac} but on BAT mode.") |
| 255 | |
| 256 | (radeon-power-profile-on-ac |
| 257 | (string "high") |
| 258 | "Radeon graphics clock speed level. Alternatives are |
| 259 | low, mid, high, auto, default.") |
| 260 | |
| 261 | (radeon-power-profile-on-bat |
| 262 | (string "low") |
| 263 | "Same as @code{radeon-power-ac} but on BAT mode.") |
| 264 | |
| 265 | (radeon-dpm-state-on-ac |
| 266 | (string "performance") |
| 267 | "Radeon dynamic power management method (DPM). Alternatives are |
| 268 | battery, performance.") |
| 269 | |
| 270 | (radeon-dpm-state-on-bat |
| 271 | (string "battery") |
| 272 | "Same as @code{radeon-dpm-state-ac} but on BAT mode.") |
| 273 | |
| 274 | (radeon-dpm-perf-level-on-ac |
| 275 | (string "auto") |
| 276 | "Radeon DPM performance level. Alternatives are |
| 277 | auto, low, high.") |
| 278 | |
| 279 | (radeon-dpm-perf-level-on-bat |
| 280 | (string "auto") |
| 281 | "Same as @code{radeon-dpm-perf-ac} but on BAT mode.") |
| 282 | |
| 283 | (wifi-pwr-on-ac? |
| 284 | (on-off-boolean #f) |
| 285 | "Wifi power saving mode.") |
| 286 | |
| 287 | (wifi-pwr-on-bat? |
| 288 | (on-off-boolean #t) |
| 289 | "Same as @code{wifi-power-ac?} but on BAT mode.") |
| 290 | |
| 291 | (wol-disable? |
| 292 | (y-n-boolean #t) |
| 293 | "Disable wake on LAN.") |
| 294 | |
| 295 | (sound-power-save-on-ac |
| 296 | (non-negative-integer 0) |
| 297 | "Timeout duration in seconds before activating audio power saving |
| 298 | on Intel HDA and AC97 devices. A value of 0 disables power saving.") |
| 299 | |
| 300 | (sound-power-save-on-bat |
| 301 | (non-negative-integer 1) |
| 302 | "Same as @code{sound-powersave-ac} but on BAT mode.") |
| 303 | |
| 304 | (sound-power-save-controller? |
| 305 | (y-n-boolean #t) |
| 306 | "Disable controller in powersaving mode on Intel HDA devices.") |
| 307 | |
| 308 | (bay-poweroff-on-bat? |
| 309 | (boolean #f) |
| 310 | "Enable optical drive in UltraBay/MediaBay on BAT mode. |
| 311 | Drive can be powered on again by releasing (and reinserting) the eject lever |
| 312 | or by pressing the disc eject button on newer models.") |
| 313 | |
| 314 | (bay-device |
| 315 | (string "sr0") |
| 316 | "Name of the optical drive device to power off.") |
| 317 | |
| 318 | (runtime-pm-on-ac |
| 319 | (string "on") |
| 320 | "Runtime Power Management for PCI(e) bus devices. Alternatives are |
| 321 | on and auto.") |
| 322 | |
| 323 | (runtime-pm-on-bat |
| 324 | (string "auto") |
| 325 | "Same as @code{runtime-pm-ac} but on BAT mode.") |
| 326 | |
| 327 | (runtime-pm-all? |
| 328 | (boolean #t) |
| 329 | "Runtime Power Management for all PCI(e) bus devices, except |
| 330 | blacklisted ones.") |
| 331 | |
| 332 | (runtime-pm-blacklist |
| 333 | (maybe-space-separated-string-list 'disabled) |
| 334 | "Exclude specified PCI(e) device addresses from Runtime Power Management.") |
| 335 | |
| 336 | (runtime-pm-driver-blacklist |
| 337 | (space-separated-string-list '("radeon" "nouveau")) |
| 338 | "Exclude PCI(e) devices assigned to the specified drivers from |
| 339 | Runtime Power Management.") |
| 340 | |
| 341 | (usb-autosuspend? |
| 342 | (boolean #t) |
| 343 | "Enable USB autosuspend feature.") |
| 344 | |
| 345 | (usb-blacklist |
| 346 | (maybe-string 'disabled) |
| 347 | "Exclude specified devices from USB autosuspend.") |
| 348 | |
| 349 | (usb-blacklist-wwan? |
| 350 | (boolean #t) |
| 351 | "Exclude WWAN devices from USB autosuspend.") |
| 352 | |
| 353 | (usb-whitelist |
| 354 | (maybe-string 'disabled) |
| 355 | "Include specified devices into USB autosuspend, even if they are |
| 356 | already excluded by the driver or via @code{usb-blacklist-wwan?}.") |
| 357 | |
| 358 | (usb-autosuspend-disable-on-shutdown? |
| 359 | (maybe-boolean 'disabled) |
| 360 | "Enable USB autosuspend before shutdown.") |
| 361 | |
| 362 | (restore-device-state-on-startup? |
| 363 | (boolean #f) |
| 364 | "Restore radio device state (bluetooth, wifi, wwan) from previous |
| 365 | shutdown on system startup.")) |
| 366 | |
| 367 | |
| 368 | (define (tlp-shepherd-service config) |
| 369 | (let* ((tlp-bin (file-append |
| 370 | (tlp-configuration-tlp config) "/bin/tlp")) |
| 371 | (tlp-action (lambda args |
| 372 | #~(lambda _ |
| 373 | (zero? (system* #$tlp-bin #$@args)))))) |
| 374 | (list (shepherd-service |
| 375 | (documentation "Run TLP script.") |
| 376 | (provision '(tlp)) |
| 377 | (requirement '(user-processes)) |
| 378 | (start (tlp-action "init" "start")) |
| 379 | (stop (tlp-action "init" "stop")))))) |
| 380 | |
| 381 | (define (tlp-activation config) |
| 382 | (let* ((config-str (with-output-to-string |
| 383 | (lambda () |
| 384 | (serialize-configuration |
| 385 | config |
| 386 | tlp-configuration-fields)))) |
| 387 | (config-file (plain-file "tlp" config-str))) |
| 388 | (with-imported-modules '((guix build utils)) |
| 389 | #~(begin |
| 390 | (use-modules (guix build utils)) |
| 391 | (copy-file #$config-file "/etc/tlp.conf"))))) |
| 392 | |
| 393 | (define tlp-service-type |
| 394 | (service-type |
| 395 | (name 'tlp) |
| 396 | (extensions |
| 397 | (list |
| 398 | (service-extension shepherd-root-service-type |
| 399 | tlp-shepherd-service) |
| 400 | (service-extension udev-service-type |
| 401 | (compose list tlp-configuration-tlp)) |
| 402 | (service-extension activation-service-type |
| 403 | tlp-activation))) |
| 404 | (default-value (tlp-configuration)) |
| 405 | (description "Run TLP, a power management tool."))) |
| 406 | |
| 407 | (define (generate-tlp-documentation) |
| 408 | (generate-documentation |
| 409 | `((tlp-configuration ,tlp-configuration-fields)) |
| 410 | 'tlp-configuration)) |
| 411 | |
| 412 | \f |
| 413 | |
| 414 | ;;; |
| 415 | ;;; thermald |
| 416 | ;;; |
| 417 | ;;; This service implements cpu scaling. Helps prevent overheating! |
| 418 | |
| 419 | (define-record-type* <thermald-configuration> |
| 420 | thermald-configuration make-thermald-configuration |
| 421 | thermald-configuration? |
| 422 | (ignore-cpuid-check? thermald-ignore-cpuid-check? ;boolean |
| 423 | (default #f)) |
| 424 | (thermald thermald-thermald ;package |
| 425 | (default thermald))) |
| 426 | |
| 427 | (define (thermald-shepherd-service config) |
| 428 | (list |
| 429 | (shepherd-service |
| 430 | (provision '(thermald)) |
| 431 | (documentation "Run thermald cpu frequency scaling.") |
| 432 | (start #~(make-forkexec-constructor |
| 433 | '(#$(file-append (thermald-thermald config) "/sbin/thermald") |
| 434 | "--no-daemon" |
| 435 | #$@(if (thermald-ignore-cpuid-check? config) |
| 436 | '("--ignore-cpuid-check") |
| 437 | '())))) |
| 438 | (stop #~(make-kill-destructor))))) |
| 439 | |
| 440 | (define thermald-service-type |
| 441 | (service-type |
| 442 | (name 'thermald) |
| 443 | (extensions (list (service-extension shepherd-root-service-type |
| 444 | thermald-shepherd-service))) |
| 445 | (default-value (thermald-configuration)) |
| 446 | (description "Run thermald, a CPU frequency scaling service that helps |
| 447 | prevent overheating."))) |