1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
33 thermald-configuration
34 thermald-service-type))
36 (define (uglify-field-name field-name)
37 (let ((str (symbol->string field-name)))
38 (string-join (string-split
40 (if (string-suffix? "?" str)
41 (substring str 0 (1- (string-length str)))
46 (define (serialize-field field-name val)
47 (format #t "~a=~a\n" (uglify-field-name field-name) val))
49 (define (serialize-boolean field-name val)
50 (serialize-field field-name (if val "1" "0")))
51 (define-maybe boolean)
53 (define (serialize-string field-name val)
54 (serialize-field field-name val))
57 (define (space-separated-string-list? val)
60 (and (string? x) (not (string-index x #\space))))
62 (define (serialize-space-separated-string-list field-name val)
63 (serialize-field field-name
65 (string-join val " "))))
66 (define-maybe space-separated-string-list)
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)
74 (define (on-off-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)
80 (define (y-n-boolean? val)
82 (define (serialize-y-n-boolean field-name val)
83 (serialize-field field-name (if val "Y" "N")))
85 (define-configuration tlp-configuration
92 "Set to true if you wish to enable TLP.")
96 "Default mode when no power supply can be detected. Alternatives are
100 (non-negative-integer 0)
101 "Number of seconds Linux kernel has to wait after the disk goes idle,
102 before syncing on AC.")
104 (disk-idle-secs-on-bat
105 (non-negative-integer 2)
106 "Same as @code{disk-idle-ac} but on BAT mode.")
108 (max-lost-work-secs-on-ac
109 (non-negative-integer 15)
110 "Dirty pages flushing periodicity, expressed in seconds.")
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.")
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.")
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.")
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.")
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.")
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.")
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.")
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.")
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.")
153 (maybe-non-negative-integer 'disabled)
154 "Same as @code{cpu-min-perf-on-ac} on BAT mode.")
157 (maybe-non-negative-integer 'disabled)
158 "Same as @code{cpu-max-perf-on-ac} on BAT mode.")
161 (maybe-boolean 'disabled)
162 "Enable CPU turbo boost feature on AC mode.")
165 (maybe-boolean 'disabled)
166 "Same as @code{cpu-boost-on-ac?} on BAT mode.")
168 (sched-powersave-on-ac?
170 "Allow Linux kernel to minimize the number of CPU cores/hyper-threads
171 used under light load conditions.")
173 (sched-powersave-on-bat?
175 "Same as @code{sched-powersave-on-ac?} but on BAT mode.")
179 "Enable Linux kernel NMI watchdog.")
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\"}.")
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.")
191 (energy-perf-policy-on-bat
193 "Same as @code{energy-perf-policy-ac} but on BAT mode.")
196 (space-separated-string-list '("sda"))
197 "Hard disk devices.")
199 (disk-apm-level-on-ac
200 (space-separated-string-list '("254" "254"))
201 "Hard disk advanced power management level.")
203 (disk-apm-level-on-bat
204 (space-separated-string-list '("128" "128"))
205 "Same as @code{disk-apm-bat} but on BAT mode.")
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.")
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.")
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.")
222 (string "max_performance")
223 "SATA aggressive link power management (ALPM) level. Alternatives are
224 min_power, medium_power, max_performance.")
228 "Same as @code{sata-linkpwr-ac} but on BAT mode.")
230 (sata-linkpwr-blacklist
231 (maybe-string 'disabled)
232 "Exclude specified SATA host devices for link power management.")
234 (ahci-runtime-pm-on-ac?
235 (maybe-on-off-boolean 'disabled)
236 "Enable Runtime Power Management for AHCI controller and disks
239 (ahci-runtime-pm-on-bat?
240 (maybe-on-off-boolean 'disabled)
241 "Same as @code{ahci-runtime-pm-on-ac} on BAT mode.")
243 (ahci-runtime-pm-timeout
244 (non-negative-integer 15)
245 "Seconds of inactivity before disk is suspended.")
248 (string "performance")
249 "PCI Express Active State Power Management level. Alternatives are
250 default, performance, powersave.")
254 "Same as @code{pcie-aspm-ac} but on BAT mode.")
256 (start-charge-thresh-bat0
257 (maybe-non-negative-integer 'disabled)
258 "Percentage when battery 0 should begin charging.")
260 (stop-charge-thresh-bat0
261 (maybe-non-negative-integer 'disabled)
262 "Percentage when battery 0 should stop charging.")
264 (start-charge-thresh-bat1
265 (maybe-non-negative-integer 'disabled)
266 "Percentage when battery 1 should begin charging.")
268 (stop-charge-thresh-bat1
269 (maybe-non-negative-integer 'disabled)
270 "Percentage when battery 1 should stop charging.")
272 (radeon-power-profile-on-ac
274 "Radeon graphics clock speed level. Alternatives are
275 low, mid, high, auto, default.")
277 (radeon-power-profile-on-bat
279 "Same as @code{radeon-power-ac} but on BAT mode.")
281 (radeon-dpm-state-on-ac
282 (string "performance")
283 "Radeon dynamic power management method (DPM). Alternatives are
284 battery, performance.")
286 (radeon-dpm-state-on-bat
288 "Same as @code{radeon-dpm-state-ac} but on BAT mode.")
290 (radeon-dpm-perf-level-on-ac
292 "Radeon DPM performance level. Alternatives are
295 (radeon-dpm-perf-level-on-bat
297 "Same as @code{radeon-dpm-perf-ac} but on BAT mode.")
301 "Wifi power saving mode.")
305 "Same as @code{wifi-power-ac?} but on BAT mode.")
309 "Disable wake on LAN.")
311 (sound-power-save-on-ac
312 (non-negative-integer 0)
313 "Timeout duration in seconds before activating audio power saving
314 on Intel HDA and AC97 devices. A value of 0 disables power saving.")
316 (sound-power-save-on-bat
317 (non-negative-integer 1)
318 "Same as @code{sound-powersave-ac} but on BAT mode.")
320 (sound-power-save-controller?
322 "Disable controller in powersaving mode on Intel HDA devices.")
324 (bay-poweroff-on-bat?
326 "Enable optical drive in UltraBay/MediaBay on BAT mode.
327 Drive can be powered on again by releasing (and reinserting) the eject lever
328 or by pressing the disc eject button on newer models.")
332 "Name of the optical drive device to power off.")
336 "Runtime Power Management for PCI(e) bus devices. Alternatives are
341 "Same as @code{runtime-pm-ac} but on BAT mode.")
345 "Runtime Power Management for all PCI(e) bus devices, except
348 (runtime-pm-blacklist
349 (maybe-space-separated-string-list 'disabled)
350 "Exclude specified PCI(e) device addresses from Runtime Power Management.")
352 (runtime-pm-driver-blacklist
353 (space-separated-string-list '("radeon" "nouveau"))
354 "Exclude PCI(e) devices assigned to the specified drivers from
355 Runtime Power Management.")
359 "Enable USB autosuspend feature.")
362 (maybe-string 'disabled)
363 "Exclude specified devices from USB autosuspend.")
367 "Exclude WWAN devices from USB autosuspend.")
370 (maybe-string 'disabled)
371 "Include specified devices into USB autosuspend, even if they are
372 already excluded by the driver or via @code{usb-blacklist-wwan?}.")
374 (usb-autosuspend-disable-on-shutdown?
375 (maybe-boolean 'disabled)
376 "Enable USB autosuspend before shutdown.")
378 (restore-device-state-on-startup?
380 "Restore radio device state (bluetooth, wifi, wwan) from previous
381 shutdown on system startup."))
384 (define (tlp-shepherd-service config)
385 (let* ((tlp-bin (file-append
386 (tlp-configuration-tlp config) "/bin/tlp"))
387 (tlp-action (lambda args
389 (zero? (system* #$tlp-bin #$@args))))))
390 (list (shepherd-service
391 (documentation "Run TLP script.")
393 (requirement '(user-processes))
394 (start (tlp-action "init" "start"))
395 (stop (tlp-action "init" "stop"))))))
397 (define (tlp-activation config)
398 (let* ((config-str (with-output-to-string
400 (serialize-configuration
402 tlp-configuration-fields))))
403 (config-file (plain-file "tlp" config-str)))
404 (with-imported-modules '((guix build utils))
406 (use-modules (guix build utils))
407 (copy-file #$config-file "/etc/tlp.conf")))))
409 (define tlp-service-type
414 (service-extension shepherd-root-service-type
415 tlp-shepherd-service)
416 (service-extension udev-service-type
417 (compose list tlp-configuration-tlp))
418 (service-extension activation-service-type
420 (default-value (tlp-configuration))
421 (description "Run TLP, a power management tool.")))
423 (define (generate-tlp-documentation)
424 (generate-documentation
425 `((tlp-configuration ,tlp-configuration-fields))
433 ;;; This service implements cpu scaling. Helps prevent overheating!
435 (define-record-type* <thermald-configuration>
436 thermald-configuration make-thermald-configuration
437 thermald-configuration?
438 (adaptive? thermald-adaptive? ;boolean
440 (ignore-cpuid-check? thermald-ignore-cpuid-check? ;boolean
442 (thermald thermald-thermald ;file-like
445 (define (thermald-shepherd-service config)
448 (provision '(thermald))
449 (documentation "Run thermald cpu frequency scaling.")
450 (start #~(make-forkexec-constructor
451 '(#$(file-append (thermald-thermald config) "/sbin/thermald")
453 #$@(if (thermald-adaptive? config)
456 #$@(if (thermald-ignore-cpuid-check? config)
457 '("--ignore-cpuid-check")
459 (stop #~(make-kill-destructor)))))
461 (define thermald-service-type
464 (extensions (list (service-extension shepherd-root-service-type
465 thermald-shepherd-service)))
466 (default-value (thermald-configuration))
467 (description "Run thermald, a CPU frequency scaling service that helps
468 prevent overheating.")))