pm.scm 14 KB

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