lib: add functions to adjust oom score
PATCH v3 1/3 lib: add functions to adjust oom score
This introduces function to LTP for adjusting the oom_score_adj of target process, which may be helpful in OOM tests to prevent kernel killing the main or lib process during test running.
The exported global 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_disable_oom_protection is recommended to combination in use.
---
Notes:
v2 --> v3
* rename to tst_disable_oom_protection
* support set PID as 0 to protect current process
include/tst_memutils.h | 20 ++++++++++++++++++++ lib/tst_memutils.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+)
include/tst_memutils.h +20 -0
@@ -25,4 +25,24 @@ void tst_pollute_memory(size_t maxsize, int fillchar);*/ long long tst_available_mem (void ); +/* +* Enable OOM protection to prevent process($PID) being killed by OOM Killer. +* echo -1000 >/proc/$PID/oom_score_adj +* If the pid is 0 which means it will set on current(self) process. +* +* 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_disable_oom_protection +* is recommended to combination in use. +*/ +void tst_enable_oom_protection (pid_t pid); + +/* +* Disable the OOM protection for the process($PID). +* echo 0 >/proc/$PID/oom_score_adj +*/ +void tst_disable_oom_protection (pid_t pid); +#endif /* TST_MEMUTILS_H__ */
lib/tst_memutils.c +32 -0
@@ -3,6 +3,7 @@* Copyright (c) 2020 SUSE LLC <[email protected]> */ +#include <stdio.h> #include <unistd.h> #include <limits.h> #include <sys/sysinfo.h>
@@ -91,3 +92,34 @@ 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 ; + } + +if (pid ==0 ) +sprintf (score_path,"/proc/self/oom_score_adj" ); +else +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_disable_oom_protection (pid_t pid) +{ +set_oom_score_adj (pid,0 ); +}
Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 2/3 lib: enable OOM protection for ltp lib process
We do protect ltp-lib ($PID) process from killing by OOM Killer, hope this can help to get the completed correct report for all of LTP tests.
This achieve by invoking tst_enable_oom_protection in tst_run_tcases, at the same time, we purposely disabling the protection for children in fork_testrun, to avoid the oom score inherit by testcase.
Fundamental principle:
ltp test harness --> library process
(enable protection) main --> tst_run_tcases --> ... --> fork_testrun
(disable protection) testrun --> run_tests --> ... --> testname
child_test --> ... --> end--- lib/tst_test.c | 2 ++ 1 file changed, 2 insertions(+)
lib/tst_test.c +2 -0
@@ -1446,6 +1446,7 @@ static int fork_testrun(void)tst_brk (TBROK |TERRNO ,"fork()" );if (!test_pid) { +tst_disable_oom_protection (0 );SAFE_SIGNAL (SIGALRM ,SIG_DFL );SAFE_SIGNAL (SIGUSR1 ,SIG_DFL );SAFE_SIGNAL (SIGINT ,SIG_DFL );
@@ -1523,6 +1524,7 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self) tst_test = self;do_setup (argc, argv); +tst_enable_oom_protection (lib_pid);SAFE_SIGNAL (SIGALRM , alarm_handler);SAFE_SIGNAL (SIGUSR1 , heartbeat_handler);
Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 2/3] lib: enable OOM protection for ltp lib process
Hi!
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 3/3 oom: enable OOM protection for mem lib process
In reply to Li WangJust simply invoke oom protection on mem library to make it can collect full state of children.
--- testcases/kernel/mem/lib/mem.c | 3 +++ 1 file changed, 3 insertions(+)
testcases/kernel/mem/lib/mem.c +3 -0
@@ -129,8 +129,11 @@ void oom(int testcase, int lite, int retcode, int allow_sigkill)pid_t pid;int status, threads; +tst_enable_oom_protection (0 ); +switch (pid =SAFE_FORK ()) {case 0 : +tst_disable_oom_protection (0 ); threads =MAX (1 ,tst_ncpus () -1 );child_alloc (testcase, lite, threads);default :
Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 3/3] oom: enable OOM protection for mem lib process
Hi!
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 1 line
This introduces function to LTP for adjusting the
oom_score_adjof
^ to adjust
Quoted 2 lines
target process, which may be helpful in OOM tests to prevent kernel killing the main or lib process during test running.
^ test run.
Quoted 5 lines
The exported global
tst_enable_oom_protectionfunction 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 whytst_disable_oom_protectionis recommended to combination in use.
^ to be used in combination.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Li,
Quoted 3 lines
v2 --> v3
* rename totst_disable_oom_protection
* support set PID as 0 to protect current process
+1 to the changes + to Cyril suggestions for docs.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Li,
lib/tst_memutils.c long long tst_available_mem(void) · line 101
Quoted 10 lines
v2 --> v3
* rename totst_disable_oom_protection
* support set PID as 0 to protect current processlib/tst_memutils.c +6
-0+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 ) {
We need to check here also /proc/PID/oom_score_adj, i.e. score_path.
Otherwise tests without root fail fail.lib/tst_memutils.c:111: TBROK: Failed to close FILE '/proc/26980/oom_score_adj': EACCES (13)
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 6 lines · Petr Vorel
Hi Li,
Quoted 3 lines
v2 --> v3
* rename totst_disable_oom_protection
* support set PID as 0 to protect current process
Quoted 8 lines
lib/tst_memutils.c long long tst_available_mem(void) · line 101
Quoted 6 lines
lib/tst_memutils.c +6
-0+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 ) {We need to check here also
/proc/PID/oom_score_adj, i.e.score_path.
Good catch, I would add a 'W_OK' checking and skip the setting with a reminder message if run without root.
how about this?
if (access(score_path, W_OK) == -1) {
tst_res(TINFO, "Warning: %s cannot be accessed for writing,
please check if test run with root user.", score_path);
return
}
Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Li,
Quoted 7 lines
Quoted 2 lines
Quoted 1 line
+ if (
access("/proc/self/oom_score_adj", F_OK)== -1) {We need to check here also
/proc/PID/oom_score_adj, i.e.score_path.Good catch, I would add a '
W_OK' checking and skip the setting with a reminder message if run without root.
Quoted 8 lines
how about this?
if (
access(score_path, W_OK)== -1) {
tst_res(TINFO, "Warning: %s cannot be accessed for writing,
please check if test run with root user.",score_path);
return
}
No, value < 0 obviously requires root, thus access() check is not enough. Try echo -1 > /proc/$$/oom_score_adj
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 16 lines
Quoted 4 lines
Quoted 3 lines
v2 --> v3
* rename totst_disable_oom_protection
* support set PID as 0 to protect current processQuoted 8 lines
lib/tst_memutils.c long long tst_available_mem(void) · line 101
Quoted 6 lines
lib/tst_memutils.c +6
-0+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 ) {We need to check here also
/proc/PID/oom_score_adj, i.e.score_path.Good catch, I would add a '
W_OK' checking and skip the setting with a reminder message if run without root.
Quoted 6 lines
how about this?
if (
access(score_path, W_OK)== -1) {
tst_res(TINFO, "Warning: %s cannot be accessed for writing,
please check if test run with root user.",score_path);
Hmm, I guess that we should produce TINFO if the file does not exist and TWARN if we cannot write to it. Something as:
if (access(score_path, F_OK)) {
tst_res(TINFO,
"'%s' does not exist, skipping OOM score adjustement",
score_path);
return;
}if (access(score_path, W_OK)) {
tst_res(TWARN, "'%s' not writeable, are you root?", score_path);
return;
}Quoted 7 lines
return
}Signature · 2 lines
Regards, Li Wang
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 19 lines · Cyril Hrubis
Hi!
Quoted 18 lines
Quoted 4 lines
Quoted 3 lines
v2 --> v3
* rename totst_disable_oom_protection
* support set PID as 0 to protect current processQuoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+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) {We need to check here also
/proc/PID/oom_score_adj, i.e.score_path.Good catch, I would add a '
W_OK' checking and skip the setting with a reminder message if run without root.how about this?
Quoted 8 lines
Quoted 4 lines
if (
access(score_path, W_OK)== -1) {
tst_res(TINFO, "Warning: %s cannot be accessed for writing,
please check if test run with root user.",score_path);Hmm, I guess that we should produce TINFO if the file does not exist and TWARN if we cannot write to it. Something as:
Not exactly, if someone gives a wrong PID, that also cannot find the score_path. So we shouldn't skip OOM adjustment only with printing the TFINO.
Quoted 8 lines
if (access(score_path, F_OK)) {
tst_res(TINFO,
"'%s' does not exist, skipping OOM score adjustement",
score_path);
return;
}Quoted 5 lines
if (access(score_path, W_OK)) {
tst_res(TWARN, "'%s' not writeable, are you root?", score_path);
return;
}As Petr points out, only root user can set a negative value to oom_score_adj,
this W_OK is not enough for ordinary users.
Consider about situation, I'd suggest go with non-safe macros and add additional check in the last.
e.g.
lib/tst_memutils.c +0 -2
@@ -108,17 +108,21 @@ static void set_oom_score_adj(pid_t pid, int value)else sprintf (score_path,"/proc/%d/oom_score_adj" , pid); -if (access (score_path,R_OK |W_OK ) ==-1 ) { - tst_res(TINFO, "Warning: %s cannot be accessed for
reading/writing,
- please check if test run with root user.",
- score_path);
- return
- }
-
- SAFE_FILE_PRINTF(score_path, "%d", value); - SAFE_FILE_SCANF(score_path, "%d", &val); - if (val != value) + if (access(score_path, F_OK) == -1)
+ tst_brk(TBROK, "%s does not exist, please check if PID is
valid");
+
+ FILE_PRINTF(score_path, "%d", value);
+ FILE_SCANF(score_path, "%d", &val);
+
+ if (val != value) {
+ if (value < 0) {
+ tst_res(TINFO, "Warning: %s cannot be set to
negative value,
+ please check if test run with root user.",
+ score_path);
+ return
+ }
tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val,
value);
+ }
}
Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 27 lines · Li Wang
Consider about situation, I'd suggest go with non-safe macros and add additional check in the last.
e.g.
lib/tst_memutils.c +6
-10@@ -108,17 +108,21 @@ static void set_oom_score_adj(pid_t pid, int value)else sprintf (score_path,"/proc/%d/oom_score_adj" , pid); -if (access (score_path,R_OK |W_OK ) ==-1 ) { - tst_res(TINFO, "Warning: %s cannot be accessed for reading/writing, - please check if test run with root user.", - score_path); - return - } - -SAFE_FILE_PRINTF (score_path,"%d" , value); -SAFE_FILE_SCANF (score_path,"%d" , &val); -if (val != value) +if (access (score_path,F_OK ) ==-1 ) +tst_brk (TBROK ,"%s does not exist, please check if PID is valid" ); + +FILE_PRINTF (score_path,"%d" , value); +FILE_SCANF (score_path,"%d" , &val); +
lib/tst_memutils.c static void set_oom_score_adj(pid_t pid, int value) · line 126
Quoted 10 lines
lib/tst_memutils.c +8
-0+if (val != value) { +if (value <0 ) { + tst_res(TINFO, "Warning: %s cannot be set to negative value, + please check if test run with root user.", + score_path); + return + }tst_brk (TBROK ,"oom_score_adj = %d, but expect %d." , val, value); + } }
Sorry, the above patch is based on my private v3.1, plz ignore it.
For better read, here give the whole function:
+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,
skipping OOM the adjustement");
+ return;
+ }
+
+ if (pid == 0)
+ sprintf(score_path, "/proc/self/oom_score_adj");
+ else
+ sprintf(score_path, "/proc/%d/oom_score_adj", pid);
+
+ if (access(score_path, F_OK) == -1)
+ tst_brk(TBROK, "%s does not exist, please check if PID
is valid");
+
+ FILE_PRINTF(score_path, "%d", value);
+ FILE_SCANF(score_path, "%d", &val);
+
+ if (val != value) {
+ if (value < 0) {
+ tst_res(TINFO, "Warning: %s cannot be set to
negative value,
+ please check if test run with root user.",
+ score_path);
+ return
+ }
+ tst_brk(TBROK, "oom_score_adj = %d, but expect %d.",
val, value);
+ }
+}
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 3 lines
Not exactly, if someone gives a wrong PID, that also cannot find the
score_path. So we shouldn't skip OOM adjustment only with printing the TFINO.
Right, we would have to check if the /proc/$PID/ directory exists first.
Quoted 13 lines
Quoted 7 lines
if (access(score_path, F_OK)) { tst_res(TINFO, "'%s' does not exist, skipping OOM score adjustement", score_path); return; }Quoted 5 lines
if (access(score_path, W_OK)) { tst_res(TWARN, "'%s' not writeable, are you root?", score_path); return; }
Quoted 3 lines
As Petr points out, only root user can set a negative value to
oom_score_adj,
thisW_OKis not enough for ordinary users.
Ah, right this makes it even more complex.
Quoted 15 lines
Consider about situation, I'd suggest go with non-safe macros and add additional check in the last.
e.g.
lib/tst_memutils.c +0
-3@@ -108,17 +108,21 @@ static void set_oom_score_adj(pid_t pid, int value)else sprintf (score_path,"/proc/%d/oom_score_adj" , pid); -if (access (score_path,R_OK |W_OK ) ==-1 ) { - tst_res(TINFO, "Warning: %s cannot be accessed for reading/writing, - please check if test run with root user.",
lib/tst_memutils.c static void set_oom_score_adj(pid_t pid, int value) · line 114
Quoted 10 lines
lib/tst_memutils.c +2
-7- score_path); -return - } - -SAFE_FILE_PRINTF (score_path,"%d" , value); -SAFE_FILE_SCANF (score_path,"%d" , &val); -if (val != value) +if (access (score_path,F_OK ) ==-1 ) +tst_brk (TBROK ,"%s does not exist, please check if PID is valid");
Maybe we should print the pid in the message here as well.
lib/tst_memutils.c static void set_oom_score_adj(pid_t pid, int value) · line 123
Quoted 9 lines
lib/tst_memutils.c +8
-0+ +FILE_PRINTF (score_path,"%d" , value); +FILE_SCANF (score_path,"%d" , &val); + +if (val != value) { +if (value <0 ) { + tst_res(TINFO, "Warning: %s cannot be set to negative value, + please check if test run with root user.",
I would say that we TBROK here, otherwise it could be silently ignored. Also I would shorten the message to something as:
"'%s' cannot be set to %i, are you root?", score_path, value);
Quoted 12 lines
lib/tst_memutils.c +3
-0+ score_path); +return + } tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val,value);
+ }
}Signature · 2 lines
Regards, Li Wang
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 7 lines · Cyril Hrubis
Hi!
Quoted 3 lines
Not exactly, if someone gives a wrong PID, that also cannot find the
score_path. So we shouldn't skip OOM adjustment only with printing the TFINO.Right, we would have to check if the
/proc/$PID/directory exists first.
Not necessary, because we did check /proc/self/oom_score_adj already, otherwise, we have no chance to get here.
Quoted 5 lines
Quoted 3 lines
(quoted diff) +2
-0+ if (access(score_path, F_OK) == -1) + tst_brk(TBROK, "%s does not exist, please check if PID isvalid");
Maybe we should print the pid in the message here as well.
Yes, and the score_path will include the pid, here I forget in the sentence.
Quoted 8 lines
Quoted 5 lines
(quoted diff) +3
-0+ if (val != value) { + if (value < 0) { + tst_res(TINFO, "Warning: %s cannot be set tonegative value,
+ please check if test run with root user.",I would say that we TBROK here, otherwise it could be silently ignored. Also I would shorten the message to something as:
Hmm, maybe tst_res(TWARN, ...), is enough, I don't want to let people who run LTP in ordinary users get TBROK here since they might even don't need the oom protection.
So, I will push (the improved) code like below, after getting Petr and you ack again:
+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,
+ skipping the adjustement");
+ return;
+ }
+
+ if (pid == 0) {
+ sprintf(score_path, "/proc/self/oom_score_adj");
+ } else {
+ sprintf(score_path, "/proc/%d/oom_score_adj", pid);
+ if (access(score_path, F_OK) == -1)
+ tst_brk(TBROK, "%s does not exist, please
check if PID is valid", score_path);
+ }
+
+ FILE_PRINTF(score_path, "%d", value);
+ FILE_SCANF(score_path, "%d", &val);
+
+ if (val != value) {
+ if (value < 0) {
+ tst_res(TWARN, "'%s' cannot be set to %i, are
you root?",
+ score_path, value);
+ return
+ }
+ tst_brk(TBROK, "oom_score_adj = %d, but expect %d.",
val, value);
+ }
+}
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Cyril Hrubis
Reviewed-byHi!
Review
Quoted 11 lines
So, I will push (the improved) code like below, after getting Petr and you ack again:
(quoted diff) +8
-0+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, + skipping the adjustement");
I'm not sure about the "Warning:" in this message, I would just dully informed the user that the interface is not available.
Quoted 24 lines
(quoted diff) +9
-0+ return;` + } + + if (pid == 0) { + sprintf(score_path, "/proc/self/oom_score_adj"); + } else { + sprintf(score_path, "/proc/%d/oom_score_adj", pid); + if (access(score_path, F_OK) == -1) + tst_brk(TBROK, "%s does not exist, pleasecheck if PID is valid",
score_path);(quoted diff) +8
-0+ } + + FILE_PRINTF(score_path, "%d", value); + FILE_SCANF(score_path, "%d", &val); + + if (val != value) { + if (value < 0) { + tst_res(TWARN, "'%s' cannot be set to %i, areyou root?",
(quoted diff) +4
-0+ score_path, value); + return + } + tst_brk(TBROK, "oom_score_adj = %d, but expect %d.",val, value);
Review
Quoted 2 lines
(quoted diff) +2
-0+ } +}
Anyways this version looks good to me:
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 4 lines
Hi!
Quoted 2 lines
So, I will push (the improved) code like below, after getting Petr and you ack again:
Quoted 11 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+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, + skipping the adjustement");I'm not sure about the "Warning:" in this message, I would just dully informed the user that the interface is not available.
I'd also prefer not having to print "Warning:" in TINFO message (I know TWARN exit non-zero).
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Petr, Cyril,
Quoted 9 lines · Petr Vorel
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +3
-0+ if (access("/proc/self/oom_score_adj", F_OK) == -1) { + tst_res(TINFO, "Warning: oom_score_adj does not exist, + skipping the adjustement");I'm not sure about the "Warning:" in this message, I would just dully informed the user that the interface is not available.
I'd also prefer not having to print "Warning:" in TINFO message (I know TWARN exit non-zero).
Okay, I deleted that keyword and pushed patchset.
Thanks a lot for your both review.
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp