1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
4 ;;; This file is part of GNU Guix.
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.
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.
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 linux)
20 #:use-module (guix gexp)
21 #:use-module (guix records)
22 #:use-module (guix modules)
23 #:use-module (gnu services)
24 #:use-module (gnu services shepherd)
25 #:use-module (gnu packages linux)
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-26)
28 #:use-module (ice-9 match)
29 #:export (earlyoom-configuration
30 earlyoom-configuration?
31 earlyoom-configuration-earlyoom
32 earlyoom-configuration-minimum-available-memory
33 earlyoom-configuration-minimum-free-swap
34 earlyoom-configuration-prefer-regexp
35 earlyoom-configuration-avoid-regexp
36 earlyoom-configuration-memory-report-interval
37 earlyoom-configuration-ignore-positive-oom-score-adj?
38 earlyoom-configuration-show-debug-messages?
39 earlyoom-configuration-send-notification-command
40 earlyoom-service-type))
47 (define-record-type* <earlyoom-configuration>
48 earlyoom-configuration make-earlyoom-configuration
49 earlyoom-configuration?
50 (earlyoom earlyoom-configuration-earlyoom
52 (minimum-available-memory earlyoom-configuration-minimum-available-memory
53 (default 10)) ; in percent
54 (minimum-free-swap earlyoom-configuration-minimum-free-swap
55 (default 10)) ; in percent
56 (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
58 (avoid-regexp earlyoom-configuration-avoid-regexp ; <string>
60 (memory-report-interval earlyoom-configuration-memory-report-interval
61 (default 0)) ; in seconds; 0 means disabled
62 (ignore-positive-oom-score-adj?
63 earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
64 (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
66 (show-debug-messages? earlyoom-configuration-show-debug-messages?
68 (send-notification-command
69 earlyoom-configuration-send-notification-command ; <string>
72 (define (earlyoom-configuration->command-line-args config)
73 "Translate a <earlyoom-configuration> object to its command line arguments
76 (($ <earlyoom-configuration> earlyoom minimum-available-memory
77 minimum-free-swap prefer-regexp avoid-regexp
78 memory-report-interval
79 ignore-positive-oom-score-adj?
80 run-with-higher-priority? show-debug-messages?
81 send-notification-command)
82 `(,(file-append earlyoom "/bin/earlyoom")
83 ,@(if minimum-available-memory
84 (list "-m" (format #f "~s" minimum-available-memory))
86 ,@(if minimum-free-swap
87 (list "-s" (format #f "~s" minimum-free-swap))
90 (list "--prefer" prefer-regexp)
93 (list "--avoid" avoid-regexp)
95 "-r" ,(format #f "~s" memory-report-interval)
96 ,@(if ignore-positive-oom-score-adj?
99 ,@(if run-with-higher-priority?
102 ,@(if show-debug-messages?
105 ,@(if send-notification-command
106 (list "-N" send-notification-command)
109 (define (earlyoom-shepherd-service config)
111 (documentation "Run the Early OOM daemon.")
112 (provision '(earlyoom))
113 (start #~(make-forkexec-constructor
114 '#$(earlyoom-configuration->command-line-args config)
115 #:log-file "/var/log/earlyoom.log"))
116 (stop #~(make-kill-destructor))))
118 (define earlyoom-service-type
121 (default-value (earlyoom-configuration))
123 (list (service-extension shepherd-root-service-type
124 (compose list earlyoom-shepherd-service))))
125 (description "Run @command{earlyoom}, the Early OOM daemon.")))