rfjakob / earlyoom

earlyoom - Early OOM Daemon for Linux
MIT License
2.93k stars 155 forks source link

What kind of regexes are used for --prefer and --avoid, and how can I test them? #285

Open Holmes5 opened 1 year ago

Holmes5 commented 1 year ago

This isn't a bug in the code, just a documentation issue.

There are subtly different regex varieties out there, and I can't tell which one is being used in earlyoom for the prefer and avoid command line options. How can I verify that a program matches the prefer or avoid regex I set before launching earlyoom?

I see that there is a test function for this in the command-line test suite, but I don't know how to run it.

Holmes5 commented 1 year ago

For reference, I'm using

EARLYOOM_ARGS="--avoid '^(Tanium[a-zA-Z]*|traefik|init|jupyter[a-zA-Z]*|sshd)$' --prefer ^(python|ipython)(2|3)?$ -p -m 8,6 -s 100"
rfjakob commented 1 year ago

You can see which process would have been killed by running with --debug --dryrun. But you don't see directly whether it was because of the regex, unfortunately.

rfjakob commented 1 year ago

As to your question: It is a POSIX regex, earlyoom uses the regexec ( https://man7.org/linux/man-pages/man3/regexec.3.html ) function.

Dragorn421 commented 10 months ago

To complement the advice given by rfjakob above, if you run

earlyoom --dryrun -m 99 --prefer 'Discord'

(where -m 99 is "start killing at >1% memory usage", to force the regex to be used) you will get output that looks like

earlyoom v1.7
dryrun mode enabled, will not kill anything
Preferring to kill process names that match regex 'Discord'
mem total: 15624 MiB, swap total:    0 MiB
sending SIGTERM when mem <= 99.00% and swap <= 10.00%,
        SIGKILL when mem <= 49.50% and swap <=  5.00%
mem avail:  5972 of 15624 MiB (38.22%), swap free:    0 of    0 MiB ( 0.00%)
low memory! at or below SIGKILL limits: mem 49.50%, swap  5.00%
sending SIGKILL to process 280898 uid 1000 "Discord": badness 1114, VmRSS 341 MiB
dryrun, not actually sending any signal
mem avail:  5972 of 15624 MiB (38.22%), swap free:    0 of    0 MiB ( 0.00%)
low memory! at or below SIGKILL limits: mem 49.50%, swap  5.00%
sending SIGKILL to process 280898 uid 1000 "Discord": badness 1114, VmRSS 341 MiB
dryrun, not actually sending any signal
...

Note:

Preferring to kill process names that match regex 'Discord'

shows the regex, which is useful to check the shell didn't mess with your regex

sending SIGKILL to process 280898 uid 1000 "Discord": badness 1114, VmRSS 341 MiB

shows which process is sent a sigkill, which is useful to debug your regex if need be

I did not find the -d (debug flag, --debug does not exist afaict) to be of much use here

attila-lendvai commented 10 months ago

the above suggestion with -m 99 kinda works, but it's slow and verbose. looks like it's just running in a loop in dry-run mode, because it just keeps printing chromium for me, a couple of matches per second...

an option would be very useful to test-run a regexp on all the currently running processes. something like:

earlyoom --test-regex 'some-pattern'

which would just iterate all the currently running processes and print the matches.