lib: add functions to adjust oom score
PATCH v2 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_cancel_oom_protection is recommended to combination in use.
---
Notes:
Changes v1->v2:
* Move commit messages to source code comment
* correct typos
include/tst_memutils.h | 19 +++++++++++++++++++ lib/tst_memutils.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
include/tst_memutils.h +19 -0
@@ -25,4 +25,23 @@ 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 +* +* 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); +#endif /* TST_MEMUTILS_H__ */
lib/tst_memutils.c +29 -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,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 ); +}
Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 3/3 oom: enable OOM protection for mem lib process
Just 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 (getpid ()); +switch (pid =SAFE_FORK ()) {case 0 : +tst_cancel_oom_protection (getpid ()); 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
PATCH v2 2/3 lib: enable OOM protection for the main process
In reply to Li WangHere invoke OOM protection in fork_testrun, since it is the key point to distiguish many process branches. We do protect main ($PID) process from killing by OOM Killer, hope this can help to get the completed correct report for all of LTP tests.
Fundamental principle:
ltp test harness --> library process
(oom protection) main --> tst_run_tcases --> ... --> fork_testrun
(cancel protection) testrun --> run_tests --> ... --> testname
child_test --> ... --> end---
Notes:
Changes v1->v2:
* remove the protection for parent process as suggested by Martin
lib/tst_test.c | 3 +++ 1 file changed, 3 insertions(+)
lib/tst_test.c +3 -0
@@ -1441,11 +1441,14 @@ static int fork_testrun(void)SAFE_SIGNAL (SIGINT , sigint_handler); +tst_enable_oom_protection (getpid ()); + test_pid =fork ();if (test_pid <0 )tst_brk (TBROK |TERRNO ,"fork()" );if (!test_pid) { +tst_cancel_oom_protection (getpid ());SAFE_SIGNAL (SIGALRM ,SIG_DFL );SAFE_SIGNAL (SIGUSR1 ,SIG_DFL );SAFE_SIGNAL (SIGINT ,SIG_DFL );
Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 8 lines
include/tst_memutils.h +8
-0+/* +* 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
include/tst_memutils.h void tst_pollute_memory(size_t maxsize, int fillchar); · line 45
Quoted 10 lines
include/tst_memutils.h +10
-0+* 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.
Quoted 51 lines
#endif /*
TST_MEMUTILS_H__*/lib/tst_memutils.c +29
-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,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 ); +}Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 2/3] lib: enable OOM protection for the main process
In reply to Li WangHi!
Quoted 32 lines
Here invoke OOM protection in
fork_testrun, since it is the key point to distiguish many process branches. We do protect main ($PID) process from killing by OOM Killer, hope this can help to get the completed correct report for all of LTP tests.Fundamental principle:
ltp test harness --> library process (oom protection) main --> tst_run_tcases --> ... --> fork_testrun (cancel protection) testrun --> run_tests --> ... --> testname child_test --> ... --> endSigned-off-by Li Wang <[email protected]>Reviewed-by Petr Vorel <[email protected]>---
Notes:
Changesv1->v2:
* remove the protection for parent process as suggested by Martinlib/tst_test.c | 3 +++ 1 file changed, 3 insertions(+)lib/tst_test.c +1
-0@@ -1441,11 +1441,14 @@ static int fork_testrun(void)SAFE_SIGNAL (SIGINT , sigint_handler); +tst_enable_oom_protection (getpid ());
lib/tst_test.c static int fork_testrun(void) · line 1454
Quoted 10 lines
lib/tst_test.c +2
-0+ test_pid =fork ();if (test_pid <0 )tst_brk (TBROK |TERRNO ,"fork()" );if (!test_pid) { +tst_cancel_oom_protection (getpid ());SAFE_SIGNAL (SIGALRM ,SIG_DFL );SAFE_SIGNAL (SIGUSR1 ,SIG_DFL );SAFE_SIGNAL (SIGINT ,SIG_DFL );
The fork_testrun() function is called in a loop, it would be a bit cleaner to put the tst_enable_protection() call to the tst_run_tcases() instead since we don't have to enable it over and over.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process
In reply to Li WangHi!
Quoted 15 lines
Just simply invoke oom protection on mem library to make it can collect full state of children.
Signed-off-by Li Wang <[email protected]>Reviewed-by Petr Vorel <[email protected]>--- testcases/kernel/mem/lib/mem.c | 3 +++ 1 file changed, 3 insertions(+)testcases/kernel/mem/lib/mem.c +0
-0@@ -129,8 +129,11 @@ void oom(int testcase, int lite, int retcode, int allow_sigkill)pid_t pid;
testcases/kernel/mem/lib/mem.c void oom(int testcase, int lite, int retcode, int allow_sigkill) · line 139
Quoted 10 lines
testcases/kernel/mem/lib/mem.c +3
-0int status, threads; +tst_enable_oom_protection (getpid ()); +switch (pid =SAFE_FORK ()) {case 0 : +tst_cancel_oom_protection (getpid ()); threads =MAX (1 ,tst_ncpus () -1 );child_alloc (testcase, lite, threads);default :
Looking at the code we have, we always pass getpid(). We may as well change the library to use /proc/self/oom_score_adj if 0 is passed instead of the pid.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v2 3/3] oom: enable OOM protection for mem lib process
Quoted 10 lines · Cyril Hrubis
testcases/kernel/mem/lib/mem.c void oom(int testcase, int lite, int retcode, int allow_sigkill) · line 139
Quoted 9 lines
testcases/kernel/mem/lib/mem.c +3
-0+tst_enable_oom_protection (getpid ()); +switch (pid =SAFE_FORK ()) {case 0 : +tst_cancel_oom_protection (getpid ()); threads =MAX (1 ,tst_ncpus () -1 );child_alloc (testcase, lite, threads);default :
Quoted 4 lines
Looking at the code we have, we always pass
getpid(). We may as well change the library to use/proc/self/oom_score_adjif 0 is passed instead of the pid.
Yes, agree with all suggestions (include comments in 1/3 2/3).
Signature · 2 lines
Regards, Li Wang
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp