pm.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. (file-like 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. (start-charge-thresh-bat0
  207. (maybe-non-negative-integer 'disabled)
  208. "Percentage when battery 0 should begin charging.")
  209. (stop-charge-thresh-bat0
  210. (maybe-non-negative-integer 'disabled)
  211. "Percentage when battery 0 should stop charging.")
  212. (start-charge-thresh-bat1
  213. (maybe-non-negative-integer 'disabled)
  214. "Percentage when battery 1 should begin charging.")
  215. (stop-charge-thresh-bat1
  216. (maybe-non-negative-integer 'disabled)
  217. "Percentage when battery 1 should stop charging.")
  218. (radeon-power-profile-on-ac
  219. (string "high")
  220. "Radeon graphics clock speed level. Alternatives are
  221. low, mid, high, auto, default.")
  222. (radeon-power-profile-on-bat
  223. (string "low")
  224. "Same as @code{radeon-power-ac} but on BAT mode.")
  225. (radeon-dpm-state-on-ac
  226. (string "performance")
  227. "Radeon dynamic power management method (DPM). Alternatives are
  228. battery, performance.")
  229. (radeon-dpm-state-on-bat
  230. (string "battery")
  231. "Same as @code{radeon-dpm-state-ac} but on BAT mode.")
  232. (radeon-dpm-perf-level-on-ac
  233. (string "auto")
  234. "Radeon DPM performance level. Alternatives are
  235. auto, low, high.")
  236. (radeon-dpm-perf-level-on-bat
  237. (string "auto")
  238. "Same as @code{radeon-dpm-perf-ac} but on BAT mode.")
  239. (wifi-pwr-on-ac?
  240. (on-off-boolean #f)
  241. "Wifi power saving mode.")
  242. (wifi-pwr-on-bat?
  243. (on-off-boolean #t)
  244. "Same as @code{wifi-power-ac?} but on BAT mode.")
  245. (wol-disable?
  246. (y-n-boolean #t)
  247. "Disable wake on LAN.")
  248. (sound-power-save-on-ac
  249. (non-negative-integer 0)
  250. "Timeout duration in seconds before activating audio power saving
  251. on Intel HDA and AC97 devices. A value of 0 disables power saving.")
  252. (sound-power-save-on-bat
  253. (non-negative-integer 1)
  254. "Same as @code{sound-powersave-ac} but on BAT mode.")
  255. (sound-power-save-controller?
  256. (y-n-boolean #t)
  257. "Disable controller in powersaving mode on Intel HDA devices.")
  258. (bay-poweroff-on-bat?
  259. (boolean #f)
  260. "Enable optical drive in UltraBay/MediaBay on BAT mode.
  261. Drive can be powered on again by releasing (and reinserting) the eject lever
  262. or by pressing the disc eject button on newer models.")
  263. (bay-device
  264. (string "sr0")
  265. "Name of the optical drive device to power off.")
  266. (runtime-pm-on-ac
  267. (string "on")
  268. "Runtime Power Management for PCI(e) bus devices. Alternatives are
  269. on and auto.")
  270. (runtime-pm-on-bat
  271. (string "auto")
  272. "Same as @code{runtime-pm-ac} but on BAT mode.")
  273. (runtime-pm-all?
  274. (boolean #t)
  275. "Runtime Power Management for all PCI(e) bus devices, except
  276. blacklisted ones.")
  277. (runtime-pm-blacklist
  278. (maybe-space-separated-string-list 'disabled)
  279. "Exclude specified PCI(e) device addresses from Runtime Power Management.")
  280. (runtime-pm-driver-blacklist
  281. (space-separated-string-list '("radeon" "nouveau"))
  282. "Exclude PCI(e) devices assigned to the specified drivers from
  283. Runtime Power Management.")
  284. (usb-autosuspend?
  285. (boolean #t)
  286. "Enable USB autosuspend feature.")
  287. (usb-blacklist
  288. (maybe-string 'disabled)
  289. "Exclude specified devices from USB autosuspend.")
  290. (usb-blacklist-wwan?
  291. (boolean #t)
  292. "Exclude WWAN devices from USB autosuspend.")
  293. (usb-whitelist
  294. (maybe-string 'disabled)
  295. "Include specified devices into USB autosuspend, even if they are
  296. already excluded by the driver or via @code{usb-blacklist-wwan?}.")
  297. (usb-autosuspend-disable-on-shutdown?
  298. (maybe-boolean 'disabled)
  299. "Enable USB autosuspend before shutdown.")
  300. (restore-device-state-on-startup?
  301. (boolean #f)
  302. "Restore radio device state (bluetooth, wifi, wwan) from previous
  303. shutdown on system startup."))
  304. (define (tlp-shepherd-service config)
  305. (let* ((tlp-bin (file-append
  306. (tlp-configuration-tlp config) "/bin/tlp"))
  307. (tlp-action (lambda args
  308. #~(lambda _
  309. (zero? (system* #$tlp-bin #$@args))))))
  310. (list (shepherd-service
  311. (documentation "Run TLP script.")
  312. (provision '(tlp))
  313. (requirement '(user-processes))
  314. (start (tlp-action "init" "start"))
  315. (stop (tlp-action "init" "stop"))))))
  316. (define (tlp-activation config)
  317. (let* ((config-str (with-output-to-string
  318. (lambda ()
  319. (serialize-configuration
  320. config
  321. tlp-configuration-fields))))
  322. (config-file (plain-file "tlp" config-str)))
  323. (with-imported-modules '((guix build utils))
  324. #~(begin
  325. (use-modules (guix build utils))
  326. (copy-file #$config-file "/etc/tlp.conf")))))
  327. (define tlp-service-type
  328. (service-type
  329. (name 'tlp)
  330. (extensions
  331. (list
  332. (service-extension shepherd-root-service-type
  333. tlp-shepherd-service)
  334. (service-extension udev-service-type
  335. (compose list tlp-configuration-tlp))
  336. (service-extension activation-service-type
  337. tlp-activation)))
  338. (default-value (tlp-configuration))
  339. (description "Run TLP, a power management tool.")))
  340. (define (generate-tlp-documentation)
  341. (generate-documentation
  342. `((tlp-configuration ,tlp-configuration-fields))
  343. 'tlp-configuration))
  344. ;;;
  345. ;;; thermald
  346. ;;;
  347. ;;; This service implements cpu scaling. Helps prevent overheating!
  348. (define-record-type* <thermald-configuration>
  349. thermald-configuration make-thermald-configuration
  350. thermald-configuration?
  351. (adaptive? thermald-adaptive? ;boolean
  352. (default #f))
  353. (ignore-cpuid-check? thermald-ignore-cpuid-check? ;boolean
  354. (default #f))
  355. (thermald thermald-thermald ;file-like
  356. (default thermald)))
  357. (define (thermald-shepherd-service config)
  358. (list
  359. (shepherd-service
  360. (provision '(thermald))
  361. (documentation "Run thermald cpu frequency scaling.")
  362. (start #~(make-forkexec-constructor
  363. '(#$(file-append (thermald-thermald config) "/sbin/thermald")
  364. "--no-daemon"
  365. #$@(if (thermald-adaptive? config)
  366. '("--adaptive")
  367. '())
  368. #$@(if (thermald-ignore-cpuid-check? config)
  369. '("--ignore-cpuid-check")
  370. '()))))
  371. (stop #~(make-kill-destructor)))))
  372. (define thermald-service-type
  373. (service-type
  374. (name 'thermald)
  375. (extensions (list (service-extension shepherd-root-service-type
  376. thermald-shepherd-service)))
  377. (default-value (thermald-configuration))
  378. (description "Run thermald, a CPU frequency scaling service that helps
  379. prevent overheating.")))