From: Cyril Hrubis <chrubis@suse.cz>
To: Li Wang <liwang@redhat.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score
Date: Fri, 17 Dec 2021 15:36:55 +0100 [thread overview]
Message-ID: <Ybygh2yfNmM9vbRW@yuki> (raw)
In-Reply-To: <20211217113751.359613-1-liwang@redhat.com>
Hi!
> +/*
> + * Enable OOM protection to prevent process($PID) being killed by OOM Killer.
> + * echo -1000 >/proc/$PID/oom_score_adj
> + *
> + * Note:
> + * This exported tst_enable_oom_protection function can be used at anywhere
> + * you want to protect, but please remember that if you do enable protection
> + * on a process($PID) that all the children will inherit its score and be
> + * ignored by OOM Killer as well. So that's why tst_cancel_oom_protection is
> + * recommended to combination in use.
> + */
> +void tst_enable_oom_protection(pid_t pid);
> +
> +/*
> + * Cancel the OOM protection for the process($PID).
> + * echo 0 >/proc/$PID/oom_score_adj
> + */
> +void tst_cancel_oom_protection(pid_t pid);
Minor nit: opposite of enable is disable not cancel.
> #endif /* TST_MEMUTILS_H__ */
> diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
> index bd09cf6fa..d97b35007 100644
> --- a/lib/tst_memutils.c
> +++ b/lib/tst_memutils.c
> @@ -3,6 +3,7 @@
> * Copyright (c) 2020 SUSE LLC <mdoucha@suse.cz>
> */
>
> +#include <stdio.h>
> #include <unistd.h>
> #include <limits.h>
> #include <sys/sysinfo.h>
> @@ -91,3 +92,31 @@ long long tst_available_mem(void)
>
> return mem_available;
> }
> +
> +static void set_oom_score_adj(pid_t pid, int value)
> +{
> + int val;
> + char score_path[64];
> +
> + if (access("/proc/self/oom_score_adj", F_OK) == -1) {
> + tst_res(TINFO, "Warning: oom_score_adj does not exist");
> + return;
> + }
> +
> + sprintf(score_path, "/proc/%d/oom_score_adj", pid);
> + SAFE_FILE_PRINTF(score_path, "%d", value);
> +
> + SAFE_FILE_SCANF(score_path, "%d", &val);
> + if (val != value)
> + tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value);
> +}
> +
> +void tst_enable_oom_protection(pid_t pid)
> +{
> + set_oom_score_adj(pid, -1000);
> +}
> +
> +void tst_cancel_oom_protection(pid_t pid)
> +{
> + set_oom_score_adj(pid, 0);
> +}
> --
> 2.31.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
prev parent reply other threads:[~2021-12-17 14:35 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-17 11:37 [LTP] [PATCH v2 1/3] lib: add functions to adjust oom score Li Wang
2021-12-17 11:37 ` [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process Li Wang
2021-12-17 14:49 ` Cyril Hrubis
2021-12-17 11:37 ` [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process Li Wang
2021-12-17 14:51 ` Cyril Hrubis
2021-12-20 5:01 ` Li Wang
2021-12-17 14:36 ` Cyril Hrubis [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Ybygh2yfNmM9vbRW@yuki \
--to=chrubis@suse.cz \
--cc=liwang@redhat.com \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox