You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
5.0 KiB

  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@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 it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; 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 linux)
  19. #:use-module (guix gexp)
  20. #:use-module (guix records)
  21. #:use-module (guix modules)
  22. #:use-module (gnu services)
  23. #:use-module (gnu services shepherd)
  24. #:use-module (gnu packages linux)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-26)
  27. #:use-module (ice-9 match)
  28. #:export (earlyoom-configuration
  29. earlyoom-configuration?
  30. earlyoom-configuration-earlyoom
  31. earlyoom-configuration-minimum-available-memory
  32. earlyoom-configuration-minimum-free-swap
  33. earlyoom-configuration-prefer-regexp
  34. earlyoom-configuration-avoid-regexp
  35. earlyoom-configuration-memory-report-interval
  36. earlyoom-configuration-ignore-positive-oom-score-adj?
  37. earlyoom-configuration-show-debug-messages?
  38. earlyoom-configuration-send-notification-command
  39. earlyoom-service-type))
  40. ;;;
  41. ;;; Early OOM daemon.
  42. ;;;
  43. (define-record-type* <earlyoom-configuration>
  44. earlyoom-configuration make-earlyoom-configuration
  45. earlyoom-configuration?
  46. (earlyoom earlyoom-configuration-earlyoom
  47. (default earlyoom))
  48. (minimum-available-memory earlyoom-configuration-minimum-available-memory
  49. (default 10)) ; in percent
  50. (minimum-free-swap earlyoom-configuration-minimum-free-swap
  51. (default 10)) ; in percent
  52. (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
  53. (default #f))
  54. (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
  55. (default #f))
  56. (memory-report-interval earlyoom-configuration-memory-report-interval
  57. (default 0)) ; in seconds; 0 means disabled
  58. (ignore-positive-oom-score-adj?
  59. earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
  60. (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
  61. (default #f))
  62. (show-debug-messages? earlyoom-configuration-show-debug-messages?
  63. (default #f))
  64. (send-notification-command
  65. earlyoom-configuration-send-notification-command ; <string>
  66. (default #f)))
  67. (define (earlyoom-configuration->command-line-args config)
  68. "Translate a <earlyoom-configuration> object to its command line arguments
  69. representation."
  70. (match config
  71. (($ <earlyoom-configuration> earlyoom minimum-available-memory
  72. minimum-free-swap prefer-regexp avoid-regexp
  73. memory-report-interval
  74. ignore-positive-oom-score-adj?
  75. run-with-higher-priority? show-debug-messages?
  76. send-notification-command)
  77. `(,(file-append earlyoom "/bin/earlyoom")
  78. ,@(if minimum-available-memory
  79. (list "-m" (format #f "~s" minimum-available-memory))
  80. '())
  81. ,@(if minimum-free-swap
  82. (list "-s" (format #f "~s" minimum-free-swap))
  83. '())
  84. ,@(if prefer-regexp
  85. (list "--prefer" prefer-regexp)
  86. '())
  87. ,@(if avoid-regexp
  88. (list "--avoid" avoid-regexp)
  89. '())
  90. "-r" ,(format #f "~s" memory-report-interval)
  91. ,@(if ignore-positive-oom-score-adj?
  92. (list "-i")
  93. '())
  94. ,@(if run-with-higher-priority?
  95. (list "-p")
  96. '())
  97. ,@(if show-debug-messages?
  98. (list "-d")
  99. '())
  100. ,@(if send-notification-command
  101. (list "-N" send-notification-command)
  102. '())))))
  103. (define (earlyoom-shepherd-service config)
  104. (shepherd-service
  105. (documentation "Run the Early OOM daemon.")
  106. (provision '(earlyoom))
  107. (start #~(make-forkexec-constructor
  108. '#$(earlyoom-configuration->command-line-args config)
  109. #:log-file "/var/log/earlyoom.log"))
  110. (stop #~(make-kill-destructor))))
  111. (define earlyoom-service-type
  112. (service-type
  113. (name 'earlyoom)
  114. (default-value (earlyoom-configuration))
  115. (extensions
  116. (list (service-extension shepherd-root-service-type
  117. (compose list earlyoom-shepherd-service))))
  118. (description "Run @command{earlyoom}, the Early OOM daemon.")))