Subject: Re: bug#39283: [PATCH] gnu: Add earlyoom.

From: Maxim Cournoyer <[email-masked]>
Date: Fri, 31 Jan 2020 02:43:44 -0500

Hello Pkill -9,

Pkill -9 <[email-masked]> writes:

> * gnu/packages/linux.scm (earlyoom): New variable.
> ---
>  gnu/packages/linux.scm | 37 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 37 insertions(+)
>
> diff --git a/gnu/packages/linux.scm b/gnu/packages/linux.scm
> index e5cc773ecc..892906cc70 100644
> --- a/gnu/packages/linux.scm
> +++ b/gnu/packages/linux.scm
> @@ -6215,3 +6215,40 @@ the Linux kernel, C standard library, and libdl (for dynamic linking).  While
>  ELL is designed to be efficient and compact enough for use on embedded Linux
>  platforms, it is not limited to resource-constrained systems.")
>      (license license:lgpl2.1+)))
> +
> +(define-public earlyoom
> +  (package
> +   (name "earlyoom")
> +   (version "1.3")
> +   (source (origin
> +            (method git-fetch)
> +            (uri (git-reference
> +                  (url "https://github.com/rfjakob/earlyoom.git")
> +                  (commit (string-append "v" version))))
> +            (file-name (git-file-name name version))
> +            (sha256
> +             (base32
> +              "0fwbx0y80nqgkxrc9kf9j3iwa0wbps2jmqir3pgqbc2cj0wjh0lr"))))
> +   (build-system gnu-build-system)
> +   (arguments
> +    `(#:make-flags `("CC=gcc"
> +                     ,(string-append "DESTDIR="
> +                                     (assoc-ref %outputs "out"))
> +                     "PREFIX=")
> +      #:tests? #f ;; no tests
> +      #:phases (modify-phases
> +                %standard-phases
> +                (delete 'configure)
> +                (add-after 'install 'remove-systemd-service
> +                           (lambda* (#:key outputs #:allow-other-keys)
> +                             (let ((out (assoc-ref outputs "out")))
> +                               (delete-file-recursively
> +                                (string-append out "/etc/systemd")))
> +                             #t)))))
> +   (home-page "https://github.com/rfjakob/earlyoom")
> +   (synopsis "Automatically kill processes in low-memory situations")
> +   (description "Earlyoom checks the amount of available memory and free swap
> +up to 10 times a second (less often if there is a lot of free memory).  By
> +default if both are below 10%, it will kill the largest process (highest
> +oom_score).  The percentage value is configurable via command line arguments.")
> +   (license license:expat)))

I happened to have prepared earlyoom recently, but delayed its inclusion
due to finishing a service for it (only the doc remains).

I'm attaching it.  My version seems slightly more complete (I generate
the doc and run the tests).  If you don't have an objection I could push
it to master.

>From 3692937bb2eb0659493dfb59c43a83b2ae094214 Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <[email-masked]>
Date: Sun, 5 Jan 2020 00:44:48 -0500
Subject: [PATCH 1/2] gnu: Add earlyoom.

* gnu/packages/linux.scm (earlyoom): New variable.
---
 gnu/packages/linux.scm | 47 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/gnu/packages/linux.scm b/gnu/packages/linux.scm
index cc3455b52b..7b0241ff4b 100644
--- a/gnu/packages/linux.scm
+++ b/gnu/packages/linux.scm
@@ -33,7 +33,7 @@
 ;;; Copyright © 2018 Pierre Langlois <[email-masked]>
 ;;; Copyright © 2018 Vasile Dumitrascu <[email-masked]>
 ;;; Copyright © 2019 Tim Gesthuizen <[email-masked]>
-;;; Copyright © 2019 Maxim Cournoyer <[email-masked]>
+;;; Copyright © 2019, 2020 Maxim Cournoyer <[email-masked]>
 ;;; Copyright © 2019 Stefan Stefanović <[email-masked]>
 ;;; Copyright © 2019 Pierre Langlois <[email-masked]>
 ;;; Copyright © 2019 Brice Waegeneire <[email-masked]>
@@ -86,6 +86,7 @@
   #:use-module (gnu packages gperf)
   #:use-module (gnu packages gstreamer)
   #:use-module (gnu packages gtk)
+  #:use-module (gnu packages haskell-xyz)
   #:use-module (gnu packages libunwind)
   #:use-module (gnu packages libusb)
   #:use-module (gnu packages man)
@@ -2640,6 +2641,50 @@ kmod.  The aim is to be compatible with tools, configurations and indices
 from the module-init-tools project.")
     (license license:gpl2+))) ; library under lgpl2.1+
 
+(define-public earlyoom
+  (package
+    (name "earlyoom")
+    (version "1.3")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/rfjakob/earlyoom.git")
+                    (commit (string-append "v" version))))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "0fwbx0y80nqgkxrc9kf9j3iwa0wbps2jmqir3pgqbc2cj0wjh0lr"))))
+    (build-system gnu-build-system)
+    (arguments
+     `(#:phases (modify-phases %standard-phases
+                  (delete 'configure)
+                  (add-before 'check 'set-home
+                    (lambda _
+                      (setenv "HOME" (getcwd))
+                      #t))
+                  (add-after 'build 'install-contribs
+                    ;; Install what seems useful from the contrib directory.
+                    (lambda* (#:key outputs #:allow-other-keys)
+                      (let* ((out (assoc-ref outputs "out"))
+                             (contrib (string-append
+                                       out "/share/earlyoom/contrib")))
+                        (install-file "contrib/notify_all_users.py" contrib)
+                        #t))))
+       #:make-flags (let* ((prefix (assoc-ref %outputs "out")))
+                      (list "CC=gcc"
+                            (string-append "VERSION=v" ,version)
+                            (string-append "PREFIX=" prefix)
+                            (string-append "SYSCONFDIR=" prefix "/etc")))
+       #:test-target "test"))
+    (native-inputs `(("go" ,go)               ;for the test suite
+                     ("pandoc" ,ghc-pandoc))) ;to generate the manpage
+    (home-page "https://github.com/rfjakob/earlyoom")
+    (synopsis "Simple out of memory (OOM) daemon for the Linux kernel")
+    (description "Early OOM is a minimalist out of memory (OOM) daemon that
+runs in user space and provides a more responsive and configurable alternative
+to the in-kernel OOM killer.")
+    (license license:expat)))
+
 (define-public eudev
   ;; The post-systemd fork, maintained by Gentoo.
   (package
-- 
2.24.1

Then there is a Shepherd service for convenience.  I haven't tried all
the switches, but I presume it'd work, given that they seem to be
translated correctly into earyloom's command line options (there's a
unit test for it).

>From 8ffb34822b6a394ef4699cdaf850e405e92009f6 Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer <[email-masked]>
Date: Mon, 6 Jan 2020 01:36:00 -0500
Subject: [PATCH 2/2] gnu: Add earlyoom-service-type.

* gnu/services/linux.scm: New file.
* tests/services/linux.scm: Add test.
* Makefile.am (SCM_TESTS): Register test.
* doc/guix.texi (Linux Services): Document the new service.
---
 Makefile.am              |   1 +
 doc/guix.texi            |  69 ++++++++++++++++++++-
 gnu/services/linux.scm   | 125 +++++++++++++++++++++++++++++++++++++++
 tests/services/linux.scm |  57 ++++++++++++++++++
 4 files changed, 251 insertions(+), 1 deletion(-)
 create mode 100644 gnu/services/linux.scm
 create mode 100644 tests/services/linux.scm

diff --git a/Makefile.am b/Makefile.am
index 7474b7f375..c6a2e6cf6c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -426,6 +426,7 @@ SCM_TESTS =					\
   tests/scripts-build.scm			\
   tests/search-paths.scm			\
   tests/services.scm				\
+  tests/services/linux.scm			\
   tests/sets.scm				\
   tests/size.scm				\
   tests/snix.scm				\
diff --git a/doc/guix.texi b/doc/guix.texi
index 3141c4582f..3f1caa764b 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -48,7 +48,7 @@ Copyright @copyright{} 2017 humanitiesNerd@*
 Copyright @copyright{} 2017 Christopher Allan Webber@*
 Copyright @copyright{} 2017, 2018, 2019 Marius Bakke@*
 Copyright @copyright{} 2017, 2019 Hartmut Goebel@*
-Copyright @copyright{} 2017, 2019 Maxim Cournoyer@*
+Copyright @copyright{} 2017, 2019, 2020 Maxim Cournoyer@*
 Copyright @copyright{} 2017, 2018, 2019, 2020 Tobias Geerinckx-Rice@*
 Copyright @copyright{} 2017 George Clemmer@*
 Copyright @copyright{} 2017 Andy Wingo@*
@@ -310,6 +310,7 @@ Services
 * Version Control Services::    Providing remote access to Git repositories.
 * Game Services::               Game servers.
 * PAM Mount Service::           Service to mount volumes when logging in.
+* Linux Services::              Services tied to the Linux kernel.
 * Miscellaneous Services::      Other services.
 
 Defining Services
@@ -12084,6 +12085,7 @@ declaration.
 * Game Services::               Game servers.
 * PAM Mount Service::           Service to mount volumes when logging in.
 * Guix Services::               Services relating specifically to Guix.
+* Linux Services::              Services tied to the Linux kernel.
 * Miscellaneous Services::      Other services.
 @end menu
 
@@ -25129,6 +25131,71 @@ list.
 @end table
 @end deftp
 
+@node Linux Services
+@subsubheading Linux Services
+
+@cindex oom
+@cindex out of memory killer
+@cindex earlyoom
+@cindex early out of memory daemon
+@subsection Early OOM Service
+
+@uref{https://github.com/rfjakob/earlyoom,Early OOM}, also known as
+Earlyoom, is a minimalist out of memory (OOM) daemon that runs in user
+space and provides a more responsive and configurable alternative to the
+in-kernel OOM killer.  It is useful to prevent the system from becoming
+unresponsive when it runs out of memory.
+
+@deffn {Scheme Variable} earlyoom-service-type
+The service type for running @command{earlyoom}, the Early OOM daemon.
+Its value must be a @code{earlyoom-configuration} object, described
+below.  The service can be instantiated in its default configuration
+with:
+
+@lisp
+(service earlyoom-service-type)
+@end lisp
+@end deffn
+
+@deftp {Data Type} earlyoom-configuration
+This is the configuration record for the @code{earlyoom-service-type}.
+
+@table @asis
+@item @code{earlyoom} (default: @var{earlyoom})
+The Earlyoom package to use.
+
+@item @code{minimum-available-memory} (default: @code{10})
+Threshold for the minimum @emph{available} memory, in percents.
+
+@item @code{minimum-free-swap} (default: @code{10})
+Threshold for the minimum free swap memory, in percents.
+
+@item @code{prefer-regexp} (default: @code{#f})
+A regular expression (as a string) to match the names of the processes
+that should be preferably killed.
+
+@item @code{avoid-regexp} (default: @code{#f})
+A regular expression (as a string) to match the names of the processes
+that should @emph{not} be killed.
+
+@item @code{memory-report-interval} (default: @code{0})
+The interval in seconds at which a memory report is printed.  It is
+disabled by default.
+
+@item @code{ignore-positive-oom-score-adj?} (default: @code{#f})
+A boolean indicating whether the positive adjustments set in
+@file{/proc/*/oom_score_adj}.
+
+@item @code{show-debug-messages?} (default: @code{#f})
+A boolean indicating whether debug messages should be printed.  The logs
+are saved at @file{/var/log/earlyoom.log}.
+
+@item @code{send-notification-command} (default: @code{#f})
+This can be used to provide a custom command used for sending
+notifications.
+@end table
+@end deftp
+
 
 @node Miscellaneous Services
 @subsection Miscellaneous Services
diff --git a/gnu/services/linux.scm b/gnu/services/linux.scm
new file mode 100644
index 0000000000..caa0326c31
--- /dev/null
+++ b/gnu/services/linux.scm
@@ -0,0 +1,125 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2020 Maxim Cournoyer <[email-masked]>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu services linux)
+  #:use-module (guix gexp)
+  #:use-module (guix records)
+  #:use-module (guix modules)
+  #:use-module (gnu services)
+  #:use-module (gnu services shepherd)
+  #:use-module (gnu packages linux)
+  #:use-module (srfi srfi-1)
+  #:use-module (srfi srfi-26)
+  #:use-module (ice-9 match)
+  #:export (earlyoom-configuration
+            earlyoom-configuration?
+            earlyoom-configuration-earlyoom
+            earlyoom-configuration-minimum-available-memory
+            earlyoom-configuration-minimum-free-swap
+            earlyoom-configuration-prefer-regexp
+            earlyoom-configuration-avoid-regexp
+            earlyoom-configuration-memory-report-interval
+            earlyoom-configuration-ignore-positive-oom-score-adj?
+            earlyoom-configuration-show-debug-messages?
+            earlyoom-configuration-send-notification-command
+            earlyoom-service-type))
+
+
+;;;
+;;; Early OOM daemon.
+;;;
+
+(define-record-type* <earlyoom-configuration>
+  earlyoom-configuration make-earlyoom-configuration
+  earlyoom-configuration?
+  (earlyoom earlyoom-configuration-earlyoom
+            (default earlyoom))
+  (minimum-available-memory earlyoom-configuration-minimum-available-memory
+                            (default 10)) ; in percent
+  (minimum-free-swap earlyoom-configuration-minimum-free-swap
+                     (default 10))      ; in percent
+  (prefer-regexp earlyoom-configuration-prefer-regexp ; <string>
+                 (default #f))
+  (avoid-regexp earlyoom-configuration-avoid-regexp  ; <string>
+                (default #f))
+  (memory-report-interval earlyoom-configuration-memory-report-interval
+                          (default 0)) ; in seconds; 0 means disabled
+  (ignore-positive-oom-score-adj?
+   earlyoom-configuration-ignore-positive-oom-score-adj? (default #f))
+  (run-with-higher-priority? earlyoom-configuration-run-with-higher-priority?
+                             (default #f))
+  (show-debug-messages? earlyoom-configuration-show-debug-messages?
+                        (default #f))
+  (send-notification-command
+   earlyoom-configuration-send-notification-command  ; <string>
+   (default #f)))
+
+(define (earlyoom-configuration->command-line-args config)
+  "Translate a <earlyoom-configuration> object to its command line arguments
+representation."
+  (match config
+    (($ <earlyoom-configuration> earlyoom minimum-available-memory
+                                 minimum-free-swap prefer-regexp avoid-regexp
+                                 memory-report-interval
+                                 ignore-positive-oom-score-adj?
+                                 run-with-higher-priority? show-debug-messages?
+                                 send-notification-command)
+     `(,(file-append earlyoom "/bin/earlyoom")
+       ,@(if minimum-available-memory
+             (list "-m" (format #f "~s" minimum-available-memory))
+             '())
+       ,@(if minimum-free-swap
+             (list "-s" (format #f "~s" minimum-free-swap))
+             '())
+       ,@(if prefer-regexp
+             (list "--prefer" prefer-regexp)
+             '())
+       ,@(if avoid-regexp
+             (list "--avoid" avoid-regexp)
+             '())
+       "-r" ,(format #f "~s" memory-report-interval)
+       ,@(if ignore-positive-oom-score-adj?
+             (list "-i")
+             '())
+       ,@(if run-with-higher-priority?
+             (list "-p")
+             '())
+       ,@(if show-debug-messages?
+             (list "-d")
+             '())
+       ,@(if send-notification-command
+             (list "-N" send-notification-command)
+             '())))))
+
+(define (earlyoom-shepherd-service config)
+  (shepherd-service
+   (documentation "Run the Early OOM daemon.")
+   (provision '(earlyoom))
+   (start #~(make-forkexec-constructor
+             '#$(earlyoom-configuration->command-line-args config)
+             #:log-file "/var/log/earlyoom.log"))
+   (stop #~(make-kill-destructor))))
+
+(define earlyoom-service-type
+  (service-type
+   (name 'earlyoom)
+   (default-value (earlyoom-configuration))
+   (extensions
+    (list (service-extension shepherd-root-service-type
+                             (compose list earlyoom-shepherd-service))))
+   (description "Run @command{earlyoom}, the Early OOM daemon.")))
diff --git a/tests/services/linux.scm b/tests/services/linux.scm
new file mode 100644
index 0000000000..8ad119c49f
--- /dev/null
+++ b/tests/services/linux.scm
@@ -0,0 +1,57 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2020 Maxim Cournoyer <[email-masked]>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (tests services linux)
+  #:use-module (ice-9 match)
+  #:use-module (gnu packages linux)
+  #:use-module (gnu services linux)
+  #:use-module (guix gexp)
+  #:use-module (srfi srfi-64))
+
+;;; Tests for the (gnu services linux) module.
+
+(test-begin "linux-services")
+
+
+;;;
+;;; Early OOM daemon.
+;;;
+
+(define earlyoom-configuration->command-line-args
+  (@@ (gnu services linux) earlyoom-configuration->command-line-args))
+
+(define %earlyoom-configuration-sample
+  (earlyoom-configuration
+   (minimum-available-memory 10)
+   (minimum-free-swap 20)
+   (prefer-regexp "icecat")
+   (avoid-regexp "guix-daemon")
+   (memory-report-interval 60)
+   (ignore-positive-oom-score-adj? #f)
+   (run-with-higher-priority? #t)
+   (show-debug-messages? #f)
+   (send-notification-command "python \"/some/path/notify-all-users.py\"")))
+
+(test-equal "earlyoom-configuration->command-line-args"
+  (list (file-append earlyoom "/bin/earlyoom")
+        "-m" "10" "-s" "20" "--prefer" "icecat"
+        "--avoid" "guix-daemon" "-r" "60" "-p"
+        "-N" "python \"/some/path/notify-all-users.py\"")
+  (earlyoom-configuration->command-line-args %earlyoom-configuration-sample))
+
+(test-end "linux-services")
-- 
2.24.1

And then enjoy your hang free system :-)

Thank you,

Maxim